blob: c84591d52eccaa8ea8c11ead59612c35929b0871 [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Nate Begeman1d4d4142005-09-01 00:19:25 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
Nate Begeman1d4d4142005-09-01 00:19:25 +000012//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "dagcombine"
Nate Begeman1d4d4142005-09-01 00:19:25 +000016#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner00161a62008-01-25 07:20:16 +000017#include "llvm/CodeGen/MachineFunction.h"
18#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000019#include "llvm/Analysis/AliasAnalysis.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000020#include "llvm/Target/TargetData.h"
Chris Lattner1329cb82008-01-26 19:45:50 +000021#include "llvm/Target/TargetFrameInfo.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000022#include "llvm/Target/TargetLowering.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000023#include "llvm/Target/TargetMachine.h"
Chris Lattnerddae4bd2007-01-08 23:04:05 +000024#include "llvm/Target/TargetOptions.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000025#include "llvm/ADT/SmallPtrSet.h"
26#include "llvm/ADT/Statistic.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000028#include "llvm/Support/CommandLine.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/MathExtras.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000031#include <algorithm>
Dan Gohmanee335e32008-05-23 20:40:06 +000032#include <set>
Nate Begeman1d4d4142005-09-01 00:19:25 +000033using namespace llvm;
34
Chris Lattnercd3245a2006-12-19 22:41:21 +000035STATISTIC(NodesCombined , "Number of dag nodes combined");
36STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
37STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
38
Nate Begeman1d4d4142005-09-01 00:19:25 +000039namespace {
Jim Laskey71382342006-10-07 23:37:56 +000040 static cl::opt<bool>
41 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000042 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000043
Jim Laskey07a27092006-10-18 19:08:31 +000044 static cl::opt<bool>
45 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
46 cl::desc("Include global information in alias analysis"));
47
Jim Laskeybc588b82006-10-05 15:07:25 +000048//------------------------------ DAGCombiner ---------------------------------//
49
Jim Laskey71382342006-10-07 23:37:56 +000050 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000051 SelectionDAG &DAG;
Dan Gohman79ce2762009-01-15 19:20:50 +000052 const TargetLowering &TLI;
Duncan Sands25cf2272008-11-24 14:53:14 +000053 CombineLevel Level;
54 bool LegalOperations;
55 bool LegalTypes;
Dan Gohmana2676512008-08-20 16:30:28 +000056 bool Fast;
Nate Begeman1d4d4142005-09-01 00:19:25 +000057
58 // Worklist of all of the nodes that need to be simplified.
Evan Cheng17a568b2008-08-29 22:21:44 +000059 std::vector<SDNode*> WorkList;
Nate Begeman1d4d4142005-09-01 00:19:25 +000060
Jim Laskeyc7c3f112006-10-16 20:52:31 +000061 // AA - Used for DAG load/store alias analysis.
62 AliasAnalysis &AA;
63
Nate Begeman1d4d4142005-09-01 00:19:25 +000064 /// AddUsersToWorkList - When an instruction is simplified, add all users of
65 /// the instruction to the work lists because they might get more simplified
66 /// now.
67 ///
68 void AddUsersToWorkList(SDNode *N) {
69 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000070 UI != UE; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +000071 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000072 }
73
Dan Gohman389079b2007-10-08 17:57:15 +000074 /// visit - call the node-specific routine that knows how to fold each
75 /// particular type of node.
Dan Gohman475871a2008-07-27 21:46:04 +000076 SDValue visit(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +000077
Chris Lattner24664722006-03-01 04:53:38 +000078 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +000079 /// AddToWorkList - Add to the work list making sure it's instance is at the
80 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +000081 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000082 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +000083 WorkList.push_back(N);
84 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000085
Chris Lattnerf8dc0612008-02-03 06:49:24 +000086 /// removeFromWorkList - remove all instances of N from the worklist.
87 ///
88 void removeFromWorkList(SDNode *N) {
89 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
90 WorkList.end());
Chris Lattner01a22022005-10-10 22:04:48 +000091 }
Nate Begeman368e18d2006-02-16 21:11:51 +000092
Dan Gohman475871a2008-07-27 21:46:04 +000093 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Chris Lattnerf8dc0612008-02-03 06:49:24 +000094 bool AddTo = true);
95
Dan Gohman475871a2008-07-27 21:46:04 +000096 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Jim Laskey274062c2006-10-13 23:32:28 +000097 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +000098 }
99
Dan Gohman475871a2008-07-27 21:46:04 +0000100 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Jim Laskey274062c2006-10-13 23:32:28 +0000101 bool AddTo = true) {
Dan Gohman475871a2008-07-27 21:46:04 +0000102 SDValue To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000103 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000104 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000105
106 void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO);
Chris Lattner0bd48932008-01-17 07:00:52 +0000107
Chris Lattner24664722006-03-01 04:53:38 +0000108 private:
109
Chris Lattner012f2412006-02-17 21:58:01 +0000110 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000111 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000112 /// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000113 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000114 APInt Demanded = APInt::getAllOnesValue(Op.getValueSizeInBits());
115 return SimplifyDemandedBits(Op, Demanded);
116 }
117
Dan Gohman475871a2008-07-27 21:46:04 +0000118 bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded);
Chris Lattner87514ca2005-10-10 22:31:19 +0000119
Chris Lattner448f2192006-11-11 00:39:41 +0000120 bool CombineToPreIndexedLoadStore(SDNode *N);
121 bool CombineToPostIndexedLoadStore(SDNode *N);
122
123
Dan Gohman389079b2007-10-08 17:57:15 +0000124 /// combine - call the node-specific routine that knows how to fold each
125 /// particular type of node. If that doesn't do anything, try the
126 /// target-specific DAG combines.
Dan Gohman475871a2008-07-27 21:46:04 +0000127 SDValue combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000128
129 // Visitation implementation - Implement dag node combining for different
130 // node types. The semantics are as follows:
131 // Return Value:
Evan Cheng17a568b2008-08-29 22:21:44 +0000132 // SDValue.getNode() == 0 - No change was made
133 // SDValue.getNode() == N - N was replaced, is dead and has been handled.
134 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000135 //
Dan Gohman475871a2008-07-27 21:46:04 +0000136 SDValue visitTokenFactor(SDNode *N);
137 SDValue visitMERGE_VALUES(SDNode *N);
138 SDValue visitADD(SDNode *N);
139 SDValue visitSUB(SDNode *N);
140 SDValue visitADDC(SDNode *N);
141 SDValue visitADDE(SDNode *N);
142 SDValue visitMUL(SDNode *N);
143 SDValue visitSDIV(SDNode *N);
144 SDValue visitUDIV(SDNode *N);
145 SDValue visitSREM(SDNode *N);
146 SDValue visitUREM(SDNode *N);
147 SDValue visitMULHU(SDNode *N);
148 SDValue visitMULHS(SDNode *N);
149 SDValue visitSMUL_LOHI(SDNode *N);
150 SDValue visitUMUL_LOHI(SDNode *N);
151 SDValue visitSDIVREM(SDNode *N);
152 SDValue visitUDIVREM(SDNode *N);
153 SDValue visitAND(SDNode *N);
154 SDValue visitOR(SDNode *N);
155 SDValue visitXOR(SDNode *N);
156 SDValue SimplifyVBinOp(SDNode *N);
157 SDValue visitSHL(SDNode *N);
158 SDValue visitSRA(SDNode *N);
159 SDValue visitSRL(SDNode *N);
160 SDValue visitCTLZ(SDNode *N);
161 SDValue visitCTTZ(SDNode *N);
162 SDValue visitCTPOP(SDNode *N);
163 SDValue visitSELECT(SDNode *N);
164 SDValue visitSELECT_CC(SDNode *N);
165 SDValue visitSETCC(SDNode *N);
166 SDValue visitSIGN_EXTEND(SDNode *N);
167 SDValue visitZERO_EXTEND(SDNode *N);
168 SDValue visitANY_EXTEND(SDNode *N);
169 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
170 SDValue visitTRUNCATE(SDNode *N);
171 SDValue visitBIT_CONVERT(SDNode *N);
172 SDValue visitBUILD_PAIR(SDNode *N);
173 SDValue visitFADD(SDNode *N);
174 SDValue visitFSUB(SDNode *N);
175 SDValue visitFMUL(SDNode *N);
176 SDValue visitFDIV(SDNode *N);
177 SDValue visitFREM(SDNode *N);
178 SDValue visitFCOPYSIGN(SDNode *N);
179 SDValue visitSINT_TO_FP(SDNode *N);
180 SDValue visitUINT_TO_FP(SDNode *N);
181 SDValue visitFP_TO_SINT(SDNode *N);
182 SDValue visitFP_TO_UINT(SDNode *N);
183 SDValue visitFP_ROUND(SDNode *N);
184 SDValue visitFP_ROUND_INREG(SDNode *N);
185 SDValue visitFP_EXTEND(SDNode *N);
186 SDValue visitFNEG(SDNode *N);
187 SDValue visitFABS(SDNode *N);
188 SDValue visitBRCOND(SDNode *N);
189 SDValue visitBR_CC(SDNode *N);
190 SDValue visitLOAD(SDNode *N);
191 SDValue visitSTORE(SDNode *N);
192 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
193 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
194 SDValue visitBUILD_VECTOR(SDNode *N);
195 SDValue visitCONCAT_VECTORS(SDNode *N);
196 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000197
Dan Gohman475871a2008-07-27 21:46:04 +0000198 SDValue XformToShuffleWithZero(SDNode *N);
Bill Wendling35247c32009-01-30 00:45:56 +0000199 SDValue ReassociateOps(unsigned Opc, DebugLoc DL, SDValue LHS, SDValue RHS);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000200
Dan Gohman475871a2008-07-27 21:46:04 +0000201 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattnere70da202007-12-06 07:33:36 +0000202
Dan Gohman475871a2008-07-27 21:46:04 +0000203 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
204 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
205 SDValue SimplifySelect(SDValue N0, SDValue N1, SDValue N2);
206 SDValue SimplifySelectCC(SDValue N0, SDValue N1, SDValue N2,
207 SDValue N3, ISD::CondCode CC,
Chris Lattner1eba01e2007-04-11 06:50:51 +0000208 bool NotExtCompare = false);
Duncan Sands25cf2272008-11-24 14:53:14 +0000209 SDValue SimplifySetCC(MVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond,
210 bool foldBooleans = true);
Dan Gohman475871a2008-07-27 21:46:04 +0000211 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner5eee4272008-01-26 01:09:19 +0000212 unsigned HiOp);
Dan Gohman475871a2008-07-27 21:46:04 +0000213 SDValue CombineConsecutiveLoads(SDNode *N, MVT VT);
214 SDValue ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, MVT);
215 SDValue BuildSDIV(SDNode *N);
216 SDValue BuildUDIV(SDNode *N);
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))
Bill Wendling35247c32009-01-30 00:45:56 +0000393 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000394 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000395 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000396 Op.getOperand(1));
397 // -(A+B) -> -B - A
Bill Wendling35247c32009-01-30 00:45:56 +0000398 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000399 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000400 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000401 Op.getOperand(0));
402 case ISD::FSUB:
403 // We can't turn -(A-B) into B-A when we honor signed zeros.
404 assert(UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000405
406 // -(0-B) -> B
407 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000408 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000409 return Op.getOperand(1);
Chris Lattner29446522007-05-14 22:04:50 +0000410
411 // -(A-B) -> B-A
Bill Wendling35247c32009-01-30 00:45:56 +0000412 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
413 Op.getOperand(1), Op.getOperand(0));
Chris Lattner29446522007-05-14 22:04:50 +0000414
415 case ISD::FMUL:
416 case ISD::FDIV:
417 assert(!HonorSignDependentRoundingFPMath());
418
419 // -(X*Y) -> -X * Y
Duncan Sands25cf2272008-11-24 14:53:14 +0000420 if (isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Bill Wendling35247c32009-01-30 00:45:56 +0000421 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000422 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000423 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000424 Op.getOperand(1));
425
426 // -(X*Y) -> X * -Y
Bill Wendling35247c32009-01-30 00:45:56 +0000427 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Chris Lattner29446522007-05-14 22:04:50 +0000428 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000429 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000430 LegalOperations, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000431
432 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000433 case ISD::FSIN:
Bill Wendling35247c32009-01-30 00:45:56 +0000434 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000435 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000436 LegalOperations, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000437 case ISD::FP_ROUND:
Bill Wendling35247c32009-01-30 00:45:56 +0000438 return DAG.getNode(ISD::FP_ROUND, Op.getDebugLoc(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000439 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000440 LegalOperations, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000441 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000442 }
443}
Chris Lattner24664722006-03-01 04:53:38 +0000444
445
Nate Begeman4ebd8052005-09-01 23:24:04 +0000446// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
447// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000448// Also, set the incoming LHS, RHS, and CC references to the appropriate
449// nodes based on the type of node we are checking. This simplifies life a
450// bit for the callers.
Dan Gohman475871a2008-07-27 21:46:04 +0000451static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
452 SDValue &CC) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000453 if (N.getOpcode() == ISD::SETCC) {
454 LHS = N.getOperand(0);
455 RHS = N.getOperand(1);
456 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000457 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000458 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000459 if (N.getOpcode() == ISD::SELECT_CC &&
460 N.getOperand(2).getOpcode() == ISD::Constant &&
461 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000462 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000463 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
464 LHS = N.getOperand(0);
465 RHS = N.getOperand(1);
466 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000467 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000468 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000469 return false;
470}
471
Nate Begeman99801192005-09-07 23:25:52 +0000472// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
473// one use. If this is true, it allows the users to invert the operation for
474// free when it is profitable to do so.
Dan Gohman475871a2008-07-27 21:46:04 +0000475static bool isOneUseSetCC(SDValue N) {
476 SDValue N0, N1, N2;
Gabor Greifba36cb52008-08-28 21:40:38 +0000477 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000478 return true;
479 return false;
480}
481
Bill Wendling35247c32009-01-30 00:45:56 +0000482SDValue DAGCombiner::ReassociateOps(unsigned Opc, DebugLoc DL,
483 SDValue N0, SDValue N1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000484 MVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000485 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
486 if (isa<ConstantSDNode>(N1)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000487 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
Bill Wendlingd69c3142009-01-30 02:23:43 +0000488 SDValue OpNode = DAG.getNode(Opc, N1.getDebugLoc(), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000489 N0.getOperand(1), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000490 AddToWorkList(OpNode.getNode());
Bill Wendlingd69c3142009-01-30 02:23:43 +0000491 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000492 } else if (N0.hasOneUse()) {
Bill Wendling35247c32009-01-30 00:45:56 +0000493 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
494 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
495 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000496 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000497 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000498 }
499 }
Bill Wendling35247c32009-01-30 00:45:56 +0000500
Nate Begemancd4d58c2006-02-03 06:46:56 +0000501 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
502 if (isa<ConstantSDNode>(N0)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000503 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
504 SDValue OpNode = DAG.getNode(Opc, N1.getDebugLoc(), VT,
505 N1.getOperand(1), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000506 AddToWorkList(OpNode.getNode());
Bill Wendlingd69c3142009-01-30 02:23:43 +0000507 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000508 } else if (N1.hasOneUse()) {
Bill Wendling35247c32009-01-30 00:45:56 +0000509 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
Bill Wendlingd69c3142009-01-30 02:23:43 +0000510 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000511 N1.getOperand(0), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000512 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000513 return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000514 }
515 }
Bill Wendling35247c32009-01-30 00:45:56 +0000516
Dan Gohman475871a2008-07-27 21:46:04 +0000517 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000518}
519
Dan Gohman475871a2008-07-27 21:46:04 +0000520SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
521 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000522 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
523 ++NodesCombined;
524 DOUT << "\nReplacing.1 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +0000525 DOUT << "\nWith: "; DEBUG(To[0].getNode()->dump(&DAG));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000526 DOUT << " and " << NumTo-1 << " other values\n";
Dan Gohman764fd0c2009-01-21 15:17:51 +0000527 DEBUG(for (unsigned i = 0, e = NumTo; i != e; ++i)
528 assert(N->getValueType(i) == To[i].getValueType() &&
529 "Cannot combine value to value of different type!"));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000530 WorkListRemover DeadNodes(*this);
531 DAG.ReplaceAllUsesWith(N, To, &DeadNodes);
532
533 if (AddTo) {
534 // Push the new nodes and any users onto the worklist
535 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000536 AddToWorkList(To[i].getNode());
537 AddUsersToWorkList(To[i].getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000538 }
539 }
540
Dan Gohmandbe664a2009-01-19 21:44:21 +0000541 // Finally, if the node is now dead, remove it from the graph. The node
542 // may not be dead if the replacement process recursively simplified to
543 // something else needing this node.
544 if (N->use_empty()) {
545 // Nodes can be reintroduced into the worklist. Make sure we do not
546 // process a node that has been replaced.
547 removeFromWorkList(N);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000548
Dan Gohmandbe664a2009-01-19 21:44:21 +0000549 // Finally, since the node is now dead, remove it from the graph.
550 DAG.DeleteNode(N);
551 }
Dan Gohman475871a2008-07-27 21:46:04 +0000552 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000553}
554
Dan Gohmane5af2d32009-01-29 01:59:02 +0000555void
556DAGCombiner::CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &
557 TLO) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000558 // Replace all uses. If any nodes become isomorphic to other nodes and
559 // are deleted, make sure to remove them from our worklist.
560 WorkListRemover DeadNodes(*this);
561 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000562
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000563 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000564 AddToWorkList(TLO.New.getNode());
565 AddUsersToWorkList(TLO.New.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000566
567 // Finally, if the node is now dead, remove it from the graph. The node
568 // may not be dead if the replacement process recursively simplified to
569 // something else needing this node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000570 if (TLO.Old.getNode()->use_empty()) {
571 removeFromWorkList(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000572
573 // If the operands of this node are only used by the node, they will now
574 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greifba36cb52008-08-28 21:40:38 +0000575 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
576 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
577 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000578
Gabor Greifba36cb52008-08-28 21:40:38 +0000579 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000580 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000581}
582
583/// SimplifyDemandedBits - Check the specified integer node value to see if
584/// it can be simplified or if things it uses can be simplified by bit
585/// propagation. If so, return true.
586bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
587 TargetLowering::TargetLoweringOpt TLO(DAG);
588 APInt KnownZero, KnownOne;
589 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
590 return false;
591
592 // Revisit the node.
593 AddToWorkList(Op.getNode());
594
595 // Replace the old value with the new one.
596 ++NodesCombined;
597 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.getNode()->dump(&DAG));
598 DOUT << "\nWith: "; DEBUG(TLO.New.getNode()->dump(&DAG));
599 DOUT << '\n';
600
601 CommitTargetLoweringOpt(TLO);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000602 return true;
603}
604
Chris Lattner29446522007-05-14 22:04:50 +0000605//===----------------------------------------------------------------------===//
606// Main DAG Combiner implementation
607//===----------------------------------------------------------------------===//
608
Duncan Sands25cf2272008-11-24 14:53:14 +0000609void DAGCombiner::Run(CombineLevel AtLevel) {
610 // set the instance variables, so that the various visit routines may use it.
611 Level = AtLevel;
612 LegalOperations = Level >= NoIllegalOperations;
613 LegalTypes = Level >= NoIllegalTypes;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000614
Evan Cheng17a568b2008-08-29 22:21:44 +0000615 // Add all the dag nodes to the worklist.
616 WorkList.reserve(DAG.allnodes_size());
617 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
618 E = DAG.allnodes_end(); I != E; ++I)
619 WorkList.push_back(I);
Duncan Sands25cf2272008-11-24 14:53:14 +0000620
Evan Cheng17a568b2008-08-29 22:21:44 +0000621 // Create a dummy node (which is not added to allnodes), that adds a reference
622 // to the root node, preventing it from being deleted, and tracking any
623 // changes of the root.
624 HandleSDNode Dummy(DAG.getRoot());
625
Jim Laskey26f7fa72006-10-17 19:33:52 +0000626 // The root of the dag may dangle to deleted nodes until the dag combiner is
627 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +0000628 DAG.setRoot(SDValue());
Chris Lattner24664722006-03-01 04:53:38 +0000629
Evan Cheng17a568b2008-08-29 22:21:44 +0000630 // while the worklist isn't empty, inspect the node on the end of it and
631 // try and combine it.
632 while (!WorkList.empty()) {
633 SDNode *N = WorkList.back();
634 WorkList.pop_back();
635
636 // If N has no uses, it is dead. Make sure to revisit all N's operands once
637 // N is deleted from the DAG, since they too may now be dead or may have a
638 // reduced number of uses, allowing other xforms.
639 if (N->use_empty() && N != &Dummy) {
640 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
641 AddToWorkList(N->getOperand(i).getNode());
642
643 DAG.DeleteNode(N);
644 continue;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000645 }
Evan Cheng17a568b2008-08-29 22:21:44 +0000646
647 SDValue RV = combine(N);
648
649 if (RV.getNode() == 0)
650 continue;
651
652 ++NodesCombined;
653
654 // If we get back the same node we passed in, rather than a new node or
655 // zero, we know that the node must have defined multiple values and
656 // CombineTo was used. Since CombineTo takes care of the worklist
657 // mechanics for us, we have no work to do in this case.
658 if (RV.getNode() == N)
659 continue;
660
661 assert(N->getOpcode() != ISD::DELETED_NODE &&
662 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
663 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +0000664
Evan Cheng17a568b2008-08-29 22:21:44 +0000665 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
666 DOUT << "\nWith: "; DEBUG(RV.getNode()->dump(&DAG));
667 DOUT << '\n';
668 WorkListRemover DeadNodes(*this);
669 if (N->getNumValues() == RV.getNode()->getNumValues())
670 DAG.ReplaceAllUsesWith(N, RV.getNode(), &DeadNodes);
671 else {
672 assert(N->getValueType(0) == RV.getValueType() &&
673 N->getNumValues() == 1 && "Type mismatch");
674 SDValue OpV = RV;
675 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
676 }
677
678 // Push the new node and any users onto the worklist
679 AddToWorkList(RV.getNode());
680 AddUsersToWorkList(RV.getNode());
681
682 // Add any uses of the old node to the worklist in case this node is the
683 // last one that uses them. They may become dead after this node is
684 // deleted.
685 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
686 AddToWorkList(N->getOperand(i).getNode());
687
Dan Gohmandbe664a2009-01-19 21:44:21 +0000688 // Finally, if the node is now dead, remove it from the graph. The node
689 // may not be dead if the replacement process recursively simplified to
690 // something else needing this node.
691 if (N->use_empty()) {
692 // Nodes can be reintroduced into the worklist. Make sure we do not
693 // process a node that has been replaced.
694 removeFromWorkList(N);
Evan Cheng17a568b2008-08-29 22:21:44 +0000695
Dan Gohmandbe664a2009-01-19 21:44:21 +0000696 // Finally, since the node is now dead, remove it from the graph.
697 DAG.DeleteNode(N);
698 }
Evan Cheng17a568b2008-08-29 22:21:44 +0000699 }
700
Chris Lattner95038592005-10-05 06:35:28 +0000701 // If the root changed (e.g. it was a dead load, update the root).
702 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000703}
704
Dan Gohman475871a2008-07-27 21:46:04 +0000705SDValue DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000706 switch(N->getOpcode()) {
707 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000708 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000709 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000710 case ISD::ADD: return visitADD(N);
711 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000712 case ISD::ADDC: return visitADDC(N);
713 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000714 case ISD::MUL: return visitMUL(N);
715 case ISD::SDIV: return visitSDIV(N);
716 case ISD::UDIV: return visitUDIV(N);
717 case ISD::SREM: return visitSREM(N);
718 case ISD::UREM: return visitUREM(N);
719 case ISD::MULHU: return visitMULHU(N);
720 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000721 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
722 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
723 case ISD::SDIVREM: return visitSDIVREM(N);
724 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000725 case ISD::AND: return visitAND(N);
726 case ISD::OR: return visitOR(N);
727 case ISD::XOR: return visitXOR(N);
728 case ISD::SHL: return visitSHL(N);
729 case ISD::SRA: return visitSRA(N);
730 case ISD::SRL: return visitSRL(N);
731 case ISD::CTLZ: return visitCTLZ(N);
732 case ISD::CTTZ: return visitCTTZ(N);
733 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000734 case ISD::SELECT: return visitSELECT(N);
735 case ISD::SELECT_CC: return visitSELECT_CC(N);
736 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000737 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
738 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000739 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000740 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
741 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000742 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +0000743 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000744 case ISD::FADD: return visitFADD(N);
745 case ISD::FSUB: return visitFSUB(N);
746 case ISD::FMUL: return visitFMUL(N);
747 case ISD::FDIV: return visitFDIV(N);
748 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000749 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000750 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
751 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
752 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
753 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
754 case ISD::FP_ROUND: return visitFP_ROUND(N);
755 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
756 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
757 case ISD::FNEG: return visitFNEG(N);
758 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000759 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000760 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000761 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000762 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000763 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +0000764 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000765 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
766 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000767 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000768 }
Dan Gohman475871a2008-07-27 21:46:04 +0000769 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000770}
771
Dan Gohman475871a2008-07-27 21:46:04 +0000772SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +0000773 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000774
775 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +0000776 if (RV.getNode() == 0) {
Dan Gohman389079b2007-10-08 17:57:15 +0000777 assert(N->getOpcode() != ISD::DELETED_NODE &&
778 "Node was deleted but visit returned NULL!");
779
780 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
781 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
782
783 // Expose the DAG combiner to the target combiner impls.
784 TargetLowering::DAGCombinerInfo
Duncan Sands25cf2272008-11-24 14:53:14 +0000785 DagCombineInfo(DAG, Level == Unrestricted, false, this);
Dan Gohman389079b2007-10-08 17:57:15 +0000786
787 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
788 }
789 }
790
Evan Cheng08b11732008-03-22 01:55:50 +0000791 // If N is a commutative binary node, try commuting it to enable more
792 // sdisel CSE.
Gabor Greifba36cb52008-08-28 21:40:38 +0000793 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +0000794 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
795 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +0000796 SDValue N0 = N->getOperand(0);
797 SDValue N1 = N->getOperand(1);
Bill Wendling5c71acf2009-01-30 01:13:16 +0000798
Evan Cheng08b11732008-03-22 01:55:50 +0000799 // Constant operands are canonicalized to RHS.
800 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000801 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +0000802 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
803 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +0000804 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +0000805 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +0000806 }
807 }
808
Dan Gohman389079b2007-10-08 17:57:15 +0000809 return RV;
810}
811
Chris Lattner6270f682006-10-08 22:57:01 +0000812/// getInputChainForNode - Given a node, return its input chain if it has one,
813/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000814static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000815 if (unsigned NumOps = N->getNumOperands()) {
816 if (N->getOperand(0).getValueType() == MVT::Other)
817 return N->getOperand(0);
818 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
819 return N->getOperand(NumOps-1);
820 for (unsigned i = 1; i < NumOps-1; ++i)
821 if (N->getOperand(i).getValueType() == MVT::Other)
822 return N->getOperand(i);
823 }
Bill Wendling5c71acf2009-01-30 01:13:16 +0000824 return SDValue();
Chris Lattner6270f682006-10-08 22:57:01 +0000825}
826
Dan Gohman475871a2008-07-27 21:46:04 +0000827SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000828 // If N has two operands, where one has an input chain equal to the other,
829 // the 'other' chain is redundant.
830 if (N->getNumOperands() == 2) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000831 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +0000832 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000833 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +0000834 return N->getOperand(1);
835 }
836
Chris Lattnerc76d4412007-05-16 06:37:59 +0000837 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +0000838 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Chris Lattnerc76d4412007-05-16 06:37:59 +0000839 SmallPtrSet<SDNode*, 16> SeenOps;
840 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000841
842 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000843 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000844
Jim Laskey71382342006-10-07 23:37:56 +0000845 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000846 // encountered.
847 for (unsigned i = 0; i < TFs.size(); ++i) {
848 SDNode *TF = TFs[i];
849
Jim Laskey6ff23e52006-10-04 16:53:27 +0000850 // Check each of the operands.
851 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000852 SDValue Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000853
Jim Laskey6ff23e52006-10-04 16:53:27 +0000854 switch (Op.getOpcode()) {
855 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000856 // Entry tokens don't need to be added to the list. They are
857 // rededundant.
858 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000859 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000860
Jim Laskey6ff23e52006-10-04 16:53:27 +0000861 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000862 if ((CombinerAA || Op.hasOneUse()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000863 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000864 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +0000865 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +0000866 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +0000867 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +0000868 Changed = true;
869 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000870 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000871 // Fall thru
872
873 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000874 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +0000875 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +0000876 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000877 else
878 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000879 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000880 }
881 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000882 }
883
Dan Gohman475871a2008-07-27 21:46:04 +0000884 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000885
886 // If we've change things around then replace token factor.
887 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +0000888 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000889 // The entry token is the only possible outcome.
890 Result = DAG.getEntryNode();
891 } else {
892 // New and improved token factor.
Bill Wendling5c71acf2009-01-30 01:13:16 +0000893 Result = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
894 MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000895 }
Bill Wendling5c71acf2009-01-30 01:13:16 +0000896
Jim Laskey274062c2006-10-13 23:32:28 +0000897 // Don't add users to work list.
898 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000899 }
Jim Laskey279f0532006-09-25 16:29:54 +0000900
Jim Laskey6ff23e52006-10-04 16:53:27 +0000901 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000902}
903
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000904/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +0000905SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000906 WorkListRemover DeadNodes(*this);
907 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Dan Gohman475871a2008-07-27 21:46:04 +0000908 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i),
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000909 &DeadNodes);
910 removeFromWorkList(N);
911 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000912 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000913}
914
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000915static
Bill Wendlingd69c3142009-01-30 02:23:43 +0000916SDValue combineShlAddConstant(DebugLoc DL, SDValue N0, SDValue N1,
917 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000918 MVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000919 SDValue N00 = N0.getOperand(0);
920 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000921 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Bill Wendlingd69c3142009-01-30 02:23:43 +0000922
Gabor Greifba36cb52008-08-28 21:40:38 +0000923 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000924 isa<ConstantSDNode>(N00.getOperand(1))) {
Bill Wendlingd69c3142009-01-30 02:23:43 +0000925 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
926 N0 = DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT,
927 DAG.getNode(ISD::SHL, N00.getDebugLoc(), VT,
928 N00.getOperand(0), N01),
929 DAG.getNode(ISD::SHL, N01.getDebugLoc(), VT,
930 N00.getOperand(1), N01));
931 return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000932 }
Bill Wendlingd69c3142009-01-30 02:23:43 +0000933
Dan Gohman475871a2008-07-27 21:46:04 +0000934 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000935}
936
Evan Chengb13cdbd2007-06-21 07:39:16 +0000937static
Dan Gohman475871a2008-07-27 21:46:04 +0000938SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Bill Wendlingae89bb12008-11-11 08:25:46 +0000939 SelectionDAG &DAG, const TargetLowering &TLI,
Duncan Sands25cf2272008-11-24 14:53:14 +0000940 bool LegalOperations) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000941 MVT VT = N->getValueType(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000942 unsigned Opc = N->getOpcode();
943 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
Dan Gohman475871a2008-07-27 21:46:04 +0000944 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
945 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000946 ISD::CondCode CC = ISD::SETCC_INVALID;
Bill Wendlingae89bb12008-11-11 08:25:46 +0000947
948 if (isSlctCC) {
Evan Chengb13cdbd2007-06-21 07:39:16 +0000949 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
Bill Wendlingae89bb12008-11-11 08:25:46 +0000950 } else {
Dan Gohman475871a2008-07-27 21:46:04 +0000951 SDValue CCOp = Slct.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000952 if (CCOp.getOpcode() == ISD::SETCC)
953 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
954 }
955
956 bool DoXform = false;
957 bool InvCC = false;
958 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
959 "Bad input!");
Bill Wendlingae89bb12008-11-11 08:25:46 +0000960
Evan Chengb13cdbd2007-06-21 07:39:16 +0000961 if (LHS.getOpcode() == ISD::Constant &&
Bill Wendlingae89bb12008-11-11 08:25:46 +0000962 cast<ConstantSDNode>(LHS)->isNullValue()) {
Evan Chengb13cdbd2007-06-21 07:39:16 +0000963 DoXform = true;
Bill Wendlingae89bb12008-11-11 08:25:46 +0000964 } else if (CC != ISD::SETCC_INVALID &&
965 RHS.getOpcode() == ISD::Constant &&
966 cast<ConstantSDNode>(RHS)->isNullValue()) {
Evan Chengb13cdbd2007-06-21 07:39:16 +0000967 std::swap(LHS, RHS);
Dan Gohman475871a2008-07-27 21:46:04 +0000968 SDValue Op0 = Slct.getOperand(0);
Bill Wendlingae89bb12008-11-11 08:25:46 +0000969 MVT OpVT = isSlctCC ? Op0.getValueType() :
970 Op0.getOperand(0).getValueType();
971 bool isInt = OpVT.isInteger();
Evan Chengb13cdbd2007-06-21 07:39:16 +0000972 CC = ISD::getSetCCInverse(CC, isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +0000973
Duncan Sands25cf2272008-11-24 14:53:14 +0000974 if (LegalOperations && !TLI.isCondCodeLegal(CC, OpVT))
Bill Wendlingae89bb12008-11-11 08:25:46 +0000975 return SDValue(); // Inverse operator isn't legal.
976
Evan Chengb13cdbd2007-06-21 07:39:16 +0000977 DoXform = true;
978 InvCC = true;
979 }
980
981 if (DoXform) {
Bill Wendlingd69c3142009-01-30 02:23:43 +0000982 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000983 if (isSlctCC)
Bill Wendlingd69c3142009-01-30 02:23:43 +0000984 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
Evan Chengb13cdbd2007-06-21 07:39:16 +0000985 Slct.getOperand(0), Slct.getOperand(1), CC);
Dan Gohman475871a2008-07-27 21:46:04 +0000986 SDValue CCOp = Slct.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000987 if (InvCC)
Bill Wendlingd69c3142009-01-30 02:23:43 +0000988 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
989 CCOp.getOperand(0), CCOp.getOperand(1), CC);
990 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
991 CCOp, OtherOp, Result);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000992 }
Dan Gohman475871a2008-07-27 21:46:04 +0000993 return SDValue();
Evan Chengb13cdbd2007-06-21 07:39:16 +0000994}
995
Dan Gohman475871a2008-07-27 21:46:04 +0000996SDValue DAGCombiner::visitADD(SDNode *N) {
997 SDValue N0 = N->getOperand(0);
998 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000999 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1000 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001001 MVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00001002
1003 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001004 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001005 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001006 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001007 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001008
Dan Gohman613e0d82007-07-03 14:03:57 +00001009 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001010 if (N0.getOpcode() == ISD::UNDEF)
1011 return N0;
1012 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001013 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001014 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001015 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001016 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001017 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001018 if (N0C && !N1C)
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001019 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001020 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001021 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001022 return N0;
Dan Gohman6520e202008-10-18 02:06:02 +00001023 // fold (add Sym, c) -> Sym+c
1024 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001025 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman6520e202008-10-18 02:06:02 +00001026 GA->getOpcode() == ISD::GlobalAddress)
1027 return DAG.getGlobalAddress(GA->getGlobal(), VT,
1028 GA->getOffset() +
1029 (uint64_t)N1C->getSExtValue());
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001030 // fold ((c1-A)+c2) -> (c1+c2)-A
1031 if (N1C && N0.getOpcode() == ISD::SUB)
1032 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001033 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
Dan Gohman002e5d02008-03-13 22:13:53 +00001034 DAG.getConstant(N1C->getAPIntValue()+
1035 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001036 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +00001037 // reassociate add
Bill Wendling35247c32009-01-30 00:45:56 +00001038 SDValue RADD = ReassociateOps(ISD::ADD, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001039 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001040 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001041 // fold ((0-A) + B) -> B-A
1042 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1043 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001044 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001045 // fold (A + (0-B)) -> A-B
1046 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1047 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001048 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001049 // fold (A+(B-A)) -> B
1050 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001051 return N1.getOperand(0);
Dale Johannesen56eca912008-11-27 00:43:21 +00001052 // fold ((B-A)+A) -> B
1053 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1054 return N0.getOperand(0);
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001055 // fold (A+(B-(A+C))) to (B-C)
1056 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001057 N0 == N1.getOperand(1).getOperand(0))
1058 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001059 N1.getOperand(1).getOperand(1));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001060 // fold (A+(B-(C+A))) to (B-C)
1061 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001062 N0 == N1.getOperand(1).getOperand(1))
1063 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001064 N1.getOperand(1).getOperand(0));
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001065 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesen34d79852008-12-02 18:40:40 +00001066 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1067 N1.getOperand(0).getOpcode() == ISD::SUB &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001068 N0 == N1.getOperand(0).getOperand(1))
1069 return DAG.getNode(N1.getOpcode(), N->getDebugLoc(), VT,
1070 N1.getOperand(0).getOperand(0), N1.getOperand(1));
Dale Johannesen34d79852008-12-02 18:40:40 +00001071
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001072 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1073 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1074 SDValue N00 = N0.getOperand(0);
1075 SDValue N01 = N0.getOperand(1);
1076 SDValue N10 = N1.getOperand(0);
1077 SDValue N11 = N1.getOperand(1);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001078
1079 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10))
1080 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1081 DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT, N00, N10),
1082 DAG.getNode(ISD::ADD, N1.getDebugLoc(), VT, N01, N11));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001083 }
Chris Lattner947c2892006-03-13 06:51:27 +00001084
Dan Gohman475871a2008-07-27 21:46:04 +00001085 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1086 return SDValue(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +00001087
1088 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001089 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001090 APInt LHSZero, LHSOne;
1091 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001092 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001093 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001094
Dan Gohman948d8ea2008-02-20 16:33:30 +00001095 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001096 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +00001097
1098 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1099 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1100 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1101 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001102 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1);
Chris Lattner947c2892006-03-13 06:51:27 +00001103 }
1104 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001105
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001106 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001107 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001108 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001109 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001110 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001111 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001112 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001113 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001114 }
1115
Evan Chengb13cdbd2007-06-21 07:39:16 +00001116 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Gabor Greifba36cb52008-08-28 21:40:38 +00001117 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
Duncan Sands25cf2272008-11-24 14:53:14 +00001118 SDValue Result = combineSelectAndUse(N, N0, N1, DAG, TLI, LegalOperations);
Gabor Greifba36cb52008-08-28 21:40:38 +00001119 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001120 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001121 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
Duncan Sands25cf2272008-11-24 14:53:14 +00001122 SDValue Result = combineSelectAndUse(N, N1, N0, DAG, TLI, LegalOperations);
Gabor Greifba36cb52008-08-28 21:40:38 +00001123 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001124 }
1125
Dan Gohman475871a2008-07-27 21:46:04 +00001126 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001127}
1128
Dan Gohman475871a2008-07-27 21:46:04 +00001129SDValue DAGCombiner::visitADDC(SDNode *N) {
1130 SDValue N0 = N->getOperand(0);
1131 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001132 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1133 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001134 MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001135
1136 // If the flag result is dead, turn this into an ADD.
1137 if (N->hasNUsesOfValue(0, 1))
1138 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +00001139 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +00001140
1141 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001142 if (N0C && !N1C)
Dan Gohman56867522008-06-21 22:06:07 +00001143 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Chris Lattner91153682007-03-04 20:03:15 +00001144
Chris Lattnerb6541762007-03-04 20:40:38 +00001145 // fold (addc x, 0) -> x + no carry out
1146 if (N1C && N1C->isNullValue())
1147 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1148
1149 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001150 APInt LHSZero, LHSOne;
1151 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001152 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001153 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001154 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001155 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001156
1157 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1158 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1159 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1160 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1161 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
1162 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1163 }
Chris Lattner91153682007-03-04 20:03:15 +00001164
Dan Gohman475871a2008-07-27 21:46:04 +00001165 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001166}
1167
Dan Gohman475871a2008-07-27 21:46:04 +00001168SDValue DAGCombiner::visitADDE(SDNode *N) {
1169 SDValue N0 = N->getOperand(0);
1170 SDValue N1 = N->getOperand(1);
1171 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001172 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1173 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001174 //MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001175
1176 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001177 if (N0C && !N1C)
Dan Gohman56867522008-06-21 22:06:07 +00001178 return DAG.getNode(ISD::ADDE, N->getVTList(), N1, N0, CarryIn);
Chris Lattner91153682007-03-04 20:03:15 +00001179
Chris Lattnerb6541762007-03-04 20:40:38 +00001180 // fold (adde x, y, false) -> (addc x, y)
Dan Gohman0a4627d2008-06-23 15:29:14 +00001181 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Dan Gohman56867522008-06-21 22:06:07 +00001182 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Chris Lattner91153682007-03-04 20:03:15 +00001183
Dan Gohman475871a2008-07-27 21:46:04 +00001184 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001185}
1186
1187
1188
Dan Gohman475871a2008-07-27 21:46:04 +00001189SDValue DAGCombiner::visitSUB(SDNode *N) {
1190 SDValue N0 = N->getOperand(0);
1191 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001192 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1193 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001194 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001195
Dan Gohman7f321562007-06-25 16:23:39 +00001196 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001197 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001198 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001199 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001200 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001201
Chris Lattner854077d2005-10-17 01:07:11 +00001202 // fold (sub x, x) -> 0
Evan Chengc8e3b142008-03-12 07:02:50 +00001203 if (N0 == N1)
Chris Lattner854077d2005-10-17 01:07:11 +00001204 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001205 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001206 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001207 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattner05b57432005-10-11 06:07:15 +00001208 // fold (sub x, c) -> (add x, -c)
1209 if (N1C)
Dan Gohman002e5d02008-03-13 22:13:53 +00001210 return DAG.getNode(ISD::ADD, VT, N0,
1211 DAG.getConstant(-N1C->getAPIntValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001212 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001213 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001214 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001215 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001216 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001217 return N0.getOperand(0);
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001218 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001219 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001220 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1221 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001222 N0.getOperand(1).getOperand(0) == N1)
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001223 return DAG.getNode(N0.getOperand(1).getOpcode(), VT, N0.getOperand(0),
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001224 N0.getOperand(1).getOperand(1));
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001225 // fold ((A+(C+B))-B) -> A+C
1226 if (N0.getOpcode() == ISD::ADD &&
1227 N0.getOperand(1).getOpcode() == ISD::ADD &&
1228 N0.getOperand(1).getOperand(1) == N1)
1229 return DAG.getNode(ISD::ADD, VT, N0.getOperand(0),
1230 N0.getOperand(1).getOperand(0));
Dale Johannesen58e39b02008-12-23 01:59:54 +00001231 // fold ((A-(B-C))-C) -> A-B
1232 if (N0.getOpcode() == ISD::SUB &&
1233 N0.getOperand(1).getOpcode() == ISD::SUB &&
1234 N0.getOperand(1).getOperand(1) == N1)
1235 return DAG.getNode(ISD::SUB, VT, N0.getOperand(0),
1236 N0.getOperand(1).getOperand(0));
Evan Chengb13cdbd2007-06-21 07:39:16 +00001237 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Gabor Greifba36cb52008-08-28 21:40:38 +00001238 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
Duncan Sands25cf2272008-11-24 14:53:14 +00001239 SDValue Result = combineSelectAndUse(N, N1, N0, DAG, TLI, LegalOperations);
Gabor Greifba36cb52008-08-28 21:40:38 +00001240 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001241 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001242 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001243 if (N0.getOpcode() == ISD::UNDEF)
1244 return N0;
1245 if (N1.getOpcode() == ISD::UNDEF)
1246 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001247
Dan Gohman6520e202008-10-18 02:06:02 +00001248 // If the relocation model supports it, consider symbol offsets.
1249 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001250 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman6520e202008-10-18 02:06:02 +00001251 // fold (sub Sym, c) -> Sym-c
1252 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
1253 return DAG.getGlobalAddress(GA->getGlobal(), VT,
1254 GA->getOffset() -
1255 (uint64_t)N1C->getSExtValue());
1256 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1257 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1258 if (GA->getGlobal() == GB->getGlobal())
1259 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1260 VT);
1261 }
1262
Dan Gohman475871a2008-07-27 21:46:04 +00001263 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001264}
1265
Dan Gohman475871a2008-07-27 21:46:04 +00001266SDValue DAGCombiner::visitMUL(SDNode *N) {
1267 SDValue N0 = N->getOperand(0);
1268 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001269 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1270 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001271 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001272
Dan Gohman7f321562007-06-25 16:23:39 +00001273 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001274 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001275 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001276 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001277 }
Dan Gohman7f321562007-06-25 16:23:39 +00001278
Dan Gohman613e0d82007-07-03 14:03:57 +00001279 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001280 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001281 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001282 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001283 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001284 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001285 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001286 if (N0C && !N1C)
1287 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001288 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001289 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001290 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001291 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001292 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001293 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001294 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001295 if (N1C && N1C->getAPIntValue().isPowerOf2())
Chris Lattner3e6099b2005-10-30 06:41:49 +00001296 return DAG.getNode(ISD::SHL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001297 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001298 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001299 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Dan Gohman7810bfe2008-09-26 21:54:37 +00001300 if (N1C && isPowerOf2_64(-N1C->getSExtValue())) {
Chris Lattner3e6099b2005-10-30 06:41:49 +00001301 // FIXME: If the input is something that is easily negated (e.g. a
1302 // single-use add), we should put the negate there.
1303 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1304 DAG.getNode(ISD::SHL, VT, N0,
Dan Gohman7810bfe2008-09-26 21:54:37 +00001305 DAG.getConstant(Log2_64(-N1C->getSExtValue()),
Chris Lattner3e6099b2005-10-30 06:41:49 +00001306 TLI.getShiftAmountTy())));
1307 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001308
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001309 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1310 if (N1C && N0.getOpcode() == ISD::SHL &&
1311 isa<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001312 SDValue C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001313 AddToWorkList(C3.getNode());
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001314 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1315 }
1316
1317 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1318 // use.
1319 {
Dan Gohman475871a2008-07-27 21:46:04 +00001320 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001321 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1322 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001323 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001324 Sh = N0; Y = N1;
1325 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001326 isa<ConstantSDNode>(N1.getOperand(1)) &&
1327 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001328 Sh = N1; Y = N0;
1329 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001330 if (Sh.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001331 SDValue Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001332 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1333 }
1334 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001335 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Gabor Greifba36cb52008-08-28 21:40:38 +00001336 if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
Chris Lattnera1deca32006-03-04 23:33:26 +00001337 isa<ConstantSDNode>(N0.getOperand(1))) {
1338 return DAG.getNode(ISD::ADD, VT,
1339 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1340 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1341 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001342
Nate Begemancd4d58c2006-02-03 06:46:56 +00001343 // reassociate mul
Bill Wendling35247c32009-01-30 00:45:56 +00001344 SDValue RMUL = ReassociateOps(ISD::MUL, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001345 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001346 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001347
Dan Gohman475871a2008-07-27 21:46:04 +00001348 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001349}
1350
Dan Gohman475871a2008-07-27 21:46:04 +00001351SDValue DAGCombiner::visitSDIV(SDNode *N) {
1352 SDValue N0 = N->getOperand(0);
1353 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001354 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1355 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001356 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001357
Dan Gohman7f321562007-06-25 16:23:39 +00001358 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001359 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001360 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001361 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001362 }
Dan Gohman7f321562007-06-25 16:23:39 +00001363
Nate Begeman1d4d4142005-09-01 00:19:25 +00001364 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001365 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001366 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001367 // fold (sdiv X, 1) -> X
Dan Gohman7810bfe2008-09-26 21:54:37 +00001368 if (N1C && N1C->getSExtValue() == 1LL)
Nate Begeman405e3ec2005-10-21 00:02:42 +00001369 return N0;
1370 // fold (sdiv X, -1) -> 0-X
1371 if (N1C && N1C->isAllOnesValue())
1372 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001373 // If we know the sign bits of both operands are zero, strength reduce to a
1374 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001375 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001376 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattnerf32aac32008-01-27 23:32:17 +00001377 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
1378 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001379 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman002e5d02008-03-13 22:13:53 +00001380 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Dan Gohman7810bfe2008-09-26 21:54:37 +00001381 (isPowerOf2_64(N1C->getSExtValue()) ||
1382 isPowerOf2_64(-N1C->getSExtValue()))) {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001383 // If dividing by powers of two is cheap, then don't perform the following
1384 // fold.
1385 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001386 return SDValue();
Dan Gohman7810bfe2008-09-26 21:54:37 +00001387 int64_t pow2 = N1C->getSExtValue();
Nate Begeman405e3ec2005-10-21 00:02:42 +00001388 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001389 unsigned lg2 = Log2_64(abs2);
1390 // Splat the sign bit into the register
Dan Gohman475871a2008-07-27 21:46:04 +00001391 SDValue SGN = DAG.getNode(ISD::SRA, VT, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001392 DAG.getConstant(VT.getSizeInBits()-1,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001393 TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00001394 AddToWorkList(SGN.getNode());
Chris Lattner8f4880b2006-02-16 08:02:36 +00001395 // Add (N0 < 0) ? abs2 - 1 : 0;
Dan Gohman475871a2008-07-27 21:46:04 +00001396 SDValue SRL = DAG.getNode(ISD::SRL, VT, SGN,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001397 DAG.getConstant(VT.getSizeInBits()-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001398 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00001399 SDValue ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001400 AddToWorkList(SRL.getNode());
1401 AddToWorkList(ADD.getNode()); // Divide by pow2
Dan Gohman475871a2008-07-27 21:46:04 +00001402 SDValue SRA = DAG.getNode(ISD::SRA, VT, ADD,
Chris Lattner8f4880b2006-02-16 08:02:36 +00001403 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001404 // If we're dividing by a positive value, we're done. Otherwise, we must
1405 // negate the result.
1406 if (pow2 > 0)
1407 return SRA;
Gabor Greifba36cb52008-08-28 21:40:38 +00001408 AddToWorkList(SRA.getNode());
Nate Begeman405e3ec2005-10-21 00:02:42 +00001409 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1410 }
Nate Begeman69575232005-10-20 02:15:44 +00001411 // if integer divide is expensive and we satisfy the requirements, emit an
1412 // alternate sequence.
Dan Gohman7810bfe2008-09-26 21:54:37 +00001413 if (N1C && (N1C->getSExtValue() < -1 || N1C->getSExtValue() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001414 !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001415 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001416 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001417 }
Dan Gohman7f321562007-06-25 16:23:39 +00001418
Dan Gohman613e0d82007-07-03 14:03:57 +00001419 // undef / X -> 0
1420 if (N0.getOpcode() == ISD::UNDEF)
1421 return DAG.getConstant(0, VT);
1422 // X / undef -> undef
1423 if (N1.getOpcode() == ISD::UNDEF)
1424 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001425
Dan Gohman475871a2008-07-27 21:46:04 +00001426 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001427}
1428
Dan Gohman475871a2008-07-27 21:46:04 +00001429SDValue DAGCombiner::visitUDIV(SDNode *N) {
1430 SDValue N0 = N->getOperand(0);
1431 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001432 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1433 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001434 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001435
Dan Gohman7f321562007-06-25 16:23:39 +00001436 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001437 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001438 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001439 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001440 }
Dan Gohman7f321562007-06-25 16:23:39 +00001441
Nate Begeman1d4d4142005-09-01 00:19:25 +00001442 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001443 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001444 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001445 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001446 if (N1C && N1C->getAPIntValue().isPowerOf2())
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001447 return DAG.getNode(ISD::SRL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001448 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001449 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001450 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1451 if (N1.getOpcode() == ISD::SHL) {
1452 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001453 if (SHC->getAPIntValue().isPowerOf2()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001454 MVT ADDVT = N1.getOperand(1).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001455 SDValue Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001456 DAG.getConstant(SHC->getAPIntValue()
1457 .logBase2(),
Nate Begemanc031e332006-02-05 07:36:48 +00001458 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001459 AddToWorkList(Add.getNode());
Nate Begemanc031e332006-02-05 07:36:48 +00001460 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001461 }
1462 }
1463 }
Nate Begeman69575232005-10-20 02:15:44 +00001464 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001465 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001466 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001467 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00001468 }
Dan Gohman7f321562007-06-25 16:23:39 +00001469
Dan Gohman613e0d82007-07-03 14:03:57 +00001470 // undef / X -> 0
1471 if (N0.getOpcode() == ISD::UNDEF)
1472 return DAG.getConstant(0, VT);
1473 // X / undef -> undef
1474 if (N1.getOpcode() == ISD::UNDEF)
1475 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001476
Dan Gohman475871a2008-07-27 21:46:04 +00001477 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001478}
1479
Dan Gohman475871a2008-07-27 21:46:04 +00001480SDValue DAGCombiner::visitSREM(SDNode *N) {
1481 SDValue N0 = N->getOperand(0);
1482 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001483 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1484 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001485 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001486
1487 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001488 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001489 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001490 // If we know the sign bits of both operands are zero, strength reduce to a
1491 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00001492 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001493 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattneree339f42008-01-27 23:21:58 +00001494 return DAG.getNode(ISD::UREM, VT, N0, N1);
1495 }
Chris Lattner26d29902006-10-12 20:58:32 +00001496
Dan Gohman77003042007-11-26 23:46:11 +00001497 // If X/C can be simplified by the division-by-constant logic, lower
1498 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001499 if (N1C && !N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001500 SDValue Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001501 AddToWorkList(Div.getNode());
1502 SDValue OptimizedDiv = combine(Div.getNode());
1503 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001504 SDValue Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1505 SDValue Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001506 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001507 return Sub;
1508 }
Chris Lattner26d29902006-10-12 20:58:32 +00001509 }
1510
Dan Gohman613e0d82007-07-03 14:03:57 +00001511 // undef % X -> 0
1512 if (N0.getOpcode() == ISD::UNDEF)
1513 return DAG.getConstant(0, VT);
1514 // X % undef -> undef
1515 if (N1.getOpcode() == ISD::UNDEF)
1516 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001517
Dan Gohman475871a2008-07-27 21:46:04 +00001518 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001519}
1520
Dan Gohman475871a2008-07-27 21:46:04 +00001521SDValue DAGCombiner::visitUREM(SDNode *N) {
1522 SDValue N0 = N->getOperand(0);
1523 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001524 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1525 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001526 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001527
1528 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001529 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001530 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001531 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001532 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
1533 return DAG.getNode(ISD::AND, VT, N0,
1534 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001535 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1536 if (N1.getOpcode() == ISD::SHL) {
1537 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001538 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001539 SDValue Add =
Dan Gohman002e5d02008-03-13 22:13:53 +00001540 DAG.getNode(ISD::ADD, VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001541 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00001542 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001543 AddToWorkList(Add.getNode());
Nate Begemanc031e332006-02-05 07:36:48 +00001544 return DAG.getNode(ISD::AND, VT, N0, Add);
1545 }
1546 }
1547 }
Chris Lattner26d29902006-10-12 20:58:32 +00001548
Dan Gohman77003042007-11-26 23:46:11 +00001549 // If X/C can be simplified by the division-by-constant logic, lower
1550 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001551 if (N1C && !N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001552 SDValue Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00001553 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00001554 SDValue OptimizedDiv = combine(Div.getNode());
1555 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001556 SDValue Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1557 SDValue Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001558 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001559 return Sub;
1560 }
Chris Lattner26d29902006-10-12 20:58:32 +00001561 }
1562
Dan Gohman613e0d82007-07-03 14:03:57 +00001563 // undef % X -> 0
1564 if (N0.getOpcode() == ISD::UNDEF)
1565 return DAG.getConstant(0, VT);
1566 // X % undef -> undef
1567 if (N1.getOpcode() == ISD::UNDEF)
1568 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001569
Dan Gohman475871a2008-07-27 21:46:04 +00001570 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001571}
1572
Dan Gohman475871a2008-07-27 21:46:04 +00001573SDValue DAGCombiner::visitMULHS(SDNode *N) {
1574 SDValue N0 = N->getOperand(0);
1575 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001576 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001577 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001578
1579 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001580 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001581 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001582 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001583 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001584 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001585 DAG.getConstant(N0.getValueType().getSizeInBits()-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001586 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001587 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001588 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001589 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001590
Dan Gohman475871a2008-07-27 21:46:04 +00001591 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001592}
1593
Dan Gohman475871a2008-07-27 21:46:04 +00001594SDValue DAGCombiner::visitMULHU(SDNode *N) {
1595 SDValue N0 = N->getOperand(0);
1596 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001597 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001598 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001599
1600 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001601 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001602 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001603 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00001604 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001605 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001606 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001607 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001608 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001609
Dan Gohman475871a2008-07-27 21:46:04 +00001610 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001611}
1612
Dan Gohman389079b2007-10-08 17:57:15 +00001613/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1614/// compute two values. LoOp and HiOp give the opcodes for the two computations
1615/// that are being performed. Return true if a simplification was made.
1616///
Dan Gohman475871a2008-07-27 21:46:04 +00001617SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1618 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001619 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001620 bool HiExists = N->hasAnyUseOfValue(1);
1621 if (!HiExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001622 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00001623 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001624 SDValue Res = DAG.getNode(LoOp, N->getValueType(0), N->op_begin(),
Duncan Sands25cf2272008-11-24 14:53:14 +00001625 N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001626 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001627 }
1628
1629 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001630 bool LoExists = N->hasAnyUseOfValue(0);
1631 if (!LoExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001632 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00001633 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001634 SDValue Res = DAG.getNode(HiOp, N->getValueType(1), N->op_begin(),
Duncan Sands25cf2272008-11-24 14:53:14 +00001635 N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001636 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001637 }
1638
Evan Cheng44711942007-11-08 09:25:29 +00001639 // If both halves are used, return as it is.
1640 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00001641 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00001642
1643 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001644 if (LoExists) {
Dan Gohman475871a2008-07-27 21:46:04 +00001645 SDValue Lo = DAG.getNode(LoOp, N->getValueType(0),
Evan Cheng44711942007-11-08 09:25:29 +00001646 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001647 AddToWorkList(Lo.getNode());
1648 SDValue LoOpt = combine(Lo.getNode());
1649 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001650 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001651 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001652 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001653 }
1654
Evan Cheng44711942007-11-08 09:25:29 +00001655 if (HiExists) {
Dan Gohman475871a2008-07-27 21:46:04 +00001656 SDValue Hi = DAG.getNode(HiOp, N->getValueType(1),
Duncan Sands25cf2272008-11-24 14:53:14 +00001657 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001658 AddToWorkList(Hi.getNode());
1659 SDValue HiOpt = combine(Hi.getNode());
1660 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001661 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001662 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001663 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001664 }
Dan Gohman475871a2008-07-27 21:46:04 +00001665 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001666}
1667
Dan Gohman475871a2008-07-27 21:46:04 +00001668SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
1669 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00001670 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001671
Dan Gohman475871a2008-07-27 21:46:04 +00001672 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001673}
1674
Dan Gohman475871a2008-07-27 21:46:04 +00001675SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
1676 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00001677 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001678
Dan Gohman475871a2008-07-27 21:46:04 +00001679 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001680}
1681
Dan Gohman475871a2008-07-27 21:46:04 +00001682SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
1683 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001684 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001685
Dan Gohman475871a2008-07-27 21:46:04 +00001686 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001687}
1688
Dan Gohman475871a2008-07-27 21:46:04 +00001689SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
1690 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001691 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001692
Dan Gohman475871a2008-07-27 21:46:04 +00001693 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001694}
1695
Chris Lattner35e5c142006-05-05 05:51:50 +00001696/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1697/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00001698SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1699 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001700 MVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00001701 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1702
Chris Lattner540121f2006-05-05 06:31:05 +00001703 // For each of OP in AND/OR/XOR:
1704 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1705 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1706 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001707 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001708 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001709 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001710 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001711 SDValue ORNode = DAG.getNode(N->getOpcode(),
Chris Lattner35e5c142006-05-05 05:51:50 +00001712 N0.getOperand(0).getValueType(),
1713 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001714 AddToWorkList(ORNode.getNode());
Chris Lattner540121f2006-05-05 06:31:05 +00001715 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001716 }
1717
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001718 // For each of OP in SHL/SRL/SRA/AND...
1719 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1720 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1721 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001722 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001723 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001724 N0.getOperand(1) == N1.getOperand(1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001725 SDValue ORNode = DAG.getNode(N->getOpcode(),
Chris Lattner35e5c142006-05-05 05:51:50 +00001726 N0.getOperand(0).getValueType(),
1727 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001728 AddToWorkList(ORNode.getNode());
Chris Lattner35e5c142006-05-05 05:51:50 +00001729 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1730 }
1731
Dan Gohman475871a2008-07-27 21:46:04 +00001732 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00001733}
1734
Dan Gohman475871a2008-07-27 21:46:04 +00001735SDValue DAGCombiner::visitAND(SDNode *N) {
1736 SDValue N0 = N->getOperand(0);
1737 SDValue N1 = N->getOperand(1);
1738 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001739 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1740 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001741 MVT VT = N1.getValueType();
1742 unsigned BitWidth = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001743
Dan Gohman7f321562007-06-25 16:23:39 +00001744 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001745 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001746 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001747 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001748 }
Dan Gohman7f321562007-06-25 16:23:39 +00001749
Dan Gohman613e0d82007-07-03 14:03:57 +00001750 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001751 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001752 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001753 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001754 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001755 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001756 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001757 if (N0C && !N1C)
1758 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001759 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001760 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001761 return N0;
1762 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00001763 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001764 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001765 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001766 // reassociate and
Bill Wendling35247c32009-01-30 00:45:56 +00001767 SDValue RAND = ReassociateOps(ISD::AND, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001768 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001769 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001770 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001771 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001772 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001773 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001774 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001775 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1776 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00001777 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001778 APInt Mask = ~N1C->getAPIntValue();
1779 Mask.trunc(N0Op0.getValueSizeInBits());
1780 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001781 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001782 N0Op0);
Chris Lattner1ec05d12006-03-01 21:47:21 +00001783
1784 // Replace uses of the AND with uses of the Zero extend node.
1785 CombineTo(N, Zext);
1786
Chris Lattner3603cd62006-02-02 07:17:31 +00001787 // We actually want to replace all uses of the any_extend with the
1788 // zero_extend, to avoid duplicating things. This will later cause this
1789 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00001790 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00001791 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001792 }
1793 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001794 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1795 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1796 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1797 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1798
1799 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001800 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001801 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001802 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
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 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1808 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00001809 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001810 AddToWorkList(ANDNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001811 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1812 }
1813 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1814 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00001815 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001816 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001817 return DAG.getSetCC(VT, ORNode, LR, Op1);
1818 }
1819 }
1820 // canonicalize equivalent to ll == rl
1821 if (LL == RR && LR == RL) {
1822 Op1 = ISD::getSetCCSwappedOperands(Op1);
1823 std::swap(RL, RR);
1824 }
1825 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001826 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001827 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00001828 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001829 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001830 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1831 }
1832 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001833
1834 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1835 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001836 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001837 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001838 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001839
Nate Begemande996292006-02-03 22:24:05 +00001840 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1841 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001842 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00001843 SimplifyDemandedBits(SDValue(N, 0)))
1844 return SDValue(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001845 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00001846 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00001847 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001848 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001849 // If we zero all the possible extended bits, then we can turn this into
1850 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001851 unsigned BitWidth = N1.getValueSizeInBits();
1852 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001853 BitWidth - EVT.getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001854 ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00001855 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001856 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00001857 LN0->getBasePtr(), LN0->getSrcValue(),
1858 LN0->getSrcValueOffset(), EVT,
1859 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001860 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001861 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001862 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001863 }
1864 }
1865 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00001866 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00001867 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001868 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001869 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001870 // If we zero all the possible extended bits, then we can turn this into
1871 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001872 unsigned BitWidth = N1.getValueSizeInBits();
1873 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001874 BitWidth - EVT.getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001875 ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00001876 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001877 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00001878 LN0->getBasePtr(), LN0->getSrcValue(),
1879 LN0->getSrcValueOffset(), EVT,
1880 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001881 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001882 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001883 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001884 }
1885 }
Chris Lattner15045b62006-02-28 06:35:35 +00001886
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001887 // fold (and (load x), 255) -> (zextload x, i8)
1888 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001889 if (N1C && N0.getOpcode() == ISD::LOAD) {
1890 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1891 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001892 LN0->isUnindexed() && N0.hasOneUse() &&
1893 // Do not change the width of a volatile load.
1894 !LN0->isVolatile()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00001895 MVT EVT = MVT::Other;
1896 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
1897 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue()))
1898 EVT = MVT::getIntegerVT(ActiveBits);
1899
1900 MVT LoadedVT = LN0->getMemoryVT();
Duncan Sandsad205a72008-06-16 08:14:38 +00001901 // Do not generate loads of non-round integer types since these can
1902 // be expensive (and would be wrong if the type is not byte sized).
1903 if (EVT != MVT::Other && LoadedVT.bitsGT(EVT) && EVT.isRound() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001904 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001905 MVT PtrType = N0.getOperand(1).getValueType();
Evan Cheng466685d2006-10-09 20:57:25 +00001906 // For big endian targets, we need to add an offset to the pointer to
1907 // load the correct bytes. For little endian systems, we merely need to
1908 // read fewer bytes from the same pointer.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001909 unsigned LVTStoreBytes = LoadedVT.getStoreSizeInBits()/8;
1910 unsigned EVTStoreBytes = EVT.getStoreSizeInBits()/8;
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001911 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001912 unsigned Alignment = LN0->getAlignment();
Dan Gohman475871a2008-07-27 21:46:04 +00001913 SDValue NewPtr = LN0->getBasePtr();
Duncan Sands0753fc12008-02-11 10:37:04 +00001914 if (TLI.isBigEndian()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001915 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1916 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001917 Alignment = MinAlign(Alignment, PtrOff);
1918 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001919 AddToWorkList(NewPtr.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00001920 SDValue Load =
Evan Cheng466685d2006-10-09 20:57:25 +00001921 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001922 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00001923 LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001924 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001925 CombineTo(N0.getNode(), Load, Load.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001926 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng466685d2006-10-09 20:57:25 +00001927 }
Chris Lattner15045b62006-02-28 06:35:35 +00001928 }
1929 }
1930
Dan Gohman475871a2008-07-27 21:46:04 +00001931 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001932}
1933
Dan Gohman475871a2008-07-27 21:46:04 +00001934SDValue DAGCombiner::visitOR(SDNode *N) {
1935 SDValue N0 = N->getOperand(0);
1936 SDValue N1 = N->getOperand(1);
1937 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001938 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1939 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001940 MVT VT = N1.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001941
Dan Gohman7f321562007-06-25 16:23:39 +00001942 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001943 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001944 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001945 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001946 }
Dan Gohman7f321562007-06-25 16:23:39 +00001947
Dan Gohman613e0d82007-07-03 14:03:57 +00001948 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001949 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001950 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001951 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001952 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001953 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001954 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001955 if (N0C && !N1C)
1956 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001957 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001958 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001959 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001960 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001961 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001962 return N1;
1963 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001964 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001965 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001966 // reassociate or
Bill Wendling35247c32009-01-30 00:45:56 +00001967 SDValue ROR = ReassociateOps(ISD::OR, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001968 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001969 return ROR;
1970 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Gabor Greifba36cb52008-08-28 21:40:38 +00001971 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001972 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001973 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1974 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1975 N1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001976 DAG.getConstant(N1C->getAPIntValue() |
1977 C1->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001978 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001979 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1980 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1981 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1982 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1983
1984 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001985 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001986 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1987 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001988 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001989 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001990 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001991 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001992 return DAG.getSetCC(VT, ORNode, LR, Op1);
1993 }
1994 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1995 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1996 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1997 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001998 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001999 AddToWorkList(ANDNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002000 return DAG.getSetCC(VT, ANDNode, LR, Op1);
2001 }
2002 }
2003 // canonicalize equivalent to ll == rl
2004 if (LL == RR && LR == RL) {
2005 Op1 = ISD::getSetCCSwappedOperands(Op1);
2006 std::swap(RL, RR);
2007 }
2008 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002009 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002010 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00002011 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002012 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002013 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
2014 }
2015 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002016
2017 // Simplify: or (op x...), (op y...) -> (op (or x, y))
2018 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002019 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002020 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002021 }
Chris Lattner516b9622006-09-14 20:50:57 +00002022
Chris Lattner1ec72732006-09-14 21:11:37 +00002023 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
2024 if (N0.getOpcode() == ISD::AND &&
2025 N1.getOpcode() == ISD::AND &&
2026 N0.getOperand(1).getOpcode() == ISD::Constant &&
2027 N1.getOperand(1).getOpcode() == ISD::Constant &&
2028 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00002029 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00002030 // We can only do this xform if we know that bits from X that are set in C2
2031 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002032 const APInt &LHSMask =
2033 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
2034 const APInt &RHSMask =
2035 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Chris Lattner1ec72732006-09-14 21:11:37 +00002036
Dan Gohmanea859be2007-06-22 14:59:07 +00002037 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
2038 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002039 SDValue X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
Chris Lattner1ec72732006-09-14 21:11:37 +00002040 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
2041 }
2042 }
2043
2044
Chris Lattner516b9622006-09-14 20:50:57 +00002045 // See if this is some rotate idiom.
2046 if (SDNode *Rot = MatchRotate(N0, N1))
Dan Gohman475871a2008-07-27 21:46:04 +00002047 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00002048
Dan Gohman475871a2008-07-27 21:46:04 +00002049 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002050}
2051
Chris Lattner516b9622006-09-14 20:50:57 +00002052
2053/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00002054static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00002055 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00002056 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00002057 Mask = Op.getOperand(1);
2058 Op = Op.getOperand(0);
2059 } else {
2060 return false;
2061 }
2062 }
2063
2064 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
2065 Shift = Op;
2066 return true;
2067 }
2068 return false;
2069}
2070
2071
2072// MatchRotate - Handle an 'or' of two operands. If this is one of the many
2073// idioms for rotate, and if the target supports rotation instructions, generate
2074// a rot[lr].
Dan Gohman475871a2008-07-27 21:46:04 +00002075SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002076 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002077 MVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00002078 if (!TLI.isTypeLegal(VT)) return 0;
2079
2080 // The target must have at least one rotate flavor.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002081 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
2082 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Chris Lattner516b9622006-09-14 20:50:57 +00002083 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002084
Chris Lattner516b9622006-09-14 20:50:57 +00002085 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00002086 SDValue LHSShift; // The shift.
2087 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00002088 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
2089 return 0; // Not part of a rotate.
2090
Dan Gohman475871a2008-07-27 21:46:04 +00002091 SDValue RHSShift; // The shift.
2092 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00002093 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
2094 return 0; // Not part of a rotate.
2095
2096 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
2097 return 0; // Not shifting the same value.
2098
2099 if (LHSShift.getOpcode() == RHSShift.getOpcode())
2100 return 0; // Shifts must disagree.
2101
2102 // Canonicalize shl to left side in a shl/srl pair.
2103 if (RHSShift.getOpcode() == ISD::SHL) {
2104 std::swap(LHS, RHS);
2105 std::swap(LHSShift, RHSShift);
2106 std::swap(LHSMask , RHSMask );
2107 }
2108
Duncan Sands83ec4b62008-06-06 12:08:01 +00002109 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00002110 SDValue LHSShiftArg = LHSShift.getOperand(0);
2111 SDValue LHSShiftAmt = LHSShift.getOperand(1);
2112 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00002113
2114 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
2115 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00002116 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
2117 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002118 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
2119 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00002120 if ((LShVal + RShVal) != OpSizeInBits)
2121 return 0;
2122
Dan Gohman475871a2008-07-27 21:46:04 +00002123 SDValue Rot;
Chris Lattner516b9622006-09-14 20:50:57 +00002124 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002125 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002126 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002127 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002128
2129 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00002130 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002131 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Chris Lattner516b9622006-09-14 20:50:57 +00002132
Gabor Greifba36cb52008-08-28 21:40:38 +00002133 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002134 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2135 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002136 }
Gabor Greifba36cb52008-08-28 21:40:38 +00002137 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002138 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2139 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002140 }
2141
2142 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
2143 }
2144
Gabor Greifba36cb52008-08-28 21:40:38 +00002145 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002146 }
2147
2148 // If there is a mask here, and we have a variable shift, we can't be sure
2149 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00002150 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00002151 return 0;
2152
2153 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2154 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002155 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2156 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002157 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002158 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002159 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002160 if (HasROTL)
Gabor Greifba36cb52008-08-28 21:40:38 +00002161 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002162 else
Gabor Greifba36cb52008-08-28 21:40:38 +00002163 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002164 }
Chris Lattner516b9622006-09-14 20:50:57 +00002165 }
2166 }
2167
2168 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2169 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002170 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2171 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002172 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002173 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002174 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling2692d592008-08-31 01:13:31 +00002175 if (HasROTR)
Gabor Greifba36cb52008-08-28 21:40:38 +00002176 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).getNode();
Bill Wendling2692d592008-08-31 01:13:31 +00002177 else
2178 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002179 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002180 }
2181 }
2182
Dan Gohman74feef22008-10-17 01:23:35 +00002183 // Look for sign/zext/any-extended or truncate cases:
Scott Michelc9dc1142007-04-02 21:36:32 +00002184 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2185 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00002186 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2187 || LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
Scott Michelc9dc1142007-04-02 21:36:32 +00002188 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2189 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00002190 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2191 || RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002192 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
2193 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +00002194 if (RExtOp0.getOpcode() == ISD::SUB &&
2195 RExtOp0.getOperand(1) == LExtOp0) {
2196 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002197 // (rotl x, y)
Scott Michelc9dc1142007-04-02 21:36:32 +00002198 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002199 // (rotr x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00002200 if (ConstantSDNode *SUBC =
2201 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002202 if (SUBC->getAPIntValue() == OpSizeInBits) {
Gabor Greif12632d22008-08-30 19:29:20 +00002203 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, VT, LHSShiftArg,
2204 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002205 }
2206 }
2207 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2208 RExtOp0 == LExtOp0.getOperand(1)) {
Bill Wendling353dea22008-08-31 01:04:56 +00002209 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002210 // (rotr x, y)
Bill Wendling353dea22008-08-31 01:04:56 +00002211 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002212 // (rotl x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00002213 if (ConstantSDNode *SUBC =
2214 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002215 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling353dea22008-08-31 01:04:56 +00002216 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, VT, LHSShiftArg,
2217 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002218 }
2219 }
Chris Lattner516b9622006-09-14 20:50:57 +00002220 }
2221 }
2222
2223 return 0;
2224}
2225
2226
Dan Gohman475871a2008-07-27 21:46:04 +00002227SDValue DAGCombiner::visitXOR(SDNode *N) {
2228 SDValue N0 = N->getOperand(0);
2229 SDValue N1 = N->getOperand(1);
2230 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00002231 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2232 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002233 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002234
Dan Gohman7f321562007-06-25 16:23:39 +00002235 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002236 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002237 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002238 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002239 }
Dan Gohman7f321562007-06-25 16:23:39 +00002240
Evan Cheng26471c42008-03-25 20:08:07 +00002241 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
2242 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
2243 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00002244 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002245 if (N0.getOpcode() == ISD::UNDEF)
2246 return N0;
2247 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002248 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002249 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002250 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002251 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002252 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002253 if (N0C && !N1C)
2254 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002255 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002256 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002257 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002258 // reassociate xor
Bill Wendling35247c32009-01-30 00:45:56 +00002259 SDValue RXOR = ReassociateOps(ISD::XOR, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002260 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002261 return RXOR;
Bill Wendlingae89bb12008-11-11 08:25:46 +00002262
Nate Begeman1d4d4142005-09-01 00:19:25 +00002263 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00002264 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002265 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00002266 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2267 isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +00002268
Duncan Sands25cf2272008-11-24 14:53:14 +00002269 if (!LegalOperations || TLI.isCondCodeLegal(NotCC, LHS.getValueType())) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00002270 switch (N0.getOpcode()) {
2271 default:
2272 assert(0 && "Unhandled SetCC Equivalent!");
2273 abort();
2274 case ISD::SETCC:
2275 return DAG.getSetCC(VT, LHS, RHS, NotCC);
2276 case ISD::SELECT_CC:
2277 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),
2278 N0.getOperand(3), NotCC);
2279 }
2280 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00002281 }
Bill Wendlingae89bb12008-11-11 08:25:46 +00002282
Chris Lattner61c5ff42007-09-10 21:39:07 +00002283 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002284 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00002285 N0.getNode()->hasOneUse() &&
2286 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00002287 SDValue V = N0.getOperand(0);
Chris Lattner61c5ff42007-09-10 21:39:07 +00002288 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002289 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002290 AddToWorkList(V.getNode());
Chris Lattner61c5ff42007-09-10 21:39:07 +00002291 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2292 }
2293
Nate Begeman99801192005-09-07 23:25:52 +00002294 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Dan Gohman002e5d02008-03-13 22:13:53 +00002295 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002296 (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 (isOneUseSetCC(RHS) || isOneUseSetCC(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 Begeman99801192005-09-07 23:25:52 +00002306 // fold !(x or y) -> (!x and !y) iff x or y are constants
2307 if (N1C && N1C->isAllOnesValue() &&
2308 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002309 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002310 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2311 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002312 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2313 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002314 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Nate Begeman99801192005-09-07 23:25:52 +00002315 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002316 }
2317 }
Nate Begeman223df222005-09-08 20:18:10 +00002318 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2319 if (N1C && N0.getOpcode() == ISD::XOR) {
2320 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2321 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2322 if (N00C)
2323 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00002324 DAG.getConstant(N1C->getAPIntValue()^
2325 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002326 if (N01C)
2327 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
Dan Gohman002e5d02008-03-13 22:13:53 +00002328 DAG.getConstant(N1C->getAPIntValue()^
2329 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002330 }
2331 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002332 if (N0 == N1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002333 if (!VT.isVector()) {
Chris Lattner4fbdd592006-03-28 19:11:05 +00002334 return DAG.getConstant(0, VT);
Duncan Sands25cf2272008-11-24 14:53:14 +00002335 } else if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)){
Chris Lattner4fbdd592006-03-28 19:11:05 +00002336 // Produce a vector of zeros.
Dan Gohman475871a2008-07-27 21:46:04 +00002337 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
2338 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002339 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002340 }
2341 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002342
2343 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2344 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002345 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002346 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002347 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002348
Chris Lattner3e104b12006-04-08 04:15:24 +00002349 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002350 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002351 SimplifyDemandedBits(SDValue(N, 0)))
2352 return SDValue(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002353
Dan Gohman475871a2008-07-27 21:46:04 +00002354 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002355}
2356
Chris Lattnere70da202007-12-06 07:33:36 +00002357/// visitShiftByConstant - Handle transforms common to the three shifts, when
2358/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00002359SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002360 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00002361 if (!LHS->hasOneUse()) return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002362
2363 // We want to pull some binops through shifts, so that we have (and (shift))
2364 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2365 // thing happens with address calculations, so it's important to canonicalize
2366 // it.
2367 bool HighBitSet = false; // Can we transform this if the high bit is set?
2368
2369 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002370 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002371 case ISD::OR:
2372 case ISD::XOR:
2373 HighBitSet = false; // We can only transform sra if the high bit is clear.
2374 break;
2375 case ISD::AND:
2376 HighBitSet = true; // We can only transform sra if the high bit is set.
2377 break;
2378 case ISD::ADD:
2379 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00002380 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00002381 HighBitSet = false; // We can only transform sra if the high bit is clear.
2382 break;
2383 }
2384
2385 // We require the RHS of the binop to be a constant as well.
2386 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002387 if (!BinOpCst) return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002388
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002389
2390 // FIXME: disable this for unless the input to the binop is a shift by a
2391 // constant. If it is not a shift, it pessimizes some common cases like:
2392 //
2393 //void foo(int *X, int i) { X[i & 1235] = 1; }
2394 //int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00002395 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002396 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2397 BinOpLHSVal->getOpcode() != ISD::SRA &&
2398 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2399 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00002400 return SDValue();
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002401
Duncan Sands83ec4b62008-06-06 12:08:01 +00002402 MVT VT = N->getValueType(0);
Chris Lattnere70da202007-12-06 07:33:36 +00002403
2404 // If this is a signed shift right, and the high bit is modified
2405 // by the logical operation, do not perform the transformation.
2406 // The highBitSet boolean indicates the value of the high bit of
2407 // the constant which would cause it to be modified for this
2408 // operation.
2409 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00002410 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2411 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00002412 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002413 }
2414
2415 // Fold the constants, shifting the binop RHS by the shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00002416 SDValue NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
Chris Lattnere70da202007-12-06 07:33:36 +00002417 LHS->getOperand(1), N->getOperand(1));
2418
2419 // Create the new shift.
Dan Gohman475871a2008-07-27 21:46:04 +00002420 SDValue NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
Chris Lattnere70da202007-12-06 07:33:36 +00002421 N->getOperand(1));
2422
2423 // Create the new binop.
2424 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2425}
2426
2427
Dan Gohman475871a2008-07-27 21:46:04 +00002428SDValue DAGCombiner::visitSHL(SDNode *N) {
2429 SDValue N0 = N->getOperand(0);
2430 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002431 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2432 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002433 MVT VT = N0.getValueType();
2434 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002435
2436 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002437 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002438 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002439 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002440 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002441 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002442 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002443 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002444 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002445 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002446 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002447 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002448 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002449 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002450 APInt::getAllOnesValue(VT.getSizeInBits())))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002451 return DAG.getConstant(0, VT);
Evan Chengeb9f8922008-08-30 02:03:58 +00002452 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), c))
2453 // iff (trunc c) == c
2454 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002455 N1.getOperand(0).getOpcode() == ISD::AND &&
2456 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002457 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002458 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002459 MVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002460 SDValue N100 = N1.getOperand(0).getOperand(0);
Dan Gohmance9bc122009-01-27 20:39:34 +00002461 uint64_t TruncC = TruncVT.getIntegerVTBitMask() &
2462 N101C->getZExtValue();
Evan Cheng242ebd12008-09-22 18:19:24 +00002463 return DAG.getNode(ISD::SHL, VT, N0,
2464 DAG.getNode(ISD::AND, TruncVT,
2465 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00002466 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002467 }
2468 }
2469
Dan Gohman475871a2008-07-27 21:46:04 +00002470 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2471 return SDValue(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002472 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002473 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002474 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002475 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2476 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002477 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002478 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002479 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002480 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002481 }
2482 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2483 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002484 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002485 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002486 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2487 uint64_t c2 = N1C->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00002488 SDValue Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman1d4d4142005-09-01 00:19:25 +00002489 DAG.getConstant(~0ULL << c1, VT));
2490 if (c2 > c1)
2491 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002492 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002493 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002494 return DAG.getNode(ISD::SRL, VT, Mask,
2495 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002496 }
2497 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002498 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002499 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002500 DAG.getConstant(~0ULL << N1C->getZExtValue(), VT));
Chris Lattnere70da202007-12-06 07:33:36 +00002501
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002502 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002503}
2504
Dan Gohman475871a2008-07-27 21:46:04 +00002505SDValue DAGCombiner::visitSRA(SDNode *N) {
2506 SDValue N0 = N->getOperand(0);
2507 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002508 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2509 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002510 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002511
2512 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002513 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002514 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002515 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002516 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002517 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002518 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002519 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002520 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002521 // fold (sra x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002522 if (N1C && N1C->getZExtValue() >= VT.getSizeInBits())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002523 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002524 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002525 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002526 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002527 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2528 // sext_inreg.
2529 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002530 unsigned LowBits = VT.getSizeInBits() - (unsigned)N1C->getZExtValue();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002531 MVT EVT = MVT::getIntegerVT(LowBits);
Duncan Sands25cf2272008-11-24 14:53:14 +00002532 if ((!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT)))
Nate Begemanfb7217b2006-02-17 19:54:08 +00002533 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2534 DAG.getValueType(EVT));
2535 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002536
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002537 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2538 if (N1C && N0.getOpcode() == ISD::SRA) {
2539 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002540 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002541 if (Sum >= VT.getSizeInBits()) Sum = VT.getSizeInBits()-1;
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002542 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2543 DAG.getConstant(Sum, N1C->getValueType(0)));
2544 }
2545 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00002546
2547 // fold sra (shl X, m), result_size - n
2548 // -> (sign_extend (trunc (shl X, result_size - n - m))) for
Christopher Lambb9b04282008-03-20 04:31:39 +00002549 // result_size - n != m.
2550 // If truncate is free for the target sext(shl) is likely to result in better
2551 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002552 if (N0.getOpcode() == ISD::SHL) {
2553 // Get the two constanst of the shifts, CN0 = m, CN = n.
2554 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2555 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002556 // Determine what the truncate's result bitsize and type would be.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002557 unsigned VTValSize = VT.getSizeInBits();
2558 MVT TruncVT =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002559 MVT::getIntegerVT(VTValSize - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00002560 // Determine the residual right-shift amount.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002561 unsigned ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002562
Christopher Lambb9b04282008-03-20 04:31:39 +00002563 // If the shift is not a no-op (in which case this should be just a sign
2564 // extend already), the truncated to type is legal, sign_extend is legal
Gabor Greif12632d22008-08-30 19:29:20 +00002565 // on that type, and the the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00002566 // perform the transform.
Duncan Sands25cf2272008-11-24 14:53:14 +00002567 if (ShiftAmt &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002568 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
2569 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00002570 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002571
Dan Gohman475871a2008-07-27 21:46:04 +00002572 SDValue Amt = DAG.getConstant(ShiftAmt, TLI.getShiftAmountTy());
2573 SDValue Shift = DAG.getNode(ISD::SRL, VT, N0.getOperand(0), Amt);
2574 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, TruncVT, Shift);
Christopher Lambb9b04282008-03-20 04:31:39 +00002575 return DAG.getNode(ISD::SIGN_EXTEND, N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00002576 }
2577 }
2578 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002579
Evan Chengeb9f8922008-08-30 02:03:58 +00002580 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), c))
2581 // iff (trunc c) == c
2582 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002583 N1.getOperand(0).getOpcode() == ISD::AND &&
2584 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002585 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002586 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002587 MVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002588 SDValue N100 = N1.getOperand(0).getOperand(0);
Dan Gohmance9bc122009-01-27 20:39:34 +00002589 uint64_t TruncC = TruncVT.getIntegerVTBitMask() &
2590 N101C->getZExtValue();
Evan Cheng242ebd12008-09-22 18:19:24 +00002591 return DAG.getNode(ISD::SRA, VT, N0,
2592 DAG.getNode(ISD::AND, TruncVT,
2593 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00002594 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002595 }
2596 }
2597
Chris Lattnera8504462006-05-08 20:51:54 +00002598 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00002599 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2600 return SDValue(N, 0);
Chris Lattnera8504462006-05-08 20:51:54 +00002601
2602
Nate Begeman1d4d4142005-09-01 00:19:25 +00002603 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002604 if (DAG.SignBitIsZero(N0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002605 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002606
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002607 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002608}
2609
Dan Gohman475871a2008-07-27 21:46:04 +00002610SDValue DAGCombiner::visitSRL(SDNode *N) {
2611 SDValue N0 = N->getOperand(0);
2612 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002613 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2614 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002615 MVT VT = N0.getValueType();
2616 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002617
2618 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002619 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002620 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002621 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002622 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002623 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002624 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002625 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002626 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002627 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002628 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002629 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002630 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002631 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002632 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002633 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002634
Nate Begeman1d4d4142005-09-01 00:19:25 +00002635 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002636 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002637 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002638 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2639 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002640 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002641 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002642 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002643 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002644 }
Chris Lattner350bec02006-04-02 06:11:11 +00002645
Chris Lattner06afe072006-05-05 22:53:17 +00002646 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2647 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2648 // Shifting in all undef bits?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002649 MVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002650 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Chris Lattner06afe072006-05-05 22:53:17 +00002651 return DAG.getNode(ISD::UNDEF, VT);
2652
Dan Gohman475871a2008-07-27 21:46:04 +00002653 SDValue SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002654 AddToWorkList(SmallShift.getNode());
Chris Lattner06afe072006-05-05 22:53:17 +00002655 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2656 }
2657
Chris Lattner3657ffe2006-10-12 20:23:19 +00002658 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2659 // bit, which is unmodified by sra.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002660 if (N1C && N1C->getZExtValue()+1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00002661 if (N0.getOpcode() == ISD::SRA)
2662 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2663 }
2664
Chris Lattner350bec02006-04-02 06:11:11 +00002665 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2666 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002667 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00002668 APInt KnownZero, KnownOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002669 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00002670 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002671
2672 // If any of the input bits are KnownOne, then the input couldn't be all
2673 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002674 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Chris Lattner350bec02006-04-02 06:11:11 +00002675
2676 // If all of the bits input the to ctlz node are known to be zero, then
2677 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002678 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00002679 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2680
2681 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2682 if ((UnknownBits & (UnknownBits-1)) == 0) {
2683 // Okay, we know that only that the single bit specified by UnknownBits
2684 // could be set on input to the CTLZ node. If this bit is set, the SRL
2685 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2686 // to an SRL,XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002687 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00002688 SDValue Op = N0.getOperand(0);
Chris Lattner350bec02006-04-02 06:11:11 +00002689 if (ShAmt) {
2690 Op = DAG.getNode(ISD::SRL, VT, Op,
2691 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002692 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00002693 }
2694 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2695 }
2696 }
Evan Chengeb9f8922008-08-30 02:03:58 +00002697
2698 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), c))
2699 // iff (trunc c) == c
2700 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002701 N1.getOperand(0).getOpcode() == ISD::AND &&
2702 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002703 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002704 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002705 MVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002706 SDValue N100 = N1.getOperand(0).getOperand(0);
Dan Gohmance9bc122009-01-27 20:39:34 +00002707 uint64_t TruncC = TruncVT.getIntegerVTBitMask() &
2708 N101C->getZExtValue();
Evan Cheng242ebd12008-09-22 18:19:24 +00002709 return DAG.getNode(ISD::SRL, VT, N0,
2710 DAG.getNode(ISD::AND, TruncVT,
2711 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00002712 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002713 }
2714 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002715
2716 // fold operands of srl based on knowledge that the low bits are not
2717 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00002718 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2719 return SDValue(N, 0);
Chris Lattner61a4c072007-04-18 03:06:49 +00002720
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002721 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002722}
2723
Dan Gohman475871a2008-07-27 21:46:04 +00002724SDValue DAGCombiner::visitCTLZ(SDNode *N) {
2725 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002726 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002727
2728 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002729 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002730 return DAG.getNode(ISD::CTLZ, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002731 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002732}
2733
Dan Gohman475871a2008-07-27 21:46:04 +00002734SDValue DAGCombiner::visitCTTZ(SDNode *N) {
2735 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002736 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002737
2738 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002739 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002740 return DAG.getNode(ISD::CTTZ, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002741 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002742}
2743
Dan Gohman475871a2008-07-27 21:46:04 +00002744SDValue DAGCombiner::visitCTPOP(SDNode *N) {
2745 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002746 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002747
2748 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002749 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002750 return DAG.getNode(ISD::CTPOP, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002751 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002752}
2753
Dan Gohman475871a2008-07-27 21:46:04 +00002754SDValue DAGCombiner::visitSELECT(SDNode *N) {
2755 SDValue N0 = N->getOperand(0);
2756 SDValue N1 = N->getOperand(1);
2757 SDValue N2 = N->getOperand(2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002758 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2759 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2760 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002761 MVT VT = N->getValueType(0);
2762 MVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002763
Nate Begeman452d7be2005-09-16 00:54:12 +00002764 // fold select C, X, X -> X
2765 if (N1 == N2)
2766 return N1;
2767 // fold select true, X, Y -> X
2768 if (N0C && !N0C->isNullValue())
2769 return N1;
2770 // fold select false, X, Y -> Y
2771 if (N0C && N0C->isNullValue())
2772 return N2;
2773 // fold select C, 1, X -> C | X
Duncan Sands83ec4b62008-06-06 12:08:01 +00002774 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002775 return DAG.getNode(ISD::OR, VT, N0, N2);
Bob Wilson67ba2232009-01-22 22:05:48 +00002776 // fold select C, 0, 1 -> C ^ 1
2777 if (VT.isInteger() &&
2778 (VT0 == MVT::i1 ||
2779 (VT0.isInteger() &&
2780 TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00002781 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00002782 SDValue XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
Evan Cheng571c4782007-08-18 05:57:05 +00002783 if (VT == VT0)
2784 return XORNode;
Gabor Greifba36cb52008-08-28 21:40:38 +00002785 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00002786 if (VT.bitsGT(VT0))
Evan Cheng571c4782007-08-18 05:57:05 +00002787 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2788 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2789 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002790 // fold select C, 0, X -> ~C & X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002791 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Bob Wilson4c245462009-01-22 17:39:32 +00002792 SDValue NOTNode = DAG.getNOT(N0, VT);
2793 AddToWorkList(NOTNode.getNode());
2794 return DAG.getNode(ISD::AND, VT, NOTNode, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002795 }
2796 // fold select C, X, 1 -> ~C | X
Dan Gohman002e5d02008-03-13 22:13:53 +00002797 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Bob Wilson4c245462009-01-22 17:39:32 +00002798 SDValue NOTNode = DAG.getNOT(N0, VT);
2799 AddToWorkList(NOTNode.getNode());
2800 return DAG.getNode(ISD::OR, VT, NOTNode, N1);
Nate Begeman452d7be2005-09-16 00:54:12 +00002801 }
2802 // fold select C, X, 0 -> C & X
Duncan Sands83ec4b62008-06-06 12:08:01 +00002803 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Nate Begeman452d7be2005-09-16 00:54:12 +00002804 return DAG.getNode(ISD::AND, VT, N0, N1);
2805 // fold X ? X : Y --> X ? 1 : Y --> X | Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002806 if (VT == MVT::i1 && N0 == N1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002807 return DAG.getNode(ISD::OR, VT, N0, N2);
2808 // fold X ? Y : X --> X ? Y : 0 --> X & Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002809 if (VT == MVT::i1 && N0 == N2)
Nate Begeman452d7be2005-09-16 00:54:12 +00002810 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002811
Chris Lattner40c62d52005-10-18 06:04:22 +00002812 // If we can fold this based on the true/false value, do so.
2813 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00002814 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002815
Nate Begeman44728a72005-09-19 22:34:01 +00002816 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002817 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002818 // FIXME:
2819 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2820 // having to say they don't support SELECT_CC on every type the DAG knows
2821 // about, since there is no way to mark an opcode illegal at all value types
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002822 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other))
Nate Begeman750ac1b2006-02-01 07:19:44 +00002823 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2824 N1, N2, N0.getOperand(2));
2825 else
2826 return SimplifySelect(N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002827 }
Dan Gohman475871a2008-07-27 21:46:04 +00002828 return SDValue();
Nate Begeman452d7be2005-09-16 00:54:12 +00002829}
2830
Dan Gohman475871a2008-07-27 21:46:04 +00002831SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
2832 SDValue N0 = N->getOperand(0);
2833 SDValue N1 = N->getOperand(1);
2834 SDValue N2 = N->getOperand(2);
2835 SDValue N3 = N->getOperand(3);
2836 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002837 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2838
Nate Begeman44728a72005-09-19 22:34:01 +00002839 // fold select_cc lhs, rhs, x, x, cc -> x
2840 if (N2 == N3)
2841 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002842
Chris Lattner5f42a242006-09-20 06:19:26 +00002843 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00002844 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
2845 N0, N1, CC, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00002846 if (SCC.getNode()) AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00002847
Gabor Greifba36cb52008-08-28 21:40:38 +00002848 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002849 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00002850 return N2; // cond always true -> true val
2851 else
2852 return N3; // cond always false -> false val
2853 }
2854
2855 // Fold to a simpler select_cc
Gabor Greifba36cb52008-08-28 21:40:38 +00002856 if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC)
Chris Lattner5f42a242006-09-20 06:19:26 +00002857 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2858 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2859 SCC.getOperand(2));
2860
Chris Lattner40c62d52005-10-18 06:04:22 +00002861 // If we can fold this based on the true/false value, do so.
2862 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00002863 return SDValue(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002864
Nate Begeman44728a72005-09-19 22:34:01 +00002865 // fold select_cc into other things, such as min/max/abs
2866 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002867}
2868
Dan Gohman475871a2008-07-27 21:46:04 +00002869SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002870 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2871 cast<CondCodeSDNode>(N->getOperand(2))->get());
2872}
2873
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002874// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2875// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2876// transformation. Returns true if extension are possible and the above
2877// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00002878static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002879 unsigned ExtOpc,
2880 SmallVector<SDNode*, 4> &ExtendNodes,
Dan Gohman79ce2762009-01-15 19:20:50 +00002881 const TargetLowering &TLI) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002882 bool HasCopyToRegUses = false;
2883 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00002884 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
2885 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002886 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002887 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002888 if (User == N)
2889 continue;
2890 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2891 if (User->getOpcode() == ISD::SETCC) {
2892 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2893 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2894 // Sign bits will be lost after a zext.
2895 return false;
2896 bool Add = false;
2897 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002898 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002899 if (UseOp == N0)
2900 continue;
2901 if (!isa<ConstantSDNode>(UseOp))
2902 return false;
2903 Add = true;
2904 }
2905 if (Add)
2906 ExtendNodes.push_back(User);
2907 } else {
2908 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002909 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002910 if (UseOp == N0) {
2911 // If truncate from extended type to original load type is free
2912 // on this target, then it's ok to extend a CopyToReg.
2913 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2914 HasCopyToRegUses = true;
2915 else
2916 return false;
2917 }
2918 }
2919 }
2920 }
2921
2922 if (HasCopyToRegUses) {
2923 bool BothLiveOut = false;
2924 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2925 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002926 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002927 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002928 SDValue UseOp = User->getOperand(i);
Gabor Greifba36cb52008-08-28 21:40:38 +00002929 if (UseOp.getNode() == N && UseOp.getResNo() == 0) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002930 BothLiveOut = true;
2931 break;
2932 }
2933 }
2934 }
2935 if (BothLiveOut)
2936 // Both unextended and extended values are live out. There had better be
2937 // good a reason for the transformation.
2938 return ExtendNodes.size();
2939 }
2940 return true;
2941}
2942
Dan Gohman475871a2008-07-27 21:46:04 +00002943SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
2944 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002945 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002946
Nate Begeman1d4d4142005-09-01 00:19:25 +00002947 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002948 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002949 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002950
Nate Begeman1d4d4142005-09-01 00:19:25 +00002951 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002952 // fold (sext (aext x)) -> (sext x)
2953 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002954 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002955
Chris Lattner22558872007-02-26 03:13:59 +00002956 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002957 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2958 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002959 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
2960 if (NarrowLoad.getNode()) {
2961 if (NarrowLoad.getNode() != N0.getNode())
2962 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00002963 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2964 }
Evan Chengc88138f2007-03-22 01:54:19 +00002965
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002966 // See if the value being truncated is already sign extended. If so, just
2967 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00002968 SDValue Op = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002969 unsigned OpBits = Op.getValueType().getSizeInBits();
2970 unsigned MidBits = N0.getValueType().getSizeInBits();
2971 unsigned DestBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002972 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002973
2974 if (OpBits == DestBits) {
2975 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2976 // bits, it is already ready.
2977 if (NumSignBits > DestBits-MidBits)
2978 return Op;
2979 } else if (OpBits < DestBits) {
2980 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2981 // bits, just sext from i32.
2982 if (NumSignBits > OpBits-MidBits)
2983 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2984 } else {
2985 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2986 // bits, just truncate to i32.
2987 if (NumSignBits > OpBits-MidBits)
2988 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002989 }
Chris Lattner22558872007-02-26 03:13:59 +00002990
2991 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sands25cf2272008-11-24 14:53:14 +00002992 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2993 N0.getValueType())) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00002994 if (Op.getValueType().bitsLT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002995 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002996 else if (Op.getValueType().bitsGT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002997 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2998 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2999 DAG.getValueType(N0.getValueType()));
3000 }
Chris Lattner6007b842006-09-21 06:00:20 +00003001 }
Chris Lattner310b5782006-05-06 23:06:26 +00003002
Evan Cheng110dec22005-12-14 02:19:23 +00003003 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003004 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003005 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003006 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003007 bool DoXform = true;
3008 SmallVector<SDNode*, 4> SetCCs;
3009 if (!N0.hasOneUse())
3010 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
3011 if (DoXform) {
3012 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003013 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003014 LN0->getBasePtr(), LN0->getSrcValue(),
3015 LN0->getSrcValueOffset(),
3016 N0.getValueType(),
3017 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003018 CombineTo(N, ExtLoad);
Dan Gohman475871a2008-07-27 21:46:04 +00003019 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003020 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003021 // Extend SetCC uses if necessary.
3022 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3023 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00003024 SmallVector<SDValue, 4> Ops;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003025 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00003026 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003027 if (SOp == Trunc)
3028 Ops.push_back(ExtLoad);
3029 else
3030 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
3031 }
3032 Ops.push_back(SetCC->getOperand(2));
3033 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
3034 &Ops[0], Ops.size()));
3035 }
Dan Gohman475871a2008-07-27 21:46:04 +00003036 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003037 }
Nate Begeman3df4d522005-10-12 20:40:40 +00003038 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003039
3040 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
3041 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003042 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3043 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003044 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003045 MVT EVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00003046 if ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003047 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003048 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003049 LN0->getBasePtr(), LN0->getSrcValue(),
3050 LN0->getSrcValueOffset(), EVT,
3051 LN0->isVolatile(), LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003052 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00003053 CombineTo(N0.getNode(),
3054 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003055 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003056 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003057 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003058 }
3059
Chris Lattner20a35c32007-04-11 05:32:27 +00003060 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
3061 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003062 SDValue SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003063 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3064 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
3065 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003066 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003067 }
3068
Dan Gohman8f0ad582008-04-28 16:58:24 +00003069 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sands25cf2272008-11-24 14:53:14 +00003070 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman187db7b2008-04-28 18:47:17 +00003071 DAG.SignBitIsZero(N0))
Dan Gohman8f0ad582008-04-28 16:58:24 +00003072 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3073
Dan Gohman475871a2008-07-27 21:46:04 +00003074 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003075}
3076
Dan Gohman475871a2008-07-27 21:46:04 +00003077SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
3078 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003079 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003080
Nate Begeman1d4d4142005-09-01 00:19:25 +00003081 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00003082 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003083 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003084 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00003085 // fold (zext (aext x)) -> (zext x)
3086 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003087 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00003088
Evan Chengc88138f2007-03-22 01:54:19 +00003089 // fold (zext (truncate (load x))) -> (zext (smaller load x))
3090 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00003091 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003092 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3093 if (NarrowLoad.getNode()) {
3094 if (NarrowLoad.getNode() != N0.getNode())
3095 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003096 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
3097 }
Evan Chengc88138f2007-03-22 01:54:19 +00003098 }
3099
Chris Lattner6007b842006-09-21 06:00:20 +00003100 // fold (zext (truncate x)) -> (and x, mask)
3101 if (N0.getOpcode() == ISD::TRUNCATE &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003102 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00003103 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003104 if (Op.getValueType().bitsLT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00003105 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003106 } else if (Op.getValueType().bitsGT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00003107 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
3108 }
3109 return DAG.getZeroExtendInReg(Op, N0.getValueType());
3110 }
3111
Chris Lattner111c2282006-09-21 06:14:31 +00003112 // fold (zext (and (trunc x), cst)) -> (and x, cst).
3113 if (N0.getOpcode() == ISD::AND &&
3114 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3115 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman475871a2008-07-27 21:46:04 +00003116 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003117 if (X.getValueType().bitsLT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00003118 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003119 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00003120 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3121 }
Dan Gohman220a8232008-03-03 23:51:38 +00003122 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003123 Mask.zext(VT.getSizeInBits());
Chris Lattner111c2282006-09-21 06:14:31 +00003124 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3125 }
3126
Evan Cheng110dec22005-12-14 02:19:23 +00003127 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003128 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003129 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003130 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003131 bool DoXform = true;
3132 SmallVector<SDNode*, 4> SetCCs;
3133 if (!N0.hasOneUse())
3134 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
3135 if (DoXform) {
3136 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003137 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003138 LN0->getBasePtr(), LN0->getSrcValue(),
3139 LN0->getSrcValueOffset(),
3140 N0.getValueType(),
3141 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003142 CombineTo(N, ExtLoad);
Dan Gohman475871a2008-07-27 21:46:04 +00003143 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003144 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003145 // Extend SetCC uses if necessary.
3146 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3147 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00003148 SmallVector<SDValue, 4> Ops;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003149 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00003150 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003151 if (SOp == Trunc)
3152 Ops.push_back(ExtLoad);
3153 else
Evan Chengde1631b2007-10-30 20:11:21 +00003154 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003155 }
3156 Ops.push_back(SetCC->getOperand(2));
3157 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
3158 &Ops[0], Ops.size()));
3159 }
Dan Gohman475871a2008-07-27 21:46:04 +00003160 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003161 }
Evan Cheng110dec22005-12-14 02:19:23 +00003162 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003163
3164 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
3165 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003166 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3167 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003168 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003169 MVT EVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00003170 if ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003171 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003172 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003173 LN0->getBasePtr(), LN0->getSrcValue(),
3174 LN0->getSrcValueOffset(), EVT,
3175 LN0->isVolatile(), LN0->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003176 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00003177 CombineTo(N0.getNode(),
3178 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003179 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003180 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003181 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003182 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003183
3184 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3185 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003186 SDValue SCC =
Chris Lattner20a35c32007-04-11 05:32:27 +00003187 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3188 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003189 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003190 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003191 }
3192
Dan Gohman475871a2008-07-27 21:46:04 +00003193 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003194}
3195
Dan Gohman475871a2008-07-27 21:46:04 +00003196SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
3197 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003198 MVT VT = N->getValueType(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00003199
3200 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003201 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00003202 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
3203 // fold (aext (aext x)) -> (aext x)
3204 // fold (aext (zext x)) -> (zext x)
3205 // fold (aext (sext x)) -> (sext x)
3206 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3207 N0.getOpcode() == ISD::ZERO_EXTEND ||
3208 N0.getOpcode() == ISD::SIGN_EXTEND)
3209 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
3210
Evan Chengc88138f2007-03-22 01:54:19 +00003211 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3212 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3213 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003214 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3215 if (NarrowLoad.getNode()) {
3216 if (NarrowLoad.getNode() != N0.getNode())
3217 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003218 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
3219 }
Evan Chengc88138f2007-03-22 01:54:19 +00003220 }
3221
Chris Lattner84750582006-09-20 06:29:17 +00003222 // fold (aext (truncate x))
3223 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00003224 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00003225 if (TruncOp.getValueType() == VT)
3226 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003227 if (TruncOp.getValueType().bitsGT(VT))
Chris Lattner84750582006-09-20 06:29:17 +00003228 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
3229 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
3230 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00003231
3232 // fold (aext (and (trunc x), cst)) -> (and x, cst).
3233 if (N0.getOpcode() == ISD::AND &&
3234 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3235 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman475871a2008-07-27 21:46:04 +00003236 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003237 if (X.getValueType().bitsLT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003238 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003239 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003240 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3241 }
Dan Gohman220a8232008-03-03 23:51:38 +00003242 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003243 Mask.zext(VT.getSizeInBits());
Chris Lattner0e4b9222006-09-21 06:40:43 +00003244 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3245 }
3246
Chris Lattner5ffc0662006-05-05 05:58:59 +00003247 // fold (aext (load x)) -> (aext (truncate (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003248 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003249 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003250 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003251 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003252 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003253 LN0->getBasePtr(), LN0->getSrcValue(),
3254 LN0->getSrcValueOffset(),
3255 N0.getValueType(),
3256 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003257 CombineTo(N, ExtLoad);
Dan Gohman75dcf082008-07-31 00:50:31 +00003258 // Redirect any chain users to the new load.
Evan Cheng45299662008-08-29 23:20:46 +00003259 DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1),
3260 SDValue(ExtLoad.getNode(), 1));
Dan Gohman75dcf082008-07-31 00:50:31 +00003261 // If any node needs the original loaded value, recompute it.
3262 if (!LN0->use_empty())
3263 CombineTo(LN0, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3264 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003265 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003266 }
3267
3268 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3269 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3270 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003271 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003272 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003273 N0.hasOneUse()) {
3274 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003275 MVT EVT = LN0->getMemoryVT();
Dan Gohman475871a2008-07-27 21:46:04 +00003276 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00003277 LN0->getChain(), LN0->getBasePtr(),
3278 LN0->getSrcValue(),
3279 LN0->getSrcValueOffset(), EVT,
3280 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003281 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00003282 CombineTo(N0.getNode(),
3283 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00003284 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003285 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003286 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003287
3288 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3289 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003290 SDValue SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003291 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3292 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003293 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003294 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003295 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003296 }
3297
Dan Gohman475871a2008-07-27 21:46:04 +00003298 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00003299}
3300
Chris Lattner2b4c2792007-10-13 06:35:54 +00003301/// GetDemandedBits - See if the specified operand can be simplified with the
3302/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00003303/// simpler operand, otherwise return a null SDValue.
3304SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003305 switch (V.getOpcode()) {
3306 default: break;
3307 case ISD::OR:
3308 case ISD::XOR:
3309 // If the LHS or RHS don't contribute bits to the or, drop them.
3310 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3311 return V.getOperand(1);
3312 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3313 return V.getOperand(0);
3314 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003315 case ISD::SRL:
3316 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003317 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00003318 break;
3319 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3320 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003321 unsigned Amt = RHSC->getZExtValue();
Dan Gohmancc91d632009-01-03 19:22:06 +00003322 // Watch out for shift count overflow though.
3323 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003324 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00003325 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Gabor Greifba36cb52008-08-28 21:40:38 +00003326 if (SimplifyLHS.getNode()) {
Chris Lattnere33544c2007-10-13 06:58:48 +00003327 return DAG.getNode(ISD::SRL, V.getValueType(),
3328 SimplifyLHS, V.getOperand(1));
3329 }
3330 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003331 }
Dan Gohman475871a2008-07-27 21:46:04 +00003332 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00003333}
3334
Evan Chengc88138f2007-03-22 01:54:19 +00003335/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3336/// bits and then truncated to a narrower type and where N is a multiple
3337/// of number of bits of the narrower type, transform it to a narrower load
3338/// from address + N / num of bits of new type. If the result is to be
3339/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00003340SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00003341 unsigned Opc = N->getOpcode();
3342 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00003343 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003344 MVT VT = N->getValueType(0);
Dan Gohman764fd0c2009-01-21 15:17:51 +00003345 MVT EVT = VT;
Evan Chengc88138f2007-03-22 01:54:19 +00003346
Dan Gohman7f8613e2008-08-14 20:04:46 +00003347 // This transformation isn't valid for vector loads.
3348 if (VT.isVector())
3349 return SDValue();
3350
Evan Chenge177e302007-03-23 22:13:36 +00003351 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3352 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003353 if (Opc == ISD::SIGN_EXTEND_INREG) {
3354 ExtType = ISD::SEXTLOAD;
3355 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00003356 if (LegalOperations && !TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))
Dan Gohman475871a2008-07-27 21:46:04 +00003357 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003358 }
3359
Duncan Sands83ec4b62008-06-06 12:08:01 +00003360 unsigned EVTBits = EVT.getSizeInBits();
Evan Chengc88138f2007-03-22 01:54:19 +00003361 unsigned ShAmt = 0;
3362 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3363 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003364 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003365 // Is the shift amount a multiple of size of VT?
3366 if ((ShAmt & (EVTBits-1)) == 0) {
3367 N0 = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003368 if (N0.getValueType().getSizeInBits() <= EVTBits)
Dan Gohman475871a2008-07-27 21:46:04 +00003369 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003370 }
3371 }
3372 }
3373
Duncan Sandsad205a72008-06-16 08:14:38 +00003374 // Do not generate loads of non-round integer types since these can
3375 // be expensive (and would be wrong if the type is not byte sized).
Dan Gohmanf5add582009-01-20 01:06:45 +00003376 if (isa<LoadSDNode>(N0) && N0.hasOneUse() && EVT.isRound() &&
Dan Gohman75dcf082008-07-31 00:50:31 +00003377 cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() > EVTBits &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003378 // Do not change the width of a volatile load.
3379 !cast<LoadSDNode>(N0)->isVolatile()) {
Evan Chengc88138f2007-03-22 01:54:19 +00003380 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003381 MVT PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00003382 // For big endian targets, we need to adjust the offset to the pointer to
3383 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003384 if (TLI.isBigEndian()) {
Dan Gohman75dcf082008-07-31 00:50:31 +00003385 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003386 unsigned EVTStoreBits = EVT.getStoreSizeInBits();
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003387 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3388 }
Evan Chengdae54ce2007-03-24 00:02:43 +00003389 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003390 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Dan Gohman475871a2008-07-27 21:46:04 +00003391 SDValue NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003392 DAG.getConstant(PtrOff, PtrType));
Gabor Greifba36cb52008-08-28 21:40:38 +00003393 AddToWorkList(NewPtr.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00003394 SDValue Load = (ExtType == ISD::NON_EXTLOAD)
Evan Chengc88138f2007-03-22 01:54:19 +00003395 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003396 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
Duncan Sandsdc846502007-10-28 12:59:45 +00003397 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003398 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003399 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
3400 EVT, LN0->isVolatile(), NewAlign);
Dan Gohman764fd0c2009-01-21 15:17:51 +00003401 // Replace the old load's chain with the new load's chain.
3402 WorkListRemover DeadNodes(*this);
3403 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3404 &DeadNodes);
3405 // Return the new loaded value.
3406 return Load;
Evan Chengc88138f2007-03-22 01:54:19 +00003407 }
3408
Dan Gohman475871a2008-07-27 21:46:04 +00003409 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003410}
3411
Chris Lattner5ffc0662006-05-05 05:58:59 +00003412
Dan Gohman475871a2008-07-27 21:46:04 +00003413SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
3414 SDValue N0 = N->getOperand(0);
3415 SDValue N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003416 MVT VT = N->getValueType(0);
3417 MVT EVT = cast<VTSDNode>(N1)->getVT();
3418 unsigned VTBits = VT.getSizeInBits();
3419 unsigned EVTBits = EVT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003420
Nate Begeman1d4d4142005-09-01 00:19:25 +00003421 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003422 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003423 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003424
Chris Lattner541a24f2006-05-06 22:43:44 +00003425 // If the input is already sign extended, just drop the extension.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003426 if (DAG.ComputeNumSignBits(N0) >= VT.getSizeInBits()-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003427 return N0;
3428
Nate Begeman646d7e22005-09-02 21:18:40 +00003429 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3430 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00003431 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003432 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003433 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003434
Dan Gohman75dcf082008-07-31 00:50:31 +00003435 // fold (sext_in_reg (sext x)) -> (sext x)
3436 // fold (sext_in_reg (aext x)) -> (sext x)
3437 // if x is small enough.
3438 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
3439 SDValue N00 = N0.getOperand(0);
3440 if (N00.getValueType().getSizeInBits() < EVTBits)
3441 return DAG.getNode(ISD::SIGN_EXTEND, VT, N00, N1);
3442 }
3443
Chris Lattner95a5e052007-04-17 19:03:21 +00003444 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003445 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Nate Begemande996292006-02-03 22:24:05 +00003446 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003447
Chris Lattner95a5e052007-04-17 19:03:21 +00003448 // fold operands of sext_in_reg based on knowledge that the top bits are not
3449 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00003450 if (SimplifyDemandedBits(SDValue(N, 0)))
3451 return SDValue(N, 0);
Chris Lattner95a5e052007-04-17 19:03:21 +00003452
Evan Chengc88138f2007-03-22 01:54:19 +00003453 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3454 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00003455 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003456 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00003457 return NarrowLoad;
3458
Chris Lattner4b37e872006-05-08 21:18:59 +00003459 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3460 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3461 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3462 if (N0.getOpcode() == ISD::SRL) {
3463 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003464 if (ShAmt->getZExtValue()+EVTBits <= VT.getSizeInBits()) {
Chris Lattner4b37e872006-05-08 21:18:59 +00003465 // We can turn this into an SRA iff the input to the SRL is already sign
3466 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003467 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003468 if (VT.getSizeInBits()-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Chris Lattner4b37e872006-05-08 21:18:59 +00003469 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3470 }
3471 }
Evan Chengc88138f2007-03-22 01:54:19 +00003472
Nate Begemanded49632005-10-13 03:11:28 +00003473 // fold (sext_inreg (extload x)) -> (sextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00003474 if (ISD::isEXTLoad(N0.getNode()) &&
3475 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003476 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003477 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003478 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003479 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003480 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003481 LN0->getBasePtr(), LN0->getSrcValue(),
3482 LN0->getSrcValueOffset(), EVT,
3483 LN0->isVolatile(), LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003484 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003485 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003486 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003487 }
3488 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00003489 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003490 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003491 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003492 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003493 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003494 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003495 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003496 LN0->getBasePtr(), LN0->getSrcValue(),
3497 LN0->getSrcValueOffset(), EVT,
3498 LN0->isVolatile(), LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003499 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003500 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003501 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003502 }
Dan Gohman475871a2008-07-27 21:46:04 +00003503 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003504}
3505
Dan Gohman475871a2008-07-27 21:46:04 +00003506SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
3507 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003508 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003509
3510 // noop truncate
3511 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003512 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003513 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003514 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003515 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003516 // fold (truncate (truncate x)) -> (truncate x)
3517 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003518 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003519 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003520 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3521 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00003522 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003523 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003524 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00003525 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003526 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003527 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003528 else
3529 // if the source and dest are the same type, we can drop both the extend
3530 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003531 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003532 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003533
Chris Lattner2b4c2792007-10-13 06:35:54 +00003534 // See if we can simplify the input to this truncate through knowledge that
3535 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3536 // -> trunc y
Dan Gohman475871a2008-07-27 21:46:04 +00003537 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003538 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00003539 VT.getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003540 if (Shorter.getNode())
Chris Lattner2b4c2792007-10-13 06:35:54 +00003541 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3542
Nate Begeman3df4d522005-10-12 20:40:40 +00003543 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003544 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003545 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003546}
3547
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003548static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003549 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003550 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00003551 return Elt.getNode();
3552 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003553}
3554
3555/// CombineConsecutiveLoads - build_pair (load, load) -> load
3556/// if load locations are consecutive.
Dan Gohman475871a2008-07-27 21:46:04 +00003557SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, MVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003558 assert(N->getOpcode() == ISD::BUILD_PAIR);
3559
3560 SDNode *LD1 = getBuildPairElt(N, 0);
3561 if (!ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00003562 return SDValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003563 MVT LD1VT = LD1->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003564 SDNode *LD2 = getBuildPairElt(N, 1);
3565 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3566 if (ISD::isNON_EXTLoad(LD2) &&
3567 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003568 // If both are volatile this would reduce the number of volatile loads.
3569 // If one is volatile it might be ok, but play conservative and bail out.
3570 !cast<LoadSDNode>(LD1)->isVolatile() &&
3571 !cast<LoadSDNode>(LD2)->isVolatile() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003572 TLI.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1, MFI)) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003573 LoadSDNode *LD = cast<LoadSDNode>(LD1);
3574 unsigned Align = LD->getAlignment();
Dan Gohman6448d912008-09-04 15:39:15 +00003575 unsigned NewAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003576 getABITypeAlignment(VT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003577 if (NewAlign <= Align &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003578 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003579 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(),
3580 LD->getSrcValue(), LD->getSrcValueOffset(),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003581 false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003582 }
Dan Gohman475871a2008-07-27 21:46:04 +00003583 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003584}
3585
Dan Gohman475871a2008-07-27 21:46:04 +00003586SDValue DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3587 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003588 MVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00003589
Dan Gohman7f321562007-06-25 16:23:39 +00003590 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3591 // Only do this before legalize, since afterward the target may be depending
3592 // on the bitconvert.
3593 // First check to see if this is all constant.
Duncan Sands25cf2272008-11-24 14:53:14 +00003594 if (!LegalTypes &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003595 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003596 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003597 bool isSimple = true;
3598 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3599 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3600 N0.getOperand(i).getOpcode() != ISD::Constant &&
3601 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3602 isSimple = false;
3603 break;
3604 }
3605
Duncan Sands83ec4b62008-06-06 12:08:01 +00003606 MVT DestEltVT = N->getValueType(0).getVectorElementType();
3607 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00003608 "Element type of vector ValueType must not be vector!");
3609 if (isSimple) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003610 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00003611 }
3612 }
3613
Dan Gohman3dd168d2008-09-05 01:58:21 +00003614 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00003615 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003616 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
Gabor Greifba36cb52008-08-28 21:40:38 +00003617 if (Res.getNode() != N) return Res;
Chris Lattner94683772005-12-23 05:30:37 +00003618 }
3619
Chris Lattnerc8547d82005-12-23 05:37:50 +00003620 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3621 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003622
Chris Lattner57104102005-12-23 05:44:41 +00003623 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003624 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00003625 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003626 // Do not change the width of a volatile load.
3627 !cast<LoadSDNode>(N0)->isVolatile() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003628 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003629 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman6448d912008-09-04 15:39:15 +00003630 unsigned Align = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003631 getABITypeAlignment(VT.getTypeForMVT());
Evan Cheng59d5b682007-05-07 21:27:48 +00003632 unsigned OrigAlign = LN0->getAlignment();
3633 if (Align <= OrigAlign) {
Dan Gohman475871a2008-07-27 21:46:04 +00003634 SDValue Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003635 LN0->getSrcValue(), LN0->getSrcValueOffset(),
3636 LN0->isVolatile(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00003637 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00003638 CombineTo(N0.getNode(),
3639 DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00003640 Load.getValue(1));
3641 return Load;
3642 }
Chris Lattner57104102005-12-23 05:44:41 +00003643 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003644
Chris Lattner3bd39d42008-01-27 17:42:27 +00003645 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3646 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3647 // This often reduces constant pool loads.
3648 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003649 N0.getNode()->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003650 SDValue NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00003651 AddToWorkList(NewConv.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003652
Duncan Sands83ec4b62008-06-06 12:08:01 +00003653 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003654 if (N0.getOpcode() == ISD::FNEG)
3655 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3656 assert(N0.getOpcode() == ISD::FABS);
3657 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3658 }
3659
3660 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3661 // Note that we don't handle copysign(x,cst) because this can always be folded
3662 // to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003663 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00003664 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003665 VT.isInteger() && !VT.isVector()) {
3666 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Duncan Sands25cf2272008-11-24 14:53:14 +00003667 MVT IntXVT = MVT::getIntegerVT(OrigXWidth);
3668 if (TLI.isTypeLegal(IntXVT) || !LegalTypes) {
3669 SDValue X = DAG.getNode(ISD::BIT_CONVERT, IntXVT, N0.getOperand(1));
3670 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003671
Duncan Sands25cf2272008-11-24 14:53:14 +00003672 // If X has a different width than the result/lhs, sext it or truncate it.
3673 unsigned VTWidth = VT.getSizeInBits();
3674 if (OrigXWidth < VTWidth) {
3675 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
3676 AddToWorkList(X.getNode());
3677 } else if (OrigXWidth > VTWidth) {
3678 // To get the sign bit in the right place, we have to shift it right
3679 // before truncating.
3680 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3681 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
3682 AddToWorkList(X.getNode());
3683 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3684 AddToWorkList(X.getNode());
3685 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003686
Duncan Sands25cf2272008-11-24 14:53:14 +00003687 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
3688 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
3689 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003690
Duncan Sands25cf2272008-11-24 14:53:14 +00003691 SDValue Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3692 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
3693 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003694
Duncan Sands25cf2272008-11-24 14:53:14 +00003695 return DAG.getNode(ISD::OR, VT, X, Cst);
3696 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003697 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003698
3699 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
3700 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003701 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
3702 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003703 return CombineLD;
3704 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003705
Dan Gohman475871a2008-07-27 21:46:04 +00003706 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00003707}
3708
Dan Gohman475871a2008-07-27 21:46:04 +00003709SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003710 MVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003711 return CombineConsecutiveLoads(N, VT);
3712}
3713
Dan Gohman7f321562007-06-25 16:23:39 +00003714/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003715/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3716/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00003717SDValue DAGCombiner::
Duncan Sands83ec4b62008-06-06 12:08:01 +00003718ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT DstEltVT) {
3719 MVT SrcEltVT = BV->getOperand(0).getValueType();
Chris Lattner6258fb22006-04-02 02:53:43 +00003720
3721 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00003722 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003723
Duncan Sands83ec4b62008-06-06 12:08:01 +00003724 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
3725 unsigned DstBitSize = DstEltVT.getSizeInBits();
Chris Lattner6258fb22006-04-02 02:53:43 +00003726
3727 // If this is a conversion of N elements of one type to N elements of another
3728 // type, convert each element. This handles FP<->INT cases.
3729 if (SrcBitSize == DstBitSize) {
Dan Gohman475871a2008-07-27 21:46:04 +00003730 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003731 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003732 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Gabor Greifba36cb52008-08-28 21:40:38 +00003733 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00003734 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003735 MVT VT = MVT::getVectorVT(DstEltVT,
3736 BV->getValueType(0).getVectorNumElements());
Dan Gohman7f321562007-06-25 16:23:39 +00003737 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003738 }
3739
3740 // Otherwise, we're growing or shrinking the elements. To avoid having to
3741 // handle annoying details of growing/shrinking FP values, we convert them to
3742 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003743 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003744 // Convert the input float vector to a int vector where the elements are the
3745 // same sizes.
3746 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003747 MVT IntVT = MVT::getIntegerVT(SrcEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00003748 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00003749 SrcEltVT = IntVT;
3750 }
3751
3752 // Now we know the input is an integer vector. If the output is a FP type,
3753 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003754 if (DstEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003755 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003756 MVT TmpVT = MVT::getIntegerVT(DstEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00003757 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00003758
3759 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003760 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003761 }
3762
3763 // Okay, we know the src/dst types are both integers of differing types.
3764 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003765 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00003766 if (SrcBitSize < DstBitSize) {
3767 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3768
Dan Gohman475871a2008-07-27 21:46:04 +00003769 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003770 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003771 i += NumInputsPerOutput) {
3772 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00003773 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003774 bool EltIsUndef = true;
3775 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3776 // Shift the previously computed bits over.
3777 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00003778 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00003779 if (Op.getOpcode() == ISD::UNDEF) continue;
3780 EltIsUndef = false;
3781
Dan Gohman220a8232008-03-03 23:51:38 +00003782 NewBits |=
3783 APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003784 }
3785
3786 if (EltIsUndef)
3787 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3788 else
3789 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3790 }
3791
Duncan Sands83ec4b62008-06-06 12:08:01 +00003792 MVT VT = MVT::getVectorVT(DstEltVT, Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00003793 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003794 }
3795
3796 // Finally, this must be the case where we are shrinking elements: each input
3797 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00003798 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00003799 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003800 MVT VT = MVT::getVectorVT(DstEltVT, NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00003801 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003802 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003803 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3804 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3805 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3806 continue;
3807 }
Dan Gohman220a8232008-03-03 23:51:38 +00003808 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getAPIntValue();
Chris Lattner6258fb22006-04-02 02:53:43 +00003809 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohman220a8232008-03-03 23:51:38 +00003810 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003811 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohman220a8232008-03-03 23:51:38 +00003812 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00003813 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
3814 return DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00003815 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003816 }
3817
3818 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00003819 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00003820 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3821 }
Dan Gohman7f321562007-06-25 16:23:39 +00003822 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003823}
3824
3825
3826
Dan Gohman475871a2008-07-27 21:46:04 +00003827SDValue DAGCombiner::visitFADD(SDNode *N) {
3828 SDValue N0 = N->getOperand(0);
3829 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003830 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3831 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003832 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003833
Dan Gohman7f321562007-06-25 16:23:39 +00003834 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003835 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003836 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003837 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003838 }
Dan Gohman7f321562007-06-25 16:23:39 +00003839
Nate Begemana0e221d2005-10-18 00:28:13 +00003840 // fold (fadd c1, c2) -> c1+c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003841 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003842 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003843 // canonicalize constant to RHS
3844 if (N0CFP && !N1CFP)
3845 return DAG.getNode(ISD::FADD, VT, N1, N0);
Dan Gohman760f86f2009-01-22 21:58:43 +00003846 // fold (A + 0) -> A
3847 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
3848 return N0;
Chris Lattner01b3d732005-09-28 22:28:18 +00003849 // fold (A + (-B)) -> A-B
Duncan Sands25cf2272008-11-24 14:53:14 +00003850 if (isNegatibleForFree(N1, LegalOperations) == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003851 return DAG.getNode(ISD::FSUB, VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00003852 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner01b3d732005-09-28 22:28:18 +00003853 // fold ((-A) + B) -> B-A
Duncan Sands25cf2272008-11-24 14:53:14 +00003854 if (isNegatibleForFree(N0, LegalOperations) == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003855 return DAG.getNode(ISD::FSUB, VT, N1,
Duncan Sands25cf2272008-11-24 14:53:14 +00003856 GetNegatedExpression(N0, DAG, LegalOperations));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003857
3858 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3859 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003860 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003861 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3862 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3863
Dan Gohman475871a2008-07-27 21:46:04 +00003864 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003865}
3866
Dan Gohman475871a2008-07-27 21:46:04 +00003867SDValue DAGCombiner::visitFSUB(SDNode *N) {
3868 SDValue N0 = N->getOperand(0);
3869 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003870 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3871 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003872 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003873
Dan Gohman7f321562007-06-25 16:23:39 +00003874 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003875 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003876 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003877 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003878 }
Dan Gohman7f321562007-06-25 16:23:39 +00003879
Nate Begemana0e221d2005-10-18 00:28:13 +00003880 // fold (fsub c1, c2) -> c1-c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003881 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003882 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohmana90c8e62009-01-23 19:10:37 +00003883 // fold (A-0) -> A
3884 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
3885 return N0;
Dan Gohman23ff1822007-07-02 15:48:56 +00003886 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003887 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Duncan Sands25cf2272008-11-24 14:53:14 +00003888 if (isNegatibleForFree(N1, LegalOperations))
3889 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman760f86f2009-01-22 21:58:43 +00003890 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
3891 return DAG.getNode(ISD::FNEG, VT, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003892 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003893 // fold (A-(-B)) -> A+B
Duncan Sands25cf2272008-11-24 14:53:14 +00003894 if (isNegatibleForFree(N1, LegalOperations))
Chris Lattner0254e702008-02-26 07:04:54 +00003895 return DAG.getNode(ISD::FADD, VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00003896 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00003897
Dan Gohman475871a2008-07-27 21:46:04 +00003898 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003899}
3900
Dan Gohman475871a2008-07-27 21:46:04 +00003901SDValue DAGCombiner::visitFMUL(SDNode *N) {
3902 SDValue N0 = N->getOperand(0);
3903 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003904 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3905 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003906 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003907
Dan Gohman7f321562007-06-25 16:23:39 +00003908 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003909 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003910 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003911 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003912 }
Dan Gohman7f321562007-06-25 16:23:39 +00003913
Nate Begeman11af4ea2005-10-17 20:40:11 +00003914 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003915 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003916 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003917 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003918 if (N0CFP && !N1CFP)
3919 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Dan Gohman760f86f2009-01-22 21:58:43 +00003920 // fold (A * 0) -> 0
3921 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
3922 return N1;
Nate Begeman11af4ea2005-10-17 20:40:11 +00003923 // fold (fmul X, 2.0) -> (fadd X, X)
3924 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3925 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003926 // fold (fmul X, -1.0) -> (fneg X)
3927 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman760f86f2009-01-22 21:58:43 +00003928 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
3929 return DAG.getNode(ISD::FNEG, VT, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003930
3931 // -X * -Y -> X*Y
Duncan Sands25cf2272008-11-24 14:53:14 +00003932 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
3933 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Chris Lattner29446522007-05-14 22:04:50 +00003934 // Both can be negated for free, check to see if at least one is cheaper
3935 // negated.
3936 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003937 return DAG.getNode(ISD::FMUL, VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00003938 GetNegatedExpression(N0, DAG, LegalOperations),
3939 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00003940 }
3941 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003942
3943 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3944 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003945 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003946 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3947 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3948
Dan Gohman475871a2008-07-27 21:46:04 +00003949 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003950}
3951
Dan Gohman475871a2008-07-27 21:46:04 +00003952SDValue DAGCombiner::visitFDIV(SDNode *N) {
3953 SDValue N0 = N->getOperand(0);
3954 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003955 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3956 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003957 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003958
Dan Gohman7f321562007-06-25 16:23:39 +00003959 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003960 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003961 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003962 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003963 }
Dan Gohman7f321562007-06-25 16:23:39 +00003964
Nate Begemana148d982006-01-18 22:35:16 +00003965 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003966 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003967 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003968
3969
3970 // -X / -Y -> X*Y
Duncan Sands25cf2272008-11-24 14:53:14 +00003971 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
3972 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Chris Lattner29446522007-05-14 22:04:50 +00003973 // Both can be negated for free, check to see if at least one is cheaper
3974 // negated.
3975 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003976 return DAG.getNode(ISD::FDIV, VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00003977 GetNegatedExpression(N0, DAG, LegalOperations),
3978 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00003979 }
3980 }
3981
Dan Gohman475871a2008-07-27 21:46:04 +00003982 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003983}
3984
Dan Gohman475871a2008-07-27 21:46:04 +00003985SDValue DAGCombiner::visitFREM(SDNode *N) {
3986 SDValue N0 = N->getOperand(0);
3987 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003988 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3989 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003990 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003991
Nate Begemana148d982006-01-18 22:35:16 +00003992 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003993 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003994 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003995
Dan Gohman475871a2008-07-27 21:46:04 +00003996 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003997}
3998
Dan Gohman475871a2008-07-27 21:46:04 +00003999SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
4000 SDValue N0 = N->getOperand(0);
4001 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00004002 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4003 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004004 MVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00004005
Dale Johannesendb44bf82007-10-16 23:38:29 +00004006 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00004007 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
4008
4009 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00004010 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00004011 // copysign(x, c1) -> fabs(x) iff ispos(c1)
4012 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman760f86f2009-01-22 21:58:43 +00004013 if (!V.isNegative()) {
4014 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
4015 return DAG.getNode(ISD::FABS, VT, N0);
4016 } else {
4017 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
4018 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
4019 }
Chris Lattner12d83032006-03-05 05:30:57 +00004020 }
4021
4022 // copysign(fabs(x), y) -> copysign(x, y)
4023 // copysign(fneg(x), y) -> copysign(x, y)
4024 // copysign(copysign(x,z), y) -> copysign(x, y)
4025 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
4026 N0.getOpcode() == ISD::FCOPYSIGN)
4027 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
4028
4029 // copysign(x, abs(y)) -> abs(x)
4030 if (N1.getOpcode() == ISD::FABS)
4031 return DAG.getNode(ISD::FABS, VT, N0);
4032
4033 // copysign(x, copysign(y,z)) -> copysign(x, z)
4034 if (N1.getOpcode() == ISD::FCOPYSIGN)
4035 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
4036
4037 // copysign(x, fp_extend(y)) -> copysign(x, y)
4038 // copysign(x, fp_round(y)) -> copysign(x, y)
4039 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
4040 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
4041
Dan Gohman475871a2008-07-27 21:46:04 +00004042 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00004043}
4044
4045
Chris Lattner01b3d732005-09-28 22:28:18 +00004046
Dan Gohman475871a2008-07-27 21:46:04 +00004047SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
4048 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00004049 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004050 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00004051 MVT OpVT = N0.getValueType();
4052
Nate Begeman1d4d4142005-09-01 00:19:25 +00004053 // fold (sint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00004054 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004055 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00004056
4057 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
4058 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004059 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
4060 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Chris Lattnercda88752008-06-26 00:16:49 +00004061 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
4062 if (DAG.SignBitIsZero(N0))
4063 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
4064 }
4065
4066
Dan Gohman475871a2008-07-27 21:46:04 +00004067 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004068}
4069
Dan Gohman475871a2008-07-27 21:46:04 +00004070SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
4071 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00004072 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004073 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00004074 MVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00004075
Nate Begeman1d4d4142005-09-01 00:19:25 +00004076 // fold (uint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00004077 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004078 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00004079
4080 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
4081 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004082 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
4083 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Chris Lattnercda88752008-06-26 00:16:49 +00004084 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
4085 if (DAG.SignBitIsZero(N0))
4086 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
4087 }
4088
Dan Gohman475871a2008-07-27 21:46:04 +00004089 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004090}
4091
Dan Gohman475871a2008-07-27 21:46:04 +00004092SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
4093 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004094 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004095 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004096
4097 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00004098 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00004099 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004100 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004101}
4102
Dan Gohman475871a2008-07-27 21:46:04 +00004103SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
4104 SDValue N0 = N->getOperand(0);
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_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00004109 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004110 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004111 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004112}
4113
Dan Gohman475871a2008-07-27 21:46:04 +00004114SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
4115 SDValue N0 = N->getOperand(0);
4116 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00004117 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004118 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004119
4120 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00004121 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner0bd48932008-01-17 07:00:52 +00004122 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00004123
4124 // fold (fp_round (fp_extend x)) -> x
4125 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
4126 return N0.getOperand(0);
4127
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00004128 // fold (fp_round (fp_round x)) -> (fp_round x)
4129 if (N0.getOpcode() == ISD::FP_ROUND) {
4130 // This is a value preserving truncation if both round's are.
4131 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004132 N0.getNode()->getConstantOperandVal(1) == 1;
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00004133 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
4134 DAG.getIntPtrConstant(IsTrunc));
4135 }
4136
Chris Lattner79dbea52006-03-13 06:26:26 +00004137 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00004138 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004139 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00004140 AddToWorkList(Tmp.getNode());
Chris Lattner79dbea52006-03-13 06:26:26 +00004141 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
4142 }
4143
Dan Gohman475871a2008-07-27 21:46:04 +00004144 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004145}
4146
Dan Gohman475871a2008-07-27 21:46:04 +00004147SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
4148 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004149 MVT VT = N->getValueType(0);
4150 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00004151 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004152
Nate Begeman1d4d4142005-09-01 00:19:25 +00004153 // fold (fp_round_inreg c1fp) -> c1fp
Duncan Sands25cf2272008-11-24 14:53:14 +00004154 if (N0CFP && (TLI.isTypeLegal(EVT) || !LegalTypes)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00004155 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00004156 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004157 }
Dan Gohman475871a2008-07-27 21:46:04 +00004158 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004159}
4160
Dan Gohman475871a2008-07-27 21:46:04 +00004161SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
4162 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004163 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004164 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004165
Chris Lattner5938bef2007-12-29 06:55:23 +00004166 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Roman Levenstein9cac5252008-04-16 16:15:27 +00004167 if (N->hasOneUse() &&
Dan Gohmane7852d02009-01-26 04:35:06 +00004168 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman475871a2008-07-27 21:46:04 +00004169 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004170
Nate Begeman1d4d4142005-09-01 00:19:25 +00004171 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00004172 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004173 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004174
4175 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
4176 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00004177 if (N0.getOpcode() == ISD::FP_ROUND
4178 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004179 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004180 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00004181 if (VT.bitsLT(In.getValueType()))
Chris Lattner0bd48932008-01-17 07:00:52 +00004182 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
4183 return DAG.getNode(ISD::FP_EXTEND, VT, In);
4184 }
4185
4186 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004187 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004188 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004189 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00004190 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004191 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004192 LN0->getBasePtr(), LN0->getSrcValue(),
4193 LN0->getSrcValueOffset(),
4194 N0.getValueType(),
4195 LN0->isVolatile(), LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00004196 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004197 CombineTo(N0.getNode(), DAG.getNode(ISD::FP_ROUND, N0.getValueType(),
4198 ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00004199 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004200 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00004201 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004202
Dan Gohman475871a2008-07-27 21:46:04 +00004203 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004204}
4205
Dan Gohman475871a2008-07-27 21:46:04 +00004206SDValue DAGCombiner::visitFNEG(SDNode *N) {
4207 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004208
Duncan Sands25cf2272008-11-24 14:53:14 +00004209 if (isNegatibleForFree(N0, LegalOperations))
4210 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohman23ff1822007-07-02 15:48:56 +00004211
Chris Lattner3bd39d42008-01-27 17:42:27 +00004212 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
4213 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004214 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004215 N0.getOperand(0).getValueType().isInteger() &&
4216 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004217 SDValue Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004218 MVT IntVT = Int.getValueType();
4219 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004220 Int = DAG.getNode(ISD::XOR, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004221 DAG.getConstant(IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004222 AddToWorkList(Int.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004223 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4224 }
4225 }
4226
Dan Gohman475871a2008-07-27 21:46:04 +00004227 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004228}
4229
Dan Gohman475871a2008-07-27 21:46:04 +00004230SDValue DAGCombiner::visitFABS(SDNode *N) {
4231 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004232 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004233 MVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00004234
Nate Begeman1d4d4142005-09-01 00:19:25 +00004235 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00004236 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004237 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004238 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004239 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00004240 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004241 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004242 // fold (fabs (fcopysign x, y)) -> (fabs x)
4243 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
4244 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
4245
Chris Lattner3bd39d42008-01-27 17:42:27 +00004246 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
4247 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004248 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004249 N0.getOperand(0).getValueType().isInteger() &&
4250 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004251 SDValue Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004252 MVT IntVT = Int.getValueType();
4253 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004254 Int = DAG.getNode(ISD::AND, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004255 DAG.getConstant(~IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004256 AddToWorkList(Int.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004257 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4258 }
4259 }
4260
Dan Gohman475871a2008-07-27 21:46:04 +00004261 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004262}
4263
Dan Gohman475871a2008-07-27 21:46:04 +00004264SDValue DAGCombiner::visitBRCOND(SDNode *N) {
4265 SDValue Chain = N->getOperand(0);
4266 SDValue N1 = N->getOperand(1);
4267 SDValue N2 = N->getOperand(2);
Nate Begeman44728a72005-09-19 22:34:01 +00004268 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4269
4270 // never taken branch, fold to chain
4271 if (N1C && N1C->isNullValue())
4272 return Chain;
4273 // unconditional branch
Dan Gohman002e5d02008-03-13 22:13:53 +00004274 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00004275 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004276 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
4277 // on the target.
4278 if (N1.getOpcode() == ISD::SETCC &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004279 TLI.isOperationLegalOrCustom(ISD::BR_CC, MVT::Other)) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004280 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
4281 N1.getOperand(0), N1.getOperand(1), N2);
4282 }
Dan Gohman475871a2008-07-27 21:46:04 +00004283 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004284}
4285
Chris Lattner3ea0b472005-10-05 06:47:48 +00004286// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
4287//
Dan Gohman475871a2008-07-27 21:46:04 +00004288SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00004289 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004290 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Chris Lattner3ea0b472005-10-05 06:47:48 +00004291
Duncan Sands8eab8a22008-06-09 11:32:28 +00004292 // Use SimplifySetCC to simplify SETCC's.
Duncan Sands5480c042009-01-01 15:52:00 +00004293 SDValue Simp = SimplifySetCC(TLI.getSetCCResultType(CondLHS.getValueType()),
Duncan Sands25cf2272008-11-24 14:53:14 +00004294 CondLHS, CondRHS, CC->get(), false);
Gabor Greifba36cb52008-08-28 21:40:38 +00004295 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00004296
Gabor Greifba36cb52008-08-28 21:40:38 +00004297 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.getNode());
Nate Begemane17daeb2005-10-05 21:43:42 +00004298
4299 // fold br_cc true, dest -> br dest (unconditional branch)
Dan Gohman002e5d02008-03-13 22:13:53 +00004300 if (SCCC && !SCCC->isNullValue())
Nate Begemane17daeb2005-10-05 21:43:42 +00004301 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
4302 N->getOperand(4));
4303 // fold br_cc false, dest -> unconditional fall through
4304 if (SCCC && SCCC->isNullValue())
4305 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00004306
Nate Begemane17daeb2005-10-05 21:43:42 +00004307 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00004308 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Nate Begemane17daeb2005-10-05 21:43:42 +00004309 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
4310 Simp.getOperand(2), Simp.getOperand(0),
4311 Simp.getOperand(1), N->getOperand(4));
Dan Gohman475871a2008-07-27 21:46:04 +00004312 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004313}
4314
Chris Lattner448f2192006-11-11 00:39:41 +00004315
Duncan Sandsec87aa82008-06-15 20:12:31 +00004316/// CombineToPreIndexedLoadStore - Try turning a load / store into a
4317/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00004318/// and it has other uses besides the load / store. After the
4319/// transformation, the new indexed load / store has effectively folded
4320/// the add / subtract in and all of its other uses are redirected to the
4321/// new load / store.
4322bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Duncan Sands25cf2272008-11-24 14:53:14 +00004323 if (!LegalOperations)
Chris Lattner448f2192006-11-11 00:39:41 +00004324 return false;
4325
4326 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004327 SDValue Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004328 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004329 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004330 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004331 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004332 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00004333 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00004334 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
4335 return false;
4336 Ptr = LD->getBasePtr();
4337 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004338 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004339 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004340 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004341 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
4342 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
4343 return false;
4344 Ptr = ST->getBasePtr();
4345 isLoad = false;
4346 } else
4347 return false;
4348
Chris Lattner9f1794e2006-11-11 00:56:29 +00004349 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4350 // out. There is no reason to make this a preinc/predec.
4351 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00004352 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004353 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004354
Chris Lattner9f1794e2006-11-11 00:56:29 +00004355 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00004356 SDValue BasePtr;
4357 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004358 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4359 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4360 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00004361 // Don't create a indexed load / store with zero offset.
4362 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004363 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004364 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004365
Chris Lattner41e53fd2006-11-11 01:00:15 +00004366 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00004367 // 1) The new base ptr is a frame index.
4368 // 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 +00004369 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00004370 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00004371 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00004372 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00004373
Chris Lattner41e53fd2006-11-11 01:00:15 +00004374 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4375 // (plus the implicit offset) to a register to preinc anyway.
4376 if (isa<FrameIndexSDNode>(BasePtr))
4377 return false;
4378
4379 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004380 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004381 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004382 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004383 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004384 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004385
Evan Chengc843abe2007-05-24 02:35:39 +00004386 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004387 bool RealUse = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00004388 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4389 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004390 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004391 if (Use == N)
4392 continue;
Evan Cheng917be682008-03-04 00:41:45 +00004393 if (Use->isPredecessorOf(N))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004394 return false;
4395
4396 if (!((Use->getOpcode() == ISD::LOAD &&
4397 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004398 (Use->getOpcode() == ISD::STORE &&
4399 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004400 RealUse = true;
4401 }
4402 if (!RealUse)
4403 return false;
4404
Dan Gohman475871a2008-07-27 21:46:04 +00004405 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004406 if (isLoad)
Dan Gohman475871a2008-07-27 21:46:04 +00004407 Result = DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004408 else
Dan Gohman475871a2008-07-27 21:46:04 +00004409 Result = DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004410 ++PreIndexedNodes;
4411 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004412 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004413 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004414 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004415 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004416 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004417 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004418 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004419 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004420 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004421 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004422 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004423 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004424 }
4425
Chris Lattner9f1794e2006-11-11 00:56:29 +00004426 // Finally, since the node is now dead, remove it from the graph.
4427 DAG.DeleteNode(N);
4428
4429 // Replace the uses of Ptr with uses of the updated base value.
4430 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004431 &DeadNodes);
Gabor Greifba36cb52008-08-28 21:40:38 +00004432 removeFromWorkList(Ptr.getNode());
4433 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00004434
4435 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004436}
4437
Duncan Sandsec87aa82008-06-15 20:12:31 +00004438/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00004439/// add / sub of the base pointer node into a post-indexed load / store.
4440/// The transformation folded the add / subtract into the new indexed
4441/// load / store effectively and all of its uses are redirected to the
4442/// new load / store.
4443bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Duncan Sands25cf2272008-11-24 14:53:14 +00004444 if (!LegalOperations)
Chris Lattner448f2192006-11-11 00:39:41 +00004445 return false;
4446
4447 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004448 SDValue Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004449 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004450 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004451 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004452 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004453 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004454 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4455 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4456 return false;
4457 Ptr = LD->getBasePtr();
4458 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004459 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004460 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004461 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004462 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4463 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4464 return false;
4465 Ptr = ST->getBasePtr();
4466 isLoad = false;
4467 } else
4468 return false;
4469
Gabor Greifba36cb52008-08-28 21:40:38 +00004470 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004471 return false;
4472
Gabor Greifba36cb52008-08-28 21:40:38 +00004473 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4474 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004475 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004476 if (Op == N ||
4477 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4478 continue;
4479
Dan Gohman475871a2008-07-27 21:46:04 +00004480 SDValue BasePtr;
4481 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004482 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4483 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4484 if (Ptr == Offset)
4485 std::swap(BasePtr, Offset);
4486 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00004487 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00004488 // Don't create a indexed load / store with zero offset.
4489 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004490 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004491 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004492
Chris Lattner9f1794e2006-11-11 00:56:29 +00004493 // Try turning it into a post-indexed load / store except when
4494 // 1) All uses are load / store ops that use it as base ptr.
4495 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4496 // nor a successor of N. Otherwise, if Op is folded that would
4497 // create a cycle.
4498
4499 // Check for #1.
4500 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00004501 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
4502 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00004503 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00004504 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00004505 continue;
4506
Chris Lattner9f1794e2006-11-11 00:56:29 +00004507 // If all the uses are load / store addresses, then don't do the
4508 // transformation.
4509 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4510 bool RealUse = false;
4511 for (SDNode::use_iterator III = Use->use_begin(),
4512 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00004513 SDNode *UseUse = *III;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004514 if (!((UseUse->getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004515 cast<LoadSDNode>(UseUse)->getBasePtr().getNode() == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004516 (UseUse->getOpcode() == ISD::STORE &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004517 cast<StoreSDNode>(UseUse)->getBasePtr().getNode() == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004518 RealUse = true;
4519 }
Chris Lattner448f2192006-11-11 00:39:41 +00004520
Chris Lattner9f1794e2006-11-11 00:56:29 +00004521 if (!RealUse) {
4522 TryNext = true;
4523 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004524 }
4525 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004526 }
4527 if (TryNext)
4528 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004529
Chris Lattner9f1794e2006-11-11 00:56:29 +00004530 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00004531 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004532 SDValue Result = isLoad
4533 ? DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM)
4534 : DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004535 ++PostIndexedNodes;
4536 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004537 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004538 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004539 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004540 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004541 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004542 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004543 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004544 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004545 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004546 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004547 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004548 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00004549 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004550
Chris Lattner9f1794e2006-11-11 00:56:29 +00004551 // Finally, since the node is now dead, remove it from the graph.
4552 DAG.DeleteNode(N);
4553
4554 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00004555 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Chris Lattner9f1794e2006-11-11 00:56:29 +00004556 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004557 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004558 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004559 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004560 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004561 }
4562 }
4563 }
4564 return false;
4565}
4566
Chris Lattner00161a62008-01-25 07:20:16 +00004567/// InferAlignment - If we can infer some alignment information from this
4568/// pointer, return it.
Dan Gohman475871a2008-07-27 21:46:04 +00004569static unsigned InferAlignment(SDValue Ptr, SelectionDAG &DAG) {
Chris Lattner00161a62008-01-25 07:20:16 +00004570 // If this is a direct reference to a stack slot, use information about the
4571 // stack slot's alignment.
Chris Lattner1329cb82008-01-26 19:45:50 +00004572 int FrameIdx = 1 << 31;
4573 int64_t FrameOffset = 0;
Chris Lattner00161a62008-01-25 07:20:16 +00004574 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1329cb82008-01-26 19:45:50 +00004575 FrameIdx = FI->getIndex();
4576 } else if (Ptr.getOpcode() == ISD::ADD &&
4577 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4578 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4579 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4580 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner00161a62008-01-25 07:20:16 +00004581 }
Chris Lattner1329cb82008-01-26 19:45:50 +00004582
4583 if (FrameIdx != (1 << 31)) {
4584 // FIXME: Handle FI+CST.
4585 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4586 if (MFI.isFixedObjectIndex(FrameIdx)) {
Dan Gohman8cea8ff2008-08-11 18:27:03 +00004587 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx) + FrameOffset;
Chris Lattner1329cb82008-01-26 19:45:50 +00004588
4589 // The alignment of the frame index can be determined from its offset from
4590 // the incoming frame position. If the frame object is at offset 32 and
4591 // the stack is guaranteed to be 16-byte aligned, then we know that the
4592 // object is 16-byte aligned.
4593 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4594 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4595
4596 // Finally, the frame object itself may have a known alignment. Factor
4597 // the alignment + offset into a new alignment. For example, if we know
4598 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4599 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4600 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4601 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4602 FrameOffset);
4603 return std::max(Align, FIInfoAlign);
4604 }
4605 }
Chris Lattner00161a62008-01-25 07:20:16 +00004606
4607 return 0;
4608}
Chris Lattner448f2192006-11-11 00:39:41 +00004609
Dan Gohman475871a2008-07-27 21:46:04 +00004610SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004611 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004612 SDValue Chain = LD->getChain();
4613 SDValue Ptr = LD->getBasePtr();
Chris Lattner00161a62008-01-25 07:20:16 +00004614
4615 // Try to infer better alignment information than the load already has.
Dan Gohmana2676512008-08-20 16:30:28 +00004616 if (!Fast && LD->isUnindexed()) {
Chris Lattner00161a62008-01-25 07:20:16 +00004617 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4618 if (Align > LD->getAlignment())
4619 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4620 Chain, Ptr, LD->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004621 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004622 LD->isVolatile(), Align);
4623 }
4624 }
4625
Evan Cheng45a7ca92007-05-01 00:38:21 +00004626
4627 // If load is not volatile and there are no uses of the loaded value (and
4628 // the updated indexed value in case of indexed loads), change uses of the
4629 // chain value into uses of the chain input (i.e. delete the dead load).
4630 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00004631 if (N->getValueType(1) == MVT::Other) {
4632 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004633 if (N->hasNUsesOfValue(0, 0)) {
4634 // It's not safe to use the two value CombineTo variant here. e.g.
4635 // v1, chain2 = load chain1, loc
4636 // v2, chain3 = load chain2, loc
4637 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004638 // Now we replace use of chain2 with chain1. This makes the second load
4639 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004640 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004641 DOUT << "\nWith chain: "; DEBUG(Chain.getNode()->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004642 DOUT << "\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004643 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004644 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain, &DeadNodes);
Chris Lattner125991a2008-01-24 07:57:06 +00004645 if (N->use_empty()) {
4646 removeFromWorkList(N);
4647 DAG.DeleteNode(N);
4648 }
Dan Gohman475871a2008-07-27 21:46:04 +00004649 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00004650 }
Evan Cheng498f5592007-05-01 08:53:39 +00004651 } else {
4652 // Indexed loads.
4653 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4654 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004655 SDValue Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
Evan Cheng02c42852008-01-16 23:11:54 +00004656 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004657 DOUT << "\nWith: "; DEBUG(Undef.getNode()->dump(&DAG));
Evan Cheng02c42852008-01-16 23:11:54 +00004658 DOUT << " and 2 other values\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004659 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004660 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef, &DeadNodes);
4661 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Chris Lattner4626b252008-01-17 07:20:38 +00004662 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004663 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004664 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004665 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00004666 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004667 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004668 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004669 }
4670 }
Chris Lattner01a22022005-10-10 22:04:48 +00004671
4672 // If this load is directly stored, replace the load value with the stored
4673 // value.
4674 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004675 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohmanb061c4b2008-03-31 20:32:52 +00004676 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
4677 !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004678 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004679 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4680 if (PrevST->getBasePtr() == Ptr &&
4681 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004682 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004683 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004684 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004685
Jim Laskey7ca56af2006-10-11 13:47:09 +00004686 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004687 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00004688 SDValue BetterChain = FindBetterChain(N, Chain);
Jim Laskey279f0532006-09-25 16:29:54 +00004689
Jim Laskey6ff23e52006-10-04 16:53:27 +00004690 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004691 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00004692 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004693
Jim Laskey279f0532006-09-25 16:29:54 +00004694 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004695 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4696 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004697 LD->getSrcValue(), LD->getSrcValueOffset(),
4698 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004699 } else {
4700 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4701 LD->getValueType(0),
4702 BetterChain, Ptr, LD->getSrcValue(),
4703 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004704 LD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004705 LD->isVolatile(),
4706 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004707 }
Jim Laskey279f0532006-09-25 16:29:54 +00004708
Jim Laskey6ff23e52006-10-04 16:53:27 +00004709 // Create token factor to keep old chain connected.
Dan Gohman475871a2008-07-27 21:46:04 +00004710 SDValue Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
Jim Laskey288af5e2006-09-25 19:32:58 +00004711 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004712
Jim Laskey274062c2006-10-13 23:32:28 +00004713 // Replace uses with load result and token factor. Don't add users
4714 // to work list.
4715 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004716 }
4717 }
4718
Evan Cheng7fc033a2006-11-03 03:06:21 +00004719 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004720 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00004721 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00004722
Dan Gohman475871a2008-07-27 21:46:04 +00004723 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00004724}
4725
Chris Lattner07649d92008-01-08 23:08:06 +00004726
Dan Gohman475871a2008-07-27 21:46:04 +00004727SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004728 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004729 SDValue Chain = ST->getChain();
4730 SDValue Value = ST->getValue();
4731 SDValue Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004732
Chris Lattner00161a62008-01-25 07:20:16 +00004733 // Try to infer better alignment information than the store already has.
Dan Gohmana2676512008-08-20 16:30:28 +00004734 if (!Fast && ST->isUnindexed()) {
Chris Lattner00161a62008-01-25 07:20:16 +00004735 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4736 if (Align > ST->getAlignment())
4737 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004738 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004739 ST->isVolatile(), Align);
4740 }
4741 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004742
Evan Cheng59d5b682007-05-07 21:27:48 +00004743 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004744 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004745 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004746 ST->isUnindexed()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004747 unsigned Align = ST->getAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004748 MVT SVT = Value.getOperand(0).getValueType();
Dan Gohman6448d912008-09-04 15:39:15 +00004749 unsigned OrigAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004750 getABITypeAlignment(SVT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004751 if (Align <= OrigAlign &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004752 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004753 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Evan Cheng59d5b682007-05-07 21:27:48 +00004754 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohman77455612008-06-28 00:45:22 +00004755 ST->getSrcValueOffset(), ST->isVolatile(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00004756 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004757
Nate Begeman2cbba892006-12-11 02:23:46 +00004758 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004759 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004760 // NOTE: If the original store is volatile, this transform must not increase
4761 // the number of stores. For example, on x86-32 an f64 can be stored in one
4762 // processor operation but an i64 (which is not legal) requires two. So the
4763 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00004764 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00004765 SDValue Tmp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004766 switch (CFP->getValueType(0).getSimpleVT()) {
Chris Lattner62be1a72006-12-12 04:16:14 +00004767 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004768 case MVT::f80: // We don't do this for these yet.
4769 case MVT::f128:
4770 case MVT::ppcf128:
4771 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004772 case MVT::f32:
Duncan Sands25cf2272008-11-24 14:53:14 +00004773 if (((TLI.isTypeLegal(MVT::i32) || !LegalTypes) && !LegalOperations &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004774 !ST->isVolatile()) ||
4775 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004776 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Dale Johannesen7111b022008-10-09 18:53:47 +00004777 bitcastToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004778 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004779 ST->getSrcValueOffset(), ST->isVolatile(),
4780 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004781 }
4782 break;
4783 case MVT::f64:
Duncan Sands25cf2272008-11-24 14:53:14 +00004784 if (((TLI.isTypeLegal(MVT::i64) || !LegalTypes) && !LegalOperations &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004785 !ST->isVolatile()) ||
4786 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen7111b022008-10-09 18:53:47 +00004787 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004788 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004789 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004790 ST->getSrcValueOffset(), ST->isVolatile(),
4791 ST->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004792 } else if (!ST->isVolatile() &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004793 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004794 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004795 // argument passing. Since this is so common, custom legalize the
4796 // 64-bit integer store into two 32-bit stores.
Dale Johannesen7111b022008-10-09 18:53:47 +00004797 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00004798 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4799 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00004800 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00004801
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004802 int SVOffset = ST->getSrcValueOffset();
4803 unsigned Alignment = ST->getAlignment();
4804 bool isVolatile = ST->isVolatile();
4805
Dan Gohman475871a2008-07-27 21:46:04 +00004806 SDValue St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004807 ST->getSrcValueOffset(),
4808 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004809 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4810 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004811 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004812 Alignment = MinAlign(Alignment, 4U);
Dan Gohman475871a2008-07-27 21:46:04 +00004813 SDValue St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004814 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004815 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4816 }
4817 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004818 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004819 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004820 }
4821
Jim Laskey279f0532006-09-25 16:29:54 +00004822 if (CombinerAA) {
4823 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00004824 SDValue BetterChain = FindBetterChain(N, Chain);
Jim Laskey279f0532006-09-25 16:29:54 +00004825
Jim Laskey6ff23e52006-10-04 16:53:27 +00004826 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004827 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004828 // Replace the chain to avoid dependency.
Dan Gohman475871a2008-07-27 21:46:04 +00004829 SDValue ReplStore;
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004830 if (ST->isTruncatingStore()) {
4831 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004832 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004833 ST->getMemoryVT(),
Chris Lattner4626b252008-01-17 07:20:38 +00004834 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004835 } else {
4836 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004837 ST->getSrcValue(), ST->getSrcValueOffset(),
4838 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004839 }
4840
Jim Laskey279f0532006-09-25 16:29:54 +00004841 // Create token to keep both nodes around.
Dan Gohman475871a2008-07-27 21:46:04 +00004842 SDValue Token =
Jim Laskey274062c2006-10-13 23:32:28 +00004843 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4844
4845 // Don't add users to work list.
4846 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004847 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004848 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004849
Evan Cheng33dbedc2006-11-05 09:31:14 +00004850 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004851 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00004852 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00004853
Chris Lattner3c872852007-12-29 06:26:16 +00004854 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00004855 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004856 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00004857 // See if we can simplify the input to this truncstore with knowledge that
4858 // only the low bits are being used. For example:
4859 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Dan Gohman475871a2008-07-27 21:46:04 +00004860 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004861 GetDemandedBits(Value,
4862 APInt::getLowBitsSet(Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004863 ST->getMemoryVT().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00004864 AddToWorkList(Value.getNode());
4865 if (Shorter.getNode())
Chris Lattner2b4c2792007-10-13 06:35:54 +00004866 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004867 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner2b4c2792007-10-13 06:35:54 +00004868 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004869
4870 // Otherwise, see if we can simplify the operation with
4871 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00004872 if (SimplifyDemandedBits(Value,
4873 APInt::getLowBitsSet(
4874 Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004875 ST->getMemoryVT().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00004876 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004877 }
4878
Chris Lattner3c872852007-12-29 06:26:16 +00004879 // If this is a load followed by a store to the same location, then the store
4880 // is dead/noop.
4881 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004882 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004883 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00004884 // There can't be any side effects between the load and store, such as
4885 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00004886 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00004887 // The store is dead, remove it.
4888 return Chain;
4889 }
4890 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004891
Chris Lattnerddf89562008-01-17 19:59:44 +00004892 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4893 // truncating store. We can do this even if this is already a truncstore.
4894 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00004895 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004896 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004897 ST->getMemoryVT())) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004898 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004899 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnerddf89562008-01-17 19:59:44 +00004900 ST->isVolatile(), ST->getAlignment());
4901 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004902
Dan Gohman475871a2008-07-27 21:46:04 +00004903 return SDValue();
Chris Lattner87514ca2005-10-10 22:31:19 +00004904}
4905
Dan Gohman475871a2008-07-27 21:46:04 +00004906SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4907 SDValue InVec = N->getOperand(0);
4908 SDValue InVal = N->getOperand(1);
4909 SDValue EltNo = N->getOperand(2);
Chris Lattnerca242442006-03-19 01:27:56 +00004910
4911 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4912 // vector with the inserted element.
4913 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004914 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Gabor Greif12632d22008-08-30 19:29:20 +00004915 SmallVector<SDValue, 8> Ops(InVec.getNode()->op_begin(),
4916 InVec.getNode()->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004917 if (Elt < Ops.size())
4918 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004919 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4920 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004921 }
4922
Dan Gohman475871a2008-07-27 21:46:04 +00004923 return SDValue();
Chris Lattnerca242442006-03-19 01:27:56 +00004924}
4925
Dan Gohman475871a2008-07-27 21:46:04 +00004926SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00004927 // (vextract (scalar_to_vector val, 0) -> val
4928 SDValue InVec = N->getOperand(0);
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00004929
Mon P Wange3bc6ae2009-01-18 06:43:40 +00004930 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR)
4931 return InVec.getOperand(0);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004932
4933 // Perform only after legalization to ensure build_vector / vector_shuffle
4934 // optimizations have already been done.
Duncan Sands25cf2272008-11-24 14:53:14 +00004935 if (!LegalOperations) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004936
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00004937 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
4938 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
4939 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Mon P Wange3bc6ae2009-01-18 06:43:40 +00004940 SDValue EltNo = N->getOperand(1);
Evan Cheng513da432007-10-06 08:19:55 +00004941
Evan Cheng513da432007-10-06 08:19:55 +00004942 if (isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004943 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00004944 bool NewLoad = false;
Mon P Wanga60b5232008-12-11 00:26:16 +00004945 bool BCNumEltsChanged = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004946 MVT VT = InVec.getValueType();
4947 MVT EVT = VT.getVectorElementType();
4948 MVT LVT = EVT;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004949 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004950 MVT BCVT = InVec.getOperand(0).getValueType();
Mon P Wanga60b5232008-12-11 00:26:16 +00004951 if (!BCVT.isVector() || EVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004952 return SDValue();
Mon P Wanga60b5232008-12-11 00:26:16 +00004953 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
4954 BCNumEltsChanged = true;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004955 InVec = InVec.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004956 EVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004957 NewLoad = true;
4958 }
Evan Cheng513da432007-10-06 08:19:55 +00004959
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004960 LoadSDNode *LN0 = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004961 if (ISD::isNormalLoad(InVec.getNode()))
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004962 LN0 = cast<LoadSDNode>(InVec);
4963 else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4964 InVec.getOperand(0).getValueType() == EVT &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004965 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004966 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4967 } else if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE) {
4968 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
4969 // =>
4970 // (load $addr+1*size)
Mon P Wanga60b5232008-12-11 00:26:16 +00004971
4972 // If the bit convert changed the number of elements, it is unsafe
4973 // to examine the mask.
4974 if (BCNumEltsChanged)
4975 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004976 unsigned Idx = cast<ConstantSDNode>(InVec.getOperand(2).
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004977 getOperand(Elt))->getZExtValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004978 unsigned NumElems = InVec.getOperand(2).getNumOperands();
4979 InVec = (Idx < NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
4980 if (InVec.getOpcode() == ISD::BIT_CONVERT)
4981 InVec = InVec.getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00004982 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004983 LN0 = cast<LoadSDNode>(InVec);
4984 Elt = (Idx < NumElems) ? Idx : Idx - NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00004985 }
4986 }
Duncan Sandsec87aa82008-06-15 20:12:31 +00004987 if (!LN0 || !LN0->hasOneUse() || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00004988 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004989
4990 unsigned Align = LN0->getAlignment();
4991 if (NewLoad) {
4992 // Check the resultant load doesn't need a higher alignment than the
4993 // original load.
Dan Gohman6448d912008-09-04 15:39:15 +00004994 unsigned NewAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004995 getABITypeAlignment(LVT.getTypeForMVT());
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004996 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00004997 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004998 Align = NewAlign;
4999 }
5000
Dan Gohman475871a2008-07-27 21:46:04 +00005001 SDValue NewPtr = LN0->getBasePtr();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005002 if (Elt) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005003 unsigned PtrOff = LVT.getSizeInBits() * Elt / 8;
5004 MVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005005 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00005006 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005007 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
5008 DAG.getConstant(PtrOff, PtrType));
5009 }
5010 return DAG.getLoad(LVT, LN0->getChain(), NewPtr,
5011 LN0->getSrcValue(), LN0->getSrcValueOffset(),
5012 LN0->isVolatile(), Align);
Evan Cheng513da432007-10-06 08:19:55 +00005013 }
Dan Gohman475871a2008-07-27 21:46:04 +00005014 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00005015}
5016
5017
Dan Gohman475871a2008-07-27 21:46:04 +00005018SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005019 unsigned NumInScalars = N->getNumOperands();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005020 MVT VT = N->getValueType(0);
5021 unsigned NumElts = VT.getVectorNumElements();
5022 MVT EltType = VT.getVectorElementType();
Chris Lattnerca242442006-03-19 01:27:56 +00005023
Dan Gohman7f321562007-06-25 16:23:39 +00005024 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
5025 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
5026 // at most two distinct vectors, turn this into a shuffle node.
Dan Gohman475871a2008-07-27 21:46:04 +00005027 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00005028 for (unsigned i = 0; i != NumInScalars; ++i) {
5029 // Ignore undef inputs.
5030 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5031
Dan Gohman7f321562007-06-25 16:23:39 +00005032 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00005033 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00005034 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00005035 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00005036 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005037 break;
5038 }
5039
Dan Gohman7f321562007-06-25 16:23:39 +00005040 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00005041 // we can't make a shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00005042 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005043 if (ExtractedFromVec.getValueType() != VT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005044 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005045 break;
5046 }
5047
5048 // Otherwise, remember this. We allow up to two distinct input vectors.
5049 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
5050 continue;
5051
Gabor Greifba36cb52008-08-28 21:40:38 +00005052 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00005053 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00005054 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00005055 VecIn2 = ExtractedFromVec;
5056 } else {
5057 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00005058 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005059 break;
5060 }
5061 }
5062
5063 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00005064 if (VecIn1.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005065 SmallVector<SDValue, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00005066 for (unsigned i = 0; i != NumInScalars; ++i) {
5067 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00005068 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00005069 continue;
5070 }
5071
Dan Gohman475871a2008-07-27 21:46:04 +00005072 SDValue Extract = N->getOperand(i);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005073
5074 // If extracting from the first vector, just use the index directly.
5075 if (Extract.getOperand(0) == VecIn1) {
5076 BuildVecIndices.push_back(Extract.getOperand(1));
5077 continue;
5078 }
5079
5080 // Otherwise, use InIdx + VecSize
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005081 unsigned Idx =
5082 cast<ConstantSDNode>(Extract.getOperand(1))->getZExtValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00005083 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Chris Lattnerd7648c82006-03-28 20:28:38 +00005084 }
5085
5086 // Add count and size info.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005087 MVT BuildVecVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Duncan Sands25cf2272008-11-24 14:53:14 +00005088 if (!TLI.isTypeLegal(BuildVecVT) && LegalTypes)
5089 return SDValue();
5090
Dan Gohman7f321562007-06-25 16:23:39 +00005091 // Return the new VECTOR_SHUFFLE node.
Dan Gohman475871a2008-07-27 21:46:04 +00005092 SDValue Ops[5];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005093 Ops[0] = VecIn1;
Gabor Greifba36cb52008-08-28 21:40:38 +00005094 if (VecIn2.getNode()) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005095 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00005096 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00005097 // Use an undef build_vector as input for the second operand.
Dan Gohman475871a2008-07-27 21:46:04 +00005098 std::vector<SDValue> UnOps(NumInScalars,
Chris Lattnercef896e2006-03-28 22:19:47 +00005099 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00005100 EltType));
5101 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005102 &UnOps[0], UnOps.size());
Gabor Greifba36cb52008-08-28 21:40:38 +00005103 AddToWorkList(Ops[1].getNode());
Chris Lattnercef896e2006-03-28 22:19:47 +00005104 }
Dan Gohman7f321562007-06-25 16:23:39 +00005105 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005106 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00005107 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005108 }
5109
Dan Gohman475871a2008-07-27 21:46:04 +00005110 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00005111}
5112
Dan Gohman475871a2008-07-27 21:46:04 +00005113SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005114 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
5115 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
5116 // inputs come from at most two distinct vectors, turn this into a shuffle
5117 // node.
5118
5119 // If we only have one input vector, we don't need to do any concatenation.
5120 if (N->getNumOperands() == 1) {
5121 return N->getOperand(0);
5122 }
5123
Dan Gohman475871a2008-07-27 21:46:04 +00005124 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00005125}
5126
Dan Gohman475871a2008-07-27 21:46:04 +00005127SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
5128 SDValue ShufMask = N->getOperand(2);
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005129 unsigned NumElts = ShufMask.getNumOperands();
5130
Mon P Wangaeb06d22008-11-10 04:46:22 +00005131 SDValue N0 = N->getOperand(0);
5132 SDValue N1 = N->getOperand(1);
5133
5134 assert(N0.getValueType().getVectorNumElements() == NumElts &&
5135 "Vector shuffle must be normalized in DAG");
5136
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005137 // If the shuffle mask is an identity operation on the LHS, return the LHS.
5138 bool isIdentity = true;
5139 for (unsigned i = 0; i != NumElts; ++i) {
5140 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005141 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() != i) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005142 isIdentity = false;
5143 break;
5144 }
5145 }
5146 if (isIdentity) return N->getOperand(0);
5147
5148 // If the shuffle mask is an identity operation on the RHS, return the RHS.
5149 isIdentity = true;
5150 for (unsigned i = 0; i != NumElts; ++i) {
5151 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005152 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() !=
5153 i+NumElts) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005154 isIdentity = false;
5155 break;
5156 }
5157 }
5158 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00005159
5160 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
5161 // needed at all.
5162 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00005163 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00005164 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00005165 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00005166 for (unsigned i = 0; i != NumElts; ++i)
5167 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005168 unsigned Idx=cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue();
Evan Chenge7bec0d2006-07-20 22:44:41 +00005169 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00005170 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00005171 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00005172 BaseIdx = Idx;
5173 } else {
5174 if (BaseIdx != Idx)
5175 isSplat = false;
5176 if (VecNum != V) {
5177 isUnary = false;
5178 break;
5179 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00005180 }
5181 }
5182
Evan Chenge7bec0d2006-07-20 22:44:41 +00005183 // Normalize unary shuffle so the RHS is undef.
5184 if (isUnary && VecNum == 1)
5185 std::swap(N0, N1);
5186
Evan Cheng917ec982006-07-21 08:25:53 +00005187 // If it is a splat, check if the argument vector is a build_vector with
5188 // all scalar elements the same.
5189 if (isSplat) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005190 SDNode *V = N0.getNode();
Evan Cheng917ec982006-07-21 08:25:53 +00005191
Dan Gohman7f321562007-06-25 16:23:39 +00005192 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00005193 // not the number of vector elements, look through it. Be careful not to
5194 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00005195 if (V->getOpcode() == ISD::BIT_CONVERT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005196 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00005197 if (ConvInput.getValueType().isVector() &&
5198 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +00005199 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +00005200 }
5201
Dan Gohman7f321562007-06-25 16:23:39 +00005202 if (V->getOpcode() == ISD::BUILD_VECTOR) {
5203 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00005204 if (NumElems > BaseIdx) {
Dan Gohman475871a2008-07-27 21:46:04 +00005205 SDValue Base;
Evan Cheng917ec982006-07-21 08:25:53 +00005206 bool AllSame = true;
5207 for (unsigned i = 0; i != NumElems; ++i) {
5208 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
5209 Base = V->getOperand(i);
5210 break;
5211 }
5212 }
5213 // Splat of <u, u, u, u>, return <u, u, u, u>
Gabor Greifba36cb52008-08-28 21:40:38 +00005214 if (!Base.getNode())
Evan Cheng917ec982006-07-21 08:25:53 +00005215 return N0;
5216 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00005217 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00005218 AllSame = false;
5219 break;
5220 }
5221 }
5222 // Splat of <x, x, x, x>, return <x, x, x, x>
5223 if (AllSame)
5224 return N0;
5225 }
5226 }
5227 }
5228
Evan Chenge7bec0d2006-07-20 22:44:41 +00005229 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
5230 // into an undef.
5231 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00005232 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
5233 // first operand.
Dan Gohman475871a2008-07-27 21:46:04 +00005234 SmallVector<SDValue, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00005235 for (unsigned i = 0; i != NumElts; ++i) {
5236 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005237 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() <
5238 NumElts) {
Chris Lattner17614ea2006-04-08 05:34:25 +00005239 MappedOps.push_back(ShufMask.getOperand(i));
5240 } else {
5241 unsigned NewIdx =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005242 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() -
5243 NumElts;
Duncan Sandsd038e042008-07-21 10:20:31 +00005244 MappedOps.push_back(DAG.getConstant(NewIdx,
5245 ShufMask.getOperand(i).getValueType()));
Chris Lattner17614ea2006-04-08 05:34:25 +00005246 }
5247 }
Dan Gohman7f321562007-06-25 16:23:39 +00005248 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005249 &MappedOps[0], MappedOps.size());
Gabor Greifba36cb52008-08-28 21:40:38 +00005250 AddToWorkList(ShufMask.getNode());
Dan Gohman7f321562007-06-25 16:23:39 +00005251 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
5252 N0,
5253 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
5254 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00005255 }
Dan Gohman7f321562007-06-25 16:23:39 +00005256
Dan Gohman475871a2008-07-27 21:46:04 +00005257 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005258}
5259
Evan Cheng44f1f092006-04-20 08:56:16 +00005260/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00005261/// an AND to a vector_shuffle with the destination vector and a zero vector.
5262/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00005263/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00005264SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
5265 SDValue LHS = N->getOperand(0);
5266 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00005267 if (N->getOpcode() == ISD::AND) {
5268 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00005269 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005270 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00005271 std::vector<SDValue> IdxOps;
Evan Cheng44f1f092006-04-20 08:56:16 +00005272 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00005273 unsigned NumElts = NumOps;
Evan Cheng44f1f092006-04-20 08:56:16 +00005274 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005275 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00005276 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00005277 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005278 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Duncan Sands94989ac2008-10-19 14:58:05 +00005279 IdxOps.push_back(DAG.getIntPtrConstant(i));
Evan Cheng44f1f092006-04-20 08:56:16 +00005280 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Duncan Sands94989ac2008-10-19 14:58:05 +00005281 IdxOps.push_back(DAG.getIntPtrConstant(NumElts));
Evan Cheng44f1f092006-04-20 08:56:16 +00005282 else
Dan Gohman475871a2008-07-27 21:46:04 +00005283 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005284 }
5285
5286 // Let's see if the target supports this vector_shuffle.
Duncan Sands94989ac2008-10-19 14:58:05 +00005287 if (!TLI.isVectorClearMaskLegal(IdxOps, TLI.getPointerTy(), DAG))
Dan Gohman475871a2008-07-27 21:46:04 +00005288 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005289
Dan Gohman7f321562007-06-25 16:23:39 +00005290 // Return the new VECTOR_SHUFFLE node.
Duncan Sands94989ac2008-10-19 14:58:05 +00005291 MVT EVT = RHS.getValueType().getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005292 MVT VT = MVT::getVectorVT(EVT, NumElts);
Evan Cheng3eb57d52008-11-05 06:04:18 +00005293 MVT MaskVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Dan Gohman475871a2008-07-27 21:46:04 +00005294 std::vector<SDValue> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005295 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00005296 Ops.push_back(LHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00005297 AddToWorkList(LHS.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00005298 std::vector<SDValue> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00005299 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005300 &ZeroOps[0], ZeroOps.size()));
Evan Cheng3eb57d52008-11-05 06:04:18 +00005301 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005302 &IdxOps[0], IdxOps.size()));
Dan Gohman475871a2008-07-27 21:46:04 +00005303 SDValue Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005304 &Ops[0], Ops.size());
Dan Gohman7a9a5af2008-07-16 16:13:58 +00005305 if (VT != N->getValueType(0))
5306 Result = DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00005307 return Result;
5308 }
5309 }
Dan Gohman475871a2008-07-27 21:46:04 +00005310 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005311}
5312
Dan Gohman7f321562007-06-25 16:23:39 +00005313/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00005314SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005315 // After legalize, the target may be depending on adds and other
5316 // binary ops to provide legal ways to construct constants or other
5317 // things. Simplifying them may result in a loss of legality.
Duncan Sands25cf2272008-11-24 14:53:14 +00005318 if (LegalOperations) return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00005319
Duncan Sands83ec4b62008-06-06 12:08:01 +00005320 MVT VT = N->getValueType(0);
5321 assert(VT.isVector() && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00005322
Duncan Sands83ec4b62008-06-06 12:08:01 +00005323 MVT EltType = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00005324 SDValue LHS = N->getOperand(0);
5325 SDValue RHS = N->getOperand(1);
5326 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005327 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +00005328
Dan Gohman7f321562007-06-25 16:23:39 +00005329 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00005330 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00005331 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
5332 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00005333 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005334 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005335 SDValue LHSOp = LHS.getOperand(i);
5336 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00005337 // If these two elements can't be folded, bail out.
5338 if ((LHSOp.getOpcode() != ISD::UNDEF &&
5339 LHSOp.getOpcode() != ISD::Constant &&
5340 LHSOp.getOpcode() != ISD::ConstantFP) ||
5341 (RHSOp.getOpcode() != ISD::UNDEF &&
5342 RHSOp.getOpcode() != ISD::Constant &&
5343 RHSOp.getOpcode() != ISD::ConstantFP))
5344 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00005345 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00005346 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
5347 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00005348 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005349 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +00005350 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005351 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00005352 break;
5353 }
Dan Gohman7f321562007-06-25 16:23:39 +00005354 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Gabor Greifba36cb52008-08-28 21:40:38 +00005355 AddToWorkList(Ops.back().getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +00005356 assert((Ops.back().getOpcode() == ISD::UNDEF ||
5357 Ops.back().getOpcode() == ISD::Constant ||
5358 Ops.back().getOpcode() == ISD::ConstantFP) &&
5359 "Scalar binop didn't fold!");
5360 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005361
Dan Gohman7f321562007-06-25 16:23:39 +00005362 if (Ops.size() == LHS.getNumOperands()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005363 MVT VT = LHS.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00005364 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005365 }
Chris Lattneredab1b92006-04-02 03:25:57 +00005366 }
5367
Dan Gohman475871a2008-07-27 21:46:04 +00005368 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00005369}
5370
Dan Gohman475871a2008-07-27 21:46:04 +00005371SDValue DAGCombiner::SimplifySelect(SDValue N0, SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00005372 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
5373
Dan Gohman475871a2008-07-27 21:46:04 +00005374 SDValue SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00005375 cast<CondCodeSDNode>(N0.getOperand(2))->get());
5376 // If we got a simplified select_cc node back from SimplifySelectCC, then
5377 // break it down into a new SETCC node, and a new SELECT node, and then return
5378 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +00005379 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005380 // Check to see if we got a select_cc back (to turn into setcc/select).
5381 // Otherwise, just return whatever node we got back, like fabs.
5382 if (SCC.getOpcode() == ISD::SELECT_CC) {
Dan Gohman475871a2008-07-27 21:46:04 +00005383 SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
Nate Begemanf845b452005-10-08 00:29:44 +00005384 SCC.getOperand(0), SCC.getOperand(1),
5385 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +00005386 AddToWorkList(SETCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005387 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
5388 SCC.getOperand(3), SETCC);
5389 }
5390 return SCC;
5391 }
Dan Gohman475871a2008-07-27 21:46:04 +00005392 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005393}
5394
Chris Lattner40c62d52005-10-18 06:04:22 +00005395/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
5396/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00005397/// select. Callers of this should assume that TheSelect is deleted if this
5398/// returns true. As such, they should return the appropriate thing (e.g. the
5399/// node) back to the top-level of the DAG combiner loop to avoid it being
5400/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00005401///
Dan Gohman475871a2008-07-27 21:46:04 +00005402bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
5403 SDValue RHS) {
Chris Lattner40c62d52005-10-18 06:04:22 +00005404
5405 // If this is a select from two identical things, try to pull the operation
5406 // through the select.
5407 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00005408 // If this is a load and the token chain is identical, replace the select
5409 // of two loads with a load through a select of the address to load from.
5410 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
5411 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00005412 if (LHS.getOpcode() == ISD::LOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005413 // Do not let this transformation reduce the number of volatile loads.
5414 !cast<LoadSDNode>(LHS)->isVolatile() &&
5415 !cast<LoadSDNode>(RHS)->isVolatile() &&
Chris Lattner40c62d52005-10-18 06:04:22 +00005416 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00005417 LHS.getOperand(0) == RHS.getOperand(0)) {
5418 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5419 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5420
5421 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005422 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00005423 // FIXME: this conflates two src values, discarding one. This is not
5424 // the right thing to do, but nothing uses srcvalues now. When they do,
5425 // turn SrcValue into a list of locations.
Dan Gohman475871a2008-07-27 21:46:04 +00005426 SDValue Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005427 if (TheSelect->getOpcode() == ISD::SELECT) {
5428 // Check that the condition doesn't reach either load. If so, folding
5429 // this will induce a cycle into the DAG.
Gabor Greifba36cb52008-08-28 21:40:38 +00005430 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5431 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode())) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005432 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
5433 TheSelect->getOperand(0), LLD->getBasePtr(),
5434 RLD->getBasePtr());
5435 }
5436 } else {
5437 // Check that the condition doesn't reach either load. If so, folding
5438 // this will induce a cycle into the DAG.
Gabor Greifba36cb52008-08-28 21:40:38 +00005439 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5440 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5441 !LLD->isPredecessorOf(TheSelect->getOperand(1).getNode()) &&
5442 !RLD->isPredecessorOf(TheSelect->getOperand(1).getNode())) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005443 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00005444 TheSelect->getOperand(0),
5445 TheSelect->getOperand(1),
5446 LLD->getBasePtr(), RLD->getBasePtr(),
5447 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005448 }
Evan Cheng466685d2006-10-09 20:57:25 +00005449 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005450
Gabor Greifba36cb52008-08-28 21:40:38 +00005451 if (Addr.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005452 SDValue Load;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005453 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
5454 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
5455 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005456 LLD->getSrcValueOffset(),
5457 LLD->isVolatile(),
5458 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005459 else {
5460 Load = DAG.getExtLoad(LLD->getExtensionType(),
5461 TheSelect->getValueType(0),
5462 LLD->getChain(), Addr, LLD->getSrcValue(),
5463 LLD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005464 LLD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005465 LLD->isVolatile(),
5466 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005467 }
5468 // Users of the select now use the result of the load.
5469 CombineTo(TheSelect, Load);
5470
5471 // Users of the old loads now use the new load's chain. We know the
5472 // old-load value is dead now.
Gabor Greifba36cb52008-08-28 21:40:38 +00005473 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
5474 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005475 return true;
5476 }
Evan Chengc5484282006-10-04 00:56:09 +00005477 }
Chris Lattner40c62d52005-10-18 06:04:22 +00005478 }
5479 }
5480
5481 return false;
5482}
5483
Dan Gohman475871a2008-07-27 21:46:04 +00005484SDValue DAGCombiner::SimplifySelectCC(SDValue N0, SDValue N1,
5485 SDValue N2, SDValue N3,
5486 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00005487
Duncan Sands83ec4b62008-06-06 12:08:01 +00005488 MVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +00005489 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
5490 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
5491 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005492
5493 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00005494 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
5495 N0, N1, CC, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00005496 if (SCC.getNode()) AddToWorkList(SCC.getNode());
5497 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005498
5499 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00005500 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005501 return N2;
5502 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00005503 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005504 return N3;
5505
5506 // Check to see if we can simplify the select into an fabs node
5507 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5508 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00005509 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005510 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5511 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5512 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5513 N2 == N3.getOperand(0))
5514 return DAG.getNode(ISD::FABS, VT, N0);
5515
5516 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5517 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5518 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5519 N2.getOperand(0) == N3)
5520 return DAG.getNode(ISD::FABS, VT, N3);
5521 }
5522 }
5523
5524 // Check to see if we can perform the "gzip trick", transforming
5525 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00005526 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005527 N0.getValueType().isInteger() &&
5528 N2.getValueType().isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005529 (N1C->isNullValue() || // (a < 0) ? b : 0
5530 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Duncan Sands83ec4b62008-06-06 12:08:01 +00005531 MVT XType = N0.getValueType();
5532 MVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00005533 if (XType.bitsGE(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005534 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00005535 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00005536 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
5537 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005538 ShCtV = XType.getSizeInBits()-ShCtV-1;
Dan Gohman475871a2008-07-27 21:46:04 +00005539 SDValue ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5540 SDValue Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +00005541 AddToWorkList(Shift.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00005542 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005543 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005544 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005545 }
5546 return DAG.getNode(ISD::AND, AType, Shift, N2);
5547 }
Dan Gohman475871a2008-07-27 21:46:04 +00005548 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005549 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005550 TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00005551 AddToWorkList(Shift.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00005552 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005553 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005554 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005555 }
5556 return DAG.getNode(ISD::AND, AType, Shift, N2);
5557 }
5558 }
Nate Begeman07ed4172005-10-10 21:26:48 +00005559
5560 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00005561 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands03228082008-11-23 15:47:28 +00005562 TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00005563
5564 // If the caller doesn't want us to simplify this into a zext of a compare,
5565 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00005566 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00005567 return SDValue();
Chris Lattner1eba01e2007-04-11 06:50:51 +00005568
Nate Begeman07ed4172005-10-10 21:26:48 +00005569 // Get a SetCC of the condition
5570 // FIXME: Should probably make sure that setcc is legal if we ever have a
5571 // target where it isn't.
Dan Gohman475871a2008-07-27 21:46:04 +00005572 SDValue Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00005573 // cast from setcc result type to select result type
Duncan Sands25cf2272008-11-24 14:53:14 +00005574 if (LegalTypes) {
Duncan Sands5480c042009-01-01 15:52:00 +00005575 SCC = DAG.getSetCC(TLI.getSetCCResultType(N0.getValueType()),
5576 N0, N1, CC);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005577 if (N2.getValueType().bitsLT(SCC.getValueType()))
Chris Lattner555d8d62006-12-07 22:36:47 +00005578 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5579 else
5580 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005581 } else {
5582 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00005583 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005584 }
Gabor Greifba36cb52008-08-28 21:40:38 +00005585 AddToWorkList(SCC.getNode());
5586 AddToWorkList(Temp.getNode());
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005587
Dan Gohman002e5d02008-03-13 22:13:53 +00005588 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005589 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00005590 // shl setcc result by log2 n2c
5591 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00005592 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Nate Begeman07ed4172005-10-10 21:26:48 +00005593 TLI.getShiftAmountTy()));
5594 }
5595
Nate Begemanf845b452005-10-08 00:29:44 +00005596 // Check to see if this is the equivalent of setcc
5597 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5598 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00005599 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005600 MVT XType = N0.getValueType();
Duncan Sands25cf2272008-11-24 14:53:14 +00005601 if (!LegalOperations ||
Duncan Sands5480c042009-01-01 15:52:00 +00005602 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(XType))) {
5603 SDValue Res = DAG.getSetCC(TLI.getSetCCResultType(XType), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00005604 if (Res.getValueType() != VT)
5605 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5606 return Res;
5607 }
5608
5609 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5610 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005611 (!LegalOperations ||
Duncan Sands184a8762008-06-14 17:48:34 +00005612 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Dan Gohman475871a2008-07-27 21:46:04 +00005613 SDValue Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
Nate Begemanf845b452005-10-08 00:29:44 +00005614 return DAG.getNode(ISD::SRL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005615 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Nate Begemanf845b452005-10-08 00:29:44 +00005616 TLI.getShiftAmountTy()));
5617 }
5618 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5619 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005620 SDValue NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
Nate Begemanf845b452005-10-08 00:29:44 +00005621 N0);
Bob Wilson4c245462009-01-22 17:39:32 +00005622 SDValue NotN0 = DAG.getNOT(N0, XType);
Nate Begemanf845b452005-10-08 00:29:44 +00005623 return DAG.getNode(ISD::SRL, XType,
5624 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00005625 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005626 TLI.getShiftAmountTy()));
5627 }
5628 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5629 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005630 SDValue Sign = DAG.getNode(ISD::SRL, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005631 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005632 TLI.getShiftAmountTy()));
5633 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5634 }
5635 }
5636
5637 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5638 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5639 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005640 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005641 N2.getOperand(0) == N1 && N0.getValueType().isInteger()) {
5642 MVT XType = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00005643 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005644 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005645 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00005646 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005647 AddToWorkList(Shift.getNode());
5648 AddToWorkList(Add.getNode());
Chris Lattner1982ef22007-04-11 05:11:38 +00005649 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5650 }
5651 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5652 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5653 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5654 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5655 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005656 MVT XType = N0.getValueType();
5657 if (SubC->isNullValue() && XType.isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005658 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005659 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005660 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00005661 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005662 AddToWorkList(Shift.getNode());
5663 AddToWorkList(Add.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005664 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5665 }
5666 }
5667 }
Chris Lattner1982ef22007-04-11 05:11:38 +00005668
Dan Gohman475871a2008-07-27 21:46:04 +00005669 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005670}
5671
Evan Chengfa1eb272007-02-08 22:13:59 +00005672/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Dan Gohman475871a2008-07-27 21:46:04 +00005673SDValue DAGCombiner::SimplifySetCC(MVT VT, SDValue N0,
5674 SDValue N1, ISD::CondCode Cond,
5675 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00005676 TargetLowering::DAGCombinerInfo
Duncan Sands25cf2272008-11-24 14:53:14 +00005677 DagCombineInfo(DAG, Level == Unrestricted, false, this);
Evan Chengfa1eb272007-02-08 22:13:59 +00005678 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00005679}
5680
Nate Begeman69575232005-10-20 02:15:44 +00005681/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5682/// return a DAG expression to select that will generate the same value by
5683/// multiplying by a magic number. See:
5684/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00005685SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005686 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00005687 SDValue S = TLI.BuildSDIV(N, DAG, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005688
Andrew Lenharth232c9102006-06-12 16:07:18 +00005689 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005690 ii != ee; ++ii)
5691 AddToWorkList(*ii);
5692 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005693}
5694
5695/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5696/// return a DAG expression to select that will generate the same value by
5697/// multiplying by a magic number. See:
5698/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00005699SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005700 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00005701 SDValue S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00005702
Andrew Lenharth232c9102006-06-12 16:07:18 +00005703 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005704 ii != ee; ++ii)
5705 AddToWorkList(*ii);
5706 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005707}
5708
Jim Laskey71382342006-10-07 23:37:56 +00005709/// FindBaseOffset - Return true if base is known not to alias with anything
5710/// but itself. Provides base object and offset as results.
Dan Gohman475871a2008-07-27 21:46:04 +00005711static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset) {
Jim Laskey71382342006-10-07 23:37:56 +00005712 // Assume it is a primitive operation.
5713 Base = Ptr; Offset = 0;
5714
5715 // If it's an adding a simple constant then integrate the offset.
5716 if (Base.getOpcode() == ISD::ADD) {
5717 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5718 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005719 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +00005720 }
5721 }
5722
5723 // If it's any of the following then it can't alias with anything but itself.
5724 return isa<FrameIndexSDNode>(Base) ||
5725 isa<ConstantPoolSDNode>(Base) ||
5726 isa<GlobalAddressSDNode>(Base);
5727}
5728
5729/// isAlias - Return true if there is any possibility that the two addresses
5730/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +00005731bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +00005732 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman475871a2008-07-27 21:46:04 +00005733 SDValue Ptr2, int64_t Size2,
Jim Laskey096c22e2006-10-18 12:29:57 +00005734 const Value *SrcValue2, int SrcValueOffset2)
5735{
Jim Laskey71382342006-10-07 23:37:56 +00005736 // If they are the same then they must be aliases.
5737 if (Ptr1 == Ptr2) return true;
5738
5739 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +00005740 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +00005741 int64_t Offset1, Offset2;
5742 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5743 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5744
5745 // If they have a same base address then...
5746 if (Base1 == Base2) {
5747 // Check to see if the addresses overlap.
5748 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5749 }
5750
Jim Laskey096c22e2006-10-18 12:29:57 +00005751 // If we know both bases then they can't alias.
5752 if (KnownBase1 && KnownBase2) return false;
5753
Jim Laskey07a27092006-10-18 19:08:31 +00005754 if (CombinerGlobalAA) {
5755 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00005756 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5757 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5758 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00005759 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00005760 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00005761 if (AAResult == AliasAnalysis::NoAlias)
5762 return false;
5763 }
Jim Laskey096c22e2006-10-18 12:29:57 +00005764
5765 // Otherwise we have to assume they alias.
5766 return true;
Jim Laskey71382342006-10-07 23:37:56 +00005767}
5768
5769/// FindAliasInfo - Extracts the relevant alias information from the memory
5770/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00005771bool DAGCombiner::FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +00005772 SDValue &Ptr, int64_t &Size,
Jim Laskey096c22e2006-10-18 12:29:57 +00005773 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005774 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5775 Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005776 Size = LD->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005777 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005778 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00005779 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005780 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005781 Ptr = ST->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005782 Size = ST->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005783 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005784 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00005785 } else {
Jim Laskey71382342006-10-07 23:37:56 +00005786 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005787 }
5788
5789 return false;
5790}
5791
Jim Laskey6ff23e52006-10-04 16:53:27 +00005792/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5793/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +00005794void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
5795 SmallVector<SDValue, 8> &Aliases) {
5796 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005797 std::set<SDNode *> Visited; // Visited node set.
5798
Jim Laskey279f0532006-09-25 16:29:54 +00005799 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +00005800 SDValue Ptr;
Jim Laskey279f0532006-09-25 16:29:54 +00005801 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005802 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005803 int SrcValueOffset;
5804 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005805
Jim Laskey6ff23e52006-10-04 16:53:27 +00005806 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005807 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005808
Jim Laskeybc588b82006-10-05 15:07:25 +00005809 // Look at each chain and determine if it is an alias. If so, add it to the
5810 // aliases list. If not, then continue up the chain looking for the next
5811 // candidate.
5812 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005813 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +00005814 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005815
Jim Laskeybc588b82006-10-05 15:07:25 +00005816 // Don't bother if we've been before.
Gabor Greifba36cb52008-08-28 21:40:38 +00005817 if (Visited.find(Chain.getNode()) != Visited.end()) continue;
5818 Visited.insert(Chain.getNode());
Jim Laskeybc588b82006-10-05 15:07:25 +00005819
5820 switch (Chain.getOpcode()) {
5821 case ISD::EntryToken:
5822 // Entry token is ideal chain operand, but handled in FindBetterChain.
5823 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005824
Jim Laskeybc588b82006-10-05 15:07:25 +00005825 case ISD::LOAD:
5826 case ISD::STORE: {
5827 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +00005828 SDValue OpPtr;
Jim Laskeybc588b82006-10-05 15:07:25 +00005829 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005830 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005831 int OpSrcValueOffset;
Gabor Greifba36cb52008-08-28 21:40:38 +00005832 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Jim Laskey096c22e2006-10-18 12:29:57 +00005833 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005834
5835 // If chain is alias then stop here.
5836 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005837 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5838 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005839 Aliases.push_back(Chain);
5840 } else {
5841 // Look further up the chain.
5842 Chains.push_back(Chain.getOperand(0));
5843 // Clean up old chain.
Gabor Greifba36cb52008-08-28 21:40:38 +00005844 AddToWorkList(Chain.getNode());
Jim Laskey279f0532006-09-25 16:29:54 +00005845 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005846 break;
5847 }
5848
5849 case ISD::TokenFactor:
5850 // We have to check each of the operands of the token factor, so we queue
5851 // then up. Adding the operands to the queue (stack) in reverse order
5852 // maintains the original order and increases the likelihood that getNode
5853 // will find a matching token factor (CSE.)
5854 for (unsigned n = Chain.getNumOperands(); n;)
5855 Chains.push_back(Chain.getOperand(--n));
5856 // Eliminate the token factor if we can.
Gabor Greifba36cb52008-08-28 21:40:38 +00005857 AddToWorkList(Chain.getNode());
Jim Laskeybc588b82006-10-05 15:07:25 +00005858 break;
5859
5860 default:
5861 // For all other instructions we will just have to take what we can get.
5862 Aliases.push_back(Chain);
5863 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005864 }
5865 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005866}
5867
5868/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5869/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +00005870SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
5871 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005872
Jim Laskey6ff23e52006-10-04 16:53:27 +00005873 // Accumulate all the aliases to this node.
5874 GatherAllAliases(N, OldChain, Aliases);
5875
5876 if (Aliases.size() == 0) {
5877 // If no operands then chain to entry token.
5878 return DAG.getEntryNode();
5879 } else if (Aliases.size() == 1) {
5880 // If a single operand then chain to it. We don't need to revisit it.
5881 return Aliases[0];
5882 }
5883
5884 // Construct a custom tailored token factor.
Dan Gohman475871a2008-07-27 21:46:04 +00005885 SDValue NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005886 &Aliases[0], Aliases.size());
5887
5888 // Make sure the old chain gets cleaned up.
Gabor Greifba36cb52008-08-28 21:40:38 +00005889 if (NewChain != OldChain) AddToWorkList(OldChain.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00005890
5891 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005892}
5893
Nate Begeman1d4d4142005-09-01 00:19:25 +00005894// SelectionDAG::Combine - This is the entry point for the file.
5895//
Duncan Sands25cf2272008-11-24 14:53:14 +00005896void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA, bool Fast) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00005897 /// run - This is the main entry point to this class.
5898 ///
Duncan Sands25cf2272008-11-24 14:53:14 +00005899 DAGCombiner(*this, AA, Fast).Run(Level);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005900}