blob: 848051940f7edf44781cbd73de7f3b9cf2ee3b2f [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);
199 SDValue ReassociateOps(unsigned Opc, 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);
217 SDNode *MatchRotate(SDValue LHS, SDValue RHS);
218 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))
Chris Lattner29446522007-05-14 22:04:50 +0000393 return DAG.getNode(ISD::FSUB, 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
398 return DAG.getNode(ISD::FSUB, 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
412 return DAG.getNode(ISD::FSUB, Op.getValueType(), Op.getOperand(1),
413 Op.getOperand(0));
414
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))
Chris Lattner29446522007-05-14 22:04:50 +0000421 return DAG.getNode(Op.getOpcode(), 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
427 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
428 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:
434 return DAG.getNode(Op.getOpcode(), 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:
438 return DAG.getNode(ISD::FP_ROUND, 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
Dan Gohman475871a2008-07-27 21:46:04 +0000482SDValue DAGCombiner::ReassociateOps(unsigned Opc, SDValue N0, SDValue N1){
Duncan Sands83ec4b62008-06-06 12:08:01 +0000483 MVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000484 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
485 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
486 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
487 if (isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000488 SDValue OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000489 AddToWorkList(OpNode.getNode());
Nate Begemancd4d58c2006-02-03 06:46:56 +0000490 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
491 } else if (N0.hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000492 SDValue OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000493 AddToWorkList(OpNode.getNode());
Nate Begemancd4d58c2006-02-03 06:46:56 +0000494 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
495 }
496 }
497 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
498 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
499 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
500 if (isa<ConstantSDNode>(N0)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000501 SDValue OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000502 AddToWorkList(OpNode.getNode());
Nate Begemancd4d58c2006-02-03 06:46:56 +0000503 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
504 } else if (N1.hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000505 SDValue OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000506 AddToWorkList(OpNode.getNode());
Nate Begemancd4d58c2006-02-03 06:46:56 +0000507 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
508 }
509 }
Dan Gohman475871a2008-07-27 21:46:04 +0000510 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000511}
512
Dan Gohman475871a2008-07-27 21:46:04 +0000513SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
514 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000515 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
516 ++NodesCombined;
517 DOUT << "\nReplacing.1 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +0000518 DOUT << "\nWith: "; DEBUG(To[0].getNode()->dump(&DAG));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000519 DOUT << " and " << NumTo-1 << " other values\n";
Dan Gohman764fd0c2009-01-21 15:17:51 +0000520 DEBUG(for (unsigned i = 0, e = NumTo; i != e; ++i)
521 assert(N->getValueType(i) == To[i].getValueType() &&
522 "Cannot combine value to value of different type!"));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000523 WorkListRemover DeadNodes(*this);
524 DAG.ReplaceAllUsesWith(N, To, &DeadNodes);
525
526 if (AddTo) {
527 // Push the new nodes and any users onto the worklist
528 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000529 AddToWorkList(To[i].getNode());
530 AddUsersToWorkList(To[i].getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000531 }
532 }
533
Dan Gohmandbe664a2009-01-19 21:44:21 +0000534 // Finally, if the node is now dead, remove it from the graph. The node
535 // may not be dead if the replacement process recursively simplified to
536 // something else needing this node.
537 if (N->use_empty()) {
538 // Nodes can be reintroduced into the worklist. Make sure we do not
539 // process a node that has been replaced.
540 removeFromWorkList(N);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000541
Dan Gohmandbe664a2009-01-19 21:44:21 +0000542 // Finally, since the node is now dead, remove it from the graph.
543 DAG.DeleteNode(N);
544 }
Dan Gohman475871a2008-07-27 21:46:04 +0000545 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000546}
547
Dan Gohmane5af2d32009-01-29 01:59:02 +0000548void
549DAGCombiner::CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &
550 TLO) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000551 // Replace all uses. If any nodes become isomorphic to other nodes and
552 // are deleted, make sure to remove them from our worklist.
553 WorkListRemover DeadNodes(*this);
554 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000555
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000556 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000557 AddToWorkList(TLO.New.getNode());
558 AddUsersToWorkList(TLO.New.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000559
560 // Finally, if the node is now dead, remove it from the graph. The node
561 // may not be dead if the replacement process recursively simplified to
562 // something else needing this node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000563 if (TLO.Old.getNode()->use_empty()) {
564 removeFromWorkList(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000565
566 // If the operands of this node are only used by the node, they will now
567 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greifba36cb52008-08-28 21:40:38 +0000568 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
569 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
570 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000571
Gabor Greifba36cb52008-08-28 21:40:38 +0000572 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000573 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000574}
575
576/// SimplifyDemandedBits - Check the specified integer node value to see if
577/// it can be simplified or if things it uses can be simplified by bit
578/// propagation. If so, return true.
579bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
580 TargetLowering::TargetLoweringOpt TLO(DAG);
581 APInt KnownZero, KnownOne;
582 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
583 return false;
584
585 // Revisit the node.
586 AddToWorkList(Op.getNode());
587
588 // Replace the old value with the new one.
589 ++NodesCombined;
590 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.getNode()->dump(&DAG));
591 DOUT << "\nWith: "; DEBUG(TLO.New.getNode()->dump(&DAG));
592 DOUT << '\n';
593
594 CommitTargetLoweringOpt(TLO);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000595 return true;
596}
597
Chris Lattner29446522007-05-14 22:04:50 +0000598//===----------------------------------------------------------------------===//
599// Main DAG Combiner implementation
600//===----------------------------------------------------------------------===//
601
Duncan Sands25cf2272008-11-24 14:53:14 +0000602void DAGCombiner::Run(CombineLevel AtLevel) {
603 // set the instance variables, so that the various visit routines may use it.
604 Level = AtLevel;
605 LegalOperations = Level >= NoIllegalOperations;
606 LegalTypes = Level >= NoIllegalTypes;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000607
Evan Cheng17a568b2008-08-29 22:21:44 +0000608 // Add all the dag nodes to the worklist.
609 WorkList.reserve(DAG.allnodes_size());
610 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
611 E = DAG.allnodes_end(); I != E; ++I)
612 WorkList.push_back(I);
Duncan Sands25cf2272008-11-24 14:53:14 +0000613
Evan Cheng17a568b2008-08-29 22:21:44 +0000614 // Create a dummy node (which is not added to allnodes), that adds a reference
615 // to the root node, preventing it from being deleted, and tracking any
616 // changes of the root.
617 HandleSDNode Dummy(DAG.getRoot());
618
Jim Laskey26f7fa72006-10-17 19:33:52 +0000619 // The root of the dag may dangle to deleted nodes until the dag combiner is
620 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +0000621 DAG.setRoot(SDValue());
Chris Lattner24664722006-03-01 04:53:38 +0000622
Evan Cheng17a568b2008-08-29 22:21:44 +0000623 // while the worklist isn't empty, inspect the node on the end of it and
624 // try and combine it.
625 while (!WorkList.empty()) {
626 SDNode *N = WorkList.back();
627 WorkList.pop_back();
628
629 // If N has no uses, it is dead. Make sure to revisit all N's operands once
630 // N is deleted from the DAG, since they too may now be dead or may have a
631 // reduced number of uses, allowing other xforms.
632 if (N->use_empty() && N != &Dummy) {
633 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
634 AddToWorkList(N->getOperand(i).getNode());
635
636 DAG.DeleteNode(N);
637 continue;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000638 }
Evan Cheng17a568b2008-08-29 22:21:44 +0000639
640 SDValue RV = combine(N);
641
642 if (RV.getNode() == 0)
643 continue;
644
645 ++NodesCombined;
646
647 // If we get back the same node we passed in, rather than a new node or
648 // zero, we know that the node must have defined multiple values and
649 // CombineTo was used. Since CombineTo takes care of the worklist
650 // mechanics for us, we have no work to do in this case.
651 if (RV.getNode() == N)
652 continue;
653
654 assert(N->getOpcode() != ISD::DELETED_NODE &&
655 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
656 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +0000657
Evan Cheng17a568b2008-08-29 22:21:44 +0000658 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
659 DOUT << "\nWith: "; DEBUG(RV.getNode()->dump(&DAG));
660 DOUT << '\n';
661 WorkListRemover DeadNodes(*this);
662 if (N->getNumValues() == RV.getNode()->getNumValues())
663 DAG.ReplaceAllUsesWith(N, RV.getNode(), &DeadNodes);
664 else {
665 assert(N->getValueType(0) == RV.getValueType() &&
666 N->getNumValues() == 1 && "Type mismatch");
667 SDValue OpV = RV;
668 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
669 }
670
671 // Push the new node and any users onto the worklist
672 AddToWorkList(RV.getNode());
673 AddUsersToWorkList(RV.getNode());
674
675 // Add any uses of the old node to the worklist in case this node is the
676 // last one that uses them. They may become dead after this node is
677 // deleted.
678 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
679 AddToWorkList(N->getOperand(i).getNode());
680
Dan Gohmandbe664a2009-01-19 21:44:21 +0000681 // Finally, if the node is now dead, remove it from the graph. The node
682 // may not be dead if the replacement process recursively simplified to
683 // something else needing this node.
684 if (N->use_empty()) {
685 // Nodes can be reintroduced into the worklist. Make sure we do not
686 // process a node that has been replaced.
687 removeFromWorkList(N);
Evan Cheng17a568b2008-08-29 22:21:44 +0000688
Dan Gohmandbe664a2009-01-19 21:44:21 +0000689 // Finally, since the node is now dead, remove it from the graph.
690 DAG.DeleteNode(N);
691 }
Evan Cheng17a568b2008-08-29 22:21:44 +0000692 }
693
Chris Lattner95038592005-10-05 06:35:28 +0000694 // If the root changed (e.g. it was a dead load, update the root).
695 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000696}
697
Dan Gohman475871a2008-07-27 21:46:04 +0000698SDValue DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000699 switch(N->getOpcode()) {
700 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000701 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000702 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000703 case ISD::ADD: return visitADD(N);
704 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000705 case ISD::ADDC: return visitADDC(N);
706 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000707 case ISD::MUL: return visitMUL(N);
708 case ISD::SDIV: return visitSDIV(N);
709 case ISD::UDIV: return visitUDIV(N);
710 case ISD::SREM: return visitSREM(N);
711 case ISD::UREM: return visitUREM(N);
712 case ISD::MULHU: return visitMULHU(N);
713 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000714 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
715 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
716 case ISD::SDIVREM: return visitSDIVREM(N);
717 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000718 case ISD::AND: return visitAND(N);
719 case ISD::OR: return visitOR(N);
720 case ISD::XOR: return visitXOR(N);
721 case ISD::SHL: return visitSHL(N);
722 case ISD::SRA: return visitSRA(N);
723 case ISD::SRL: return visitSRL(N);
724 case ISD::CTLZ: return visitCTLZ(N);
725 case ISD::CTTZ: return visitCTTZ(N);
726 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000727 case ISD::SELECT: return visitSELECT(N);
728 case ISD::SELECT_CC: return visitSELECT_CC(N);
729 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000730 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
731 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000732 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000733 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
734 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000735 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +0000736 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000737 case ISD::FADD: return visitFADD(N);
738 case ISD::FSUB: return visitFSUB(N);
739 case ISD::FMUL: return visitFMUL(N);
740 case ISD::FDIV: return visitFDIV(N);
741 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000742 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000743 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
744 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
745 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
746 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
747 case ISD::FP_ROUND: return visitFP_ROUND(N);
748 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
749 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
750 case ISD::FNEG: return visitFNEG(N);
751 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000752 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000753 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000754 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000755 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000756 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +0000757 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000758 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
759 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000760 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000761 }
Dan Gohman475871a2008-07-27 21:46:04 +0000762 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000763}
764
Dan Gohman475871a2008-07-27 21:46:04 +0000765SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman389079b2007-10-08 17:57:15 +0000766
Dan Gohman475871a2008-07-27 21:46:04 +0000767 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000768
769 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +0000770 if (RV.getNode() == 0) {
Dan Gohman389079b2007-10-08 17:57:15 +0000771 assert(N->getOpcode() != ISD::DELETED_NODE &&
772 "Node was deleted but visit returned NULL!");
773
774 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
775 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
776
777 // Expose the DAG combiner to the target combiner impls.
778 TargetLowering::DAGCombinerInfo
Duncan Sands25cf2272008-11-24 14:53:14 +0000779 DagCombineInfo(DAG, Level == Unrestricted, false, this);
Dan Gohman389079b2007-10-08 17:57:15 +0000780
781 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
782 }
783 }
784
Evan Cheng08b11732008-03-22 01:55:50 +0000785 // If N is a commutative binary node, try commuting it to enable more
786 // sdisel CSE.
Gabor Greifba36cb52008-08-28 21:40:38 +0000787 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +0000788 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
789 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +0000790 SDValue N0 = N->getOperand(0);
791 SDValue N1 = N->getOperand(1);
Evan Cheng08b11732008-03-22 01:55:50 +0000792 // Constant operands are canonicalized to RHS.
793 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000794 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +0000795 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
796 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +0000797 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +0000798 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +0000799 }
800 }
801
Dan Gohman389079b2007-10-08 17:57:15 +0000802 return RV;
803}
804
Chris Lattner6270f682006-10-08 22:57:01 +0000805/// getInputChainForNode - Given a node, return its input chain if it has one,
806/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000807static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000808 if (unsigned NumOps = N->getNumOperands()) {
809 if (N->getOperand(0).getValueType() == MVT::Other)
810 return N->getOperand(0);
811 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
812 return N->getOperand(NumOps-1);
813 for (unsigned i = 1; i < NumOps-1; ++i)
814 if (N->getOperand(i).getValueType() == MVT::Other)
815 return N->getOperand(i);
816 }
Dan Gohman475871a2008-07-27 21:46:04 +0000817 return SDValue(0, 0);
Chris Lattner6270f682006-10-08 22:57:01 +0000818}
819
Dan Gohman475871a2008-07-27 21:46:04 +0000820SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000821 // If N has two operands, where one has an input chain equal to the other,
822 // the 'other' chain is redundant.
823 if (N->getNumOperands() == 2) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000824 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +0000825 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000826 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +0000827 return N->getOperand(1);
828 }
829
Chris Lattnerc76d4412007-05-16 06:37:59 +0000830 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +0000831 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Chris Lattnerc76d4412007-05-16 06:37:59 +0000832 SmallPtrSet<SDNode*, 16> SeenOps;
833 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000834
835 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000836 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000837
Jim Laskey71382342006-10-07 23:37:56 +0000838 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000839 // encountered.
840 for (unsigned i = 0; i < TFs.size(); ++i) {
841 SDNode *TF = TFs[i];
842
Jim Laskey6ff23e52006-10-04 16:53:27 +0000843 // Check each of the operands.
844 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000845 SDValue Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000846
Jim Laskey6ff23e52006-10-04 16:53:27 +0000847 switch (Op.getOpcode()) {
848 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000849 // Entry tokens don't need to be added to the list. They are
850 // rededundant.
851 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000852 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000853
Jim Laskey6ff23e52006-10-04 16:53:27 +0000854 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000855 if ((CombinerAA || Op.hasOneUse()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000856 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000857 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +0000858 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +0000859 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +0000860 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +0000861 Changed = true;
862 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000863 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000864 // Fall thru
865
866 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000867 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +0000868 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +0000869 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000870 else
871 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000872 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000873 }
874 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000875 }
876
Dan Gohman475871a2008-07-27 21:46:04 +0000877 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000878
879 // If we've change things around then replace token factor.
880 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +0000881 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000882 // The entry token is the only possible outcome.
883 Result = DAG.getEntryNode();
884 } else {
885 // New and improved token factor.
886 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000887 }
Jim Laskey274062c2006-10-13 23:32:28 +0000888
889 // Don't add users to work list.
890 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000891 }
Jim Laskey279f0532006-09-25 16:29:54 +0000892
Jim Laskey6ff23e52006-10-04 16:53:27 +0000893 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000894}
895
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000896/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +0000897SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000898 WorkListRemover DeadNodes(*this);
899 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Dan Gohman475871a2008-07-27 21:46:04 +0000900 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i),
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000901 &DeadNodes);
902 removeFromWorkList(N);
903 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000904 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000905}
906
907
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000908static
Dan Gohman475871a2008-07-27 21:46:04 +0000909SDValue combineShlAddConstant(SDValue N0, SDValue N1, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000910 MVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000911 SDValue N00 = N0.getOperand(0);
912 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000913 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Gabor Greifba36cb52008-08-28 21:40:38 +0000914 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000915 isa<ConstantSDNode>(N00.getOperand(1))) {
916 N0 = DAG.getNode(ISD::ADD, VT,
917 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
918 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
919 return DAG.getNode(ISD::ADD, VT, N0, N1);
920 }
Dan Gohman475871a2008-07-27 21:46:04 +0000921 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000922}
923
Evan Chengb13cdbd2007-06-21 07:39:16 +0000924static
Dan Gohman475871a2008-07-27 21:46:04 +0000925SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Bill Wendlingae89bb12008-11-11 08:25:46 +0000926 SelectionDAG &DAG, const TargetLowering &TLI,
Duncan Sands25cf2272008-11-24 14:53:14 +0000927 bool LegalOperations) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000928 MVT VT = N->getValueType(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000929 unsigned Opc = N->getOpcode();
930 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
Dan Gohman475871a2008-07-27 21:46:04 +0000931 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
932 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000933 ISD::CondCode CC = ISD::SETCC_INVALID;
Bill Wendlingae89bb12008-11-11 08:25:46 +0000934
935 if (isSlctCC) {
Evan Chengb13cdbd2007-06-21 07:39:16 +0000936 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
Bill Wendlingae89bb12008-11-11 08:25:46 +0000937 } else {
Dan Gohman475871a2008-07-27 21:46:04 +0000938 SDValue CCOp = Slct.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000939 if (CCOp.getOpcode() == ISD::SETCC)
940 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
941 }
942
943 bool DoXform = false;
944 bool InvCC = false;
945 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
946 "Bad input!");
Bill Wendlingae89bb12008-11-11 08:25:46 +0000947
Evan Chengb13cdbd2007-06-21 07:39:16 +0000948 if (LHS.getOpcode() == ISD::Constant &&
Bill Wendlingae89bb12008-11-11 08:25:46 +0000949 cast<ConstantSDNode>(LHS)->isNullValue()) {
Evan Chengb13cdbd2007-06-21 07:39:16 +0000950 DoXform = true;
Bill Wendlingae89bb12008-11-11 08:25:46 +0000951 } else if (CC != ISD::SETCC_INVALID &&
952 RHS.getOpcode() == ISD::Constant &&
953 cast<ConstantSDNode>(RHS)->isNullValue()) {
Evan Chengb13cdbd2007-06-21 07:39:16 +0000954 std::swap(LHS, RHS);
Dan Gohman475871a2008-07-27 21:46:04 +0000955 SDValue Op0 = Slct.getOperand(0);
Bill Wendlingae89bb12008-11-11 08:25:46 +0000956 MVT OpVT = isSlctCC ? Op0.getValueType() :
957 Op0.getOperand(0).getValueType();
958 bool isInt = OpVT.isInteger();
Evan Chengb13cdbd2007-06-21 07:39:16 +0000959 CC = ISD::getSetCCInverse(CC, isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +0000960
Duncan Sands25cf2272008-11-24 14:53:14 +0000961 if (LegalOperations && !TLI.isCondCodeLegal(CC, OpVT))
Bill Wendlingae89bb12008-11-11 08:25:46 +0000962 return SDValue(); // Inverse operator isn't legal.
963
Evan Chengb13cdbd2007-06-21 07:39:16 +0000964 DoXform = true;
965 InvCC = true;
966 }
967
968 if (DoXform) {
Dan Gohman475871a2008-07-27 21:46:04 +0000969 SDValue Result = DAG.getNode(Opc, VT, OtherOp, RHS);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000970 if (isSlctCC)
971 return DAG.getSelectCC(OtherOp, Result,
972 Slct.getOperand(0), Slct.getOperand(1), CC);
Dan Gohman475871a2008-07-27 21:46:04 +0000973 SDValue CCOp = Slct.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000974 if (InvCC)
975 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
976 CCOp.getOperand(1), CC);
977 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
978 }
Dan Gohman475871a2008-07-27 21:46:04 +0000979 return SDValue();
Evan Chengb13cdbd2007-06-21 07:39:16 +0000980}
981
Dan Gohman475871a2008-07-27 21:46:04 +0000982SDValue DAGCombiner::visitADD(SDNode *N) {
983 SDValue N0 = N->getOperand(0);
984 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000985 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
986 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000987 MVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000988
989 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +0000990 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000991 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +0000992 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +0000993 }
Bill Wendling2476e5d2008-12-10 22:36:00 +0000994
Dan Gohman613e0d82007-07-03 14:03:57 +0000995 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +0000996 if (N0.getOpcode() == ISD::UNDEF)
997 return N0;
998 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +0000999 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001000 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001001 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001002 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001003 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001004 if (N0C && !N1C)
1005 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001006 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001007 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001008 return N0;
Dan Gohman6520e202008-10-18 02:06:02 +00001009 // fold (add Sym, c) -> Sym+c
1010 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001011 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman6520e202008-10-18 02:06:02 +00001012 GA->getOpcode() == ISD::GlobalAddress)
1013 return DAG.getGlobalAddress(GA->getGlobal(), VT,
1014 GA->getOffset() +
1015 (uint64_t)N1C->getSExtValue());
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001016 // fold ((c1-A)+c2) -> (c1+c2)-A
1017 if (N1C && N0.getOpcode() == ISD::SUB)
1018 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
1019 return DAG.getNode(ISD::SUB, VT,
Dan Gohman002e5d02008-03-13 22:13:53 +00001020 DAG.getConstant(N1C->getAPIntValue()+
1021 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001022 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +00001023 // reassociate add
Dan Gohman475871a2008-07-27 21:46:04 +00001024 SDValue RADD = ReassociateOps(ISD::ADD, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001025 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001026 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001027 // fold ((0-A) + B) -> B-A
1028 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1029 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +00001030 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001031 // fold (A + (0-B)) -> A-B
1032 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1033 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +00001034 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001035 // fold (A+(B-A)) -> B
1036 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001037 return N1.getOperand(0);
Dale Johannesen56eca912008-11-27 00:43:21 +00001038 // fold ((B-A)+A) -> B
1039 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1040 return N0.getOperand(0);
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001041 // fold (A+(B-(A+C))) to (B-C)
1042 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
1043 N0 == N1.getOperand(1).getOperand(0)) {
1044 return DAG.getNode(ISD::SUB, VT, N1.getOperand(0),
1045 N1.getOperand(1).getOperand(1));
1046 }
1047 // fold (A+(B-(C+A))) to (B-C)
1048 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
1049 N0 == N1.getOperand(1).getOperand(1)) {
1050 return DAG.getNode(ISD::SUB, VT, N1.getOperand(0),
1051 N1.getOperand(1).getOperand(0));
1052 }
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001053 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesen34d79852008-12-02 18:40:40 +00001054 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1055 N1.getOperand(0).getOpcode() == ISD::SUB &&
1056 N0 == N1.getOperand(0).getOperand(1)) {
1057 return DAG.getNode(N1.getOpcode(), VT, N1.getOperand(0).getOperand(0),
1058 N1.getOperand(1));
1059 }
1060
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001061 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1062 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1063 SDValue N00 = N0.getOperand(0);
1064 SDValue N01 = N0.getOperand(1);
1065 SDValue N10 = N1.getOperand(0);
1066 SDValue N11 = N1.getOperand(1);
1067 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10)) {
1068 return DAG.getNode(ISD::SUB, VT,
1069 DAG.getNode(ISD::ADD, VT, N00, N10),
1070 DAG.getNode(ISD::ADD, VT, N01, N11));
1071 }
1072 }
Chris Lattner947c2892006-03-13 06:51:27 +00001073
Dan Gohman475871a2008-07-27 21:46:04 +00001074 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1075 return SDValue(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +00001076
1077 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001078 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001079 APInt LHSZero, LHSOne;
1080 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001081 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001082 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001083 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001084 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +00001085
1086 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1087 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1088 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1089 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1090 return DAG.getNode(ISD::OR, VT, N0, N1);
1091 }
1092 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001093
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001094 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001095 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001096 SDValue Result = combineShlAddConstant(N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001097 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001098 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001099 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001100 SDValue Result = combineShlAddConstant(N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001101 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001102 }
1103
Evan Chengb13cdbd2007-06-21 07:39:16 +00001104 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Gabor Greifba36cb52008-08-28 21:40:38 +00001105 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
Duncan Sands25cf2272008-11-24 14:53:14 +00001106 SDValue Result = combineSelectAndUse(N, N0, N1, DAG, TLI, LegalOperations);
Gabor Greifba36cb52008-08-28 21:40:38 +00001107 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001108 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001109 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
Duncan Sands25cf2272008-11-24 14:53:14 +00001110 SDValue Result = combineSelectAndUse(N, N1, N0, DAG, TLI, LegalOperations);
Gabor Greifba36cb52008-08-28 21:40:38 +00001111 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001112 }
1113
Dan Gohman475871a2008-07-27 21:46:04 +00001114 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001115}
1116
Dan Gohman475871a2008-07-27 21:46:04 +00001117SDValue DAGCombiner::visitADDC(SDNode *N) {
1118 SDValue N0 = N->getOperand(0);
1119 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001120 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1121 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001122 MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001123
1124 // If the flag result is dead, turn this into an ADD.
1125 if (N->hasNUsesOfValue(0, 1))
1126 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +00001127 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +00001128
1129 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001130 if (N0C && !N1C)
Dan Gohman56867522008-06-21 22:06:07 +00001131 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Chris Lattner91153682007-03-04 20:03:15 +00001132
Chris Lattnerb6541762007-03-04 20:40:38 +00001133 // fold (addc x, 0) -> x + no carry out
1134 if (N1C && N1C->isNullValue())
1135 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1136
1137 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001138 APInt LHSZero, LHSOne;
1139 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001140 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001141 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001142 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001143 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001144
1145 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1146 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1147 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1148 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1149 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
1150 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1151 }
Chris Lattner91153682007-03-04 20:03:15 +00001152
Dan Gohman475871a2008-07-27 21:46:04 +00001153 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001154}
1155
Dan Gohman475871a2008-07-27 21:46:04 +00001156SDValue DAGCombiner::visitADDE(SDNode *N) {
1157 SDValue N0 = N->getOperand(0);
1158 SDValue N1 = N->getOperand(1);
1159 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001160 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1161 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001162 //MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001163
1164 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001165 if (N0C && !N1C)
Dan Gohman56867522008-06-21 22:06:07 +00001166 return DAG.getNode(ISD::ADDE, N->getVTList(), N1, N0, CarryIn);
Chris Lattner91153682007-03-04 20:03:15 +00001167
Chris Lattnerb6541762007-03-04 20:40:38 +00001168 // fold (adde x, y, false) -> (addc x, y)
Dan Gohman0a4627d2008-06-23 15:29:14 +00001169 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Dan Gohman56867522008-06-21 22:06:07 +00001170 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Chris Lattner91153682007-03-04 20:03:15 +00001171
Dan Gohman475871a2008-07-27 21:46:04 +00001172 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001173}
1174
1175
1176
Dan Gohman475871a2008-07-27 21:46:04 +00001177SDValue DAGCombiner::visitSUB(SDNode *N) {
1178 SDValue N0 = N->getOperand(0);
1179 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001180 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1181 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001182 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001183
Dan Gohman7f321562007-06-25 16:23:39 +00001184 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001185 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001186 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001187 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001188 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001189
Chris Lattner854077d2005-10-17 01:07:11 +00001190 // fold (sub x, x) -> 0
Evan Chengc8e3b142008-03-12 07:02:50 +00001191 if (N0 == N1)
Chris Lattner854077d2005-10-17 01:07:11 +00001192 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001193 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001194 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001195 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattner05b57432005-10-11 06:07:15 +00001196 // fold (sub x, c) -> (add x, -c)
1197 if (N1C)
Dan Gohman002e5d02008-03-13 22:13:53 +00001198 return DAG.getNode(ISD::ADD, VT, N0,
1199 DAG.getConstant(-N1C->getAPIntValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001200 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001201 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001202 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001203 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001204 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001205 return N0.getOperand(0);
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001206 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001207 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001208 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1209 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001210 N0.getOperand(1).getOperand(0) == N1)
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001211 return DAG.getNode(N0.getOperand(1).getOpcode(), VT, N0.getOperand(0),
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001212 N0.getOperand(1).getOperand(1));
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001213 // fold ((A+(C+B))-B) -> A+C
1214 if (N0.getOpcode() == ISD::ADD &&
1215 N0.getOperand(1).getOpcode() == ISD::ADD &&
1216 N0.getOperand(1).getOperand(1) == N1)
1217 return DAG.getNode(ISD::ADD, VT, N0.getOperand(0),
1218 N0.getOperand(1).getOperand(0));
Dale Johannesen58e39b02008-12-23 01:59:54 +00001219 // fold ((A-(B-C))-C) -> A-B
1220 if (N0.getOpcode() == ISD::SUB &&
1221 N0.getOperand(1).getOpcode() == ISD::SUB &&
1222 N0.getOperand(1).getOperand(1) == N1)
1223 return DAG.getNode(ISD::SUB, VT, N0.getOperand(0),
1224 N0.getOperand(1).getOperand(0));
Evan Chengb13cdbd2007-06-21 07:39:16 +00001225 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Gabor Greifba36cb52008-08-28 21:40:38 +00001226 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
Duncan Sands25cf2272008-11-24 14:53:14 +00001227 SDValue Result = combineSelectAndUse(N, N1, N0, DAG, TLI, LegalOperations);
Gabor Greifba36cb52008-08-28 21:40:38 +00001228 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001229 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001230 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001231 if (N0.getOpcode() == ISD::UNDEF)
1232 return N0;
1233 if (N1.getOpcode() == ISD::UNDEF)
1234 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001235
Dan Gohman6520e202008-10-18 02:06:02 +00001236 // If the relocation model supports it, consider symbol offsets.
1237 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001238 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman6520e202008-10-18 02:06:02 +00001239 // fold (sub Sym, c) -> Sym-c
1240 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
1241 return DAG.getGlobalAddress(GA->getGlobal(), VT,
1242 GA->getOffset() -
1243 (uint64_t)N1C->getSExtValue());
1244 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1245 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1246 if (GA->getGlobal() == GB->getGlobal())
1247 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1248 VT);
1249 }
1250
Dan Gohman475871a2008-07-27 21:46:04 +00001251 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001252}
1253
Dan Gohman475871a2008-07-27 21:46:04 +00001254SDValue DAGCombiner::visitMUL(SDNode *N) {
1255 SDValue N0 = N->getOperand(0);
1256 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001257 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1258 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001259 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001260
Dan Gohman7f321562007-06-25 16:23:39 +00001261 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001262 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001263 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001264 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001265 }
Dan Gohman7f321562007-06-25 16:23:39 +00001266
Dan Gohman613e0d82007-07-03 14:03:57 +00001267 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001268 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001269 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001270 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001271 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001272 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001273 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001274 if (N0C && !N1C)
1275 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001276 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001277 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001278 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001279 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001280 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001281 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001282 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001283 if (N1C && N1C->getAPIntValue().isPowerOf2())
Chris Lattner3e6099b2005-10-30 06:41:49 +00001284 return DAG.getNode(ISD::SHL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001285 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001286 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001287 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Dan Gohman7810bfe2008-09-26 21:54:37 +00001288 if (N1C && isPowerOf2_64(-N1C->getSExtValue())) {
Chris Lattner3e6099b2005-10-30 06:41:49 +00001289 // FIXME: If the input is something that is easily negated (e.g. a
1290 // single-use add), we should put the negate there.
1291 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1292 DAG.getNode(ISD::SHL, VT, N0,
Dan Gohman7810bfe2008-09-26 21:54:37 +00001293 DAG.getConstant(Log2_64(-N1C->getSExtValue()),
Chris Lattner3e6099b2005-10-30 06:41:49 +00001294 TLI.getShiftAmountTy())));
1295 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001296
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001297 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1298 if (N1C && N0.getOpcode() == ISD::SHL &&
1299 isa<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001300 SDValue C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001301 AddToWorkList(C3.getNode());
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001302 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1303 }
1304
1305 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1306 // use.
1307 {
Dan Gohman475871a2008-07-27 21:46:04 +00001308 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001309 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1310 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001311 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001312 Sh = N0; Y = N1;
1313 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001314 isa<ConstantSDNode>(N1.getOperand(1)) &&
1315 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001316 Sh = N1; Y = N0;
1317 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001318 if (Sh.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001319 SDValue Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001320 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1321 }
1322 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001323 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Gabor Greifba36cb52008-08-28 21:40:38 +00001324 if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
Chris Lattnera1deca32006-03-04 23:33:26 +00001325 isa<ConstantSDNode>(N0.getOperand(1))) {
1326 return DAG.getNode(ISD::ADD, VT,
1327 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1328 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1329 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001330
Nate Begemancd4d58c2006-02-03 06:46:56 +00001331 // reassociate mul
Dan Gohman475871a2008-07-27 21:46:04 +00001332 SDValue RMUL = ReassociateOps(ISD::MUL, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001333 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001334 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001335
Dan Gohman475871a2008-07-27 21:46:04 +00001336 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001337}
1338
Dan Gohman475871a2008-07-27 21:46:04 +00001339SDValue DAGCombiner::visitSDIV(SDNode *N) {
1340 SDValue N0 = N->getOperand(0);
1341 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001342 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1343 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001344 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001345
Dan Gohman7f321562007-06-25 16:23:39 +00001346 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001347 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001348 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001349 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001350 }
Dan Gohman7f321562007-06-25 16:23:39 +00001351
Nate Begeman1d4d4142005-09-01 00:19:25 +00001352 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001353 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001354 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001355 // fold (sdiv X, 1) -> X
Dan Gohman7810bfe2008-09-26 21:54:37 +00001356 if (N1C && N1C->getSExtValue() == 1LL)
Nate Begeman405e3ec2005-10-21 00:02:42 +00001357 return N0;
1358 // fold (sdiv X, -1) -> 0-X
1359 if (N1C && N1C->isAllOnesValue())
1360 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001361 // If we know the sign bits of both operands are zero, strength reduce to a
1362 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001363 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001364 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattnerf32aac32008-01-27 23:32:17 +00001365 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
1366 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001367 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman002e5d02008-03-13 22:13:53 +00001368 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Dan Gohman7810bfe2008-09-26 21:54:37 +00001369 (isPowerOf2_64(N1C->getSExtValue()) ||
1370 isPowerOf2_64(-N1C->getSExtValue()))) {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001371 // If dividing by powers of two is cheap, then don't perform the following
1372 // fold.
1373 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001374 return SDValue();
Dan Gohman7810bfe2008-09-26 21:54:37 +00001375 int64_t pow2 = N1C->getSExtValue();
Nate Begeman405e3ec2005-10-21 00:02:42 +00001376 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001377 unsigned lg2 = Log2_64(abs2);
1378 // Splat the sign bit into the register
Dan Gohman475871a2008-07-27 21:46:04 +00001379 SDValue SGN = DAG.getNode(ISD::SRA, VT, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001380 DAG.getConstant(VT.getSizeInBits()-1,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001381 TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00001382 AddToWorkList(SGN.getNode());
Chris Lattner8f4880b2006-02-16 08:02:36 +00001383 // Add (N0 < 0) ? abs2 - 1 : 0;
Dan Gohman475871a2008-07-27 21:46:04 +00001384 SDValue SRL = DAG.getNode(ISD::SRL, VT, SGN,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001385 DAG.getConstant(VT.getSizeInBits()-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001386 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00001387 SDValue ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001388 AddToWorkList(SRL.getNode());
1389 AddToWorkList(ADD.getNode()); // Divide by pow2
Dan Gohman475871a2008-07-27 21:46:04 +00001390 SDValue SRA = DAG.getNode(ISD::SRA, VT, ADD,
Chris Lattner8f4880b2006-02-16 08:02:36 +00001391 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001392 // If we're dividing by a positive value, we're done. Otherwise, we must
1393 // negate the result.
1394 if (pow2 > 0)
1395 return SRA;
Gabor Greifba36cb52008-08-28 21:40:38 +00001396 AddToWorkList(SRA.getNode());
Nate Begeman405e3ec2005-10-21 00:02:42 +00001397 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1398 }
Nate Begeman69575232005-10-20 02:15:44 +00001399 // if integer divide is expensive and we satisfy the requirements, emit an
1400 // alternate sequence.
Dan Gohman7810bfe2008-09-26 21:54:37 +00001401 if (N1C && (N1C->getSExtValue() < -1 || N1C->getSExtValue() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001402 !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001403 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001404 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001405 }
Dan Gohman7f321562007-06-25 16:23:39 +00001406
Dan Gohman613e0d82007-07-03 14:03:57 +00001407 // undef / X -> 0
1408 if (N0.getOpcode() == ISD::UNDEF)
1409 return DAG.getConstant(0, VT);
1410 // X / undef -> undef
1411 if (N1.getOpcode() == ISD::UNDEF)
1412 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001413
Dan Gohman475871a2008-07-27 21:46:04 +00001414 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001415}
1416
Dan Gohman475871a2008-07-27 21:46:04 +00001417SDValue DAGCombiner::visitUDIV(SDNode *N) {
1418 SDValue N0 = N->getOperand(0);
1419 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001420 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1421 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001422 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001423
Dan Gohman7f321562007-06-25 16:23:39 +00001424 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001425 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001426 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001427 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001428 }
Dan Gohman7f321562007-06-25 16:23:39 +00001429
Nate Begeman1d4d4142005-09-01 00:19:25 +00001430 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001431 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001432 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001433 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001434 if (N1C && N1C->getAPIntValue().isPowerOf2())
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001435 return DAG.getNode(ISD::SRL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001436 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001437 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001438 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1439 if (N1.getOpcode() == ISD::SHL) {
1440 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001441 if (SHC->getAPIntValue().isPowerOf2()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001442 MVT ADDVT = N1.getOperand(1).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001443 SDValue Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001444 DAG.getConstant(SHC->getAPIntValue()
1445 .logBase2(),
Nate Begemanc031e332006-02-05 07:36:48 +00001446 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001447 AddToWorkList(Add.getNode());
Nate Begemanc031e332006-02-05 07:36:48 +00001448 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001449 }
1450 }
1451 }
Nate Begeman69575232005-10-20 02:15:44 +00001452 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001453 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001454 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001455 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00001456 }
Dan Gohman7f321562007-06-25 16:23:39 +00001457
Dan Gohman613e0d82007-07-03 14:03:57 +00001458 // undef / X -> 0
1459 if (N0.getOpcode() == ISD::UNDEF)
1460 return DAG.getConstant(0, VT);
1461 // X / undef -> undef
1462 if (N1.getOpcode() == ISD::UNDEF)
1463 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001464
Dan Gohman475871a2008-07-27 21:46:04 +00001465 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001466}
1467
Dan Gohman475871a2008-07-27 21:46:04 +00001468SDValue DAGCombiner::visitSREM(SDNode *N) {
1469 SDValue N0 = N->getOperand(0);
1470 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001471 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1472 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001473 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001474
1475 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001476 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001477 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001478 // If we know the sign bits of both operands are zero, strength reduce to a
1479 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00001480 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001481 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattneree339f42008-01-27 23:21:58 +00001482 return DAG.getNode(ISD::UREM, VT, N0, N1);
1483 }
Chris Lattner26d29902006-10-12 20:58:32 +00001484
Dan Gohman77003042007-11-26 23:46:11 +00001485 // If X/C can be simplified by the division-by-constant logic, lower
1486 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001487 if (N1C && !N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001488 SDValue Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001489 AddToWorkList(Div.getNode());
1490 SDValue OptimizedDiv = combine(Div.getNode());
1491 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001492 SDValue Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1493 SDValue Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001494 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001495 return Sub;
1496 }
Chris Lattner26d29902006-10-12 20:58:32 +00001497 }
1498
Dan Gohman613e0d82007-07-03 14:03:57 +00001499 // undef % X -> 0
1500 if (N0.getOpcode() == ISD::UNDEF)
1501 return DAG.getConstant(0, VT);
1502 // X % undef -> undef
1503 if (N1.getOpcode() == ISD::UNDEF)
1504 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001505
Dan Gohman475871a2008-07-27 21:46:04 +00001506 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001507}
1508
Dan Gohman475871a2008-07-27 21:46:04 +00001509SDValue DAGCombiner::visitUREM(SDNode *N) {
1510 SDValue N0 = N->getOperand(0);
1511 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001512 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1513 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001514 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001515
1516 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001517 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001518 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001519 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001520 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
1521 return DAG.getNode(ISD::AND, VT, N0,
1522 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001523 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1524 if (N1.getOpcode() == ISD::SHL) {
1525 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001526 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001527 SDValue Add =
Dan Gohman002e5d02008-03-13 22:13:53 +00001528 DAG.getNode(ISD::ADD, VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001529 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00001530 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001531 AddToWorkList(Add.getNode());
Nate Begemanc031e332006-02-05 07:36:48 +00001532 return DAG.getNode(ISD::AND, VT, N0, Add);
1533 }
1534 }
1535 }
Chris Lattner26d29902006-10-12 20:58:32 +00001536
Dan Gohman77003042007-11-26 23:46:11 +00001537 // If X/C can be simplified by the division-by-constant logic, lower
1538 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001539 if (N1C && !N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001540 SDValue Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00001541 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00001542 SDValue OptimizedDiv = combine(Div.getNode());
1543 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001544 SDValue Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1545 SDValue Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001546 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001547 return Sub;
1548 }
Chris Lattner26d29902006-10-12 20:58:32 +00001549 }
1550
Dan Gohman613e0d82007-07-03 14:03:57 +00001551 // undef % X -> 0
1552 if (N0.getOpcode() == ISD::UNDEF)
1553 return DAG.getConstant(0, VT);
1554 // X % undef -> undef
1555 if (N1.getOpcode() == ISD::UNDEF)
1556 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001557
Dan Gohman475871a2008-07-27 21:46:04 +00001558 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001559}
1560
Dan Gohman475871a2008-07-27 21:46:04 +00001561SDValue DAGCombiner::visitMULHS(SDNode *N) {
1562 SDValue N0 = N->getOperand(0);
1563 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001564 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001565 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001566
1567 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001568 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001569 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001570 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001571 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001572 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001573 DAG.getConstant(N0.getValueType().getSizeInBits()-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001574 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001575 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001576 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001577 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001578
Dan Gohman475871a2008-07-27 21:46:04 +00001579 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001580}
1581
Dan Gohman475871a2008-07-27 21:46:04 +00001582SDValue DAGCombiner::visitMULHU(SDNode *N) {
1583 SDValue N0 = N->getOperand(0);
1584 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001585 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001586 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001587
1588 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001589 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001590 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001591 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00001592 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001593 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001594 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001595 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001596 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001597
Dan Gohman475871a2008-07-27 21:46:04 +00001598 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001599}
1600
Dan Gohman389079b2007-10-08 17:57:15 +00001601/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1602/// compute two values. LoOp and HiOp give the opcodes for the two computations
1603/// that are being performed. Return true if a simplification was made.
1604///
Dan Gohman475871a2008-07-27 21:46:04 +00001605SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1606 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001607 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001608 bool HiExists = N->hasAnyUseOfValue(1);
1609 if (!HiExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001610 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00001611 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001612 SDValue Res = DAG.getNode(LoOp, N->getValueType(0), N->op_begin(),
Duncan Sands25cf2272008-11-24 14:53:14 +00001613 N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001614 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001615 }
1616
1617 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001618 bool LoExists = N->hasAnyUseOfValue(0);
1619 if (!LoExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001620 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00001621 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001622 SDValue Res = DAG.getNode(HiOp, N->getValueType(1), N->op_begin(),
Duncan Sands25cf2272008-11-24 14:53:14 +00001623 N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001624 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001625 }
1626
Evan Cheng44711942007-11-08 09:25:29 +00001627 // If both halves are used, return as it is.
1628 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00001629 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00001630
1631 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001632 if (LoExists) {
Dan Gohman475871a2008-07-27 21:46:04 +00001633 SDValue Lo = DAG.getNode(LoOp, N->getValueType(0),
Evan Cheng44711942007-11-08 09:25:29 +00001634 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001635 AddToWorkList(Lo.getNode());
1636 SDValue LoOpt = combine(Lo.getNode());
1637 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001638 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001639 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001640 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001641 }
1642
Evan Cheng44711942007-11-08 09:25:29 +00001643 if (HiExists) {
Dan Gohman475871a2008-07-27 21:46:04 +00001644 SDValue Hi = DAG.getNode(HiOp, N->getValueType(1),
Duncan Sands25cf2272008-11-24 14:53:14 +00001645 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001646 AddToWorkList(Hi.getNode());
1647 SDValue HiOpt = combine(Hi.getNode());
1648 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001649 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001650 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001651 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001652 }
Dan Gohman475871a2008-07-27 21:46:04 +00001653 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001654}
1655
Dan Gohman475871a2008-07-27 21:46:04 +00001656SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
1657 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00001658 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001659
Dan Gohman475871a2008-07-27 21:46:04 +00001660 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001661}
1662
Dan Gohman475871a2008-07-27 21:46:04 +00001663SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
1664 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00001665 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001666
Dan Gohman475871a2008-07-27 21:46:04 +00001667 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001668}
1669
Dan Gohman475871a2008-07-27 21:46:04 +00001670SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
1671 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001672 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001673
Dan Gohman475871a2008-07-27 21:46:04 +00001674 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001675}
1676
Dan Gohman475871a2008-07-27 21:46:04 +00001677SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
1678 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001679 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001680
Dan Gohman475871a2008-07-27 21:46:04 +00001681 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001682}
1683
Chris Lattner35e5c142006-05-05 05:51:50 +00001684/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1685/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00001686SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1687 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001688 MVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00001689 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1690
Chris Lattner540121f2006-05-05 06:31:05 +00001691 // For each of OP in AND/OR/XOR:
1692 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1693 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1694 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001695 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001696 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001697 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001698 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001699 SDValue ORNode = DAG.getNode(N->getOpcode(),
Chris Lattner35e5c142006-05-05 05:51:50 +00001700 N0.getOperand(0).getValueType(),
1701 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001702 AddToWorkList(ORNode.getNode());
Chris Lattner540121f2006-05-05 06:31:05 +00001703 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001704 }
1705
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001706 // For each of OP in SHL/SRL/SRA/AND...
1707 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1708 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1709 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001710 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001711 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001712 N0.getOperand(1) == N1.getOperand(1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001713 SDValue ORNode = DAG.getNode(N->getOpcode(),
Chris Lattner35e5c142006-05-05 05:51:50 +00001714 N0.getOperand(0).getValueType(),
1715 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001716 AddToWorkList(ORNode.getNode());
Chris Lattner35e5c142006-05-05 05:51:50 +00001717 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1718 }
1719
Dan Gohman475871a2008-07-27 21:46:04 +00001720 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00001721}
1722
Dan Gohman475871a2008-07-27 21:46:04 +00001723SDValue DAGCombiner::visitAND(SDNode *N) {
1724 SDValue N0 = N->getOperand(0);
1725 SDValue N1 = N->getOperand(1);
1726 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001727 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1728 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001729 MVT VT = N1.getValueType();
1730 unsigned BitWidth = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001731
Dan Gohman7f321562007-06-25 16:23:39 +00001732 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001733 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001734 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001735 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001736 }
Dan Gohman7f321562007-06-25 16:23:39 +00001737
Dan Gohman613e0d82007-07-03 14:03:57 +00001738 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001739 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001740 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001741 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001742 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001743 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001744 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001745 if (N0C && !N1C)
1746 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001747 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001748 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001749 return N0;
1750 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00001751 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001752 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001753 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001754 // reassociate and
Dan Gohman475871a2008-07-27 21:46:04 +00001755 SDValue RAND = ReassociateOps(ISD::AND, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001756 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001757 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001758 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001759 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001760 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001761 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001762 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001763 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1764 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00001765 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001766 APInt Mask = ~N1C->getAPIntValue();
1767 Mask.trunc(N0Op0.getValueSizeInBits());
1768 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001769 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001770 N0Op0);
Chris Lattner1ec05d12006-03-01 21:47:21 +00001771
1772 // Replace uses of the AND with uses of the Zero extend node.
1773 CombineTo(N, Zext);
1774
Chris Lattner3603cd62006-02-02 07:17:31 +00001775 // We actually want to replace all uses of the any_extend with the
1776 // zero_extend, to avoid duplicating things. This will later cause this
1777 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00001778 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00001779 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001780 }
1781 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001782 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1783 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1784 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1785 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1786
1787 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001788 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001789 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001790 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00001791 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001792 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001793 return DAG.getSetCC(VT, ORNode, LR, Op1);
1794 }
1795 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1796 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00001797 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001798 AddToWorkList(ANDNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001799 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1800 }
1801 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1802 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00001803 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001804 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001805 return DAG.getSetCC(VT, ORNode, LR, Op1);
1806 }
1807 }
1808 // canonicalize equivalent to ll == rl
1809 if (LL == RR && LR == RL) {
1810 Op1 = ISD::getSetCCSwappedOperands(Op1);
1811 std::swap(RL, RR);
1812 }
1813 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001814 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001815 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00001816 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001817 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001818 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1819 }
1820 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001821
1822 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1823 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001824 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001825 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001826 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001827
Nate Begemande996292006-02-03 22:24:05 +00001828 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1829 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001830 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00001831 SimplifyDemandedBits(SDValue(N, 0)))
1832 return SDValue(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001833 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00001834 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00001835 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001836 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001837 // If we zero all the possible extended bits, then we can turn this into
1838 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001839 unsigned BitWidth = N1.getValueSizeInBits();
1840 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001841 BitWidth - EVT.getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001842 ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00001843 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001844 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00001845 LN0->getBasePtr(), LN0->getSrcValue(),
1846 LN0->getSrcValueOffset(), EVT,
1847 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001848 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001849 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001850 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001851 }
1852 }
1853 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00001854 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00001855 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001856 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001857 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001858 // If we zero all the possible extended bits, then we can turn this into
1859 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001860 unsigned BitWidth = N1.getValueSizeInBits();
1861 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001862 BitWidth - EVT.getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001863 ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00001864 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001865 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00001866 LN0->getBasePtr(), LN0->getSrcValue(),
1867 LN0->getSrcValueOffset(), EVT,
1868 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001869 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001870 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001871 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001872 }
1873 }
Chris Lattner15045b62006-02-28 06:35:35 +00001874
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001875 // fold (and (load x), 255) -> (zextload x, i8)
1876 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001877 if (N1C && N0.getOpcode() == ISD::LOAD) {
1878 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1879 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001880 LN0->isUnindexed() && N0.hasOneUse() &&
1881 // Do not change the width of a volatile load.
1882 !LN0->isVolatile()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00001883 MVT EVT = MVT::Other;
1884 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
1885 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue()))
1886 EVT = MVT::getIntegerVT(ActiveBits);
1887
1888 MVT LoadedVT = LN0->getMemoryVT();
Duncan Sandsad205a72008-06-16 08:14:38 +00001889 // Do not generate loads of non-round integer types since these can
1890 // be expensive (and would be wrong if the type is not byte sized).
1891 if (EVT != MVT::Other && LoadedVT.bitsGT(EVT) && EVT.isRound() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001892 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001893 MVT PtrType = N0.getOperand(1).getValueType();
Evan Cheng466685d2006-10-09 20:57:25 +00001894 // For big endian targets, we need to add an offset to the pointer to
1895 // load the correct bytes. For little endian systems, we merely need to
1896 // read fewer bytes from the same pointer.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001897 unsigned LVTStoreBytes = LoadedVT.getStoreSizeInBits()/8;
1898 unsigned EVTStoreBytes = EVT.getStoreSizeInBits()/8;
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001899 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001900 unsigned Alignment = LN0->getAlignment();
Dan Gohman475871a2008-07-27 21:46:04 +00001901 SDValue NewPtr = LN0->getBasePtr();
Duncan Sands0753fc12008-02-11 10:37:04 +00001902 if (TLI.isBigEndian()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001903 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1904 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001905 Alignment = MinAlign(Alignment, PtrOff);
1906 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001907 AddToWorkList(NewPtr.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00001908 SDValue Load =
Evan Cheng466685d2006-10-09 20:57:25 +00001909 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001910 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00001911 LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001912 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001913 CombineTo(N0.getNode(), Load, Load.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001914 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng466685d2006-10-09 20:57:25 +00001915 }
Chris Lattner15045b62006-02-28 06:35:35 +00001916 }
1917 }
1918
Dan Gohman475871a2008-07-27 21:46:04 +00001919 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001920}
1921
Dan Gohman475871a2008-07-27 21:46:04 +00001922SDValue DAGCombiner::visitOR(SDNode *N) {
1923 SDValue N0 = N->getOperand(0);
1924 SDValue N1 = N->getOperand(1);
1925 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001926 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1927 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001928 MVT VT = N1.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001929
Dan Gohman7f321562007-06-25 16:23:39 +00001930 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001931 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001932 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001933 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001934 }
Dan Gohman7f321562007-06-25 16:23:39 +00001935
Dan Gohman613e0d82007-07-03 14:03:57 +00001936 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001937 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001938 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001939 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001940 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001941 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001942 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001943 if (N0C && !N1C)
1944 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001945 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001946 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001947 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001948 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001949 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001950 return N1;
1951 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001952 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001953 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001954 // reassociate or
Dan Gohman475871a2008-07-27 21:46:04 +00001955 SDValue ROR = ReassociateOps(ISD::OR, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001956 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001957 return ROR;
1958 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Gabor Greifba36cb52008-08-28 21:40:38 +00001959 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001960 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001961 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1962 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1963 N1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001964 DAG.getConstant(N1C->getAPIntValue() |
1965 C1->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001966 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001967 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1968 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1969 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1970 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1971
1972 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001973 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001974 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1975 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001976 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001977 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001978 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001979 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001980 return DAG.getSetCC(VT, ORNode, LR, Op1);
1981 }
1982 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1983 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1984 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1985 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001986 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001987 AddToWorkList(ANDNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001988 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1989 }
1990 }
1991 // canonicalize equivalent to ll == rl
1992 if (LL == RR && LR == RL) {
1993 Op1 = ISD::getSetCCSwappedOperands(Op1);
1994 std::swap(RL, RR);
1995 }
1996 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001997 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001998 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00001999 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002000 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002001 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
2002 }
2003 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002004
2005 // Simplify: or (op x...), (op y...) -> (op (or x, y))
2006 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002007 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002008 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002009 }
Chris Lattner516b9622006-09-14 20:50:57 +00002010
Chris Lattner1ec72732006-09-14 21:11:37 +00002011 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
2012 if (N0.getOpcode() == ISD::AND &&
2013 N1.getOpcode() == ISD::AND &&
2014 N0.getOperand(1).getOpcode() == ISD::Constant &&
2015 N1.getOperand(1).getOpcode() == ISD::Constant &&
2016 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00002017 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00002018 // We can only do this xform if we know that bits from X that are set in C2
2019 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002020 const APInt &LHSMask =
2021 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
2022 const APInt &RHSMask =
2023 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Chris Lattner1ec72732006-09-14 21:11:37 +00002024
Dan Gohmanea859be2007-06-22 14:59:07 +00002025 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
2026 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002027 SDValue X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
Chris Lattner1ec72732006-09-14 21:11:37 +00002028 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
2029 }
2030 }
2031
2032
Chris Lattner516b9622006-09-14 20:50:57 +00002033 // See if this is some rotate idiom.
2034 if (SDNode *Rot = MatchRotate(N0, N1))
Dan Gohman475871a2008-07-27 21:46:04 +00002035 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00002036
Dan Gohman475871a2008-07-27 21:46:04 +00002037 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002038}
2039
Chris Lattner516b9622006-09-14 20:50:57 +00002040
2041/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00002042static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00002043 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00002044 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00002045 Mask = Op.getOperand(1);
2046 Op = Op.getOperand(0);
2047 } else {
2048 return false;
2049 }
2050 }
2051
2052 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
2053 Shift = Op;
2054 return true;
2055 }
2056 return false;
2057}
2058
2059
2060// MatchRotate - Handle an 'or' of two operands. If this is one of the many
2061// idioms for rotate, and if the target supports rotation instructions, generate
2062// a rot[lr].
Dan Gohman475871a2008-07-27 21:46:04 +00002063SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002064 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002065 MVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00002066 if (!TLI.isTypeLegal(VT)) return 0;
2067
2068 // The target must have at least one rotate flavor.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002069 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
2070 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Chris Lattner516b9622006-09-14 20:50:57 +00002071 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002072
Chris Lattner516b9622006-09-14 20:50:57 +00002073 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00002074 SDValue LHSShift; // The shift.
2075 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00002076 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
2077 return 0; // Not part of a rotate.
2078
Dan Gohman475871a2008-07-27 21:46:04 +00002079 SDValue RHSShift; // The shift.
2080 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00002081 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
2082 return 0; // Not part of a rotate.
2083
2084 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
2085 return 0; // Not shifting the same value.
2086
2087 if (LHSShift.getOpcode() == RHSShift.getOpcode())
2088 return 0; // Shifts must disagree.
2089
2090 // Canonicalize shl to left side in a shl/srl pair.
2091 if (RHSShift.getOpcode() == ISD::SHL) {
2092 std::swap(LHS, RHS);
2093 std::swap(LHSShift, RHSShift);
2094 std::swap(LHSMask , RHSMask );
2095 }
2096
Duncan Sands83ec4b62008-06-06 12:08:01 +00002097 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00002098 SDValue LHSShiftArg = LHSShift.getOperand(0);
2099 SDValue LHSShiftAmt = LHSShift.getOperand(1);
2100 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00002101
2102 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
2103 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00002104 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
2105 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002106 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
2107 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00002108 if ((LShVal + RShVal) != OpSizeInBits)
2109 return 0;
2110
Dan Gohman475871a2008-07-27 21:46:04 +00002111 SDValue Rot;
Chris Lattner516b9622006-09-14 20:50:57 +00002112 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002113 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002114 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002115 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002116
2117 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00002118 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002119 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Chris Lattner516b9622006-09-14 20:50:57 +00002120
Gabor Greifba36cb52008-08-28 21:40:38 +00002121 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002122 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2123 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002124 }
Gabor Greifba36cb52008-08-28 21:40:38 +00002125 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002126 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2127 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002128 }
2129
2130 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
2131 }
2132
Gabor Greifba36cb52008-08-28 21:40:38 +00002133 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002134 }
2135
2136 // If there is a mask here, and we have a variable shift, we can't be sure
2137 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00002138 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00002139 return 0;
2140
2141 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2142 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002143 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2144 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002145 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002146 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002147 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002148 if (HasROTL)
Gabor Greifba36cb52008-08-28 21:40:38 +00002149 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002150 else
Gabor Greifba36cb52008-08-28 21:40:38 +00002151 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002152 }
Chris Lattner516b9622006-09-14 20:50:57 +00002153 }
2154 }
2155
2156 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2157 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002158 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2159 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002160 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002161 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002162 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling2692d592008-08-31 01:13:31 +00002163 if (HasROTR)
Gabor Greifba36cb52008-08-28 21:40:38 +00002164 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).getNode();
Bill Wendling2692d592008-08-31 01:13:31 +00002165 else
2166 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002167 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002168 }
2169 }
2170
Dan Gohman74feef22008-10-17 01:23:35 +00002171 // Look for sign/zext/any-extended or truncate cases:
Scott Michelc9dc1142007-04-02 21:36:32 +00002172 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2173 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00002174 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2175 || LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
Scott Michelc9dc1142007-04-02 21:36:32 +00002176 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2177 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00002178 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2179 || RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002180 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
2181 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +00002182 if (RExtOp0.getOpcode() == ISD::SUB &&
2183 RExtOp0.getOperand(1) == LExtOp0) {
2184 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002185 // (rotl x, y)
Scott Michelc9dc1142007-04-02 21:36:32 +00002186 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002187 // (rotr x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00002188 if (ConstantSDNode *SUBC =
2189 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002190 if (SUBC->getAPIntValue() == OpSizeInBits) {
Gabor Greif12632d22008-08-30 19:29:20 +00002191 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, VT, LHSShiftArg,
2192 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002193 }
2194 }
2195 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2196 RExtOp0 == LExtOp0.getOperand(1)) {
Bill Wendling353dea22008-08-31 01:04:56 +00002197 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002198 // (rotr x, y)
Bill Wendling353dea22008-08-31 01:04:56 +00002199 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002200 // (rotl x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00002201 if (ConstantSDNode *SUBC =
2202 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002203 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling353dea22008-08-31 01:04:56 +00002204 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, VT, LHSShiftArg,
2205 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002206 }
2207 }
Chris Lattner516b9622006-09-14 20:50:57 +00002208 }
2209 }
2210
2211 return 0;
2212}
2213
2214
Dan Gohman475871a2008-07-27 21:46:04 +00002215SDValue DAGCombiner::visitXOR(SDNode *N) {
2216 SDValue N0 = N->getOperand(0);
2217 SDValue N1 = N->getOperand(1);
2218 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00002219 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2220 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002221 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002222
Dan Gohman7f321562007-06-25 16:23:39 +00002223 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002224 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002225 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002226 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002227 }
Dan Gohman7f321562007-06-25 16:23:39 +00002228
Evan Cheng26471c42008-03-25 20:08:07 +00002229 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
2230 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
2231 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00002232 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002233 if (N0.getOpcode() == ISD::UNDEF)
2234 return N0;
2235 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002236 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002237 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002238 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002239 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002240 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002241 if (N0C && !N1C)
2242 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002243 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002244 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002245 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002246 // reassociate xor
Dan Gohman475871a2008-07-27 21:46:04 +00002247 SDValue RXOR = ReassociateOps(ISD::XOR, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002248 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002249 return RXOR;
Bill Wendlingae89bb12008-11-11 08:25:46 +00002250
Nate Begeman1d4d4142005-09-01 00:19:25 +00002251 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00002252 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002253 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00002254 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2255 isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +00002256
Duncan Sands25cf2272008-11-24 14:53:14 +00002257 if (!LegalOperations || TLI.isCondCodeLegal(NotCC, LHS.getValueType())) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00002258 switch (N0.getOpcode()) {
2259 default:
2260 assert(0 && "Unhandled SetCC Equivalent!");
2261 abort();
2262 case ISD::SETCC:
2263 return DAG.getSetCC(VT, LHS, RHS, NotCC);
2264 case ISD::SELECT_CC:
2265 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),
2266 N0.getOperand(3), NotCC);
2267 }
2268 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00002269 }
Bill Wendlingae89bb12008-11-11 08:25:46 +00002270
Chris Lattner61c5ff42007-09-10 21:39:07 +00002271 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002272 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00002273 N0.getNode()->hasOneUse() &&
2274 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00002275 SDValue V = N0.getOperand(0);
Chris Lattner61c5ff42007-09-10 21:39:07 +00002276 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002277 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002278 AddToWorkList(V.getNode());
Chris Lattner61c5ff42007-09-10 21:39:07 +00002279 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2280 }
2281
Nate Begeman99801192005-09-07 23:25:52 +00002282 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Dan Gohman002e5d02008-03-13 22:13:53 +00002283 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002284 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002285 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002286 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2287 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002288 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2289 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002290 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Nate Begeman99801192005-09-07 23:25:52 +00002291 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002292 }
2293 }
Nate Begeman99801192005-09-07 23:25:52 +00002294 // fold !(x or y) -> (!x and !y) iff x or y are constants
2295 if (N1C && N1C->isAllOnesValue() &&
2296 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002297 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002298 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2299 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002300 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2301 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002302 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Nate Begeman99801192005-09-07 23:25:52 +00002303 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002304 }
2305 }
Nate Begeman223df222005-09-08 20:18:10 +00002306 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2307 if (N1C && N0.getOpcode() == ISD::XOR) {
2308 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2309 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2310 if (N00C)
2311 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00002312 DAG.getConstant(N1C->getAPIntValue()^
2313 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002314 if (N01C)
2315 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
Dan Gohman002e5d02008-03-13 22:13:53 +00002316 DAG.getConstant(N1C->getAPIntValue()^
2317 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002318 }
2319 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002320 if (N0 == N1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002321 if (!VT.isVector()) {
Chris Lattner4fbdd592006-03-28 19:11:05 +00002322 return DAG.getConstant(0, VT);
Duncan Sands25cf2272008-11-24 14:53:14 +00002323 } else if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)){
Chris Lattner4fbdd592006-03-28 19:11:05 +00002324 // Produce a vector of zeros.
Dan Gohman475871a2008-07-27 21:46:04 +00002325 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
2326 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002327 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002328 }
2329 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002330
2331 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2332 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002333 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002334 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002335 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002336
Chris Lattner3e104b12006-04-08 04:15:24 +00002337 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002338 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002339 SimplifyDemandedBits(SDValue(N, 0)))
2340 return SDValue(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002341
Dan Gohman475871a2008-07-27 21:46:04 +00002342 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002343}
2344
Chris Lattnere70da202007-12-06 07:33:36 +00002345/// visitShiftByConstant - Handle transforms common to the three shifts, when
2346/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00002347SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002348 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00002349 if (!LHS->hasOneUse()) return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002350
2351 // We want to pull some binops through shifts, so that we have (and (shift))
2352 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2353 // thing happens with address calculations, so it's important to canonicalize
2354 // it.
2355 bool HighBitSet = false; // Can we transform this if the high bit is set?
2356
2357 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002358 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002359 case ISD::OR:
2360 case ISD::XOR:
2361 HighBitSet = false; // We can only transform sra if the high bit is clear.
2362 break;
2363 case ISD::AND:
2364 HighBitSet = true; // We can only transform sra if the high bit is set.
2365 break;
2366 case ISD::ADD:
2367 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00002368 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00002369 HighBitSet = false; // We can only transform sra if the high bit is clear.
2370 break;
2371 }
2372
2373 // We require the RHS of the binop to be a constant as well.
2374 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002375 if (!BinOpCst) return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002376
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002377
2378 // FIXME: disable this for unless the input to the binop is a shift by a
2379 // constant. If it is not a shift, it pessimizes some common cases like:
2380 //
2381 //void foo(int *X, int i) { X[i & 1235] = 1; }
2382 //int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00002383 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002384 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2385 BinOpLHSVal->getOpcode() != ISD::SRA &&
2386 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2387 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00002388 return SDValue();
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002389
Duncan Sands83ec4b62008-06-06 12:08:01 +00002390 MVT VT = N->getValueType(0);
Chris Lattnere70da202007-12-06 07:33:36 +00002391
2392 // If this is a signed shift right, and the high bit is modified
2393 // by the logical operation, do not perform the transformation.
2394 // The highBitSet boolean indicates the value of the high bit of
2395 // the constant which would cause it to be modified for this
2396 // operation.
2397 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00002398 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2399 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00002400 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002401 }
2402
2403 // Fold the constants, shifting the binop RHS by the shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00002404 SDValue NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
Chris Lattnere70da202007-12-06 07:33:36 +00002405 LHS->getOperand(1), N->getOperand(1));
2406
2407 // Create the new shift.
Dan Gohman475871a2008-07-27 21:46:04 +00002408 SDValue NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
Chris Lattnere70da202007-12-06 07:33:36 +00002409 N->getOperand(1));
2410
2411 // Create the new binop.
2412 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2413}
2414
2415
Dan Gohman475871a2008-07-27 21:46:04 +00002416SDValue DAGCombiner::visitSHL(SDNode *N) {
2417 SDValue N0 = N->getOperand(0);
2418 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002419 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2420 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002421 MVT VT = N0.getValueType();
2422 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002423
2424 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002425 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002426 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002427 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002428 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002429 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002430 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002431 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002432 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002433 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002434 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002435 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002436 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002437 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002438 APInt::getAllOnesValue(VT.getSizeInBits())))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002439 return DAG.getConstant(0, VT);
Evan Chengeb9f8922008-08-30 02:03:58 +00002440 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), c))
2441 // iff (trunc c) == c
2442 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002443 N1.getOperand(0).getOpcode() == ISD::AND &&
2444 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002445 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002446 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002447 MVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002448 SDValue N100 = N1.getOperand(0).getOperand(0);
Dan Gohmance9bc122009-01-27 20:39:34 +00002449 uint64_t TruncC = TruncVT.getIntegerVTBitMask() &
2450 N101C->getZExtValue();
Evan Cheng242ebd12008-09-22 18:19:24 +00002451 return DAG.getNode(ISD::SHL, VT, N0,
2452 DAG.getNode(ISD::AND, TruncVT,
2453 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00002454 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002455 }
2456 }
2457
Dan Gohman475871a2008-07-27 21:46:04 +00002458 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2459 return SDValue(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002460 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002461 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002462 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002463 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2464 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002465 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002466 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002467 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002468 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002469 }
2470 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2471 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002472 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002473 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002474 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2475 uint64_t c2 = N1C->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00002476 SDValue Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman1d4d4142005-09-01 00:19:25 +00002477 DAG.getConstant(~0ULL << c1, VT));
2478 if (c2 > c1)
2479 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002480 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002481 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002482 return DAG.getNode(ISD::SRL, VT, Mask,
2483 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002484 }
2485 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002486 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002487 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002488 DAG.getConstant(~0ULL << N1C->getZExtValue(), VT));
Chris Lattnere70da202007-12-06 07:33:36 +00002489
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002490 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002491}
2492
Dan Gohman475871a2008-07-27 21:46:04 +00002493SDValue DAGCombiner::visitSRA(SDNode *N) {
2494 SDValue N0 = N->getOperand(0);
2495 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002496 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2497 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002498 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002499
2500 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002501 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002502 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002503 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002504 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002505 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002506 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002507 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002508 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002509 // fold (sra x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002510 if (N1C && N1C->getZExtValue() >= VT.getSizeInBits())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002511 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002512 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002513 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002514 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002515 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2516 // sext_inreg.
2517 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002518 unsigned LowBits = VT.getSizeInBits() - (unsigned)N1C->getZExtValue();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002519 MVT EVT = MVT::getIntegerVT(LowBits);
Duncan Sands25cf2272008-11-24 14:53:14 +00002520 if ((!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT)))
Nate Begemanfb7217b2006-02-17 19:54:08 +00002521 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2522 DAG.getValueType(EVT));
2523 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002524
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002525 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2526 if (N1C && N0.getOpcode() == ISD::SRA) {
2527 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002528 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002529 if (Sum >= VT.getSizeInBits()) Sum = VT.getSizeInBits()-1;
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002530 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2531 DAG.getConstant(Sum, N1C->getValueType(0)));
2532 }
2533 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00002534
2535 // fold sra (shl X, m), result_size - n
2536 // -> (sign_extend (trunc (shl X, result_size - n - m))) for
Christopher Lambb9b04282008-03-20 04:31:39 +00002537 // result_size - n != m.
2538 // If truncate is free for the target sext(shl) is likely to result in better
2539 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002540 if (N0.getOpcode() == ISD::SHL) {
2541 // Get the two constanst of the shifts, CN0 = m, CN = n.
2542 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2543 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002544 // Determine what the truncate's result bitsize and type would be.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002545 unsigned VTValSize = VT.getSizeInBits();
2546 MVT TruncVT =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002547 MVT::getIntegerVT(VTValSize - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00002548 // Determine the residual right-shift amount.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002549 unsigned ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002550
Christopher Lambb9b04282008-03-20 04:31:39 +00002551 // If the shift is not a no-op (in which case this should be just a sign
2552 // extend already), the truncated to type is legal, sign_extend is legal
Gabor Greif12632d22008-08-30 19:29:20 +00002553 // on that type, and the the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00002554 // perform the transform.
Duncan Sands25cf2272008-11-24 14:53:14 +00002555 if (ShiftAmt &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002556 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
2557 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00002558 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002559
Dan Gohman475871a2008-07-27 21:46:04 +00002560 SDValue Amt = DAG.getConstant(ShiftAmt, TLI.getShiftAmountTy());
2561 SDValue Shift = DAG.getNode(ISD::SRL, VT, N0.getOperand(0), Amt);
2562 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, TruncVT, Shift);
Christopher Lambb9b04282008-03-20 04:31:39 +00002563 return DAG.getNode(ISD::SIGN_EXTEND, N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00002564 }
2565 }
2566 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002567
Evan Chengeb9f8922008-08-30 02:03:58 +00002568 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), c))
2569 // iff (trunc c) == c
2570 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002571 N1.getOperand(0).getOpcode() == ISD::AND &&
2572 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002573 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002574 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002575 MVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002576 SDValue N100 = N1.getOperand(0).getOperand(0);
Dan Gohmance9bc122009-01-27 20:39:34 +00002577 uint64_t TruncC = TruncVT.getIntegerVTBitMask() &
2578 N101C->getZExtValue();
Evan Cheng242ebd12008-09-22 18:19:24 +00002579 return DAG.getNode(ISD::SRA, VT, N0,
2580 DAG.getNode(ISD::AND, TruncVT,
2581 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00002582 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002583 }
2584 }
2585
Chris Lattnera8504462006-05-08 20:51:54 +00002586 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00002587 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2588 return SDValue(N, 0);
Chris Lattnera8504462006-05-08 20:51:54 +00002589
2590
Nate Begeman1d4d4142005-09-01 00:19:25 +00002591 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002592 if (DAG.SignBitIsZero(N0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002593 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002594
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002595 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002596}
2597
Dan Gohman475871a2008-07-27 21:46:04 +00002598SDValue DAGCombiner::visitSRL(SDNode *N) {
2599 SDValue N0 = N->getOperand(0);
2600 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002601 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2602 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002603 MVT VT = N0.getValueType();
2604 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002605
2606 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002607 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002608 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002609 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002610 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002611 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002612 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002613 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002614 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002615 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002616 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002617 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002618 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002619 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002620 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002621 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002622
Nate Begeman1d4d4142005-09-01 00:19:25 +00002623 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002624 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002625 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002626 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2627 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002628 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002629 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002630 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002631 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002632 }
Chris Lattner350bec02006-04-02 06:11:11 +00002633
Chris Lattner06afe072006-05-05 22:53:17 +00002634 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2635 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2636 // Shifting in all undef bits?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002637 MVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002638 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Chris Lattner06afe072006-05-05 22:53:17 +00002639 return DAG.getNode(ISD::UNDEF, VT);
2640
Dan Gohman475871a2008-07-27 21:46:04 +00002641 SDValue SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002642 AddToWorkList(SmallShift.getNode());
Chris Lattner06afe072006-05-05 22:53:17 +00002643 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2644 }
2645
Chris Lattner3657ffe2006-10-12 20:23:19 +00002646 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2647 // bit, which is unmodified by sra.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002648 if (N1C && N1C->getZExtValue()+1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00002649 if (N0.getOpcode() == ISD::SRA)
2650 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2651 }
2652
Chris Lattner350bec02006-04-02 06:11:11 +00002653 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2654 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002655 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00002656 APInt KnownZero, KnownOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002657 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00002658 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002659
2660 // If any of the input bits are KnownOne, then the input couldn't be all
2661 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002662 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Chris Lattner350bec02006-04-02 06:11:11 +00002663
2664 // If all of the bits input the to ctlz node are known to be zero, then
2665 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002666 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00002667 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2668
2669 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2670 if ((UnknownBits & (UnknownBits-1)) == 0) {
2671 // Okay, we know that only that the single bit specified by UnknownBits
2672 // could be set on input to the CTLZ node. If this bit is set, the SRL
2673 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2674 // to an SRL,XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002675 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00002676 SDValue Op = N0.getOperand(0);
Chris Lattner350bec02006-04-02 06:11:11 +00002677 if (ShAmt) {
2678 Op = DAG.getNode(ISD::SRL, VT, Op,
2679 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002680 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00002681 }
2682 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2683 }
2684 }
Evan Chengeb9f8922008-08-30 02:03:58 +00002685
2686 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), c))
2687 // iff (trunc c) == c
2688 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002689 N1.getOperand(0).getOpcode() == ISD::AND &&
2690 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002691 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002692 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002693 MVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002694 SDValue N100 = N1.getOperand(0).getOperand(0);
Dan Gohmance9bc122009-01-27 20:39:34 +00002695 uint64_t TruncC = TruncVT.getIntegerVTBitMask() &
2696 N101C->getZExtValue();
Evan Cheng242ebd12008-09-22 18:19:24 +00002697 return DAG.getNode(ISD::SRL, VT, N0,
2698 DAG.getNode(ISD::AND, TruncVT,
2699 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00002700 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002701 }
2702 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002703
2704 // fold operands of srl based on knowledge that the low bits are not
2705 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00002706 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2707 return SDValue(N, 0);
Chris Lattner61a4c072007-04-18 03:06:49 +00002708
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002709 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002710}
2711
Dan Gohman475871a2008-07-27 21:46:04 +00002712SDValue DAGCombiner::visitCTLZ(SDNode *N) {
2713 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002714 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002715
2716 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002717 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002718 return DAG.getNode(ISD::CTLZ, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002719 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002720}
2721
Dan Gohman475871a2008-07-27 21:46:04 +00002722SDValue DAGCombiner::visitCTTZ(SDNode *N) {
2723 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002724 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002725
2726 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002727 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002728 return DAG.getNode(ISD::CTTZ, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002729 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002730}
2731
Dan Gohman475871a2008-07-27 21:46:04 +00002732SDValue DAGCombiner::visitCTPOP(SDNode *N) {
2733 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002734 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002735
2736 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002737 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002738 return DAG.getNode(ISD::CTPOP, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002739 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002740}
2741
Dan Gohman475871a2008-07-27 21:46:04 +00002742SDValue DAGCombiner::visitSELECT(SDNode *N) {
2743 SDValue N0 = N->getOperand(0);
2744 SDValue N1 = N->getOperand(1);
2745 SDValue N2 = N->getOperand(2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002746 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2747 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2748 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002749 MVT VT = N->getValueType(0);
2750 MVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002751
Nate Begeman452d7be2005-09-16 00:54:12 +00002752 // fold select C, X, X -> X
2753 if (N1 == N2)
2754 return N1;
2755 // fold select true, X, Y -> X
2756 if (N0C && !N0C->isNullValue())
2757 return N1;
2758 // fold select false, X, Y -> Y
2759 if (N0C && N0C->isNullValue())
2760 return N2;
2761 // fold select C, 1, X -> C | X
Duncan Sands83ec4b62008-06-06 12:08:01 +00002762 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002763 return DAG.getNode(ISD::OR, VT, N0, N2);
Bob Wilson67ba2232009-01-22 22:05:48 +00002764 // fold select C, 0, 1 -> C ^ 1
2765 if (VT.isInteger() &&
2766 (VT0 == MVT::i1 ||
2767 (VT0.isInteger() &&
2768 TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00002769 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00002770 SDValue XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
Evan Cheng571c4782007-08-18 05:57:05 +00002771 if (VT == VT0)
2772 return XORNode;
Gabor Greifba36cb52008-08-28 21:40:38 +00002773 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00002774 if (VT.bitsGT(VT0))
Evan Cheng571c4782007-08-18 05:57:05 +00002775 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2776 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2777 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002778 // fold select C, 0, X -> ~C & X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002779 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Bob Wilson4c245462009-01-22 17:39:32 +00002780 SDValue NOTNode = DAG.getNOT(N0, VT);
2781 AddToWorkList(NOTNode.getNode());
2782 return DAG.getNode(ISD::AND, VT, NOTNode, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002783 }
2784 // fold select C, X, 1 -> ~C | X
Dan Gohman002e5d02008-03-13 22:13:53 +00002785 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Bob Wilson4c245462009-01-22 17:39:32 +00002786 SDValue NOTNode = DAG.getNOT(N0, VT);
2787 AddToWorkList(NOTNode.getNode());
2788 return DAG.getNode(ISD::OR, VT, NOTNode, N1);
Nate Begeman452d7be2005-09-16 00:54:12 +00002789 }
2790 // fold select C, X, 0 -> C & X
Duncan Sands83ec4b62008-06-06 12:08:01 +00002791 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Nate Begeman452d7be2005-09-16 00:54:12 +00002792 return DAG.getNode(ISD::AND, VT, N0, N1);
2793 // fold X ? X : Y --> X ? 1 : Y --> X | Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002794 if (VT == MVT::i1 && N0 == N1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002795 return DAG.getNode(ISD::OR, VT, N0, N2);
2796 // fold X ? Y : X --> X ? Y : 0 --> X & Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002797 if (VT == MVT::i1 && N0 == N2)
Nate Begeman452d7be2005-09-16 00:54:12 +00002798 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002799
Chris Lattner40c62d52005-10-18 06:04:22 +00002800 // If we can fold this based on the true/false value, do so.
2801 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00002802 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002803
Nate Begeman44728a72005-09-19 22:34:01 +00002804 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002805 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002806 // FIXME:
2807 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2808 // having to say they don't support SELECT_CC on every type the DAG knows
2809 // about, since there is no way to mark an opcode illegal at all value types
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002810 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other))
Nate Begeman750ac1b2006-02-01 07:19:44 +00002811 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2812 N1, N2, N0.getOperand(2));
2813 else
2814 return SimplifySelect(N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002815 }
Dan Gohman475871a2008-07-27 21:46:04 +00002816 return SDValue();
Nate Begeman452d7be2005-09-16 00:54:12 +00002817}
2818
Dan Gohman475871a2008-07-27 21:46:04 +00002819SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
2820 SDValue N0 = N->getOperand(0);
2821 SDValue N1 = N->getOperand(1);
2822 SDValue N2 = N->getOperand(2);
2823 SDValue N3 = N->getOperand(3);
2824 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002825 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2826
Nate Begeman44728a72005-09-19 22:34:01 +00002827 // fold select_cc lhs, rhs, x, x, cc -> x
2828 if (N2 == N3)
2829 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002830
Chris Lattner5f42a242006-09-20 06:19:26 +00002831 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00002832 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
2833 N0, N1, CC, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00002834 if (SCC.getNode()) AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00002835
Gabor Greifba36cb52008-08-28 21:40:38 +00002836 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002837 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00002838 return N2; // cond always true -> true val
2839 else
2840 return N3; // cond always false -> false val
2841 }
2842
2843 // Fold to a simpler select_cc
Gabor Greifba36cb52008-08-28 21:40:38 +00002844 if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC)
Chris Lattner5f42a242006-09-20 06:19:26 +00002845 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2846 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2847 SCC.getOperand(2));
2848
Chris Lattner40c62d52005-10-18 06:04:22 +00002849 // If we can fold this based on the true/false value, do so.
2850 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00002851 return SDValue(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002852
Nate Begeman44728a72005-09-19 22:34:01 +00002853 // fold select_cc into other things, such as min/max/abs
2854 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002855}
2856
Dan Gohman475871a2008-07-27 21:46:04 +00002857SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002858 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2859 cast<CondCodeSDNode>(N->getOperand(2))->get());
2860}
2861
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002862// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2863// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2864// transformation. Returns true if extension are possible and the above
2865// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00002866static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002867 unsigned ExtOpc,
2868 SmallVector<SDNode*, 4> &ExtendNodes,
Dan Gohman79ce2762009-01-15 19:20:50 +00002869 const TargetLowering &TLI) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002870 bool HasCopyToRegUses = false;
2871 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00002872 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
2873 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002874 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002875 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002876 if (User == N)
2877 continue;
2878 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2879 if (User->getOpcode() == ISD::SETCC) {
2880 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2881 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2882 // Sign bits will be lost after a zext.
2883 return false;
2884 bool Add = false;
2885 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002886 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002887 if (UseOp == N0)
2888 continue;
2889 if (!isa<ConstantSDNode>(UseOp))
2890 return false;
2891 Add = true;
2892 }
2893 if (Add)
2894 ExtendNodes.push_back(User);
2895 } else {
2896 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002897 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002898 if (UseOp == N0) {
2899 // If truncate from extended type to original load type is free
2900 // on this target, then it's ok to extend a CopyToReg.
2901 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2902 HasCopyToRegUses = true;
2903 else
2904 return false;
2905 }
2906 }
2907 }
2908 }
2909
2910 if (HasCopyToRegUses) {
2911 bool BothLiveOut = false;
2912 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2913 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002914 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002915 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002916 SDValue UseOp = User->getOperand(i);
Gabor Greifba36cb52008-08-28 21:40:38 +00002917 if (UseOp.getNode() == N && UseOp.getResNo() == 0) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002918 BothLiveOut = true;
2919 break;
2920 }
2921 }
2922 }
2923 if (BothLiveOut)
2924 // Both unextended and extended values are live out. There had better be
2925 // good a reason for the transformation.
2926 return ExtendNodes.size();
2927 }
2928 return true;
2929}
2930
Dan Gohman475871a2008-07-27 21:46:04 +00002931SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
2932 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002933 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002934
Nate Begeman1d4d4142005-09-01 00:19:25 +00002935 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002936 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002937 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002938
Nate Begeman1d4d4142005-09-01 00:19:25 +00002939 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002940 // fold (sext (aext x)) -> (sext x)
2941 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002942 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002943
Chris Lattner22558872007-02-26 03:13:59 +00002944 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002945 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2946 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002947 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
2948 if (NarrowLoad.getNode()) {
2949 if (NarrowLoad.getNode() != N0.getNode())
2950 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00002951 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2952 }
Evan Chengc88138f2007-03-22 01:54:19 +00002953
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002954 // See if the value being truncated is already sign extended. If so, just
2955 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00002956 SDValue Op = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002957 unsigned OpBits = Op.getValueType().getSizeInBits();
2958 unsigned MidBits = N0.getValueType().getSizeInBits();
2959 unsigned DestBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002960 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002961
2962 if (OpBits == DestBits) {
2963 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2964 // bits, it is already ready.
2965 if (NumSignBits > DestBits-MidBits)
2966 return Op;
2967 } else if (OpBits < DestBits) {
2968 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2969 // bits, just sext from i32.
2970 if (NumSignBits > OpBits-MidBits)
2971 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2972 } else {
2973 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2974 // bits, just truncate to i32.
2975 if (NumSignBits > OpBits-MidBits)
2976 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002977 }
Chris Lattner22558872007-02-26 03:13:59 +00002978
2979 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sands25cf2272008-11-24 14:53:14 +00002980 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2981 N0.getValueType())) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00002982 if (Op.getValueType().bitsLT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002983 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002984 else if (Op.getValueType().bitsGT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002985 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2986 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2987 DAG.getValueType(N0.getValueType()));
2988 }
Chris Lattner6007b842006-09-21 06:00:20 +00002989 }
Chris Lattner310b5782006-05-06 23:06:26 +00002990
Evan Cheng110dec22005-12-14 02:19:23 +00002991 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002992 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002993 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00002994 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002995 bool DoXform = true;
2996 SmallVector<SDNode*, 4> SetCCs;
2997 if (!N0.hasOneUse())
2998 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
2999 if (DoXform) {
3000 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003001 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003002 LN0->getBasePtr(), LN0->getSrcValue(),
3003 LN0->getSrcValueOffset(),
3004 N0.getValueType(),
3005 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003006 CombineTo(N, ExtLoad);
Dan Gohman475871a2008-07-27 21:46:04 +00003007 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003008 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003009 // Extend SetCC uses if necessary.
3010 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3011 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00003012 SmallVector<SDValue, 4> Ops;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003013 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00003014 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003015 if (SOp == Trunc)
3016 Ops.push_back(ExtLoad);
3017 else
3018 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
3019 }
3020 Ops.push_back(SetCC->getOperand(2));
3021 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
3022 &Ops[0], Ops.size()));
3023 }
Dan Gohman475871a2008-07-27 21:46:04 +00003024 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003025 }
Nate Begeman3df4d522005-10-12 20:40:40 +00003026 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003027
3028 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
3029 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003030 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3031 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003032 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003033 MVT EVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00003034 if ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003035 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003036 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003037 LN0->getBasePtr(), LN0->getSrcValue(),
3038 LN0->getSrcValueOffset(), EVT,
3039 LN0->isVolatile(), LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003040 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00003041 CombineTo(N0.getNode(),
3042 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003043 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003044 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003045 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003046 }
3047
Chris Lattner20a35c32007-04-11 05:32:27 +00003048 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
3049 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003050 SDValue SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003051 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3052 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
3053 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003054 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003055 }
3056
Dan Gohman8f0ad582008-04-28 16:58:24 +00003057 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sands25cf2272008-11-24 14:53:14 +00003058 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman187db7b2008-04-28 18:47:17 +00003059 DAG.SignBitIsZero(N0))
Dan Gohman8f0ad582008-04-28 16:58:24 +00003060 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3061
Dan Gohman475871a2008-07-27 21:46:04 +00003062 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003063}
3064
Dan Gohman475871a2008-07-27 21:46:04 +00003065SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
3066 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003067 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003068
Nate Begeman1d4d4142005-09-01 00:19:25 +00003069 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00003070 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003071 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003072 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00003073 // fold (zext (aext x)) -> (zext x)
3074 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003075 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00003076
Evan Chengc88138f2007-03-22 01:54:19 +00003077 // fold (zext (truncate (load x))) -> (zext (smaller load x))
3078 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00003079 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003080 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3081 if (NarrowLoad.getNode()) {
3082 if (NarrowLoad.getNode() != N0.getNode())
3083 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003084 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
3085 }
Evan Chengc88138f2007-03-22 01:54:19 +00003086 }
3087
Chris Lattner6007b842006-09-21 06:00:20 +00003088 // fold (zext (truncate x)) -> (and x, mask)
3089 if (N0.getOpcode() == ISD::TRUNCATE &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003090 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00003091 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003092 if (Op.getValueType().bitsLT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00003093 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003094 } else if (Op.getValueType().bitsGT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00003095 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
3096 }
3097 return DAG.getZeroExtendInReg(Op, N0.getValueType());
3098 }
3099
Chris Lattner111c2282006-09-21 06:14:31 +00003100 // fold (zext (and (trunc x), cst)) -> (and x, cst).
3101 if (N0.getOpcode() == ISD::AND &&
3102 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3103 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman475871a2008-07-27 21:46:04 +00003104 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003105 if (X.getValueType().bitsLT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00003106 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003107 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00003108 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3109 }
Dan Gohman220a8232008-03-03 23:51:38 +00003110 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003111 Mask.zext(VT.getSizeInBits());
Chris Lattner111c2282006-09-21 06:14:31 +00003112 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3113 }
3114
Evan Cheng110dec22005-12-14 02:19:23 +00003115 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003116 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003117 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003118 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003119 bool DoXform = true;
3120 SmallVector<SDNode*, 4> SetCCs;
3121 if (!N0.hasOneUse())
3122 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
3123 if (DoXform) {
3124 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003125 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003126 LN0->getBasePtr(), LN0->getSrcValue(),
3127 LN0->getSrcValueOffset(),
3128 N0.getValueType(),
3129 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003130 CombineTo(N, ExtLoad);
Dan Gohman475871a2008-07-27 21:46:04 +00003131 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003132 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003133 // Extend SetCC uses if necessary.
3134 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3135 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00003136 SmallVector<SDValue, 4> Ops;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003137 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00003138 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003139 if (SOp == Trunc)
3140 Ops.push_back(ExtLoad);
3141 else
Evan Chengde1631b2007-10-30 20:11:21 +00003142 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003143 }
3144 Ops.push_back(SetCC->getOperand(2));
3145 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
3146 &Ops[0], Ops.size()));
3147 }
Dan Gohman475871a2008-07-27 21:46:04 +00003148 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003149 }
Evan Cheng110dec22005-12-14 02:19:23 +00003150 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003151
3152 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
3153 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003154 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3155 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003156 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003157 MVT EVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00003158 if ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003159 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003160 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003161 LN0->getBasePtr(), LN0->getSrcValue(),
3162 LN0->getSrcValueOffset(), EVT,
3163 LN0->isVolatile(), LN0->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003164 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00003165 CombineTo(N0.getNode(),
3166 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003167 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003168 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003169 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003170 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003171
3172 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3173 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003174 SDValue SCC =
Chris Lattner20a35c32007-04-11 05:32:27 +00003175 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3176 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003177 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003178 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003179 }
3180
Dan Gohman475871a2008-07-27 21:46:04 +00003181 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003182}
3183
Dan Gohman475871a2008-07-27 21:46:04 +00003184SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
3185 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003186 MVT VT = N->getValueType(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00003187
3188 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003189 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00003190 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
3191 // fold (aext (aext x)) -> (aext x)
3192 // fold (aext (zext x)) -> (zext x)
3193 // fold (aext (sext x)) -> (sext x)
3194 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3195 N0.getOpcode() == ISD::ZERO_EXTEND ||
3196 N0.getOpcode() == ISD::SIGN_EXTEND)
3197 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
3198
Evan Chengc88138f2007-03-22 01:54:19 +00003199 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3200 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3201 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003202 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3203 if (NarrowLoad.getNode()) {
3204 if (NarrowLoad.getNode() != N0.getNode())
3205 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003206 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
3207 }
Evan Chengc88138f2007-03-22 01:54:19 +00003208 }
3209
Chris Lattner84750582006-09-20 06:29:17 +00003210 // fold (aext (truncate x))
3211 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00003212 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00003213 if (TruncOp.getValueType() == VT)
3214 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003215 if (TruncOp.getValueType().bitsGT(VT))
Chris Lattner84750582006-09-20 06:29:17 +00003216 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
3217 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
3218 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00003219
3220 // fold (aext (and (trunc x), cst)) -> (and x, cst).
3221 if (N0.getOpcode() == ISD::AND &&
3222 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3223 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman475871a2008-07-27 21:46:04 +00003224 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003225 if (X.getValueType().bitsLT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003226 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003227 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003228 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3229 }
Dan Gohman220a8232008-03-03 23:51:38 +00003230 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003231 Mask.zext(VT.getSizeInBits());
Chris Lattner0e4b9222006-09-21 06:40:43 +00003232 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3233 }
3234
Chris Lattner5ffc0662006-05-05 05:58:59 +00003235 // fold (aext (load x)) -> (aext (truncate (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003236 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003237 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003238 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003239 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003240 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003241 LN0->getBasePtr(), LN0->getSrcValue(),
3242 LN0->getSrcValueOffset(),
3243 N0.getValueType(),
3244 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003245 CombineTo(N, ExtLoad);
Dan Gohman75dcf082008-07-31 00:50:31 +00003246 // Redirect any chain users to the new load.
Evan Cheng45299662008-08-29 23:20:46 +00003247 DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1),
3248 SDValue(ExtLoad.getNode(), 1));
Dan Gohman75dcf082008-07-31 00:50:31 +00003249 // If any node needs the original loaded value, recompute it.
3250 if (!LN0->use_empty())
3251 CombineTo(LN0, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3252 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003253 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003254 }
3255
3256 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3257 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3258 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003259 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003260 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003261 N0.hasOneUse()) {
3262 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003263 MVT EVT = LN0->getMemoryVT();
Dan Gohman475871a2008-07-27 21:46:04 +00003264 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00003265 LN0->getChain(), LN0->getBasePtr(),
3266 LN0->getSrcValue(),
3267 LN0->getSrcValueOffset(), EVT,
3268 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003269 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00003270 CombineTo(N0.getNode(),
3271 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00003272 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003273 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003274 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003275
3276 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3277 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003278 SDValue SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003279 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3280 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003281 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003282 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003283 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003284 }
3285
Dan Gohman475871a2008-07-27 21:46:04 +00003286 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00003287}
3288
Chris Lattner2b4c2792007-10-13 06:35:54 +00003289/// GetDemandedBits - See if the specified operand can be simplified with the
3290/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00003291/// simpler operand, otherwise return a null SDValue.
3292SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003293 switch (V.getOpcode()) {
3294 default: break;
3295 case ISD::OR:
3296 case ISD::XOR:
3297 // If the LHS or RHS don't contribute bits to the or, drop them.
3298 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3299 return V.getOperand(1);
3300 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3301 return V.getOperand(0);
3302 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003303 case ISD::SRL:
3304 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003305 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00003306 break;
3307 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3308 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003309 unsigned Amt = RHSC->getZExtValue();
Dan Gohmancc91d632009-01-03 19:22:06 +00003310 // Watch out for shift count overflow though.
3311 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003312 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00003313 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Gabor Greifba36cb52008-08-28 21:40:38 +00003314 if (SimplifyLHS.getNode()) {
Chris Lattnere33544c2007-10-13 06:58:48 +00003315 return DAG.getNode(ISD::SRL, V.getValueType(),
3316 SimplifyLHS, V.getOperand(1));
3317 }
3318 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003319 }
Dan Gohman475871a2008-07-27 21:46:04 +00003320 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00003321}
3322
Evan Chengc88138f2007-03-22 01:54:19 +00003323/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3324/// bits and then truncated to a narrower type and where N is a multiple
3325/// of number of bits of the narrower type, transform it to a narrower load
3326/// from address + N / num of bits of new type. If the result is to be
3327/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00003328SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00003329 unsigned Opc = N->getOpcode();
3330 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00003331 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003332 MVT VT = N->getValueType(0);
Dan Gohman764fd0c2009-01-21 15:17:51 +00003333 MVT EVT = VT;
Evan Chengc88138f2007-03-22 01:54:19 +00003334
Dan Gohman7f8613e2008-08-14 20:04:46 +00003335 // This transformation isn't valid for vector loads.
3336 if (VT.isVector())
3337 return SDValue();
3338
Evan Chenge177e302007-03-23 22:13:36 +00003339 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3340 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003341 if (Opc == ISD::SIGN_EXTEND_INREG) {
3342 ExtType = ISD::SEXTLOAD;
3343 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00003344 if (LegalOperations && !TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))
Dan Gohman475871a2008-07-27 21:46:04 +00003345 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003346 }
3347
Duncan Sands83ec4b62008-06-06 12:08:01 +00003348 unsigned EVTBits = EVT.getSizeInBits();
Evan Chengc88138f2007-03-22 01:54:19 +00003349 unsigned ShAmt = 0;
3350 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3351 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003352 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003353 // Is the shift amount a multiple of size of VT?
3354 if ((ShAmt & (EVTBits-1)) == 0) {
3355 N0 = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003356 if (N0.getValueType().getSizeInBits() <= EVTBits)
Dan Gohman475871a2008-07-27 21:46:04 +00003357 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003358 }
3359 }
3360 }
3361
Duncan Sandsad205a72008-06-16 08:14:38 +00003362 // Do not generate loads of non-round integer types since these can
3363 // be expensive (and would be wrong if the type is not byte sized).
Dan Gohmanf5add582009-01-20 01:06:45 +00003364 if (isa<LoadSDNode>(N0) && N0.hasOneUse() && EVT.isRound() &&
Dan Gohman75dcf082008-07-31 00:50:31 +00003365 cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() > EVTBits &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003366 // Do not change the width of a volatile load.
3367 !cast<LoadSDNode>(N0)->isVolatile()) {
Evan Chengc88138f2007-03-22 01:54:19 +00003368 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003369 MVT PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00003370 // For big endian targets, we need to adjust the offset to the pointer to
3371 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003372 if (TLI.isBigEndian()) {
Dan Gohman75dcf082008-07-31 00:50:31 +00003373 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003374 unsigned EVTStoreBits = EVT.getStoreSizeInBits();
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003375 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3376 }
Evan Chengdae54ce2007-03-24 00:02:43 +00003377 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003378 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Dan Gohman475871a2008-07-27 21:46:04 +00003379 SDValue NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003380 DAG.getConstant(PtrOff, PtrType));
Gabor Greifba36cb52008-08-28 21:40:38 +00003381 AddToWorkList(NewPtr.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00003382 SDValue Load = (ExtType == ISD::NON_EXTLOAD)
Evan Chengc88138f2007-03-22 01:54:19 +00003383 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003384 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
Duncan Sandsdc846502007-10-28 12:59:45 +00003385 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003386 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003387 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
3388 EVT, LN0->isVolatile(), NewAlign);
Dan Gohman764fd0c2009-01-21 15:17:51 +00003389 // Replace the old load's chain with the new load's chain.
3390 WorkListRemover DeadNodes(*this);
3391 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3392 &DeadNodes);
3393 // Return the new loaded value.
3394 return Load;
Evan Chengc88138f2007-03-22 01:54:19 +00003395 }
3396
Dan Gohman475871a2008-07-27 21:46:04 +00003397 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003398}
3399
Chris Lattner5ffc0662006-05-05 05:58:59 +00003400
Dan Gohman475871a2008-07-27 21:46:04 +00003401SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
3402 SDValue N0 = N->getOperand(0);
3403 SDValue N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003404 MVT VT = N->getValueType(0);
3405 MVT EVT = cast<VTSDNode>(N1)->getVT();
3406 unsigned VTBits = VT.getSizeInBits();
3407 unsigned EVTBits = EVT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003408
Nate Begeman1d4d4142005-09-01 00:19:25 +00003409 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003410 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003411 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003412
Chris Lattner541a24f2006-05-06 22:43:44 +00003413 // If the input is already sign extended, just drop the extension.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003414 if (DAG.ComputeNumSignBits(N0) >= VT.getSizeInBits()-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003415 return N0;
3416
Nate Begeman646d7e22005-09-02 21:18:40 +00003417 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3418 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00003419 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003420 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003421 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003422
Dan Gohman75dcf082008-07-31 00:50:31 +00003423 // fold (sext_in_reg (sext x)) -> (sext x)
3424 // fold (sext_in_reg (aext x)) -> (sext x)
3425 // if x is small enough.
3426 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
3427 SDValue N00 = N0.getOperand(0);
3428 if (N00.getValueType().getSizeInBits() < EVTBits)
3429 return DAG.getNode(ISD::SIGN_EXTEND, VT, N00, N1);
3430 }
3431
Chris Lattner95a5e052007-04-17 19:03:21 +00003432 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003433 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Nate Begemande996292006-02-03 22:24:05 +00003434 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003435
Chris Lattner95a5e052007-04-17 19:03:21 +00003436 // fold operands of sext_in_reg based on knowledge that the top bits are not
3437 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00003438 if (SimplifyDemandedBits(SDValue(N, 0)))
3439 return SDValue(N, 0);
Chris Lattner95a5e052007-04-17 19:03:21 +00003440
Evan Chengc88138f2007-03-22 01:54:19 +00003441 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3442 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00003443 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003444 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00003445 return NarrowLoad;
3446
Chris Lattner4b37e872006-05-08 21:18:59 +00003447 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3448 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3449 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3450 if (N0.getOpcode() == ISD::SRL) {
3451 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003452 if (ShAmt->getZExtValue()+EVTBits <= VT.getSizeInBits()) {
Chris Lattner4b37e872006-05-08 21:18:59 +00003453 // We can turn this into an SRA iff the input to the SRL is already sign
3454 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003455 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003456 if (VT.getSizeInBits()-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Chris Lattner4b37e872006-05-08 21:18:59 +00003457 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3458 }
3459 }
Evan Chengc88138f2007-03-22 01:54:19 +00003460
Nate Begemanded49632005-10-13 03:11:28 +00003461 // fold (sext_inreg (extload x)) -> (sextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00003462 if (ISD::isEXTLoad(N0.getNode()) &&
3463 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003464 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003465 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003466 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003467 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003468 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003469 LN0->getBasePtr(), LN0->getSrcValue(),
3470 LN0->getSrcValueOffset(), EVT,
3471 LN0->isVolatile(), LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003472 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003473 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003474 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003475 }
3476 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00003477 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003478 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003479 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003480 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003481 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003482 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003483 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003484 LN0->getBasePtr(), LN0->getSrcValue(),
3485 LN0->getSrcValueOffset(), EVT,
3486 LN0->isVolatile(), LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003487 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003488 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003489 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003490 }
Dan Gohman475871a2008-07-27 21:46:04 +00003491 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003492}
3493
Dan Gohman475871a2008-07-27 21:46:04 +00003494SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
3495 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003496 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003497
3498 // noop truncate
3499 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003500 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003501 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003502 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003503 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003504 // fold (truncate (truncate x)) -> (truncate x)
3505 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003506 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003507 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003508 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3509 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00003510 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003511 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003512 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00003513 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003514 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003515 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003516 else
3517 // if the source and dest are the same type, we can drop both the extend
3518 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003519 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003520 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003521
Chris Lattner2b4c2792007-10-13 06:35:54 +00003522 // See if we can simplify the input to this truncate through knowledge that
3523 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3524 // -> trunc y
Dan Gohman475871a2008-07-27 21:46:04 +00003525 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003526 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00003527 VT.getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003528 if (Shorter.getNode())
Chris Lattner2b4c2792007-10-13 06:35:54 +00003529 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3530
Nate Begeman3df4d522005-10-12 20:40:40 +00003531 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003532 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003533 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003534}
3535
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003536static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003537 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003538 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00003539 return Elt.getNode();
3540 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003541}
3542
3543/// CombineConsecutiveLoads - build_pair (load, load) -> load
3544/// if load locations are consecutive.
Dan Gohman475871a2008-07-27 21:46:04 +00003545SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, MVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003546 assert(N->getOpcode() == ISD::BUILD_PAIR);
3547
3548 SDNode *LD1 = getBuildPairElt(N, 0);
3549 if (!ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00003550 return SDValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003551 MVT LD1VT = LD1->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003552 SDNode *LD2 = getBuildPairElt(N, 1);
3553 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3554 if (ISD::isNON_EXTLoad(LD2) &&
3555 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003556 // If both are volatile this would reduce the number of volatile loads.
3557 // If one is volatile it might be ok, but play conservative and bail out.
3558 !cast<LoadSDNode>(LD1)->isVolatile() &&
3559 !cast<LoadSDNode>(LD2)->isVolatile() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003560 TLI.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1, MFI)) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003561 LoadSDNode *LD = cast<LoadSDNode>(LD1);
3562 unsigned Align = LD->getAlignment();
Dan Gohman6448d912008-09-04 15:39:15 +00003563 unsigned NewAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003564 getABITypeAlignment(VT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003565 if (NewAlign <= Align &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003566 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003567 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(),
3568 LD->getSrcValue(), LD->getSrcValueOffset(),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003569 false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003570 }
Dan Gohman475871a2008-07-27 21:46:04 +00003571 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003572}
3573
Dan Gohman475871a2008-07-27 21:46:04 +00003574SDValue DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3575 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003576 MVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00003577
Dan Gohman7f321562007-06-25 16:23:39 +00003578 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3579 // Only do this before legalize, since afterward the target may be depending
3580 // on the bitconvert.
3581 // First check to see if this is all constant.
Duncan Sands25cf2272008-11-24 14:53:14 +00003582 if (!LegalTypes &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003583 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003584 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003585 bool isSimple = true;
3586 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3587 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3588 N0.getOperand(i).getOpcode() != ISD::Constant &&
3589 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3590 isSimple = false;
3591 break;
3592 }
3593
Duncan Sands83ec4b62008-06-06 12:08:01 +00003594 MVT DestEltVT = N->getValueType(0).getVectorElementType();
3595 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00003596 "Element type of vector ValueType must not be vector!");
3597 if (isSimple) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003598 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00003599 }
3600 }
3601
Dan Gohman3dd168d2008-09-05 01:58:21 +00003602 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00003603 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003604 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
Gabor Greifba36cb52008-08-28 21:40:38 +00003605 if (Res.getNode() != N) return Res;
Chris Lattner94683772005-12-23 05:30:37 +00003606 }
3607
Chris Lattnerc8547d82005-12-23 05:37:50 +00003608 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3609 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003610
Chris Lattner57104102005-12-23 05:44:41 +00003611 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003612 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00003613 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003614 // Do not change the width of a volatile load.
3615 !cast<LoadSDNode>(N0)->isVolatile() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003616 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003617 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman6448d912008-09-04 15:39:15 +00003618 unsigned Align = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003619 getABITypeAlignment(VT.getTypeForMVT());
Evan Cheng59d5b682007-05-07 21:27:48 +00003620 unsigned OrigAlign = LN0->getAlignment();
3621 if (Align <= OrigAlign) {
Dan Gohman475871a2008-07-27 21:46:04 +00003622 SDValue Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003623 LN0->getSrcValue(), LN0->getSrcValueOffset(),
3624 LN0->isVolatile(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00003625 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00003626 CombineTo(N0.getNode(),
3627 DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00003628 Load.getValue(1));
3629 return Load;
3630 }
Chris Lattner57104102005-12-23 05:44:41 +00003631 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003632
Chris Lattner3bd39d42008-01-27 17:42:27 +00003633 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3634 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3635 // This often reduces constant pool loads.
3636 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003637 N0.getNode()->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003638 SDValue NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00003639 AddToWorkList(NewConv.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003640
Duncan Sands83ec4b62008-06-06 12:08:01 +00003641 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003642 if (N0.getOpcode() == ISD::FNEG)
3643 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3644 assert(N0.getOpcode() == ISD::FABS);
3645 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3646 }
3647
3648 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3649 // Note that we don't handle copysign(x,cst) because this can always be folded
3650 // to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003651 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00003652 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003653 VT.isInteger() && !VT.isVector()) {
3654 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Duncan Sands25cf2272008-11-24 14:53:14 +00003655 MVT IntXVT = MVT::getIntegerVT(OrigXWidth);
3656 if (TLI.isTypeLegal(IntXVT) || !LegalTypes) {
3657 SDValue X = DAG.getNode(ISD::BIT_CONVERT, IntXVT, N0.getOperand(1));
3658 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003659
Duncan Sands25cf2272008-11-24 14:53:14 +00003660 // If X has a different width than the result/lhs, sext it or truncate it.
3661 unsigned VTWidth = VT.getSizeInBits();
3662 if (OrigXWidth < VTWidth) {
3663 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
3664 AddToWorkList(X.getNode());
3665 } else if (OrigXWidth > VTWidth) {
3666 // To get the sign bit in the right place, we have to shift it right
3667 // before truncating.
3668 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3669 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
3670 AddToWorkList(X.getNode());
3671 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3672 AddToWorkList(X.getNode());
3673 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003674
Duncan Sands25cf2272008-11-24 14:53:14 +00003675 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
3676 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
3677 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003678
Duncan Sands25cf2272008-11-24 14:53:14 +00003679 SDValue Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3680 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
3681 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003682
Duncan Sands25cf2272008-11-24 14:53:14 +00003683 return DAG.getNode(ISD::OR, VT, X, Cst);
3684 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003685 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003686
3687 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
3688 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003689 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
3690 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003691 return CombineLD;
3692 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003693
Dan Gohman475871a2008-07-27 21:46:04 +00003694 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00003695}
3696
Dan Gohman475871a2008-07-27 21:46:04 +00003697SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003698 MVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003699 return CombineConsecutiveLoads(N, VT);
3700}
3701
Dan Gohman7f321562007-06-25 16:23:39 +00003702/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003703/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3704/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00003705SDValue DAGCombiner::
Duncan Sands83ec4b62008-06-06 12:08:01 +00003706ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT DstEltVT) {
3707 MVT SrcEltVT = BV->getOperand(0).getValueType();
Chris Lattner6258fb22006-04-02 02:53:43 +00003708
3709 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00003710 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003711
Duncan Sands83ec4b62008-06-06 12:08:01 +00003712 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
3713 unsigned DstBitSize = DstEltVT.getSizeInBits();
Chris Lattner6258fb22006-04-02 02:53:43 +00003714
3715 // If this is a conversion of N elements of one type to N elements of another
3716 // type, convert each element. This handles FP<->INT cases.
3717 if (SrcBitSize == DstBitSize) {
Dan Gohman475871a2008-07-27 21:46:04 +00003718 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003719 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003720 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Gabor Greifba36cb52008-08-28 21:40:38 +00003721 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00003722 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003723 MVT VT = MVT::getVectorVT(DstEltVT,
3724 BV->getValueType(0).getVectorNumElements());
Dan Gohman7f321562007-06-25 16:23:39 +00003725 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003726 }
3727
3728 // Otherwise, we're growing or shrinking the elements. To avoid having to
3729 // handle annoying details of growing/shrinking FP values, we convert them to
3730 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003731 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003732 // Convert the input float vector to a int vector where the elements are the
3733 // same sizes.
3734 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003735 MVT IntVT = MVT::getIntegerVT(SrcEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00003736 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00003737 SrcEltVT = IntVT;
3738 }
3739
3740 // Now we know the input is an integer vector. If the output is a FP type,
3741 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003742 if (DstEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003743 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003744 MVT TmpVT = MVT::getIntegerVT(DstEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00003745 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00003746
3747 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003748 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003749 }
3750
3751 // Okay, we know the src/dst types are both integers of differing types.
3752 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003753 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00003754 if (SrcBitSize < DstBitSize) {
3755 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3756
Dan Gohman475871a2008-07-27 21:46:04 +00003757 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003758 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003759 i += NumInputsPerOutput) {
3760 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00003761 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003762 bool EltIsUndef = true;
3763 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3764 // Shift the previously computed bits over.
3765 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00003766 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00003767 if (Op.getOpcode() == ISD::UNDEF) continue;
3768 EltIsUndef = false;
3769
Dan Gohman220a8232008-03-03 23:51:38 +00003770 NewBits |=
3771 APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003772 }
3773
3774 if (EltIsUndef)
3775 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3776 else
3777 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3778 }
3779
Duncan Sands83ec4b62008-06-06 12:08:01 +00003780 MVT VT = MVT::getVectorVT(DstEltVT, Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00003781 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003782 }
3783
3784 // Finally, this must be the case where we are shrinking elements: each input
3785 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00003786 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00003787 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003788 MVT VT = MVT::getVectorVT(DstEltVT, NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00003789 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003790 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003791 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3792 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3793 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3794 continue;
3795 }
Dan Gohman220a8232008-03-03 23:51:38 +00003796 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getAPIntValue();
Chris Lattner6258fb22006-04-02 02:53:43 +00003797 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohman220a8232008-03-03 23:51:38 +00003798 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003799 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohman220a8232008-03-03 23:51:38 +00003800 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00003801 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
3802 return DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00003803 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003804 }
3805
3806 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00003807 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00003808 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3809 }
Dan Gohman7f321562007-06-25 16:23:39 +00003810 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003811}
3812
3813
3814
Dan Gohman475871a2008-07-27 21:46:04 +00003815SDValue DAGCombiner::visitFADD(SDNode *N) {
3816 SDValue N0 = N->getOperand(0);
3817 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003818 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3819 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003820 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003821
Dan Gohman7f321562007-06-25 16:23:39 +00003822 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003823 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003824 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003825 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003826 }
Dan Gohman7f321562007-06-25 16:23:39 +00003827
Nate Begemana0e221d2005-10-18 00:28:13 +00003828 // fold (fadd c1, c2) -> c1+c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003829 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003830 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003831 // canonicalize constant to RHS
3832 if (N0CFP && !N1CFP)
3833 return DAG.getNode(ISD::FADD, VT, N1, N0);
Dan Gohman760f86f2009-01-22 21:58:43 +00003834 // fold (A + 0) -> A
3835 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
3836 return N0;
Chris Lattner01b3d732005-09-28 22:28:18 +00003837 // fold (A + (-B)) -> A-B
Duncan Sands25cf2272008-11-24 14:53:14 +00003838 if (isNegatibleForFree(N1, LegalOperations) == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003839 return DAG.getNode(ISD::FSUB, VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00003840 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner01b3d732005-09-28 22:28:18 +00003841 // fold ((-A) + B) -> B-A
Duncan Sands25cf2272008-11-24 14:53:14 +00003842 if (isNegatibleForFree(N0, LegalOperations) == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003843 return DAG.getNode(ISD::FSUB, VT, N1,
Duncan Sands25cf2272008-11-24 14:53:14 +00003844 GetNegatedExpression(N0, DAG, LegalOperations));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003845
3846 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3847 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003848 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003849 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3850 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3851
Dan Gohman475871a2008-07-27 21:46:04 +00003852 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003853}
3854
Dan Gohman475871a2008-07-27 21:46:04 +00003855SDValue DAGCombiner::visitFSUB(SDNode *N) {
3856 SDValue N0 = N->getOperand(0);
3857 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003858 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3859 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003860 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003861
Dan Gohman7f321562007-06-25 16:23:39 +00003862 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003863 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003864 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003865 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003866 }
Dan Gohman7f321562007-06-25 16:23:39 +00003867
Nate Begemana0e221d2005-10-18 00:28:13 +00003868 // fold (fsub c1, c2) -> c1-c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003869 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003870 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohmana90c8e62009-01-23 19:10:37 +00003871 // fold (A-0) -> A
3872 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
3873 return N0;
Dan Gohman23ff1822007-07-02 15:48:56 +00003874 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003875 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Duncan Sands25cf2272008-11-24 14:53:14 +00003876 if (isNegatibleForFree(N1, LegalOperations))
3877 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman760f86f2009-01-22 21:58:43 +00003878 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
3879 return DAG.getNode(ISD::FNEG, VT, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003880 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003881 // fold (A-(-B)) -> A+B
Duncan Sands25cf2272008-11-24 14:53:14 +00003882 if (isNegatibleForFree(N1, LegalOperations))
Chris Lattner0254e702008-02-26 07:04:54 +00003883 return DAG.getNode(ISD::FADD, VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00003884 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00003885
Dan Gohman475871a2008-07-27 21:46:04 +00003886 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003887}
3888
Dan Gohman475871a2008-07-27 21:46:04 +00003889SDValue DAGCombiner::visitFMUL(SDNode *N) {
3890 SDValue N0 = N->getOperand(0);
3891 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003892 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3893 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003894 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003895
Dan Gohman7f321562007-06-25 16:23:39 +00003896 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003897 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003898 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003899 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003900 }
Dan Gohman7f321562007-06-25 16:23:39 +00003901
Nate Begeman11af4ea2005-10-17 20:40:11 +00003902 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003903 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003904 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003905 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003906 if (N0CFP && !N1CFP)
3907 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Dan Gohman760f86f2009-01-22 21:58:43 +00003908 // fold (A * 0) -> 0
3909 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
3910 return N1;
Nate Begeman11af4ea2005-10-17 20:40:11 +00003911 // fold (fmul X, 2.0) -> (fadd X, X)
3912 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3913 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003914 // fold (fmul X, -1.0) -> (fneg X)
3915 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman760f86f2009-01-22 21:58:43 +00003916 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
3917 return DAG.getNode(ISD::FNEG, VT, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003918
3919 // -X * -Y -> X*Y
Duncan Sands25cf2272008-11-24 14:53:14 +00003920 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
3921 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Chris Lattner29446522007-05-14 22:04:50 +00003922 // Both can be negated for free, check to see if at least one is cheaper
3923 // negated.
3924 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003925 return DAG.getNode(ISD::FMUL, VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00003926 GetNegatedExpression(N0, DAG, LegalOperations),
3927 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00003928 }
3929 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003930
3931 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3932 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003933 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003934 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3935 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3936
Dan Gohman475871a2008-07-27 21:46:04 +00003937 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003938}
3939
Dan Gohman475871a2008-07-27 21:46:04 +00003940SDValue DAGCombiner::visitFDIV(SDNode *N) {
3941 SDValue N0 = N->getOperand(0);
3942 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003943 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3944 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003945 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003946
Dan Gohman7f321562007-06-25 16:23:39 +00003947 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003948 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003949 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003950 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003951 }
Dan Gohman7f321562007-06-25 16:23:39 +00003952
Nate Begemana148d982006-01-18 22:35:16 +00003953 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003954 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003955 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003956
3957
3958 // -X / -Y -> X*Y
Duncan Sands25cf2272008-11-24 14:53:14 +00003959 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
3960 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Chris Lattner29446522007-05-14 22:04:50 +00003961 // Both can be negated for free, check to see if at least one is cheaper
3962 // negated.
3963 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003964 return DAG.getNode(ISD::FDIV, VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00003965 GetNegatedExpression(N0, DAG, LegalOperations),
3966 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00003967 }
3968 }
3969
Dan Gohman475871a2008-07-27 21:46:04 +00003970 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003971}
3972
Dan Gohman475871a2008-07-27 21:46:04 +00003973SDValue DAGCombiner::visitFREM(SDNode *N) {
3974 SDValue N0 = N->getOperand(0);
3975 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003976 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3977 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003978 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003979
Nate Begemana148d982006-01-18 22:35:16 +00003980 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003981 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003982 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +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::visitFCOPYSIGN(SDNode *N) {
3988 SDValue N0 = N->getOperand(0);
3989 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +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 Lattner12d83032006-03-05 05:30:57 +00003993
Dale Johannesendb44bf82007-10-16 23:38:29 +00003994 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00003995 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3996
3997 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003998 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003999 // copysign(x, c1) -> fabs(x) iff ispos(c1)
4000 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman760f86f2009-01-22 21:58:43 +00004001 if (!V.isNegative()) {
4002 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
4003 return DAG.getNode(ISD::FABS, VT, N0);
4004 } else {
4005 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
4006 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
4007 }
Chris Lattner12d83032006-03-05 05:30:57 +00004008 }
4009
4010 // copysign(fabs(x), y) -> copysign(x, y)
4011 // copysign(fneg(x), y) -> copysign(x, y)
4012 // copysign(copysign(x,z), y) -> copysign(x, y)
4013 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
4014 N0.getOpcode() == ISD::FCOPYSIGN)
4015 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
4016
4017 // copysign(x, abs(y)) -> abs(x)
4018 if (N1.getOpcode() == ISD::FABS)
4019 return DAG.getNode(ISD::FABS, VT, N0);
4020
4021 // copysign(x, copysign(y,z)) -> copysign(x, z)
4022 if (N1.getOpcode() == ISD::FCOPYSIGN)
4023 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
4024
4025 // copysign(x, fp_extend(y)) -> copysign(x, y)
4026 // copysign(x, fp_round(y)) -> copysign(x, y)
4027 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
4028 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
4029
Dan Gohman475871a2008-07-27 21:46:04 +00004030 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00004031}
4032
4033
Chris Lattner01b3d732005-09-28 22:28:18 +00004034
Dan Gohman475871a2008-07-27 21:46:04 +00004035SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
4036 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00004037 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004038 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00004039 MVT OpVT = N0.getValueType();
4040
Nate Begeman1d4d4142005-09-01 00:19:25 +00004041 // fold (sint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00004042 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004043 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00004044
4045 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
4046 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004047 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
4048 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Chris Lattnercda88752008-06-26 00:16:49 +00004049 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
4050 if (DAG.SignBitIsZero(N0))
4051 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
4052 }
4053
4054
Dan Gohman475871a2008-07-27 21:46:04 +00004055 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004056}
4057
Dan Gohman475871a2008-07-27 21:46:04 +00004058SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
4059 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00004060 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004061 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00004062 MVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00004063
Nate Begeman1d4d4142005-09-01 00:19:25 +00004064 // fold (uint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00004065 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004066 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00004067
4068 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
4069 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004070 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
4071 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Chris Lattnercda88752008-06-26 00:16:49 +00004072 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
4073 if (DAG.SignBitIsZero(N0))
4074 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
4075 }
4076
Dan Gohman475871a2008-07-27 21:46:04 +00004077 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004078}
4079
Dan Gohman475871a2008-07-27 21:46:04 +00004080SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
4081 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004082 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004083 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004084
4085 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00004086 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00004087 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004088 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004089}
4090
Dan Gohman475871a2008-07-27 21:46:04 +00004091SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
4092 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004093 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004094 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004095
4096 // fold (fp_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00004097 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004098 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004099 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004100}
4101
Dan Gohman475871a2008-07-27 21:46:04 +00004102SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
4103 SDValue N0 = N->getOperand(0);
4104 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00004105 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004106 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004107
4108 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00004109 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner0bd48932008-01-17 07:00:52 +00004110 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00004111
4112 // fold (fp_round (fp_extend x)) -> x
4113 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
4114 return N0.getOperand(0);
4115
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00004116 // fold (fp_round (fp_round x)) -> (fp_round x)
4117 if (N0.getOpcode() == ISD::FP_ROUND) {
4118 // This is a value preserving truncation if both round's are.
4119 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004120 N0.getNode()->getConstantOperandVal(1) == 1;
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00004121 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
4122 DAG.getIntPtrConstant(IsTrunc));
4123 }
4124
Chris Lattner79dbea52006-03-13 06:26:26 +00004125 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00004126 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004127 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00004128 AddToWorkList(Tmp.getNode());
Chris Lattner79dbea52006-03-13 06:26:26 +00004129 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
4130 }
4131
Dan Gohman475871a2008-07-27 21:46:04 +00004132 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004133}
4134
Dan Gohman475871a2008-07-27 21:46:04 +00004135SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
4136 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004137 MVT VT = N->getValueType(0);
4138 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00004139 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004140
Nate Begeman1d4d4142005-09-01 00:19:25 +00004141 // fold (fp_round_inreg c1fp) -> c1fp
Duncan Sands25cf2272008-11-24 14:53:14 +00004142 if (N0CFP && (TLI.isTypeLegal(EVT) || !LegalTypes)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00004143 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00004144 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004145 }
Dan Gohman475871a2008-07-27 21:46:04 +00004146 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004147}
4148
Dan Gohman475871a2008-07-27 21:46:04 +00004149SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
4150 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004151 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004152 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004153
Chris Lattner5938bef2007-12-29 06:55:23 +00004154 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Roman Levenstein9cac5252008-04-16 16:15:27 +00004155 if (N->hasOneUse() &&
Dan Gohmane7852d02009-01-26 04:35:06 +00004156 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman475871a2008-07-27 21:46:04 +00004157 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004158
Nate Begeman1d4d4142005-09-01 00:19:25 +00004159 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00004160 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004161 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004162
4163 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
4164 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00004165 if (N0.getOpcode() == ISD::FP_ROUND
4166 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004167 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004168 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00004169 if (VT.bitsLT(In.getValueType()))
Chris Lattner0bd48932008-01-17 07:00:52 +00004170 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
4171 return DAG.getNode(ISD::FP_EXTEND, VT, In);
4172 }
4173
4174 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004175 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004176 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004177 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00004178 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004179 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004180 LN0->getBasePtr(), LN0->getSrcValue(),
4181 LN0->getSrcValueOffset(),
4182 N0.getValueType(),
4183 LN0->isVolatile(), LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00004184 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004185 CombineTo(N0.getNode(), DAG.getNode(ISD::FP_ROUND, N0.getValueType(),
4186 ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00004187 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004188 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00004189 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004190
Dan Gohman475871a2008-07-27 21:46:04 +00004191 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004192}
4193
Dan Gohman475871a2008-07-27 21:46:04 +00004194SDValue DAGCombiner::visitFNEG(SDNode *N) {
4195 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004196
Duncan Sands25cf2272008-11-24 14:53:14 +00004197 if (isNegatibleForFree(N0, LegalOperations))
4198 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohman23ff1822007-07-02 15:48:56 +00004199
Chris Lattner3bd39d42008-01-27 17:42:27 +00004200 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
4201 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004202 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004203 N0.getOperand(0).getValueType().isInteger() &&
4204 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004205 SDValue Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004206 MVT IntVT = Int.getValueType();
4207 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004208 Int = DAG.getNode(ISD::XOR, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004209 DAG.getConstant(IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004210 AddToWorkList(Int.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004211 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4212 }
4213 }
4214
Dan Gohman475871a2008-07-27 21:46:04 +00004215 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004216}
4217
Dan Gohman475871a2008-07-27 21:46:04 +00004218SDValue DAGCombiner::visitFABS(SDNode *N) {
4219 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004220 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004221 MVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00004222
Nate Begeman1d4d4142005-09-01 00:19:25 +00004223 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00004224 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004225 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004226 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004227 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00004228 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004229 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004230 // fold (fabs (fcopysign x, y)) -> (fabs x)
4231 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
4232 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
4233
Chris Lattner3bd39d42008-01-27 17:42:27 +00004234 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
4235 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004236 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004237 N0.getOperand(0).getValueType().isInteger() &&
4238 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004239 SDValue Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004240 MVT IntVT = Int.getValueType();
4241 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004242 Int = DAG.getNode(ISD::AND, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004243 DAG.getConstant(~IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004244 AddToWorkList(Int.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004245 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4246 }
4247 }
4248
Dan Gohman475871a2008-07-27 21:46:04 +00004249 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004250}
4251
Dan Gohman475871a2008-07-27 21:46:04 +00004252SDValue DAGCombiner::visitBRCOND(SDNode *N) {
4253 SDValue Chain = N->getOperand(0);
4254 SDValue N1 = N->getOperand(1);
4255 SDValue N2 = N->getOperand(2);
Nate Begeman44728a72005-09-19 22:34:01 +00004256 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4257
4258 // never taken branch, fold to chain
4259 if (N1C && N1C->isNullValue())
4260 return Chain;
4261 // unconditional branch
Dan Gohman002e5d02008-03-13 22:13:53 +00004262 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00004263 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004264 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
4265 // on the target.
4266 if (N1.getOpcode() == ISD::SETCC &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004267 TLI.isOperationLegalOrCustom(ISD::BR_CC, MVT::Other)) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004268 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
4269 N1.getOperand(0), N1.getOperand(1), N2);
4270 }
Dan Gohman475871a2008-07-27 21:46:04 +00004271 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004272}
4273
Chris Lattner3ea0b472005-10-05 06:47:48 +00004274// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
4275//
Dan Gohman475871a2008-07-27 21:46:04 +00004276SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00004277 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004278 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Chris Lattner3ea0b472005-10-05 06:47:48 +00004279
Duncan Sands8eab8a22008-06-09 11:32:28 +00004280 // Use SimplifySetCC to simplify SETCC's.
Duncan Sands5480c042009-01-01 15:52:00 +00004281 SDValue Simp = SimplifySetCC(TLI.getSetCCResultType(CondLHS.getValueType()),
Duncan Sands25cf2272008-11-24 14:53:14 +00004282 CondLHS, CondRHS, CC->get(), false);
Gabor Greifba36cb52008-08-28 21:40:38 +00004283 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00004284
Gabor Greifba36cb52008-08-28 21:40:38 +00004285 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.getNode());
Nate Begemane17daeb2005-10-05 21:43:42 +00004286
4287 // fold br_cc true, dest -> br dest (unconditional branch)
Dan Gohman002e5d02008-03-13 22:13:53 +00004288 if (SCCC && !SCCC->isNullValue())
Nate Begemane17daeb2005-10-05 21:43:42 +00004289 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
4290 N->getOperand(4));
4291 // fold br_cc false, dest -> unconditional fall through
4292 if (SCCC && SCCC->isNullValue())
4293 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00004294
Nate Begemane17daeb2005-10-05 21:43:42 +00004295 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00004296 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Nate Begemane17daeb2005-10-05 21:43:42 +00004297 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
4298 Simp.getOperand(2), Simp.getOperand(0),
4299 Simp.getOperand(1), N->getOperand(4));
Dan Gohman475871a2008-07-27 21:46:04 +00004300 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004301}
4302
Chris Lattner448f2192006-11-11 00:39:41 +00004303
Duncan Sandsec87aa82008-06-15 20:12:31 +00004304/// CombineToPreIndexedLoadStore - Try turning a load / store into a
4305/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00004306/// and it has other uses besides the load / store. After the
4307/// transformation, the new indexed load / store has effectively folded
4308/// the add / subtract in and all of its other uses are redirected to the
4309/// new load / store.
4310bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Duncan Sands25cf2272008-11-24 14:53:14 +00004311 if (!LegalOperations)
Chris Lattner448f2192006-11-11 00:39:41 +00004312 return false;
4313
4314 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004315 SDValue Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004316 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004317 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004318 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004319 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004320 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00004321 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00004322 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
4323 return false;
4324 Ptr = LD->getBasePtr();
4325 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004326 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004327 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004328 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004329 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
4330 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
4331 return false;
4332 Ptr = ST->getBasePtr();
4333 isLoad = false;
4334 } else
4335 return false;
4336
Chris Lattner9f1794e2006-11-11 00:56:29 +00004337 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4338 // out. There is no reason to make this a preinc/predec.
4339 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00004340 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004341 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004342
Chris Lattner9f1794e2006-11-11 00:56:29 +00004343 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00004344 SDValue BasePtr;
4345 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004346 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4347 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4348 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00004349 // Don't create a indexed load / store with zero offset.
4350 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004351 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004352 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004353
Chris Lattner41e53fd2006-11-11 01:00:15 +00004354 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00004355 // 1) The new base ptr is a frame index.
4356 // 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 +00004357 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00004358 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00004359 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00004360 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00004361
Chris Lattner41e53fd2006-11-11 01:00:15 +00004362 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4363 // (plus the implicit offset) to a register to preinc anyway.
4364 if (isa<FrameIndexSDNode>(BasePtr))
4365 return false;
4366
4367 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004368 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004369 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004370 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004371 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004372 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004373
Evan Chengc843abe2007-05-24 02:35:39 +00004374 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004375 bool RealUse = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00004376 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4377 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004378 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004379 if (Use == N)
4380 continue;
Evan Cheng917be682008-03-04 00:41:45 +00004381 if (Use->isPredecessorOf(N))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004382 return false;
4383
4384 if (!((Use->getOpcode() == ISD::LOAD &&
4385 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004386 (Use->getOpcode() == ISD::STORE &&
4387 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004388 RealUse = true;
4389 }
4390 if (!RealUse)
4391 return false;
4392
Dan Gohman475871a2008-07-27 21:46:04 +00004393 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004394 if (isLoad)
Dan Gohman475871a2008-07-27 21:46:04 +00004395 Result = DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004396 else
Dan Gohman475871a2008-07-27 21:46:04 +00004397 Result = DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004398 ++PreIndexedNodes;
4399 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004400 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004401 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004402 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004403 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004404 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004405 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004406 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004407 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004408 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004409 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004410 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004411 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004412 }
4413
Chris Lattner9f1794e2006-11-11 00:56:29 +00004414 // Finally, since the node is now dead, remove it from the graph.
4415 DAG.DeleteNode(N);
4416
4417 // Replace the uses of Ptr with uses of the updated base value.
4418 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004419 &DeadNodes);
Gabor Greifba36cb52008-08-28 21:40:38 +00004420 removeFromWorkList(Ptr.getNode());
4421 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00004422
4423 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004424}
4425
Duncan Sandsec87aa82008-06-15 20:12:31 +00004426/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00004427/// add / sub of the base pointer node into a post-indexed load / store.
4428/// The transformation folded the add / subtract into the new indexed
4429/// load / store effectively and all of its uses are redirected to the
4430/// new load / store.
4431bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Duncan Sands25cf2272008-11-24 14:53:14 +00004432 if (!LegalOperations)
Chris Lattner448f2192006-11-11 00:39:41 +00004433 return false;
4434
4435 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004436 SDValue Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004437 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004438 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004439 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004440 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004441 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004442 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4443 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4444 return false;
4445 Ptr = LD->getBasePtr();
4446 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004447 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004448 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004449 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004450 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4451 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4452 return false;
4453 Ptr = ST->getBasePtr();
4454 isLoad = false;
4455 } else
4456 return false;
4457
Gabor Greifba36cb52008-08-28 21:40:38 +00004458 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004459 return false;
4460
Gabor Greifba36cb52008-08-28 21:40:38 +00004461 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4462 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004463 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004464 if (Op == N ||
4465 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4466 continue;
4467
Dan Gohman475871a2008-07-27 21:46:04 +00004468 SDValue BasePtr;
4469 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004470 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4471 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4472 if (Ptr == Offset)
4473 std::swap(BasePtr, Offset);
4474 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00004475 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00004476 // Don't create a indexed load / store with zero offset.
4477 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004478 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004479 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004480
Chris Lattner9f1794e2006-11-11 00:56:29 +00004481 // Try turning it into a post-indexed load / store except when
4482 // 1) All uses are load / store ops that use it as base ptr.
4483 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4484 // nor a successor of N. Otherwise, if Op is folded that would
4485 // create a cycle.
4486
4487 // Check for #1.
4488 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00004489 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
4490 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00004491 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00004492 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00004493 continue;
4494
Chris Lattner9f1794e2006-11-11 00:56:29 +00004495 // If all the uses are load / store addresses, then don't do the
4496 // transformation.
4497 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4498 bool RealUse = false;
4499 for (SDNode::use_iterator III = Use->use_begin(),
4500 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00004501 SDNode *UseUse = *III;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004502 if (!((UseUse->getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004503 cast<LoadSDNode>(UseUse)->getBasePtr().getNode() == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004504 (UseUse->getOpcode() == ISD::STORE &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004505 cast<StoreSDNode>(UseUse)->getBasePtr().getNode() == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004506 RealUse = true;
4507 }
Chris Lattner448f2192006-11-11 00:39:41 +00004508
Chris Lattner9f1794e2006-11-11 00:56:29 +00004509 if (!RealUse) {
4510 TryNext = true;
4511 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004512 }
4513 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004514 }
4515 if (TryNext)
4516 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004517
Chris Lattner9f1794e2006-11-11 00:56:29 +00004518 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00004519 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004520 SDValue Result = isLoad
4521 ? DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM)
4522 : DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004523 ++PostIndexedNodes;
4524 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004525 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004526 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004527 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004528 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004529 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004530 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004531 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004532 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004533 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004534 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004535 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004536 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00004537 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004538
Chris Lattner9f1794e2006-11-11 00:56:29 +00004539 // Finally, since the node is now dead, remove it from the graph.
4540 DAG.DeleteNode(N);
4541
4542 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00004543 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Chris Lattner9f1794e2006-11-11 00:56:29 +00004544 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004545 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004546 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004547 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004548 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004549 }
4550 }
4551 }
4552 return false;
4553}
4554
Chris Lattner00161a62008-01-25 07:20:16 +00004555/// InferAlignment - If we can infer some alignment information from this
4556/// pointer, return it.
Dan Gohman475871a2008-07-27 21:46:04 +00004557static unsigned InferAlignment(SDValue Ptr, SelectionDAG &DAG) {
Chris Lattner00161a62008-01-25 07:20:16 +00004558 // If this is a direct reference to a stack slot, use information about the
4559 // stack slot's alignment.
Chris Lattner1329cb82008-01-26 19:45:50 +00004560 int FrameIdx = 1 << 31;
4561 int64_t FrameOffset = 0;
Chris Lattner00161a62008-01-25 07:20:16 +00004562 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1329cb82008-01-26 19:45:50 +00004563 FrameIdx = FI->getIndex();
4564 } else if (Ptr.getOpcode() == ISD::ADD &&
4565 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4566 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4567 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4568 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner00161a62008-01-25 07:20:16 +00004569 }
Chris Lattner1329cb82008-01-26 19:45:50 +00004570
4571 if (FrameIdx != (1 << 31)) {
4572 // FIXME: Handle FI+CST.
4573 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4574 if (MFI.isFixedObjectIndex(FrameIdx)) {
Dan Gohman8cea8ff2008-08-11 18:27:03 +00004575 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx) + FrameOffset;
Chris Lattner1329cb82008-01-26 19:45:50 +00004576
4577 // The alignment of the frame index can be determined from its offset from
4578 // the incoming frame position. If the frame object is at offset 32 and
4579 // the stack is guaranteed to be 16-byte aligned, then we know that the
4580 // object is 16-byte aligned.
4581 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4582 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4583
4584 // Finally, the frame object itself may have a known alignment. Factor
4585 // the alignment + offset into a new alignment. For example, if we know
4586 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4587 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4588 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4589 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4590 FrameOffset);
4591 return std::max(Align, FIInfoAlign);
4592 }
4593 }
Chris Lattner00161a62008-01-25 07:20:16 +00004594
4595 return 0;
4596}
Chris Lattner448f2192006-11-11 00:39:41 +00004597
Dan Gohman475871a2008-07-27 21:46:04 +00004598SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004599 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004600 SDValue Chain = LD->getChain();
4601 SDValue Ptr = LD->getBasePtr();
Chris Lattner00161a62008-01-25 07:20:16 +00004602
4603 // Try to infer better alignment information than the load already has.
Dan Gohmana2676512008-08-20 16:30:28 +00004604 if (!Fast && LD->isUnindexed()) {
Chris Lattner00161a62008-01-25 07:20:16 +00004605 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4606 if (Align > LD->getAlignment())
4607 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4608 Chain, Ptr, LD->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004609 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004610 LD->isVolatile(), Align);
4611 }
4612 }
4613
Evan Cheng45a7ca92007-05-01 00:38:21 +00004614
4615 // If load is not volatile and there are no uses of the loaded value (and
4616 // the updated indexed value in case of indexed loads), change uses of the
4617 // chain value into uses of the chain input (i.e. delete the dead load).
4618 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00004619 if (N->getValueType(1) == MVT::Other) {
4620 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004621 if (N->hasNUsesOfValue(0, 0)) {
4622 // It's not safe to use the two value CombineTo variant here. e.g.
4623 // v1, chain2 = load chain1, loc
4624 // v2, chain3 = load chain2, loc
4625 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004626 // Now we replace use of chain2 with chain1. This makes the second load
4627 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004628 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004629 DOUT << "\nWith chain: "; DEBUG(Chain.getNode()->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004630 DOUT << "\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004631 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004632 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain, &DeadNodes);
Chris Lattner125991a2008-01-24 07:57:06 +00004633 if (N->use_empty()) {
4634 removeFromWorkList(N);
4635 DAG.DeleteNode(N);
4636 }
Dan Gohman475871a2008-07-27 21:46:04 +00004637 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00004638 }
Evan Cheng498f5592007-05-01 08:53:39 +00004639 } else {
4640 // Indexed loads.
4641 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4642 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004643 SDValue Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
Evan Cheng02c42852008-01-16 23:11:54 +00004644 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004645 DOUT << "\nWith: "; DEBUG(Undef.getNode()->dump(&DAG));
Evan Cheng02c42852008-01-16 23:11:54 +00004646 DOUT << " and 2 other values\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004647 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004648 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef, &DeadNodes);
4649 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Chris Lattner4626b252008-01-17 07:20:38 +00004650 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004651 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004652 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004653 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00004654 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004655 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004656 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004657 }
4658 }
Chris Lattner01a22022005-10-10 22:04:48 +00004659
4660 // If this load is directly stored, replace the load value with the stored
4661 // value.
4662 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004663 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohmanb061c4b2008-03-31 20:32:52 +00004664 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
4665 !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004666 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004667 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4668 if (PrevST->getBasePtr() == Ptr &&
4669 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004670 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004671 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004672 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004673
Jim Laskey7ca56af2006-10-11 13:47:09 +00004674 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004675 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00004676 SDValue BetterChain = FindBetterChain(N, Chain);
Jim Laskey279f0532006-09-25 16:29:54 +00004677
Jim Laskey6ff23e52006-10-04 16:53:27 +00004678 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004679 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00004680 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004681
Jim Laskey279f0532006-09-25 16:29:54 +00004682 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004683 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4684 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004685 LD->getSrcValue(), LD->getSrcValueOffset(),
4686 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004687 } else {
4688 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4689 LD->getValueType(0),
4690 BetterChain, Ptr, LD->getSrcValue(),
4691 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004692 LD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004693 LD->isVolatile(),
4694 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004695 }
Jim Laskey279f0532006-09-25 16:29:54 +00004696
Jim Laskey6ff23e52006-10-04 16:53:27 +00004697 // Create token factor to keep old chain connected.
Dan Gohman475871a2008-07-27 21:46:04 +00004698 SDValue Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
Jim Laskey288af5e2006-09-25 19:32:58 +00004699 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004700
Jim Laskey274062c2006-10-13 23:32:28 +00004701 // Replace uses with load result and token factor. Don't add users
4702 // to work list.
4703 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004704 }
4705 }
4706
Evan Cheng7fc033a2006-11-03 03:06:21 +00004707 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004708 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00004709 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00004710
Dan Gohman475871a2008-07-27 21:46:04 +00004711 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00004712}
4713
Chris Lattner07649d92008-01-08 23:08:06 +00004714
Dan Gohman475871a2008-07-27 21:46:04 +00004715SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004716 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004717 SDValue Chain = ST->getChain();
4718 SDValue Value = ST->getValue();
4719 SDValue Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004720
Chris Lattner00161a62008-01-25 07:20:16 +00004721 // Try to infer better alignment information than the store already has.
Dan Gohmana2676512008-08-20 16:30:28 +00004722 if (!Fast && ST->isUnindexed()) {
Chris Lattner00161a62008-01-25 07:20:16 +00004723 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4724 if (Align > ST->getAlignment())
4725 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004726 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004727 ST->isVolatile(), Align);
4728 }
4729 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004730
Evan Cheng59d5b682007-05-07 21:27:48 +00004731 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004732 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004733 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004734 ST->isUnindexed()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004735 unsigned Align = ST->getAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004736 MVT SVT = Value.getOperand(0).getValueType();
Dan Gohman6448d912008-09-04 15:39:15 +00004737 unsigned OrigAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004738 getABITypeAlignment(SVT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004739 if (Align <= OrigAlign &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004740 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004741 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Evan Cheng59d5b682007-05-07 21:27:48 +00004742 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohman77455612008-06-28 00:45:22 +00004743 ST->getSrcValueOffset(), ST->isVolatile(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00004744 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004745
Nate Begeman2cbba892006-12-11 02:23:46 +00004746 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004747 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004748 // NOTE: If the original store is volatile, this transform must not increase
4749 // the number of stores. For example, on x86-32 an f64 can be stored in one
4750 // processor operation but an i64 (which is not legal) requires two. So the
4751 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00004752 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00004753 SDValue Tmp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004754 switch (CFP->getValueType(0).getSimpleVT()) {
Chris Lattner62be1a72006-12-12 04:16:14 +00004755 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004756 case MVT::f80: // We don't do this for these yet.
4757 case MVT::f128:
4758 case MVT::ppcf128:
4759 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004760 case MVT::f32:
Duncan Sands25cf2272008-11-24 14:53:14 +00004761 if (((TLI.isTypeLegal(MVT::i32) || !LegalTypes) && !LegalOperations &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004762 !ST->isVolatile()) ||
4763 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004764 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Dale Johannesen7111b022008-10-09 18:53:47 +00004765 bitcastToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004766 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004767 ST->getSrcValueOffset(), ST->isVolatile(),
4768 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004769 }
4770 break;
4771 case MVT::f64:
Duncan Sands25cf2272008-11-24 14:53:14 +00004772 if (((TLI.isTypeLegal(MVT::i64) || !LegalTypes) && !LegalOperations &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004773 !ST->isVolatile()) ||
4774 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen7111b022008-10-09 18:53:47 +00004775 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004776 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004777 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004778 ST->getSrcValueOffset(), ST->isVolatile(),
4779 ST->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004780 } else if (!ST->isVolatile() &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004781 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004782 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004783 // argument passing. Since this is so common, custom legalize the
4784 // 64-bit integer store into two 32-bit stores.
Dale Johannesen7111b022008-10-09 18:53:47 +00004785 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00004786 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4787 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00004788 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00004789
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004790 int SVOffset = ST->getSrcValueOffset();
4791 unsigned Alignment = ST->getAlignment();
4792 bool isVolatile = ST->isVolatile();
4793
Dan Gohman475871a2008-07-27 21:46:04 +00004794 SDValue St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004795 ST->getSrcValueOffset(),
4796 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004797 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4798 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004799 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004800 Alignment = MinAlign(Alignment, 4U);
Dan Gohman475871a2008-07-27 21:46:04 +00004801 SDValue St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004802 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004803 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4804 }
4805 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004806 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004807 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004808 }
4809
Jim Laskey279f0532006-09-25 16:29:54 +00004810 if (CombinerAA) {
4811 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00004812 SDValue BetterChain = FindBetterChain(N, Chain);
Jim Laskey279f0532006-09-25 16:29:54 +00004813
Jim Laskey6ff23e52006-10-04 16:53:27 +00004814 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004815 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004816 // Replace the chain to avoid dependency.
Dan Gohman475871a2008-07-27 21:46:04 +00004817 SDValue ReplStore;
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004818 if (ST->isTruncatingStore()) {
4819 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004820 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004821 ST->getMemoryVT(),
Chris Lattner4626b252008-01-17 07:20:38 +00004822 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004823 } else {
4824 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004825 ST->getSrcValue(), ST->getSrcValueOffset(),
4826 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004827 }
4828
Jim Laskey279f0532006-09-25 16:29:54 +00004829 // Create token to keep both nodes around.
Dan Gohman475871a2008-07-27 21:46:04 +00004830 SDValue Token =
Jim Laskey274062c2006-10-13 23:32:28 +00004831 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4832
4833 // Don't add users to work list.
4834 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004835 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004836 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004837
Evan Cheng33dbedc2006-11-05 09:31:14 +00004838 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004839 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00004840 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00004841
Chris Lattner3c872852007-12-29 06:26:16 +00004842 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00004843 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004844 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00004845 // See if we can simplify the input to this truncstore with knowledge that
4846 // only the low bits are being used. For example:
4847 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Dan Gohman475871a2008-07-27 21:46:04 +00004848 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004849 GetDemandedBits(Value,
4850 APInt::getLowBitsSet(Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004851 ST->getMemoryVT().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00004852 AddToWorkList(Value.getNode());
4853 if (Shorter.getNode())
Chris Lattner2b4c2792007-10-13 06:35:54 +00004854 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004855 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner2b4c2792007-10-13 06:35:54 +00004856 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004857
4858 // Otherwise, see if we can simplify the operation with
4859 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00004860 if (SimplifyDemandedBits(Value,
4861 APInt::getLowBitsSet(
4862 Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004863 ST->getMemoryVT().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00004864 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004865 }
4866
Chris Lattner3c872852007-12-29 06:26:16 +00004867 // If this is a load followed by a store to the same location, then the store
4868 // is dead/noop.
4869 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004870 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004871 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00004872 // There can't be any side effects between the load and store, such as
4873 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00004874 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00004875 // The store is dead, remove it.
4876 return Chain;
4877 }
4878 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004879
Chris Lattnerddf89562008-01-17 19:59:44 +00004880 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4881 // truncating store. We can do this even if this is already a truncstore.
4882 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00004883 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004884 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004885 ST->getMemoryVT())) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004886 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004887 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnerddf89562008-01-17 19:59:44 +00004888 ST->isVolatile(), ST->getAlignment());
4889 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004890
Dan Gohman475871a2008-07-27 21:46:04 +00004891 return SDValue();
Chris Lattner87514ca2005-10-10 22:31:19 +00004892}
4893
Dan Gohman475871a2008-07-27 21:46:04 +00004894SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4895 SDValue InVec = N->getOperand(0);
4896 SDValue InVal = N->getOperand(1);
4897 SDValue EltNo = N->getOperand(2);
Chris Lattnerca242442006-03-19 01:27:56 +00004898
4899 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4900 // vector with the inserted element.
4901 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004902 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Gabor Greif12632d22008-08-30 19:29:20 +00004903 SmallVector<SDValue, 8> Ops(InVec.getNode()->op_begin(),
4904 InVec.getNode()->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004905 if (Elt < Ops.size())
4906 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004907 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4908 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004909 }
4910
Dan Gohman475871a2008-07-27 21:46:04 +00004911 return SDValue();
Chris Lattnerca242442006-03-19 01:27:56 +00004912}
4913
Dan Gohman475871a2008-07-27 21:46:04 +00004914SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00004915 // (vextract (scalar_to_vector val, 0) -> val
4916 SDValue InVec = N->getOperand(0);
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00004917
Mon P Wange3bc6ae2009-01-18 06:43:40 +00004918 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR)
4919 return InVec.getOperand(0);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004920
4921 // Perform only after legalization to ensure build_vector / vector_shuffle
4922 // optimizations have already been done.
Duncan Sands25cf2272008-11-24 14:53:14 +00004923 if (!LegalOperations) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004924
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00004925 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
4926 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
4927 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Mon P Wange3bc6ae2009-01-18 06:43:40 +00004928 SDValue EltNo = N->getOperand(1);
Evan Cheng513da432007-10-06 08:19:55 +00004929
Evan Cheng513da432007-10-06 08:19:55 +00004930 if (isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004931 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00004932 bool NewLoad = false;
Mon P Wanga60b5232008-12-11 00:26:16 +00004933 bool BCNumEltsChanged = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004934 MVT VT = InVec.getValueType();
4935 MVT EVT = VT.getVectorElementType();
4936 MVT LVT = EVT;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004937 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004938 MVT BCVT = InVec.getOperand(0).getValueType();
Mon P Wanga60b5232008-12-11 00:26:16 +00004939 if (!BCVT.isVector() || EVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004940 return SDValue();
Mon P Wanga60b5232008-12-11 00:26:16 +00004941 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
4942 BCNumEltsChanged = true;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004943 InVec = InVec.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004944 EVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004945 NewLoad = true;
4946 }
Evan Cheng513da432007-10-06 08:19:55 +00004947
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004948 LoadSDNode *LN0 = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004949 if (ISD::isNormalLoad(InVec.getNode()))
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004950 LN0 = cast<LoadSDNode>(InVec);
4951 else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4952 InVec.getOperand(0).getValueType() == EVT &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004953 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004954 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4955 } else if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE) {
4956 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
4957 // =>
4958 // (load $addr+1*size)
Mon P Wanga60b5232008-12-11 00:26:16 +00004959
4960 // If the bit convert changed the number of elements, it is unsafe
4961 // to examine the mask.
4962 if (BCNumEltsChanged)
4963 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004964 unsigned Idx = cast<ConstantSDNode>(InVec.getOperand(2).
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004965 getOperand(Elt))->getZExtValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004966 unsigned NumElems = InVec.getOperand(2).getNumOperands();
4967 InVec = (Idx < NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
4968 if (InVec.getOpcode() == ISD::BIT_CONVERT)
4969 InVec = InVec.getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00004970 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004971 LN0 = cast<LoadSDNode>(InVec);
4972 Elt = (Idx < NumElems) ? Idx : Idx - NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00004973 }
4974 }
Duncan Sandsec87aa82008-06-15 20:12:31 +00004975 if (!LN0 || !LN0->hasOneUse() || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00004976 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004977
4978 unsigned Align = LN0->getAlignment();
4979 if (NewLoad) {
4980 // Check the resultant load doesn't need a higher alignment than the
4981 // original load.
Dan Gohman6448d912008-09-04 15:39:15 +00004982 unsigned NewAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004983 getABITypeAlignment(LVT.getTypeForMVT());
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004984 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00004985 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004986 Align = NewAlign;
4987 }
4988
Dan Gohman475871a2008-07-27 21:46:04 +00004989 SDValue NewPtr = LN0->getBasePtr();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004990 if (Elt) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004991 unsigned PtrOff = LVT.getSizeInBits() * Elt / 8;
4992 MVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004993 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00004994 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004995 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
4996 DAG.getConstant(PtrOff, PtrType));
4997 }
4998 return DAG.getLoad(LVT, LN0->getChain(), NewPtr,
4999 LN0->getSrcValue(), LN0->getSrcValueOffset(),
5000 LN0->isVolatile(), Align);
Evan Cheng513da432007-10-06 08:19:55 +00005001 }
Dan Gohman475871a2008-07-27 21:46:04 +00005002 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00005003}
5004
5005
Dan Gohman475871a2008-07-27 21:46:04 +00005006SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005007 unsigned NumInScalars = N->getNumOperands();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005008 MVT VT = N->getValueType(0);
5009 unsigned NumElts = VT.getVectorNumElements();
5010 MVT EltType = VT.getVectorElementType();
Chris Lattnerca242442006-03-19 01:27:56 +00005011
Dan Gohman7f321562007-06-25 16:23:39 +00005012 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
5013 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
5014 // at most two distinct vectors, turn this into a shuffle node.
Dan Gohman475871a2008-07-27 21:46:04 +00005015 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00005016 for (unsigned i = 0; i != NumInScalars; ++i) {
5017 // Ignore undef inputs.
5018 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5019
Dan Gohman7f321562007-06-25 16:23:39 +00005020 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00005021 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00005022 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00005023 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00005024 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005025 break;
5026 }
5027
Dan Gohman7f321562007-06-25 16:23:39 +00005028 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00005029 // we can't make a shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00005030 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005031 if (ExtractedFromVec.getValueType() != VT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005032 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005033 break;
5034 }
5035
5036 // Otherwise, remember this. We allow up to two distinct input vectors.
5037 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
5038 continue;
5039
Gabor Greifba36cb52008-08-28 21:40:38 +00005040 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00005041 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00005042 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00005043 VecIn2 = ExtractedFromVec;
5044 } else {
5045 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00005046 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005047 break;
5048 }
5049 }
5050
5051 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00005052 if (VecIn1.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005053 SmallVector<SDValue, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00005054 for (unsigned i = 0; i != NumInScalars; ++i) {
5055 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00005056 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00005057 continue;
5058 }
5059
Dan Gohman475871a2008-07-27 21:46:04 +00005060 SDValue Extract = N->getOperand(i);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005061
5062 // If extracting from the first vector, just use the index directly.
5063 if (Extract.getOperand(0) == VecIn1) {
5064 BuildVecIndices.push_back(Extract.getOperand(1));
5065 continue;
5066 }
5067
5068 // Otherwise, use InIdx + VecSize
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005069 unsigned Idx =
5070 cast<ConstantSDNode>(Extract.getOperand(1))->getZExtValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00005071 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Chris Lattnerd7648c82006-03-28 20:28:38 +00005072 }
5073
5074 // Add count and size info.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005075 MVT BuildVecVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Duncan Sands25cf2272008-11-24 14:53:14 +00005076 if (!TLI.isTypeLegal(BuildVecVT) && LegalTypes)
5077 return SDValue();
5078
Dan Gohman7f321562007-06-25 16:23:39 +00005079 // Return the new VECTOR_SHUFFLE node.
Dan Gohman475871a2008-07-27 21:46:04 +00005080 SDValue Ops[5];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005081 Ops[0] = VecIn1;
Gabor Greifba36cb52008-08-28 21:40:38 +00005082 if (VecIn2.getNode()) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005083 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00005084 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00005085 // Use an undef build_vector as input for the second operand.
Dan Gohman475871a2008-07-27 21:46:04 +00005086 std::vector<SDValue> UnOps(NumInScalars,
Chris Lattnercef896e2006-03-28 22:19:47 +00005087 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00005088 EltType));
5089 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005090 &UnOps[0], UnOps.size());
Gabor Greifba36cb52008-08-28 21:40:38 +00005091 AddToWorkList(Ops[1].getNode());
Chris Lattnercef896e2006-03-28 22:19:47 +00005092 }
Dan Gohman7f321562007-06-25 16:23:39 +00005093 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005094 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00005095 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005096 }
5097
Dan Gohman475871a2008-07-27 21:46:04 +00005098 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00005099}
5100
Dan Gohman475871a2008-07-27 21:46:04 +00005101SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005102 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
5103 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
5104 // inputs come from at most two distinct vectors, turn this into a shuffle
5105 // node.
5106
5107 // If we only have one input vector, we don't need to do any concatenation.
5108 if (N->getNumOperands() == 1) {
5109 return N->getOperand(0);
5110 }
5111
Dan Gohman475871a2008-07-27 21:46:04 +00005112 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00005113}
5114
Dan Gohman475871a2008-07-27 21:46:04 +00005115SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
5116 SDValue ShufMask = N->getOperand(2);
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005117 unsigned NumElts = ShufMask.getNumOperands();
5118
Mon P Wangaeb06d22008-11-10 04:46:22 +00005119 SDValue N0 = N->getOperand(0);
5120 SDValue N1 = N->getOperand(1);
5121
5122 assert(N0.getValueType().getVectorNumElements() == NumElts &&
5123 "Vector shuffle must be normalized in DAG");
5124
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005125 // If the shuffle mask is an identity operation on the LHS, return the LHS.
5126 bool isIdentity = true;
5127 for (unsigned i = 0; i != NumElts; ++i) {
5128 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005129 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() != i) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005130 isIdentity = false;
5131 break;
5132 }
5133 }
5134 if (isIdentity) return N->getOperand(0);
5135
5136 // If the shuffle mask is an identity operation on the RHS, return the RHS.
5137 isIdentity = true;
5138 for (unsigned i = 0; i != NumElts; ++i) {
5139 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005140 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() !=
5141 i+NumElts) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005142 isIdentity = false;
5143 break;
5144 }
5145 }
5146 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00005147
5148 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
5149 // needed at all.
5150 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00005151 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00005152 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00005153 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00005154 for (unsigned i = 0; i != NumElts; ++i)
5155 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005156 unsigned Idx=cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue();
Evan Chenge7bec0d2006-07-20 22:44:41 +00005157 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00005158 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00005159 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00005160 BaseIdx = Idx;
5161 } else {
5162 if (BaseIdx != Idx)
5163 isSplat = false;
5164 if (VecNum != V) {
5165 isUnary = false;
5166 break;
5167 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00005168 }
5169 }
5170
Evan Chenge7bec0d2006-07-20 22:44:41 +00005171 // Normalize unary shuffle so the RHS is undef.
5172 if (isUnary && VecNum == 1)
5173 std::swap(N0, N1);
5174
Evan Cheng917ec982006-07-21 08:25:53 +00005175 // If it is a splat, check if the argument vector is a build_vector with
5176 // all scalar elements the same.
5177 if (isSplat) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005178 SDNode *V = N0.getNode();
Evan Cheng917ec982006-07-21 08:25:53 +00005179
Dan Gohman7f321562007-06-25 16:23:39 +00005180 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00005181 // not the number of vector elements, look through it. Be careful not to
5182 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00005183 if (V->getOpcode() == ISD::BIT_CONVERT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005184 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00005185 if (ConvInput.getValueType().isVector() &&
5186 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +00005187 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +00005188 }
5189
Dan Gohman7f321562007-06-25 16:23:39 +00005190 if (V->getOpcode() == ISD::BUILD_VECTOR) {
5191 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00005192 if (NumElems > BaseIdx) {
Dan Gohman475871a2008-07-27 21:46:04 +00005193 SDValue Base;
Evan Cheng917ec982006-07-21 08:25:53 +00005194 bool AllSame = true;
5195 for (unsigned i = 0; i != NumElems; ++i) {
5196 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
5197 Base = V->getOperand(i);
5198 break;
5199 }
5200 }
5201 // Splat of <u, u, u, u>, return <u, u, u, u>
Gabor Greifba36cb52008-08-28 21:40:38 +00005202 if (!Base.getNode())
Evan Cheng917ec982006-07-21 08:25:53 +00005203 return N0;
5204 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00005205 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00005206 AllSame = false;
5207 break;
5208 }
5209 }
5210 // Splat of <x, x, x, x>, return <x, x, x, x>
5211 if (AllSame)
5212 return N0;
5213 }
5214 }
5215 }
5216
Evan Chenge7bec0d2006-07-20 22:44:41 +00005217 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
5218 // into an undef.
5219 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00005220 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
5221 // first operand.
Dan Gohman475871a2008-07-27 21:46:04 +00005222 SmallVector<SDValue, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00005223 for (unsigned i = 0; i != NumElts; ++i) {
5224 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005225 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() <
5226 NumElts) {
Chris Lattner17614ea2006-04-08 05:34:25 +00005227 MappedOps.push_back(ShufMask.getOperand(i));
5228 } else {
5229 unsigned NewIdx =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005230 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() -
5231 NumElts;
Duncan Sandsd038e042008-07-21 10:20:31 +00005232 MappedOps.push_back(DAG.getConstant(NewIdx,
5233 ShufMask.getOperand(i).getValueType()));
Chris Lattner17614ea2006-04-08 05:34:25 +00005234 }
5235 }
Dan Gohman7f321562007-06-25 16:23:39 +00005236 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005237 &MappedOps[0], MappedOps.size());
Gabor Greifba36cb52008-08-28 21:40:38 +00005238 AddToWorkList(ShufMask.getNode());
Dan Gohman7f321562007-06-25 16:23:39 +00005239 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
5240 N0,
5241 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
5242 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00005243 }
Dan Gohman7f321562007-06-25 16:23:39 +00005244
Dan Gohman475871a2008-07-27 21:46:04 +00005245 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005246}
5247
Evan Cheng44f1f092006-04-20 08:56:16 +00005248/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00005249/// an AND to a vector_shuffle with the destination vector and a zero vector.
5250/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00005251/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00005252SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
5253 SDValue LHS = N->getOperand(0);
5254 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00005255 if (N->getOpcode() == ISD::AND) {
5256 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00005257 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005258 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00005259 std::vector<SDValue> IdxOps;
Evan Cheng44f1f092006-04-20 08:56:16 +00005260 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00005261 unsigned NumElts = NumOps;
Evan Cheng44f1f092006-04-20 08:56:16 +00005262 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005263 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00005264 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00005265 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005266 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Duncan Sands94989ac2008-10-19 14:58:05 +00005267 IdxOps.push_back(DAG.getIntPtrConstant(i));
Evan Cheng44f1f092006-04-20 08:56:16 +00005268 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Duncan Sands94989ac2008-10-19 14:58:05 +00005269 IdxOps.push_back(DAG.getIntPtrConstant(NumElts));
Evan Cheng44f1f092006-04-20 08:56:16 +00005270 else
Dan Gohman475871a2008-07-27 21:46:04 +00005271 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005272 }
5273
5274 // Let's see if the target supports this vector_shuffle.
Duncan Sands94989ac2008-10-19 14:58:05 +00005275 if (!TLI.isVectorClearMaskLegal(IdxOps, TLI.getPointerTy(), DAG))
Dan Gohman475871a2008-07-27 21:46:04 +00005276 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005277
Dan Gohman7f321562007-06-25 16:23:39 +00005278 // Return the new VECTOR_SHUFFLE node.
Duncan Sands94989ac2008-10-19 14:58:05 +00005279 MVT EVT = RHS.getValueType().getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005280 MVT VT = MVT::getVectorVT(EVT, NumElts);
Evan Cheng3eb57d52008-11-05 06:04:18 +00005281 MVT MaskVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Dan Gohman475871a2008-07-27 21:46:04 +00005282 std::vector<SDValue> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005283 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00005284 Ops.push_back(LHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00005285 AddToWorkList(LHS.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00005286 std::vector<SDValue> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00005287 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005288 &ZeroOps[0], ZeroOps.size()));
Evan Cheng3eb57d52008-11-05 06:04:18 +00005289 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005290 &IdxOps[0], IdxOps.size()));
Dan Gohman475871a2008-07-27 21:46:04 +00005291 SDValue Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005292 &Ops[0], Ops.size());
Dan Gohman7a9a5af2008-07-16 16:13:58 +00005293 if (VT != N->getValueType(0))
5294 Result = DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00005295 return Result;
5296 }
5297 }
Dan Gohman475871a2008-07-27 21:46:04 +00005298 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005299}
5300
Dan Gohman7f321562007-06-25 16:23:39 +00005301/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00005302SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005303 // After legalize, the target may be depending on adds and other
5304 // binary ops to provide legal ways to construct constants or other
5305 // things. Simplifying them may result in a loss of legality.
Duncan Sands25cf2272008-11-24 14:53:14 +00005306 if (LegalOperations) return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00005307
Duncan Sands83ec4b62008-06-06 12:08:01 +00005308 MVT VT = N->getValueType(0);
5309 assert(VT.isVector() && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00005310
Duncan Sands83ec4b62008-06-06 12:08:01 +00005311 MVT EltType = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00005312 SDValue LHS = N->getOperand(0);
5313 SDValue RHS = N->getOperand(1);
5314 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005315 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +00005316
Dan Gohman7f321562007-06-25 16:23:39 +00005317 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00005318 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00005319 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
5320 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00005321 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005322 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005323 SDValue LHSOp = LHS.getOperand(i);
5324 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00005325 // If these two elements can't be folded, bail out.
5326 if ((LHSOp.getOpcode() != ISD::UNDEF &&
5327 LHSOp.getOpcode() != ISD::Constant &&
5328 LHSOp.getOpcode() != ISD::ConstantFP) ||
5329 (RHSOp.getOpcode() != ISD::UNDEF &&
5330 RHSOp.getOpcode() != ISD::Constant &&
5331 RHSOp.getOpcode() != ISD::ConstantFP))
5332 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00005333 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00005334 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
5335 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00005336 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005337 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +00005338 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005339 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00005340 break;
5341 }
Dan Gohman7f321562007-06-25 16:23:39 +00005342 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Gabor Greifba36cb52008-08-28 21:40:38 +00005343 AddToWorkList(Ops.back().getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +00005344 assert((Ops.back().getOpcode() == ISD::UNDEF ||
5345 Ops.back().getOpcode() == ISD::Constant ||
5346 Ops.back().getOpcode() == ISD::ConstantFP) &&
5347 "Scalar binop didn't fold!");
5348 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005349
Dan Gohman7f321562007-06-25 16:23:39 +00005350 if (Ops.size() == LHS.getNumOperands()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005351 MVT VT = LHS.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00005352 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005353 }
Chris Lattneredab1b92006-04-02 03:25:57 +00005354 }
5355
Dan Gohman475871a2008-07-27 21:46:04 +00005356 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00005357}
5358
Dan Gohman475871a2008-07-27 21:46:04 +00005359SDValue DAGCombiner::SimplifySelect(SDValue N0, SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00005360 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
5361
Dan Gohman475871a2008-07-27 21:46:04 +00005362 SDValue SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00005363 cast<CondCodeSDNode>(N0.getOperand(2))->get());
5364 // If we got a simplified select_cc node back from SimplifySelectCC, then
5365 // break it down into a new SETCC node, and a new SELECT node, and then return
5366 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +00005367 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005368 // Check to see if we got a select_cc back (to turn into setcc/select).
5369 // Otherwise, just return whatever node we got back, like fabs.
5370 if (SCC.getOpcode() == ISD::SELECT_CC) {
Dan Gohman475871a2008-07-27 21:46:04 +00005371 SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
Nate Begemanf845b452005-10-08 00:29:44 +00005372 SCC.getOperand(0), SCC.getOperand(1),
5373 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +00005374 AddToWorkList(SETCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005375 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
5376 SCC.getOperand(3), SETCC);
5377 }
5378 return SCC;
5379 }
Dan Gohman475871a2008-07-27 21:46:04 +00005380 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005381}
5382
Chris Lattner40c62d52005-10-18 06:04:22 +00005383/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
5384/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00005385/// select. Callers of this should assume that TheSelect is deleted if this
5386/// returns true. As such, they should return the appropriate thing (e.g. the
5387/// node) back to the top-level of the DAG combiner loop to avoid it being
5388/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00005389///
Dan Gohman475871a2008-07-27 21:46:04 +00005390bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
5391 SDValue RHS) {
Chris Lattner40c62d52005-10-18 06:04:22 +00005392
5393 // If this is a select from two identical things, try to pull the operation
5394 // through the select.
5395 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00005396 // If this is a load and the token chain is identical, replace the select
5397 // of two loads with a load through a select of the address to load from.
5398 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
5399 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00005400 if (LHS.getOpcode() == ISD::LOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005401 // Do not let this transformation reduce the number of volatile loads.
5402 !cast<LoadSDNode>(LHS)->isVolatile() &&
5403 !cast<LoadSDNode>(RHS)->isVolatile() &&
Chris Lattner40c62d52005-10-18 06:04:22 +00005404 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00005405 LHS.getOperand(0) == RHS.getOperand(0)) {
5406 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5407 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5408
5409 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005410 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00005411 // FIXME: this conflates two src values, discarding one. This is not
5412 // the right thing to do, but nothing uses srcvalues now. When they do,
5413 // turn SrcValue into a list of locations.
Dan Gohman475871a2008-07-27 21:46:04 +00005414 SDValue Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005415 if (TheSelect->getOpcode() == ISD::SELECT) {
5416 // Check that the condition doesn't reach either load. If so, folding
5417 // this will induce a cycle into the DAG.
Gabor Greifba36cb52008-08-28 21:40:38 +00005418 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5419 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode())) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005420 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
5421 TheSelect->getOperand(0), LLD->getBasePtr(),
5422 RLD->getBasePtr());
5423 }
5424 } else {
5425 // Check that the condition doesn't reach either load. If so, folding
5426 // this will induce a cycle into the DAG.
Gabor Greifba36cb52008-08-28 21:40:38 +00005427 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5428 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5429 !LLD->isPredecessorOf(TheSelect->getOperand(1).getNode()) &&
5430 !RLD->isPredecessorOf(TheSelect->getOperand(1).getNode())) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005431 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00005432 TheSelect->getOperand(0),
5433 TheSelect->getOperand(1),
5434 LLD->getBasePtr(), RLD->getBasePtr(),
5435 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005436 }
Evan Cheng466685d2006-10-09 20:57:25 +00005437 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005438
Gabor Greifba36cb52008-08-28 21:40:38 +00005439 if (Addr.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005440 SDValue Load;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005441 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
5442 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
5443 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005444 LLD->getSrcValueOffset(),
5445 LLD->isVolatile(),
5446 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005447 else {
5448 Load = DAG.getExtLoad(LLD->getExtensionType(),
5449 TheSelect->getValueType(0),
5450 LLD->getChain(), Addr, LLD->getSrcValue(),
5451 LLD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005452 LLD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005453 LLD->isVolatile(),
5454 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005455 }
5456 // Users of the select now use the result of the load.
5457 CombineTo(TheSelect, Load);
5458
5459 // Users of the old loads now use the new load's chain. We know the
5460 // old-load value is dead now.
Gabor Greifba36cb52008-08-28 21:40:38 +00005461 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
5462 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005463 return true;
5464 }
Evan Chengc5484282006-10-04 00:56:09 +00005465 }
Chris Lattner40c62d52005-10-18 06:04:22 +00005466 }
5467 }
5468
5469 return false;
5470}
5471
Dan Gohman475871a2008-07-27 21:46:04 +00005472SDValue DAGCombiner::SimplifySelectCC(SDValue N0, SDValue N1,
5473 SDValue N2, SDValue N3,
5474 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00005475
Duncan Sands83ec4b62008-06-06 12:08:01 +00005476 MVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +00005477 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
5478 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
5479 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005480
5481 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00005482 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
5483 N0, N1, CC, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00005484 if (SCC.getNode()) AddToWorkList(SCC.getNode());
5485 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005486
5487 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00005488 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005489 return N2;
5490 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00005491 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005492 return N3;
5493
5494 // Check to see if we can simplify the select into an fabs node
5495 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5496 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00005497 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005498 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5499 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5500 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5501 N2 == N3.getOperand(0))
5502 return DAG.getNode(ISD::FABS, VT, N0);
5503
5504 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5505 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5506 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5507 N2.getOperand(0) == N3)
5508 return DAG.getNode(ISD::FABS, VT, N3);
5509 }
5510 }
5511
5512 // Check to see if we can perform the "gzip trick", transforming
5513 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00005514 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005515 N0.getValueType().isInteger() &&
5516 N2.getValueType().isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005517 (N1C->isNullValue() || // (a < 0) ? b : 0
5518 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Duncan Sands83ec4b62008-06-06 12:08:01 +00005519 MVT XType = N0.getValueType();
5520 MVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00005521 if (XType.bitsGE(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005522 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00005523 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00005524 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
5525 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005526 ShCtV = XType.getSizeInBits()-ShCtV-1;
Dan Gohman475871a2008-07-27 21:46:04 +00005527 SDValue ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5528 SDValue Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +00005529 AddToWorkList(Shift.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00005530 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005531 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005532 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005533 }
5534 return DAG.getNode(ISD::AND, AType, Shift, N2);
5535 }
Dan Gohman475871a2008-07-27 21:46:04 +00005536 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005537 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005538 TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00005539 AddToWorkList(Shift.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00005540 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005541 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005542 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005543 }
5544 return DAG.getNode(ISD::AND, AType, Shift, N2);
5545 }
5546 }
Nate Begeman07ed4172005-10-10 21:26:48 +00005547
5548 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00005549 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands03228082008-11-23 15:47:28 +00005550 TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00005551
5552 // If the caller doesn't want us to simplify this into a zext of a compare,
5553 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00005554 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00005555 return SDValue();
Chris Lattner1eba01e2007-04-11 06:50:51 +00005556
Nate Begeman07ed4172005-10-10 21:26:48 +00005557 // Get a SetCC of the condition
5558 // FIXME: Should probably make sure that setcc is legal if we ever have a
5559 // target where it isn't.
Dan Gohman475871a2008-07-27 21:46:04 +00005560 SDValue Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00005561 // cast from setcc result type to select result type
Duncan Sands25cf2272008-11-24 14:53:14 +00005562 if (LegalTypes) {
Duncan Sands5480c042009-01-01 15:52:00 +00005563 SCC = DAG.getSetCC(TLI.getSetCCResultType(N0.getValueType()),
5564 N0, N1, CC);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005565 if (N2.getValueType().bitsLT(SCC.getValueType()))
Chris Lattner555d8d62006-12-07 22:36:47 +00005566 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5567 else
5568 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005569 } else {
5570 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00005571 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005572 }
Gabor Greifba36cb52008-08-28 21:40:38 +00005573 AddToWorkList(SCC.getNode());
5574 AddToWorkList(Temp.getNode());
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005575
Dan Gohman002e5d02008-03-13 22:13:53 +00005576 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005577 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00005578 // shl setcc result by log2 n2c
5579 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00005580 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Nate Begeman07ed4172005-10-10 21:26:48 +00005581 TLI.getShiftAmountTy()));
5582 }
5583
Nate Begemanf845b452005-10-08 00:29:44 +00005584 // Check to see if this is the equivalent of setcc
5585 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5586 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00005587 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005588 MVT XType = N0.getValueType();
Duncan Sands25cf2272008-11-24 14:53:14 +00005589 if (!LegalOperations ||
Duncan Sands5480c042009-01-01 15:52:00 +00005590 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(XType))) {
5591 SDValue Res = DAG.getSetCC(TLI.getSetCCResultType(XType), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00005592 if (Res.getValueType() != VT)
5593 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5594 return Res;
5595 }
5596
5597 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5598 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005599 (!LegalOperations ||
Duncan Sands184a8762008-06-14 17:48:34 +00005600 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Dan Gohman475871a2008-07-27 21:46:04 +00005601 SDValue Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
Nate Begemanf845b452005-10-08 00:29:44 +00005602 return DAG.getNode(ISD::SRL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005603 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Nate Begemanf845b452005-10-08 00:29:44 +00005604 TLI.getShiftAmountTy()));
5605 }
5606 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5607 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005608 SDValue NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
Nate Begemanf845b452005-10-08 00:29:44 +00005609 N0);
Bob Wilson4c245462009-01-22 17:39:32 +00005610 SDValue NotN0 = DAG.getNOT(N0, XType);
Nate Begemanf845b452005-10-08 00:29:44 +00005611 return DAG.getNode(ISD::SRL, XType,
5612 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00005613 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005614 TLI.getShiftAmountTy()));
5615 }
5616 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5617 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005618 SDValue Sign = DAG.getNode(ISD::SRL, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005619 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005620 TLI.getShiftAmountTy()));
5621 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5622 }
5623 }
5624
5625 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5626 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5627 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005628 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005629 N2.getOperand(0) == N1 && N0.getValueType().isInteger()) {
5630 MVT XType = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00005631 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005632 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005633 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00005634 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005635 AddToWorkList(Shift.getNode());
5636 AddToWorkList(Add.getNode());
Chris Lattner1982ef22007-04-11 05:11:38 +00005637 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5638 }
5639 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5640 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5641 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5642 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5643 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005644 MVT XType = N0.getValueType();
5645 if (SubC->isNullValue() && XType.isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005646 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005647 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005648 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00005649 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005650 AddToWorkList(Shift.getNode());
5651 AddToWorkList(Add.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005652 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5653 }
5654 }
5655 }
Chris Lattner1982ef22007-04-11 05:11:38 +00005656
Dan Gohman475871a2008-07-27 21:46:04 +00005657 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005658}
5659
Evan Chengfa1eb272007-02-08 22:13:59 +00005660/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Dan Gohman475871a2008-07-27 21:46:04 +00005661SDValue DAGCombiner::SimplifySetCC(MVT VT, SDValue N0,
5662 SDValue N1, ISD::CondCode Cond,
5663 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00005664 TargetLowering::DAGCombinerInfo
Duncan Sands25cf2272008-11-24 14:53:14 +00005665 DagCombineInfo(DAG, Level == Unrestricted, false, this);
Evan Chengfa1eb272007-02-08 22:13:59 +00005666 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00005667}
5668
Nate Begeman69575232005-10-20 02:15:44 +00005669/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5670/// return a DAG expression to select that will generate the same value by
5671/// multiplying by a magic number. See:
5672/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00005673SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005674 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00005675 SDValue S = TLI.BuildSDIV(N, DAG, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005676
Andrew Lenharth232c9102006-06-12 16:07:18 +00005677 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005678 ii != ee; ++ii)
5679 AddToWorkList(*ii);
5680 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005681}
5682
5683/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5684/// return a DAG expression to select that will generate the same value by
5685/// multiplying by a magic number. See:
5686/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00005687SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005688 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00005689 SDValue S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00005690
Andrew Lenharth232c9102006-06-12 16:07:18 +00005691 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005692 ii != ee; ++ii)
5693 AddToWorkList(*ii);
5694 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005695}
5696
Jim Laskey71382342006-10-07 23:37:56 +00005697/// FindBaseOffset - Return true if base is known not to alias with anything
5698/// but itself. Provides base object and offset as results.
Dan Gohman475871a2008-07-27 21:46:04 +00005699static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset) {
Jim Laskey71382342006-10-07 23:37:56 +00005700 // Assume it is a primitive operation.
5701 Base = Ptr; Offset = 0;
5702
5703 // If it's an adding a simple constant then integrate the offset.
5704 if (Base.getOpcode() == ISD::ADD) {
5705 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5706 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005707 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +00005708 }
5709 }
5710
5711 // If it's any of the following then it can't alias with anything but itself.
5712 return isa<FrameIndexSDNode>(Base) ||
5713 isa<ConstantPoolSDNode>(Base) ||
5714 isa<GlobalAddressSDNode>(Base);
5715}
5716
5717/// isAlias - Return true if there is any possibility that the two addresses
5718/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +00005719bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +00005720 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman475871a2008-07-27 21:46:04 +00005721 SDValue Ptr2, int64_t Size2,
Jim Laskey096c22e2006-10-18 12:29:57 +00005722 const Value *SrcValue2, int SrcValueOffset2)
5723{
Jim Laskey71382342006-10-07 23:37:56 +00005724 // If they are the same then they must be aliases.
5725 if (Ptr1 == Ptr2) return true;
5726
5727 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +00005728 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +00005729 int64_t Offset1, Offset2;
5730 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5731 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5732
5733 // If they have a same base address then...
5734 if (Base1 == Base2) {
5735 // Check to see if the addresses overlap.
5736 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5737 }
5738
Jim Laskey096c22e2006-10-18 12:29:57 +00005739 // If we know both bases then they can't alias.
5740 if (KnownBase1 && KnownBase2) return false;
5741
Jim Laskey07a27092006-10-18 19:08:31 +00005742 if (CombinerGlobalAA) {
5743 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00005744 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5745 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5746 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00005747 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00005748 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00005749 if (AAResult == AliasAnalysis::NoAlias)
5750 return false;
5751 }
Jim Laskey096c22e2006-10-18 12:29:57 +00005752
5753 // Otherwise we have to assume they alias.
5754 return true;
Jim Laskey71382342006-10-07 23:37:56 +00005755}
5756
5757/// FindAliasInfo - Extracts the relevant alias information from the memory
5758/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00005759bool DAGCombiner::FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +00005760 SDValue &Ptr, int64_t &Size,
Jim Laskey096c22e2006-10-18 12:29:57 +00005761 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005762 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5763 Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005764 Size = LD->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005765 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005766 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00005767 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005768 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005769 Ptr = ST->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005770 Size = ST->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005771 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005772 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00005773 } else {
Jim Laskey71382342006-10-07 23:37:56 +00005774 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005775 }
5776
5777 return false;
5778}
5779
Jim Laskey6ff23e52006-10-04 16:53:27 +00005780/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5781/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +00005782void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
5783 SmallVector<SDValue, 8> &Aliases) {
5784 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005785 std::set<SDNode *> Visited; // Visited node set.
5786
Jim Laskey279f0532006-09-25 16:29:54 +00005787 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +00005788 SDValue Ptr;
Jim Laskey279f0532006-09-25 16:29:54 +00005789 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005790 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005791 int SrcValueOffset;
5792 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005793
Jim Laskey6ff23e52006-10-04 16:53:27 +00005794 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005795 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005796
Jim Laskeybc588b82006-10-05 15:07:25 +00005797 // Look at each chain and determine if it is an alias. If so, add it to the
5798 // aliases list. If not, then continue up the chain looking for the next
5799 // candidate.
5800 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005801 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +00005802 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005803
Jim Laskeybc588b82006-10-05 15:07:25 +00005804 // Don't bother if we've been before.
Gabor Greifba36cb52008-08-28 21:40:38 +00005805 if (Visited.find(Chain.getNode()) != Visited.end()) continue;
5806 Visited.insert(Chain.getNode());
Jim Laskeybc588b82006-10-05 15:07:25 +00005807
5808 switch (Chain.getOpcode()) {
5809 case ISD::EntryToken:
5810 // Entry token is ideal chain operand, but handled in FindBetterChain.
5811 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005812
Jim Laskeybc588b82006-10-05 15:07:25 +00005813 case ISD::LOAD:
5814 case ISD::STORE: {
5815 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +00005816 SDValue OpPtr;
Jim Laskeybc588b82006-10-05 15:07:25 +00005817 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005818 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005819 int OpSrcValueOffset;
Gabor Greifba36cb52008-08-28 21:40:38 +00005820 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Jim Laskey096c22e2006-10-18 12:29:57 +00005821 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005822
5823 // If chain is alias then stop here.
5824 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005825 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5826 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005827 Aliases.push_back(Chain);
5828 } else {
5829 // Look further up the chain.
5830 Chains.push_back(Chain.getOperand(0));
5831 // Clean up old chain.
Gabor Greifba36cb52008-08-28 21:40:38 +00005832 AddToWorkList(Chain.getNode());
Jim Laskey279f0532006-09-25 16:29:54 +00005833 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005834 break;
5835 }
5836
5837 case ISD::TokenFactor:
5838 // We have to check each of the operands of the token factor, so we queue
5839 // then up. Adding the operands to the queue (stack) in reverse order
5840 // maintains the original order and increases the likelihood that getNode
5841 // will find a matching token factor (CSE.)
5842 for (unsigned n = Chain.getNumOperands(); n;)
5843 Chains.push_back(Chain.getOperand(--n));
5844 // Eliminate the token factor if we can.
Gabor Greifba36cb52008-08-28 21:40:38 +00005845 AddToWorkList(Chain.getNode());
Jim Laskeybc588b82006-10-05 15:07:25 +00005846 break;
5847
5848 default:
5849 // For all other instructions we will just have to take what we can get.
5850 Aliases.push_back(Chain);
5851 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005852 }
5853 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005854}
5855
5856/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5857/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +00005858SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
5859 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005860
Jim Laskey6ff23e52006-10-04 16:53:27 +00005861 // Accumulate all the aliases to this node.
5862 GatherAllAliases(N, OldChain, Aliases);
5863
5864 if (Aliases.size() == 0) {
5865 // If no operands then chain to entry token.
5866 return DAG.getEntryNode();
5867 } else if (Aliases.size() == 1) {
5868 // If a single operand then chain to it. We don't need to revisit it.
5869 return Aliases[0];
5870 }
5871
5872 // Construct a custom tailored token factor.
Dan Gohman475871a2008-07-27 21:46:04 +00005873 SDValue NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005874 &Aliases[0], Aliases.size());
5875
5876 // Make sure the old chain gets cleaned up.
Gabor Greifba36cb52008-08-28 21:40:38 +00005877 if (NewChain != OldChain) AddToWorkList(OldChain.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00005878
5879 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005880}
5881
Nate Begeman1d4d4142005-09-01 00:19:25 +00005882// SelectionDAG::Combine - This is the entry point for the file.
5883//
Duncan Sands25cf2272008-11-24 14:53:14 +00005884void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA, bool Fast) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00005885 /// run - This is the main entry point to this class.
5886 ///
Duncan Sands25cf2272008-11-24 14:53:14 +00005887 DAGCombiner(*this, AA, Fast).Run(Level);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005888}