blob: 066952db2f58d5bca1703f20c1937bac7b148797 [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>
Nate Begeman1d4d4142005-09-01 00:19:25 +000032using namespace llvm;
33
Chris Lattnercd3245a2006-12-19 22:41:21 +000034STATISTIC(NodesCombined , "Number of dag nodes combined");
35STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
36STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
37
Nate Begeman1d4d4142005-09-01 00:19:25 +000038namespace {
Chris Lattner938ab022007-01-16 04:55:25 +000039#ifndef NDEBUG
40 static cl::opt<bool>
41 ViewDAGCombine1("view-dag-combine1-dags", cl::Hidden,
42 cl::desc("Pop up a window to show dags before the first "
43 "dag combine pass"));
44 static cl::opt<bool>
45 ViewDAGCombine2("view-dag-combine2-dags", cl::Hidden,
46 cl::desc("Pop up a window to show dags before the second "
47 "dag combine pass"));
48#else
49 static const bool ViewDAGCombine1 = false;
50 static const bool ViewDAGCombine2 = false;
51#endif
52
Jim Laskey71382342006-10-07 23:37:56 +000053 static cl::opt<bool>
54 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000055 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000056
Jim Laskey07a27092006-10-18 19:08:31 +000057 static cl::opt<bool>
58 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
59 cl::desc("Include global information in alias analysis"));
60
Jim Laskeybc588b82006-10-05 15:07:25 +000061//------------------------------ DAGCombiner ---------------------------------//
62
Jim Laskey71382342006-10-07 23:37:56 +000063 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000064 SelectionDAG &DAG;
65 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000066 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000067
68 // Worklist of all of the nodes that need to be simplified.
69 std::vector<SDNode*> WorkList;
70
Jim Laskeyc7c3f112006-10-16 20:52:31 +000071 // AA - Used for DAG load/store alias analysis.
72 AliasAnalysis &AA;
73
Nate Begeman1d4d4142005-09-01 00:19:25 +000074 /// AddUsersToWorkList - When an instruction is simplified, add all users of
75 /// the instruction to the work lists because they might get more simplified
76 /// now.
77 ///
78 void AddUsersToWorkList(SDNode *N) {
79 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000080 UI != UE; ++UI)
Roman Levensteindc1adac2008-04-07 10:06:32 +000081 AddToWorkList(UI->getUser());
Nate Begeman1d4d4142005-09-01 00:19:25 +000082 }
83
Dan Gohman389079b2007-10-08 17:57:15 +000084 /// visit - call the node-specific routine that knows how to fold each
85 /// particular type of node.
86 SDOperand visit(SDNode *N);
87
Chris Lattner24664722006-03-01 04:53:38 +000088 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +000089 /// AddToWorkList - Add to the work list making sure it's instance is at the
90 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +000091 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000092 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +000093 WorkList.push_back(N);
94 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000095
Chris Lattnerf8dc0612008-02-03 06:49:24 +000096 /// removeFromWorkList - remove all instances of N from the worklist.
97 ///
98 void removeFromWorkList(SDNode *N) {
99 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
100 WorkList.end());
Chris Lattner01a22022005-10-10 22:04:48 +0000101 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000102
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000103 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo,
104 bool AddTo = true);
105
Jim Laskey274062c2006-10-13 23:32:28 +0000106 SDOperand CombineTo(SDNode *N, SDOperand Res, bool AddTo = true) {
107 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000108 }
109
Jim Laskey274062c2006-10-13 23:32:28 +0000110 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1,
111 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +0000112 SDOperand To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000113 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000114 }
Chris Lattner0bd48932008-01-17 07:00:52 +0000115
Chris Lattner24664722006-03-01 04:53:38 +0000116 private:
117
Chris Lattner012f2412006-02-17 21:58:01 +0000118 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000119 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000120 /// propagation. If so, return true.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000121 bool SimplifyDemandedBits(SDOperand Op) {
122 APInt Demanded = APInt::getAllOnesValue(Op.getValueSizeInBits());
123 return SimplifyDemandedBits(Op, Demanded);
124 }
125
126 bool SimplifyDemandedBits(SDOperand Op, const APInt &Demanded);
Chris Lattner87514ca2005-10-10 22:31:19 +0000127
Chris Lattner448f2192006-11-11 00:39:41 +0000128 bool CombineToPreIndexedLoadStore(SDNode *N);
129 bool CombineToPostIndexedLoadStore(SDNode *N);
130
131
Dan Gohman389079b2007-10-08 17:57:15 +0000132 /// combine - call the node-specific routine that knows how to fold each
133 /// particular type of node. If that doesn't do anything, try the
134 /// target-specific DAG combines.
135 SDOperand combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000136
137 // Visitation implementation - Implement dag node combining for different
138 // node types. The semantics are as follows:
139 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000140 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000141 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000142 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000143 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000144 SDOperand visitTokenFactor(SDNode *N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000145 SDOperand visitMERGE_VALUES(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000146 SDOperand visitADD(SDNode *N);
147 SDOperand visitSUB(SDNode *N);
Chris Lattner91153682007-03-04 20:03:15 +0000148 SDOperand visitADDC(SDNode *N);
149 SDOperand visitADDE(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000150 SDOperand visitMUL(SDNode *N);
151 SDOperand visitSDIV(SDNode *N);
152 SDOperand visitUDIV(SDNode *N);
153 SDOperand visitSREM(SDNode *N);
154 SDOperand visitUREM(SDNode *N);
155 SDOperand visitMULHU(SDNode *N);
156 SDOperand visitMULHS(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +0000157 SDOperand visitSMUL_LOHI(SDNode *N);
158 SDOperand visitUMUL_LOHI(SDNode *N);
159 SDOperand visitSDIVREM(SDNode *N);
160 SDOperand visitUDIVREM(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000161 SDOperand visitAND(SDNode *N);
162 SDOperand visitOR(SDNode *N);
163 SDOperand visitXOR(SDNode *N);
Dan Gohman7f321562007-06-25 16:23:39 +0000164 SDOperand SimplifyVBinOp(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000165 SDOperand visitSHL(SDNode *N);
166 SDOperand visitSRA(SDNode *N);
167 SDOperand visitSRL(SDNode *N);
168 SDOperand visitCTLZ(SDNode *N);
169 SDOperand visitCTTZ(SDNode *N);
170 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000171 SDOperand visitSELECT(SDNode *N);
172 SDOperand visitSELECT_CC(SDNode *N);
173 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000174 SDOperand visitSIGN_EXTEND(SDNode *N);
175 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000176 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000177 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
178 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000179 SDOperand visitBIT_CONVERT(SDNode *N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +0000180 SDOperand visitBUILD_PAIR(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000181 SDOperand visitFADD(SDNode *N);
182 SDOperand visitFSUB(SDNode *N);
183 SDOperand visitFMUL(SDNode *N);
184 SDOperand visitFDIV(SDNode *N);
185 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000186 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000187 SDOperand visitSINT_TO_FP(SDNode *N);
188 SDOperand visitUINT_TO_FP(SDNode *N);
189 SDOperand visitFP_TO_SINT(SDNode *N);
190 SDOperand visitFP_TO_UINT(SDNode *N);
191 SDOperand visitFP_ROUND(SDNode *N);
192 SDOperand visitFP_ROUND_INREG(SDNode *N);
193 SDOperand visitFP_EXTEND(SDNode *N);
194 SDOperand visitFNEG(SDNode *N);
195 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000196 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000197 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000198 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000199 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000200 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
Evan Cheng513da432007-10-06 08:19:55 +0000201 SDOperand visitEXTRACT_VECTOR_ELT(SDNode *N);
Dan Gohman7f321562007-06-25 16:23:39 +0000202 SDOperand visitBUILD_VECTOR(SDNode *N);
203 SDOperand visitCONCAT_VECTORS(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000204 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000205
Evan Cheng44f1f092006-04-20 08:56:16 +0000206 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000207 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
208
Chris Lattnere70da202007-12-06 07:33:36 +0000209 SDOperand visitShiftByConstant(SDNode *N, unsigned Amt);
210
Chris Lattner40c62d52005-10-18 06:04:22 +0000211 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000212 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000213 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
214 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
Chris Lattner1eba01e2007-04-11 06:50:51 +0000215 SDOperand N3, ISD::CondCode CC,
216 bool NotExtCompare = false);
Nate Begeman452d7be2005-09-16 00:54:12 +0000217 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000218 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner5eee4272008-01-26 01:09:19 +0000219 SDOperand SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
220 unsigned HiOp);
Evan Cheng9bfa03c2008-05-12 23:04:07 +0000221 SDOperand CombineConsecutiveLoads(SDNode *N, MVT::ValueType VT);
Dan Gohman7f321562007-06-25 16:23:39 +0000222 SDOperand ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000223 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000224 SDOperand BuildUDIV(SDNode *N);
225 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Evan Chengc88138f2007-03-22 01:54:19 +0000226 SDOperand ReduceLoadWidth(SDNode *N);
Jim Laskey279f0532006-09-25 16:29:54 +0000227
Dan Gohman2e68b6f2008-02-25 21:11:39 +0000228 SDOperand GetDemandedBits(SDOperand V, const APInt &Mask);
Chris Lattner2b4c2792007-10-13 06:35:54 +0000229
Jim Laskey6ff23e52006-10-04 16:53:27 +0000230 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
231 /// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +0000232 void GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000233 SmallVector<SDOperand, 8> &Aliases);
234
Jim Laskey096c22e2006-10-18 12:29:57 +0000235 /// isAlias - Return true if there is any possibility that the two addresses
236 /// overlap.
237 bool isAlias(SDOperand Ptr1, int64_t Size1,
238 const Value *SrcValue1, int SrcValueOffset1,
239 SDOperand Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000240 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000241
Jim Laskey7ca56af2006-10-11 13:47:09 +0000242 /// FindAliasInfo - Extracts the relevant alias information from the memory
243 /// node. Returns true if the operand was a load.
244 bool FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +0000245 SDOperand &Ptr, int64_t &Size,
246 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000247
Jim Laskey279f0532006-09-25 16:29:54 +0000248 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000249 /// looking for a better chain (aliasing node.)
Jim Laskey279f0532006-09-25 16:29:54 +0000250 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
251
Nate Begeman1d4d4142005-09-01 00:19:25 +0000252public:
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000253 DAGCombiner(SelectionDAG &D, AliasAnalysis &A)
254 : DAG(D),
255 TLI(D.getTargetLoweringInfo()),
256 AfterLegalize(false),
257 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000258
259 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000260 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000261 };
262}
263
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000264
265namespace {
266/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
267/// nodes from the worklist.
268class VISIBILITY_HIDDEN WorkListRemover :
269 public SelectionDAG::DAGUpdateListener {
270 DAGCombiner &DC;
271public:
Dan Gohmanb5660dc2008-02-20 16:44:09 +0000272 explicit WorkListRemover(DAGCombiner &dc) : DC(dc) {}
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000273
274 virtual void NodeDeleted(SDNode *N) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000275 DC.removeFromWorkList(N);
276 }
277
278 virtual void NodeUpdated(SDNode *N) {
279 // Ignore updates.
280 }
281};
282}
283
Chris Lattner24664722006-03-01 04:53:38 +0000284//===----------------------------------------------------------------------===//
285// TargetLowering::DAGCombinerInfo implementation
286//===----------------------------------------------------------------------===//
287
288void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
289 ((DAGCombiner*)DC)->AddToWorkList(N);
290}
291
292SDOperand TargetLowering::DAGCombinerInfo::
293CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000294 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000295}
296
297SDOperand TargetLowering::DAGCombinerInfo::
298CombineTo(SDNode *N, SDOperand Res) {
299 return ((DAGCombiner*)DC)->CombineTo(N, Res);
300}
301
302
303SDOperand TargetLowering::DAGCombinerInfo::
304CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
305 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
306}
307
308
Chris Lattner24664722006-03-01 04:53:38 +0000309//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000310// Helper Functions
311//===----------------------------------------------------------------------===//
312
313/// isNegatibleForFree - Return 1 if we can compute the negated form of the
314/// specified expression for the same cost as the expression itself, or 2 if we
315/// can compute the negated form more cheaply than the expression itself.
Chris Lattner0254e702008-02-26 07:04:54 +0000316static char isNegatibleForFree(SDOperand Op, bool AfterLegalize,
317 unsigned Depth = 0) {
Dale Johannesendb44bf82007-10-16 23:38:29 +0000318 // No compile time optimizations on this type.
319 if (Op.getValueType() == MVT::ppcf128)
320 return 0;
321
Chris Lattner29446522007-05-14 22:04:50 +0000322 // fneg is removable even if it has multiple uses.
323 if (Op.getOpcode() == ISD::FNEG) return 2;
324
325 // Don't allow anything with multiple uses.
326 if (!Op.hasOneUse()) return 0;
327
Chris Lattner3adf9512007-05-25 02:19:06 +0000328 // Don't recurse exponentially.
329 if (Depth > 6) return 0;
330
Chris Lattner29446522007-05-14 22:04:50 +0000331 switch (Op.getOpcode()) {
332 default: return false;
333 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000334 // Don't invert constant FP values after legalize. The negated constant
335 // isn't necessarily legal.
336 return AfterLegalize ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000337 case ISD::FADD:
338 // FIXME: determine better conditions for this xform.
339 if (!UnsafeFPMath) return 0;
340
341 // -(A+B) -> -A - B
Chris Lattner0254e702008-02-26 07:04:54 +0000342 if (char V = isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000343 return V;
344 // -(A+B) -> -B - A
Chris Lattner0254e702008-02-26 07:04:54 +0000345 return isNegatibleForFree(Op.getOperand(1), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000346 case ISD::FSUB:
347 // We can't turn -(A-B) into B-A when we honor signed zeros.
348 if (!UnsafeFPMath) return 0;
349
350 // -(A-B) -> B-A
351 return 1;
352
353 case ISD::FMUL:
354 case ISD::FDIV:
355 if (HonorSignDependentRoundingFPMath()) return 0;
356
357 // -(X*Y) -> (-X * Y) or (X*-Y)
Chris Lattner0254e702008-02-26 07:04:54 +0000358 if (char V = isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000359 return V;
360
Chris Lattner0254e702008-02-26 07:04:54 +0000361 return isNegatibleForFree(Op.getOperand(1), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000362
363 case ISD::FP_EXTEND:
364 case ISD::FP_ROUND:
365 case ISD::FSIN:
Chris Lattner0254e702008-02-26 07:04:54 +0000366 return isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000367 }
368}
369
370/// GetNegatedExpression - If isNegatibleForFree returns true, this function
371/// returns the newly negated expression.
Chris Lattner3adf9512007-05-25 02:19:06 +0000372static SDOperand GetNegatedExpression(SDOperand Op, SelectionDAG &DAG,
Chris Lattner0254e702008-02-26 07:04:54 +0000373 bool AfterLegalize, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000374 // fneg is removable even if it has multiple uses.
375 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
376
377 // Don't allow anything with multiple uses.
378 assert(Op.hasOneUse() && "Unknown reuse!");
379
Chris Lattner3adf9512007-05-25 02:19:06 +0000380 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000381 switch (Op.getOpcode()) {
382 default: assert(0 && "Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000383 case ISD::ConstantFP: {
384 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
385 V.changeSign();
386 return DAG.getConstantFP(V, Op.getValueType());
387 }
Chris Lattner29446522007-05-14 22:04:50 +0000388 case ISD::FADD:
389 // FIXME: determine better conditions for this xform.
390 assert(UnsafeFPMath);
391
392 // -(A+B) -> -A - B
Chris Lattner0254e702008-02-26 07:04:54 +0000393 if (isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000394 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000395 GetNegatedExpression(Op.getOperand(0), DAG,
396 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000397 Op.getOperand(1));
398 // -(A+B) -> -B - A
399 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000400 GetNegatedExpression(Op.getOperand(1), DAG,
401 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000402 Op.getOperand(0));
403 case ISD::FSUB:
404 // We can't turn -(A-B) into B-A when we honor signed zeros.
405 assert(UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000406
407 // -(0-B) -> B
408 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000409 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000410 return Op.getOperand(1);
Chris Lattner29446522007-05-14 22:04:50 +0000411
412 // -(A-B) -> B-A
413 return DAG.getNode(ISD::FSUB, Op.getValueType(), Op.getOperand(1),
414 Op.getOperand(0));
415
416 case ISD::FMUL:
417 case ISD::FDIV:
418 assert(!HonorSignDependentRoundingFPMath());
419
420 // -(X*Y) -> -X * Y
Chris Lattneraeecb6c2008-02-26 17:09:59 +0000421 if (isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000422 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000423 GetNegatedExpression(Op.getOperand(0), DAG,
424 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000425 Op.getOperand(1));
426
427 // -(X*Y) -> X * -Y
428 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
429 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000430 GetNegatedExpression(Op.getOperand(1), DAG,
431 AfterLegalize, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000432
433 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000434 case ISD::FSIN:
435 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000436 GetNegatedExpression(Op.getOperand(0), DAG,
437 AfterLegalize, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000438 case ISD::FP_ROUND:
439 return DAG.getNode(ISD::FP_ROUND, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000440 GetNegatedExpression(Op.getOperand(0), DAG,
441 AfterLegalize, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000442 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000443 }
444}
Chris Lattner24664722006-03-01 04:53:38 +0000445
446
Nate Begeman4ebd8052005-09-01 23:24:04 +0000447// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
448// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000449// Also, set the incoming LHS, RHS, and CC references to the appropriate
450// nodes based on the type of node we are checking. This simplifies life a
451// bit for the callers.
452static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
453 SDOperand &CC) {
454 if (N.getOpcode() == ISD::SETCC) {
455 LHS = N.getOperand(0);
456 RHS = N.getOperand(1);
457 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000458 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000459 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000460 if (N.getOpcode() == ISD::SELECT_CC &&
461 N.getOperand(2).getOpcode() == ISD::Constant &&
462 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000463 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000464 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
465 LHS = N.getOperand(0);
466 RHS = N.getOperand(1);
467 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000468 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000469 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000470 return false;
471}
472
Nate Begeman99801192005-09-07 23:25:52 +0000473// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
474// one use. If this is true, it allows the users to invert the operation for
475// free when it is profitable to do so.
476static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000477 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000478 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000479 return true;
480 return false;
481}
482
Nate Begemancd4d58c2006-02-03 06:46:56 +0000483SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
484 MVT::ValueType VT = N0.getValueType();
485 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
486 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
487 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
488 if (isa<ConstantSDNode>(N1)) {
489 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000490 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000491 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
492 } else if (N0.hasOneUse()) {
493 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000494 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000495 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
496 }
497 }
498 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
499 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
500 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
501 if (isa<ConstantSDNode>(N0)) {
502 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000503 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000504 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
505 } else if (N1.hasOneUse()) {
506 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000507 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000508 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
509 }
510 }
511 return SDOperand();
512}
513
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000514SDOperand DAGCombiner::CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo,
515 bool AddTo) {
516 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
517 ++NodesCombined;
518 DOUT << "\nReplacing.1 "; DEBUG(N->dump(&DAG));
519 DOUT << "\nWith: "; DEBUG(To[0].Val->dump(&DAG));
520 DOUT << " and " << NumTo-1 << " other values\n";
521 WorkListRemover DeadNodes(*this);
522 DAG.ReplaceAllUsesWith(N, To, &DeadNodes);
523
524 if (AddTo) {
525 // Push the new nodes and any users onto the worklist
526 for (unsigned i = 0, e = NumTo; i != e; ++i) {
527 AddToWorkList(To[i].Val);
528 AddUsersToWorkList(To[i].Val);
529 }
530 }
531
532 // Nodes can be reintroduced into the worklist. Make sure we do not
533 // process a node that has been replaced.
534 removeFromWorkList(N);
535
536 // Finally, since the node is now dead, remove it from the graph.
537 DAG.DeleteNode(N);
538 return SDOperand(N, 0);
539}
540
541/// SimplifyDemandedBits - Check the specified integer node value to see if
542/// it can be simplified or if things it uses can be simplified by bit
543/// propagation. If so, return true.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000544bool DAGCombiner::SimplifyDemandedBits(SDOperand Op, const APInt &Demanded) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000545 TargetLowering::TargetLoweringOpt TLO(DAG, AfterLegalize);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000546 APInt KnownZero, KnownOne;
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000547 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
548 return false;
549
550 // Revisit the node.
551 AddToWorkList(Op.Val);
552
553 // Replace the old value with the new one.
554 ++NodesCombined;
555 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.Val->dump(&DAG));
556 DOUT << "\nWith: "; DEBUG(TLO.New.Val->dump(&DAG));
557 DOUT << '\n';
558
559 // Replace all uses. If any nodes become isomorphic to other nodes and
560 // are deleted, make sure to remove them from our worklist.
561 WorkListRemover DeadNodes(*this);
562 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
563
564 // Push the new node and any (possibly new) users onto the worklist.
565 AddToWorkList(TLO.New.Val);
566 AddUsersToWorkList(TLO.New.Val);
567
568 // Finally, if the node is now dead, remove it from the graph. The node
569 // may not be dead if the replacement process recursively simplified to
570 // something else needing this node.
571 if (TLO.Old.Val->use_empty()) {
572 removeFromWorkList(TLO.Old.Val);
573
574 // If the operands of this node are only used by the node, they will now
575 // be dead. Make sure to visit them first to delete dead nodes early.
576 for (unsigned i = 0, e = TLO.Old.Val->getNumOperands(); i != e; ++i)
577 if (TLO.Old.Val->getOperand(i).Val->hasOneUse())
578 AddToWorkList(TLO.Old.Val->getOperand(i).Val);
579
580 DAG.DeleteNode(TLO.Old.Val);
581 }
582 return true;
583}
584
Chris Lattner29446522007-05-14 22:04:50 +0000585//===----------------------------------------------------------------------===//
586// Main DAG Combiner implementation
587//===----------------------------------------------------------------------===//
588
Nate Begeman4ebd8052005-09-01 23:24:04 +0000589void DAGCombiner::Run(bool RunningAfterLegalize) {
590 // set the instance variable, so that the various visit routines may use it.
591 AfterLegalize = RunningAfterLegalize;
592
Nate Begeman646d7e22005-09-02 21:18:40 +0000593 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000594 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
595 E = DAG.allnodes_end(); I != E; ++I)
596 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000597
Chris Lattner95038592005-10-05 06:35:28 +0000598 // Create a dummy node (which is not added to allnodes), that adds a reference
599 // to the root node, preventing it from being deleted, and tracking any
600 // changes of the root.
601 HandleSDNode Dummy(DAG.getRoot());
602
Jim Laskey26f7fa72006-10-17 19:33:52 +0000603 // The root of the dag may dangle to deleted nodes until the dag combiner is
604 // done. Set it to null to avoid confusion.
605 DAG.setRoot(SDOperand());
Chris Lattner24664722006-03-01 04:53:38 +0000606
Nate Begeman1d4d4142005-09-01 00:19:25 +0000607 // while the worklist isn't empty, inspect the node on the end of it and
608 // try and combine it.
609 while (!WorkList.empty()) {
610 SDNode *N = WorkList.back();
611 WorkList.pop_back();
612
613 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000614 // N is deleted from the DAG, since they too may now be dead or may have a
615 // reduced number of uses, allowing other xforms.
616 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000617 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000618 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000619
Chris Lattner95038592005-10-05 06:35:28 +0000620 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000621 continue;
622 }
623
Dan Gohman389079b2007-10-08 17:57:15 +0000624 SDOperand RV = combine(N);
Chris Lattner24664722006-03-01 04:53:38 +0000625
Chris Lattner50d8e492008-01-25 23:34:24 +0000626 if (RV.Val == 0)
627 continue;
628
629 ++NodesCombined;
Chris Lattner5eee4272008-01-26 01:09:19 +0000630
Chris Lattner50d8e492008-01-25 23:34:24 +0000631 // If we get back the same node we passed in, rather than a new node or
632 // zero, we know that the node must have defined multiple values and
633 // CombineTo was used. Since CombineTo takes care of the worklist
634 // mechanics for us, we have no work to do in this case.
635 if (RV.Val == N)
636 continue;
637
638 assert(N->getOpcode() != ISD::DELETED_NODE &&
639 RV.Val->getOpcode() != ISD::DELETED_NODE &&
640 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +0000641
Chris Lattner50d8e492008-01-25 23:34:24 +0000642 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
643 DOUT << "\nWith: "; DEBUG(RV.Val->dump(&DAG));
644 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000645 WorkListRemover DeadNodes(*this);
Chris Lattner50d8e492008-01-25 23:34:24 +0000646 if (N->getNumValues() == RV.Val->getNumValues())
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000647 DAG.ReplaceAllUsesWith(N, RV.Val, &DeadNodes);
Chris Lattner50d8e492008-01-25 23:34:24 +0000648 else {
Chris Lattner5eee4272008-01-26 01:09:19 +0000649 assert(N->getValueType(0) == RV.getValueType() &&
650 N->getNumValues() == 1 && "Type mismatch");
Chris Lattner50d8e492008-01-25 23:34:24 +0000651 SDOperand OpV = RV;
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000652 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000653 }
Chris Lattner50d8e492008-01-25 23:34:24 +0000654
655 // Push the new node and any users onto the worklist
656 AddToWorkList(RV.Val);
657 AddUsersToWorkList(RV.Val);
658
659 // Add any uses of the old node to the worklist in case this node is the
660 // last one that uses them. They may become dead after this node is
661 // deleted.
662 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
663 AddToWorkList(N->getOperand(i).Val);
664
665 // Nodes can be reintroduced into the worklist. Make sure we do not
666 // process a node that has been replaced.
667 removeFromWorkList(N);
Chris Lattner50d8e492008-01-25 23:34:24 +0000668
669 // Finally, since the node is now dead, remove it from the graph.
670 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000671 }
Chris Lattner95038592005-10-05 06:35:28 +0000672
673 // If the root changed (e.g. it was a dead load, update the root).
674 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000675}
676
Nate Begeman83e75ec2005-09-06 04:43:02 +0000677SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000678 switch(N->getOpcode()) {
679 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000680 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000681 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000682 case ISD::ADD: return visitADD(N);
683 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000684 case ISD::ADDC: return visitADDC(N);
685 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000686 case ISD::MUL: return visitMUL(N);
687 case ISD::SDIV: return visitSDIV(N);
688 case ISD::UDIV: return visitUDIV(N);
689 case ISD::SREM: return visitSREM(N);
690 case ISD::UREM: return visitUREM(N);
691 case ISD::MULHU: return visitMULHU(N);
692 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000693 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
694 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
695 case ISD::SDIVREM: return visitSDIVREM(N);
696 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000697 case ISD::AND: return visitAND(N);
698 case ISD::OR: return visitOR(N);
699 case ISD::XOR: return visitXOR(N);
700 case ISD::SHL: return visitSHL(N);
701 case ISD::SRA: return visitSRA(N);
702 case ISD::SRL: return visitSRL(N);
703 case ISD::CTLZ: return visitCTLZ(N);
704 case ISD::CTTZ: return visitCTTZ(N);
705 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000706 case ISD::SELECT: return visitSELECT(N);
707 case ISD::SELECT_CC: return visitSELECT_CC(N);
708 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000709 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
710 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000711 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000712 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
713 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000714 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +0000715 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000716 case ISD::FADD: return visitFADD(N);
717 case ISD::FSUB: return visitFSUB(N);
718 case ISD::FMUL: return visitFMUL(N);
719 case ISD::FDIV: return visitFDIV(N);
720 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000721 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000722 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
723 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
724 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
725 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
726 case ISD::FP_ROUND: return visitFP_ROUND(N);
727 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
728 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
729 case ISD::FNEG: return visitFNEG(N);
730 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000731 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000732 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000733 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000734 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000735 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +0000736 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000737 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
738 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000739 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000740 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000741 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000742}
743
Dan Gohman389079b2007-10-08 17:57:15 +0000744SDOperand DAGCombiner::combine(SDNode *N) {
745
746 SDOperand RV = visit(N);
747
748 // If nothing happened, try a target-specific DAG combine.
749 if (RV.Val == 0) {
750 assert(N->getOpcode() != ISD::DELETED_NODE &&
751 "Node was deleted but visit returned NULL!");
752
753 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
754 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
755
756 // Expose the DAG combiner to the target combiner impls.
757 TargetLowering::DAGCombinerInfo
758 DagCombineInfo(DAG, !AfterLegalize, false, this);
759
760 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
761 }
762 }
763
Evan Cheng08b11732008-03-22 01:55:50 +0000764 // If N is a commutative binary node, try commuting it to enable more
765 // sdisel CSE.
766 if (RV.Val == 0 &&
767 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
768 N->getNumValues() == 1) {
769 SDOperand N0 = N->getOperand(0);
770 SDOperand N1 = N->getOperand(1);
771 // Constant operands are canonicalized to RHS.
772 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
773 SDOperand Ops[] = { N1, N0 };
774 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
775 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +0000776 if (CSENode)
Evan Cheng08b11732008-03-22 01:55:50 +0000777 return SDOperand(CSENode, 0);
778 }
779 }
780
Dan Gohman389079b2007-10-08 17:57:15 +0000781 return RV;
782}
783
Chris Lattner6270f682006-10-08 22:57:01 +0000784/// getInputChainForNode - Given a node, return its input chain if it has one,
785/// otherwise return a null sd operand.
786static SDOperand getInputChainForNode(SDNode *N) {
787 if (unsigned NumOps = N->getNumOperands()) {
788 if (N->getOperand(0).getValueType() == MVT::Other)
789 return N->getOperand(0);
790 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
791 return N->getOperand(NumOps-1);
792 for (unsigned i = 1; i < NumOps-1; ++i)
793 if (N->getOperand(i).getValueType() == MVT::Other)
794 return N->getOperand(i);
795 }
796 return SDOperand(0, 0);
797}
798
Nate Begeman83e75ec2005-09-06 04:43:02 +0000799SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000800 // If N has two operands, where one has an input chain equal to the other,
801 // the 'other' chain is redundant.
802 if (N->getNumOperands() == 2) {
803 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
804 return N->getOperand(0);
805 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
806 return N->getOperand(1);
807 }
808
Chris Lattnerc76d4412007-05-16 06:37:59 +0000809 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
810 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
811 SmallPtrSet<SDNode*, 16> SeenOps;
812 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000813
814 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000815 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000816
Jim Laskey71382342006-10-07 23:37:56 +0000817 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000818 // encountered.
819 for (unsigned i = 0; i < TFs.size(); ++i) {
820 SDNode *TF = TFs[i];
821
Jim Laskey6ff23e52006-10-04 16:53:27 +0000822 // Check each of the operands.
823 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
824 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000825
Jim Laskey6ff23e52006-10-04 16:53:27 +0000826 switch (Op.getOpcode()) {
827 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000828 // Entry tokens don't need to be added to the list. They are
829 // rededundant.
830 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000831 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000832
Jim Laskey6ff23e52006-10-04 16:53:27 +0000833 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000834 if ((CombinerAA || Op.hasOneUse()) &&
835 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000836 // Queue up for processing.
837 TFs.push_back(Op.Val);
838 // Clean up in case the token factor is removed.
839 AddToWorkList(Op.Val);
840 Changed = true;
841 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000842 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000843 // Fall thru
844
845 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000846 // Only add if it isn't already in the list.
847 if (SeenOps.insert(Op.Val))
Jim Laskeybc588b82006-10-05 15:07:25 +0000848 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000849 else
850 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000851 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000852 }
853 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000854 }
855
856 SDOperand Result;
857
858 // If we've change things around then replace token factor.
859 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +0000860 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000861 // The entry token is the only possible outcome.
862 Result = DAG.getEntryNode();
863 } else {
864 // New and improved token factor.
865 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000866 }
Jim Laskey274062c2006-10-13 23:32:28 +0000867
868 // Don't add users to work list.
869 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000870 }
Jim Laskey279f0532006-09-25 16:29:54 +0000871
Jim Laskey6ff23e52006-10-04 16:53:27 +0000872 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000873}
874
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000875/// MERGE_VALUES can always be eliminated.
876SDOperand DAGCombiner::visitMERGE_VALUES(SDNode *N) {
877 WorkListRemover DeadNodes(*this);
878 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
879 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, i), N->getOperand(i),
880 &DeadNodes);
881 removeFromWorkList(N);
882 DAG.DeleteNode(N);
883 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
884}
885
886
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000887static
888SDOperand combineShlAddConstant(SDOperand N0, SDOperand N1, SelectionDAG &DAG) {
889 MVT::ValueType VT = N0.getValueType();
890 SDOperand N00 = N0.getOperand(0);
891 SDOperand N01 = N0.getOperand(1);
892 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
893 if (N01C && N00.getOpcode() == ISD::ADD && N00.Val->hasOneUse() &&
894 isa<ConstantSDNode>(N00.getOperand(1))) {
895 N0 = DAG.getNode(ISD::ADD, VT,
896 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
897 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
898 return DAG.getNode(ISD::ADD, VT, N0, N1);
899 }
900 return SDOperand();
901}
902
Evan Chengb13cdbd2007-06-21 07:39:16 +0000903static
904SDOperand combineSelectAndUse(SDNode *N, SDOperand Slct, SDOperand OtherOp,
905 SelectionDAG &DAG) {
906 MVT::ValueType VT = N->getValueType(0);
907 unsigned Opc = N->getOpcode();
908 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
909 SDOperand LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
910 SDOperand RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
911 ISD::CondCode CC = ISD::SETCC_INVALID;
912 if (isSlctCC)
913 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
914 else {
915 SDOperand CCOp = Slct.getOperand(0);
916 if (CCOp.getOpcode() == ISD::SETCC)
917 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
918 }
919
920 bool DoXform = false;
921 bool InvCC = false;
922 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
923 "Bad input!");
924 if (LHS.getOpcode() == ISD::Constant &&
925 cast<ConstantSDNode>(LHS)->isNullValue())
926 DoXform = true;
927 else if (CC != ISD::SETCC_INVALID &&
928 RHS.getOpcode() == ISD::Constant &&
929 cast<ConstantSDNode>(RHS)->isNullValue()) {
930 std::swap(LHS, RHS);
Chris Lattner4626b252008-01-17 07:20:38 +0000931 SDOperand Op0 = Slct.getOperand(0);
932 bool isInt = MVT::isInteger(isSlctCC ? Op0.getValueType()
933 : Op0.getOperand(0).getValueType());
Evan Chengb13cdbd2007-06-21 07:39:16 +0000934 CC = ISD::getSetCCInverse(CC, isInt);
935 DoXform = true;
936 InvCC = true;
937 }
938
939 if (DoXform) {
940 SDOperand Result = DAG.getNode(Opc, VT, OtherOp, RHS);
941 if (isSlctCC)
942 return DAG.getSelectCC(OtherOp, Result,
943 Slct.getOperand(0), Slct.getOperand(1), CC);
944 SDOperand CCOp = Slct.getOperand(0);
945 if (InvCC)
946 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
947 CCOp.getOperand(1), CC);
948 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
949 }
950 return SDOperand();
951}
952
Nate Begeman83e75ec2005-09-06 04:43:02 +0000953SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000954 SDOperand N0 = N->getOperand(0);
955 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000956 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
957 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000958 MVT::ValueType VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000959
960 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +0000961 if (MVT::isVector(VT)) {
962 SDOperand FoldedVOp = SimplifyVBinOp(N);
963 if (FoldedVOp.Val) return FoldedVOp;
964 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000965
Dan Gohman613e0d82007-07-03 14:03:57 +0000966 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +0000967 if (N0.getOpcode() == ISD::UNDEF)
968 return N0;
969 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +0000970 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000971 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000972 if (N0C && N1C)
Dan Gohman002e5d02008-03-13 22:13:53 +0000973 return DAG.getConstant(N0C->getAPIntValue() + N1C->getAPIntValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000974 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000975 if (N0C && !N1C)
976 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000977 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000978 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000979 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000980 // fold ((c1-A)+c2) -> (c1+c2)-A
981 if (N1C && N0.getOpcode() == ISD::SUB)
982 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
983 return DAG.getNode(ISD::SUB, VT,
Dan Gohman002e5d02008-03-13 22:13:53 +0000984 DAG.getConstant(N1C->getAPIntValue()+
985 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000986 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000987 // reassociate add
988 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
989 if (RADD.Val != 0)
990 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000991 // fold ((0-A) + B) -> B-A
992 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
993 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000994 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000995 // fold (A + (0-B)) -> A-B
996 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
997 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000998 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000999 // fold (A+(B-A)) -> B
1000 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001001 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +00001002
Evan Cheng860771d2006-03-01 01:09:54 +00001003 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001004 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +00001005
1006 // fold (a+b) -> (a|b) iff a and b share no bits.
1007 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001008 APInt LHSZero, LHSOne;
1009 APInt RHSZero, RHSOne;
1010 APInt Mask = APInt::getAllOnesValue(MVT::getSizeInBits(VT));
Dan Gohmanea859be2007-06-22 14:59:07 +00001011 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001012 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001013 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +00001014
1015 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1016 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1017 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1018 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1019 return DAG.getNode(ISD::OR, VT, N0, N1);
1020 }
1021 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001022
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001023 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
1024 if (N0.getOpcode() == ISD::SHL && N0.Val->hasOneUse()) {
1025 SDOperand Result = combineShlAddConstant(N0, N1, DAG);
1026 if (Result.Val) return Result;
1027 }
1028 if (N1.getOpcode() == ISD::SHL && N1.Val->hasOneUse()) {
1029 SDOperand Result = combineShlAddConstant(N1, N0, DAG);
1030 if (Result.Val) return Result;
1031 }
1032
Evan Chengb13cdbd2007-06-21 07:39:16 +00001033 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
1034 if (N0.getOpcode() == ISD::SELECT && N0.Val->hasOneUse()) {
1035 SDOperand Result = combineSelectAndUse(N, N0, N1, DAG);
1036 if (Result.Val) return Result;
1037 }
1038 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1039 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1040 if (Result.Val) return Result;
1041 }
1042
Nate Begeman83e75ec2005-09-06 04:43:02 +00001043 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001044}
1045
Chris Lattner91153682007-03-04 20:03:15 +00001046SDOperand DAGCombiner::visitADDC(SDNode *N) {
1047 SDOperand N0 = N->getOperand(0);
1048 SDOperand N1 = N->getOperand(1);
1049 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1050 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1051 MVT::ValueType VT = N0.getValueType();
1052
1053 // If the flag result is dead, turn this into an ADD.
1054 if (N->hasNUsesOfValue(0, 1))
1055 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +00001056 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +00001057
1058 // canonicalize constant to RHS.
Chris Lattnerbcf24842007-03-04 20:08:45 +00001059 if (N0C && !N1C) {
1060 SDOperand Ops[] = { N1, N0 };
1061 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
1062 }
Chris Lattner91153682007-03-04 20:03:15 +00001063
Chris Lattnerb6541762007-03-04 20:40:38 +00001064 // fold (addc x, 0) -> x + no carry out
1065 if (N1C && N1C->isNullValue())
1066 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1067
1068 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001069 APInt LHSZero, LHSOne;
1070 APInt RHSZero, RHSOne;
1071 APInt Mask = APInt::getAllOnesValue(MVT::getSizeInBits(VT));
Dan Gohmanea859be2007-06-22 14:59:07 +00001072 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001073 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001074 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001075
1076 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1077 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1078 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1079 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1080 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
1081 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1082 }
Chris Lattner91153682007-03-04 20:03:15 +00001083
1084 return SDOperand();
1085}
1086
1087SDOperand DAGCombiner::visitADDE(SDNode *N) {
1088 SDOperand N0 = N->getOperand(0);
1089 SDOperand N1 = N->getOperand(1);
Chris Lattnerb6541762007-03-04 20:40:38 +00001090 SDOperand CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001091 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1092 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Chris Lattnerbcf24842007-03-04 20:08:45 +00001093 //MVT::ValueType VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001094
1095 // canonicalize constant to RHS
Chris Lattnerbcf24842007-03-04 20:08:45 +00001096 if (N0C && !N1C) {
Chris Lattnerb6541762007-03-04 20:40:38 +00001097 SDOperand Ops[] = { N1, N0, CarryIn };
Chris Lattnerbcf24842007-03-04 20:08:45 +00001098 return DAG.getNode(ISD::ADDE, N->getVTList(), Ops, 3);
1099 }
Chris Lattner91153682007-03-04 20:03:15 +00001100
Chris Lattnerb6541762007-03-04 20:40:38 +00001101 // fold (adde x, y, false) -> (addc x, y)
1102 if (CarryIn.getOpcode() == ISD::CARRY_FALSE) {
1103 SDOperand Ops[] = { N1, N0 };
1104 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
1105 }
Chris Lattner91153682007-03-04 20:03:15 +00001106
1107 return SDOperand();
1108}
1109
1110
1111
Nate Begeman83e75ec2005-09-06 04:43:02 +00001112SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001113 SDOperand N0 = N->getOperand(0);
1114 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001115 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1116 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001117 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001118
Dan Gohman7f321562007-06-25 16:23:39 +00001119 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001120 if (MVT::isVector(VT)) {
1121 SDOperand FoldedVOp = SimplifyVBinOp(N);
1122 if (FoldedVOp.Val) return FoldedVOp;
1123 }
Dan Gohman7f321562007-06-25 16:23:39 +00001124
Chris Lattner854077d2005-10-17 01:07:11 +00001125 // fold (sub x, x) -> 0
Evan Chengc8e3b142008-03-12 07:02:50 +00001126 if (N0 == N1)
Chris Lattner854077d2005-10-17 01:07:11 +00001127 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001128 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001129 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001130 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +00001131 // fold (sub x, c) -> (add x, -c)
1132 if (N1C)
Dan Gohman002e5d02008-03-13 22:13:53 +00001133 return DAG.getNode(ISD::ADD, VT, N0,
1134 DAG.getConstant(-N1C->getAPIntValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001135 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001136 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001137 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001138 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001139 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001140 return N0.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +00001141 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
1142 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1143 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1144 if (Result.Val) return Result;
1145 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001146 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001147 if (N0.getOpcode() == ISD::UNDEF)
1148 return N0;
1149 if (N1.getOpcode() == ISD::UNDEF)
1150 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001151
Nate Begeman83e75ec2005-09-06 04:43:02 +00001152 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001153}
1154
Nate Begeman83e75ec2005-09-06 04:43:02 +00001155SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001156 SDOperand N0 = N->getOperand(0);
1157 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001158 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1159 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +00001160 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001161
Dan Gohman7f321562007-06-25 16:23:39 +00001162 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001163 if (MVT::isVector(VT)) {
1164 SDOperand FoldedVOp = SimplifyVBinOp(N);
1165 if (FoldedVOp.Val) return FoldedVOp;
1166 }
Dan Gohman7f321562007-06-25 16:23:39 +00001167
Dan Gohman613e0d82007-07-03 14:03:57 +00001168 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001169 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001170 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001171 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001172 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001173 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001174 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001175 if (N0C && !N1C)
1176 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001177 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001178 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001179 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001180 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001181 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001182 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001183 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001184 if (N1C && N1C->getAPIntValue().isPowerOf2())
Chris Lattner3e6099b2005-10-30 06:41:49 +00001185 return DAG.getNode(ISD::SHL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001186 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001187 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001188 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
1189 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
1190 // FIXME: If the input is something that is easily negated (e.g. a
1191 // single-use add), we should put the negate there.
1192 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1193 DAG.getNode(ISD::SHL, VT, N0,
1194 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
1195 TLI.getShiftAmountTy())));
1196 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001197
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001198 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1199 if (N1C && N0.getOpcode() == ISD::SHL &&
1200 isa<ConstantSDNode>(N0.getOperand(1))) {
1201 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +00001202 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001203 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1204 }
1205
1206 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1207 // use.
1208 {
1209 SDOperand Sh(0,0), Y(0,0);
1210 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1211 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
1212 N0.Val->hasOneUse()) {
1213 Sh = N0; Y = N1;
1214 } else if (N1.getOpcode() == ISD::SHL &&
1215 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
1216 Sh = N1; Y = N0;
1217 }
1218 if (Sh.Val) {
1219 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
1220 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1221 }
1222 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001223 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
1224 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1225 isa<ConstantSDNode>(N0.getOperand(1))) {
1226 return DAG.getNode(ISD::ADD, VT,
1227 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1228 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1229 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001230
Nate Begemancd4d58c2006-02-03 06:46:56 +00001231 // reassociate mul
1232 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
1233 if (RMUL.Val != 0)
1234 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001235
Nate Begeman83e75ec2005-09-06 04:43:02 +00001236 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001237}
1238
Nate Begeman83e75ec2005-09-06 04:43:02 +00001239SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001240 SDOperand N0 = N->getOperand(0);
1241 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001242 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1243 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001244 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001245
Dan Gohman7f321562007-06-25 16:23:39 +00001246 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001247 if (MVT::isVector(VT)) {
1248 SDOperand FoldedVOp = SimplifyVBinOp(N);
1249 if (FoldedVOp.Val) return FoldedVOp;
1250 }
Dan Gohman7f321562007-06-25 16:23:39 +00001251
Nate Begeman1d4d4142005-09-01 00:19:25 +00001252 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001253 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001254 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001255 // fold (sdiv X, 1) -> X
1256 if (N1C && N1C->getSignExtended() == 1LL)
1257 return N0;
1258 // fold (sdiv X, -1) -> 0-X
1259 if (N1C && N1C->isAllOnesValue())
1260 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001261 // If we know the sign bits of both operands are zero, strength reduce to a
1262 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Chris Lattnerf32aac32008-01-27 23:32:17 +00001263 if (!MVT::isVector(VT)) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001264 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattnerf32aac32008-01-27 23:32:17 +00001265 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
1266 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001267 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman002e5d02008-03-13 22:13:53 +00001268 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001269 (isPowerOf2_64(N1C->getSignExtended()) ||
1270 isPowerOf2_64(-N1C->getSignExtended()))) {
1271 // If dividing by powers of two is cheap, then don't perform the following
1272 // fold.
1273 if (TLI.isPow2DivCheap())
1274 return SDOperand();
1275 int64_t pow2 = N1C->getSignExtended();
1276 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001277 unsigned lg2 = Log2_64(abs2);
1278 // Splat the sign bit into the register
1279 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001280 DAG.getConstant(MVT::getSizeInBits(VT)-1,
1281 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00001282 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +00001283 // Add (N0 < 0) ? abs2 - 1 : 0;
1284 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
1285 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001286 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +00001287 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +00001288 AddToWorkList(SRL.Val);
1289 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +00001290 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
1291 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001292 // If we're dividing by a positive value, we're done. Otherwise, we must
1293 // negate the result.
1294 if (pow2 > 0)
1295 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +00001296 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001297 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1298 }
Nate Begeman69575232005-10-20 02:15:44 +00001299 // if integer divide is expensive and we satisfy the requirements, emit an
1300 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001301 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001302 !TLI.isIntDivCheap()) {
1303 SDOperand Op = BuildSDIV(N);
1304 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001305 }
Dan Gohman7f321562007-06-25 16:23:39 +00001306
Dan Gohman613e0d82007-07-03 14:03:57 +00001307 // undef / X -> 0
1308 if (N0.getOpcode() == ISD::UNDEF)
1309 return DAG.getConstant(0, VT);
1310 // X / undef -> undef
1311 if (N1.getOpcode() == ISD::UNDEF)
1312 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001313
Nate Begeman83e75ec2005-09-06 04:43:02 +00001314 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001315}
1316
Nate Begeman83e75ec2005-09-06 04:43:02 +00001317SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001318 SDOperand N0 = N->getOperand(0);
1319 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001320 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1321 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001322 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001323
Dan Gohman7f321562007-06-25 16:23:39 +00001324 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001325 if (MVT::isVector(VT)) {
1326 SDOperand FoldedVOp = SimplifyVBinOp(N);
1327 if (FoldedVOp.Val) return FoldedVOp;
1328 }
Dan Gohman7f321562007-06-25 16:23:39 +00001329
Nate Begeman1d4d4142005-09-01 00:19:25 +00001330 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001331 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001332 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001333 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001334 if (N1C && N1C->getAPIntValue().isPowerOf2())
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001335 return DAG.getNode(ISD::SRL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001336 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001337 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001338 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1339 if (N1.getOpcode() == ISD::SHL) {
1340 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001341 if (SHC->getAPIntValue().isPowerOf2()) {
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001342 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +00001343 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001344 DAG.getConstant(SHC->getAPIntValue()
1345 .logBase2(),
Nate Begemanc031e332006-02-05 07:36:48 +00001346 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001347 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001348 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001349 }
1350 }
1351 }
Nate Begeman69575232005-10-20 02:15:44 +00001352 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001353 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Chris Lattnere9936d12005-10-22 18:50:15 +00001354 SDOperand Op = BuildUDIV(N);
1355 if (Op.Val) return Op;
1356 }
Dan Gohman7f321562007-06-25 16:23:39 +00001357
Dan Gohman613e0d82007-07-03 14:03:57 +00001358 // undef / X -> 0
1359 if (N0.getOpcode() == ISD::UNDEF)
1360 return DAG.getConstant(0, VT);
1361 // X / undef -> undef
1362 if (N1.getOpcode() == ISD::UNDEF)
1363 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001364
Nate Begeman83e75ec2005-09-06 04:43:02 +00001365 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001366}
1367
Nate Begeman83e75ec2005-09-06 04:43:02 +00001368SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001369 SDOperand N0 = N->getOperand(0);
1370 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001371 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1372 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001373 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001374
1375 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001376 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001377 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001378 // If we know the sign bits of both operands are zero, strength reduce to a
1379 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Chris Lattneree339f42008-01-27 23:21:58 +00001380 if (!MVT::isVector(VT)) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001381 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattneree339f42008-01-27 23:21:58 +00001382 return DAG.getNode(ISD::UREM, VT, N0, N1);
1383 }
Chris Lattner26d29902006-10-12 20:58:32 +00001384
Dan Gohman77003042007-11-26 23:46:11 +00001385 // If X/C can be simplified by the division-by-constant logic, lower
1386 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001387 if (N1C && !N1C->isNullValue()) {
1388 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
Chris Lattner5eee4272008-01-26 01:09:19 +00001389 AddToWorkList(Div.Val);
Dan Gohman77003042007-11-26 23:46:11 +00001390 SDOperand OptimizedDiv = combine(Div.Val);
1391 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1392 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1393 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1394 AddToWorkList(Mul.Val);
1395 return Sub;
1396 }
Chris Lattner26d29902006-10-12 20:58:32 +00001397 }
1398
Dan Gohman613e0d82007-07-03 14:03:57 +00001399 // undef % X -> 0
1400 if (N0.getOpcode() == ISD::UNDEF)
1401 return DAG.getConstant(0, VT);
1402 // X % undef -> undef
1403 if (N1.getOpcode() == ISD::UNDEF)
1404 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001405
Nate Begeman83e75ec2005-09-06 04:43:02 +00001406 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001407}
1408
Nate Begeman83e75ec2005-09-06 04:43:02 +00001409SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001410 SDOperand N0 = N->getOperand(0);
1411 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001412 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1413 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001414 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001415
1416 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001417 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001418 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001419 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001420 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
1421 return DAG.getNode(ISD::AND, VT, N0,
1422 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001423 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1424 if (N1.getOpcode() == ISD::SHL) {
1425 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001426 if (SHC->getAPIntValue().isPowerOf2()) {
1427 SDOperand Add =
1428 DAG.getNode(ISD::ADD, VT, N1,
1429 DAG.getConstant(APInt::getAllOnesValue(MVT::getSizeInBits(VT)),
1430 VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001431 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001432 return DAG.getNode(ISD::AND, VT, N0, Add);
1433 }
1434 }
1435 }
Chris Lattner26d29902006-10-12 20:58:32 +00001436
Dan Gohman77003042007-11-26 23:46:11 +00001437 // If X/C can be simplified by the division-by-constant logic, lower
1438 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001439 if (N1C && !N1C->isNullValue()) {
1440 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
Dan Gohman77003042007-11-26 23:46:11 +00001441 SDOperand OptimizedDiv = combine(Div.Val);
1442 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1443 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1444 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1445 AddToWorkList(Mul.Val);
1446 return Sub;
1447 }
Chris Lattner26d29902006-10-12 20:58:32 +00001448 }
1449
Dan Gohman613e0d82007-07-03 14:03:57 +00001450 // undef % X -> 0
1451 if (N0.getOpcode() == ISD::UNDEF)
1452 return DAG.getConstant(0, VT);
1453 // X % undef -> undef
1454 if (N1.getOpcode() == ISD::UNDEF)
1455 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001456
Nate Begeman83e75ec2005-09-06 04:43:02 +00001457 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001458}
1459
Nate Begeman83e75ec2005-09-06 04:43:02 +00001460SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001461 SDOperand N0 = N->getOperand(0);
1462 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001463 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001464 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001465
1466 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001467 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001468 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001469 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001470 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001471 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1472 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001473 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001474 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001475 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001476 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001477
Nate Begeman83e75ec2005-09-06 04:43:02 +00001478 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001479}
1480
Nate Begeman83e75ec2005-09-06 04:43:02 +00001481SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001482 SDOperand N0 = N->getOperand(0);
1483 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001484 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001485 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001486
1487 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001488 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001489 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001490 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00001491 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001492 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001493 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001494 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001495 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001496
Nate Begeman83e75ec2005-09-06 04:43:02 +00001497 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001498}
1499
Dan Gohman389079b2007-10-08 17:57:15 +00001500/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1501/// compute two values. LoOp and HiOp give the opcodes for the two computations
1502/// that are being performed. Return true if a simplification was made.
1503///
Chris Lattner5eee4272008-01-26 01:09:19 +00001504SDOperand DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1505 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001506 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001507 bool HiExists = N->hasAnyUseOfValue(1);
1508 if (!HiExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001509 (!AfterLegalize ||
1510 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001511 SDOperand Res = DAG.getNode(LoOp, N->getValueType(0), N->op_begin(),
1512 N->getNumOperands());
1513 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001514 }
1515
1516 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001517 bool LoExists = N->hasAnyUseOfValue(0);
1518 if (!LoExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001519 (!AfterLegalize ||
1520 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001521 SDOperand Res = DAG.getNode(HiOp, N->getValueType(1), N->op_begin(),
1522 N->getNumOperands());
1523 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001524 }
1525
Evan Cheng44711942007-11-08 09:25:29 +00001526 // If both halves are used, return as it is.
1527 if (LoExists && HiExists)
Chris Lattner5eee4272008-01-26 01:09:19 +00001528 return SDOperand();
Evan Cheng44711942007-11-08 09:25:29 +00001529
1530 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001531 if (LoExists) {
1532 SDOperand Lo = DAG.getNode(LoOp, N->getValueType(0),
1533 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001534 AddToWorkList(Lo.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001535 SDOperand LoOpt = combine(Lo.Val);
Chris Lattner5eee4272008-01-26 01:09:19 +00001536 if (LoOpt.Val && LoOpt.Val != Lo.Val &&
1537 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType()))
1538 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001539 }
1540
Evan Cheng44711942007-11-08 09:25:29 +00001541 if (HiExists) {
1542 SDOperand Hi = DAG.getNode(HiOp, N->getValueType(1),
1543 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001544 AddToWorkList(Hi.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001545 SDOperand HiOpt = combine(Hi.Val);
1546 if (HiOpt.Val && HiOpt != Hi &&
Chris Lattner5eee4272008-01-26 01:09:19 +00001547 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType()))
1548 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001549 }
Chris Lattner5eee4272008-01-26 01:09:19 +00001550 return SDOperand();
Dan Gohman389079b2007-10-08 17:57:15 +00001551}
1552
1553SDOperand DAGCombiner::visitSMUL_LOHI(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001554 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
1555 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001556
1557 return SDOperand();
1558}
1559
1560SDOperand DAGCombiner::visitUMUL_LOHI(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001561 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
1562 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001563
1564 return SDOperand();
1565}
1566
1567SDOperand DAGCombiner::visitSDIVREM(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001568 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
1569 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001570
1571 return SDOperand();
1572}
1573
1574SDOperand DAGCombiner::visitUDIVREM(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001575 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
1576 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001577
1578 return SDOperand();
1579}
1580
Chris Lattner35e5c142006-05-05 05:51:50 +00001581/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1582/// two operands of the same opcode, try to simplify it.
1583SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1584 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
1585 MVT::ValueType VT = N0.getValueType();
1586 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1587
Chris Lattner540121f2006-05-05 06:31:05 +00001588 // For each of OP in AND/OR/XOR:
1589 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1590 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1591 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001592 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001593 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001594 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001595 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1596 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1597 N0.getOperand(0).getValueType(),
1598 N0.getOperand(0), N1.getOperand(0));
1599 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001600 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001601 }
1602
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001603 // For each of OP in SHL/SRL/SRA/AND...
1604 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1605 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1606 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001607 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001608 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001609 N0.getOperand(1) == N1.getOperand(1)) {
1610 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1611 N0.getOperand(0).getValueType(),
1612 N0.getOperand(0), N1.getOperand(0));
1613 AddToWorkList(ORNode.Val);
1614 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1615 }
1616
1617 return SDOperand();
1618}
1619
Nate Begeman83e75ec2005-09-06 04:43:02 +00001620SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001621 SDOperand N0 = N->getOperand(0);
1622 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001623 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001624 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1625 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001626 MVT::ValueType VT = N1.getValueType();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001627 unsigned BitWidth = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001628
Dan Gohman7f321562007-06-25 16:23:39 +00001629 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001630 if (MVT::isVector(VT)) {
1631 SDOperand FoldedVOp = SimplifyVBinOp(N);
1632 if (FoldedVOp.Val) return FoldedVOp;
1633 }
Dan Gohman7f321562007-06-25 16:23:39 +00001634
Dan Gohman613e0d82007-07-03 14:03:57 +00001635 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001636 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001637 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001638 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001639 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001640 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001641 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001642 if (N0C && !N1C)
1643 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001644 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001645 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001646 return N0;
1647 // if (and x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001648 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0),
1649 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001650 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001651 // reassociate and
1652 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1653 if (RAND.Val != 0)
1654 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001655 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001656 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001657 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001658 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001659 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001660 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1661 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001662 SDOperand N0Op0 = N0.getOperand(0);
1663 APInt Mask = ~N1C->getAPIntValue();
1664 Mask.trunc(N0Op0.getValueSizeInBits());
1665 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001666 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001667 N0Op0);
Chris Lattner1ec05d12006-03-01 21:47:21 +00001668
1669 // Replace uses of the AND with uses of the Zero extend node.
1670 CombineTo(N, Zext);
1671
Chris Lattner3603cd62006-02-02 07:17:31 +00001672 // We actually want to replace all uses of the any_extend with the
1673 // zero_extend, to avoid duplicating things. This will later cause this
1674 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001675 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001676 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001677 }
1678 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001679 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1680 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1681 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1682 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1683
1684 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1685 MVT::isInteger(LL.getValueType())) {
1686 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001687 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001688 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001689 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001690 return DAG.getSetCC(VT, ORNode, LR, Op1);
1691 }
1692 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1693 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1694 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001695 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001696 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1697 }
1698 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1699 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1700 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001701 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001702 return DAG.getSetCC(VT, ORNode, LR, Op1);
1703 }
1704 }
1705 // canonicalize equivalent to ll == rl
1706 if (LL == RR && LR == RL) {
1707 Op1 = ISD::getSetCCSwappedOperands(Op1);
1708 std::swap(RL, RR);
1709 }
1710 if (LL == RL && LR == RR) {
1711 bool isInteger = MVT::isInteger(LL.getValueType());
1712 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1713 if (Result != ISD::SETCC_INVALID)
1714 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1715 }
1716 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001717
1718 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1719 if (N0.getOpcode() == N1.getOpcode()) {
1720 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1721 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001722 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001723
Nate Begemande996292006-02-03 22:24:05 +00001724 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1725 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001726 if (!MVT::isVector(VT) &&
1727 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001728 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001729 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Cheng83060c52007-03-07 08:07:03 +00001730 if (ISD::isEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001731 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001732 MVT::ValueType EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001733 // If we zero all the possible extended bits, then we can turn this into
1734 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001735 unsigned BitWidth = N1.getValueSizeInBits();
1736 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
1737 BitWidth - MVT::getSizeInBits(EVT))) &&
Evan Chengc5484282006-10-04 00:56:09 +00001738 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001739 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1740 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001741 LN0->getSrcValueOffset(), EVT,
1742 LN0->isVolatile(),
1743 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001744 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001745 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001746 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001747 }
1748 }
1749 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00001750 if (ISD::isSEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
1751 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001752 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001753 MVT::ValueType EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001754 // If we zero all the possible extended bits, then we can turn this into
1755 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001756 unsigned BitWidth = N1.getValueSizeInBits();
1757 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
1758 BitWidth - MVT::getSizeInBits(EVT))) &&
Evan Chengc5484282006-10-04 00:56:09 +00001759 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001760 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1761 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001762 LN0->getSrcValueOffset(), EVT,
1763 LN0->isVolatile(),
1764 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001765 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001766 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001767 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001768 }
1769 }
Chris Lattner15045b62006-02-28 06:35:35 +00001770
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001771 // fold (and (load x), 255) -> (zextload x, i8)
1772 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001773 if (N1C && N0.getOpcode() == ISD::LOAD) {
1774 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1775 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Chris Lattnerddf89562008-01-17 19:59:44 +00001776 LN0->isUnindexed() && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001777 MVT::ValueType EVT, LoadedVT;
Dan Gohman002e5d02008-03-13 22:13:53 +00001778 if (N1C->getAPIntValue() == 255)
Evan Cheng466685d2006-10-09 20:57:25 +00001779 EVT = MVT::i8;
Dan Gohman002e5d02008-03-13 22:13:53 +00001780 else if (N1C->getAPIntValue() == 65535)
Evan Cheng466685d2006-10-09 20:57:25 +00001781 EVT = MVT::i16;
Dan Gohman002e5d02008-03-13 22:13:53 +00001782 else if (N1C->getAPIntValue() == ~0U)
Evan Cheng466685d2006-10-09 20:57:25 +00001783 EVT = MVT::i32;
1784 else
1785 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001786
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001787 LoadedVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001788 if (EVT != MVT::Other && LoadedVT > EVT &&
1789 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1790 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1791 // For big endian targets, we need to add an offset to the pointer to
1792 // load the correct bytes. For little endian systems, we merely need to
1793 // read fewer bytes from the same pointer.
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001794 unsigned LVTStoreBytes = MVT::getStoreSizeInBits(LoadedVT)/8;
1795 unsigned EVTStoreBytes = MVT::getStoreSizeInBits(EVT)/8;
1796 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001797 unsigned Alignment = LN0->getAlignment();
Evan Cheng466685d2006-10-09 20:57:25 +00001798 SDOperand NewPtr = LN0->getBasePtr();
Duncan Sands0753fc12008-02-11 10:37:04 +00001799 if (TLI.isBigEndian()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001800 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1801 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001802 Alignment = MinAlign(Alignment, PtrOff);
1803 }
Evan Cheng466685d2006-10-09 20:57:25 +00001804 AddToWorkList(NewPtr.Val);
1805 SDOperand Load =
1806 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001807 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00001808 LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001809 AddToWorkList(N);
1810 CombineTo(N0.Val, Load, Load.getValue(1));
1811 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1812 }
Chris Lattner15045b62006-02-28 06:35:35 +00001813 }
1814 }
1815
Nate Begeman83e75ec2005-09-06 04:43:02 +00001816 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001817}
1818
Nate Begeman83e75ec2005-09-06 04:43:02 +00001819SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001820 SDOperand N0 = N->getOperand(0);
1821 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001822 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001823 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1824 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001825 MVT::ValueType VT = N1.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001826
Dan Gohman7f321562007-06-25 16:23:39 +00001827 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001828 if (MVT::isVector(VT)) {
1829 SDOperand FoldedVOp = SimplifyVBinOp(N);
1830 if (FoldedVOp.Val) return FoldedVOp;
1831 }
Dan Gohman7f321562007-06-25 16:23:39 +00001832
Dan Gohman613e0d82007-07-03 14:03:57 +00001833 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001834 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001835 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001836 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001837 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001838 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001839 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001840 if (N0C && !N1C)
1841 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001842 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001843 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001844 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001845 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001846 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001847 return N1;
1848 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001849 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001850 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001851 // reassociate or
1852 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1853 if (ROR.Val != 0)
1854 return ROR;
1855 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1856 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001857 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001858 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1859 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1860 N1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001861 DAG.getConstant(N1C->getAPIntValue() |
1862 C1->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001863 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001864 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1865 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1866 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1867 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1868
1869 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1870 MVT::isInteger(LL.getValueType())) {
1871 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1872 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001873 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001874 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1875 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001876 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001877 return DAG.getSetCC(VT, ORNode, LR, Op1);
1878 }
1879 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1880 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1881 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1882 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1883 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001884 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001885 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1886 }
1887 }
1888 // canonicalize equivalent to ll == rl
1889 if (LL == RR && LR == RL) {
1890 Op1 = ISD::getSetCCSwappedOperands(Op1);
1891 std::swap(RL, RR);
1892 }
1893 if (LL == RL && LR == RR) {
1894 bool isInteger = MVT::isInteger(LL.getValueType());
1895 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1896 if (Result != ISD::SETCC_INVALID)
1897 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1898 }
1899 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001900
1901 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1902 if (N0.getOpcode() == N1.getOpcode()) {
1903 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1904 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001905 }
Chris Lattner516b9622006-09-14 20:50:57 +00001906
Chris Lattner1ec72732006-09-14 21:11:37 +00001907 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1908 if (N0.getOpcode() == ISD::AND &&
1909 N1.getOpcode() == ISD::AND &&
1910 N0.getOperand(1).getOpcode() == ISD::Constant &&
1911 N1.getOperand(1).getOpcode() == ISD::Constant &&
1912 // Don't increase # computations.
1913 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1914 // We can only do this xform if we know that bits from X that are set in C2
1915 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001916 const APInt &LHSMask =
1917 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1918 const APInt &RHSMask =
1919 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Chris Lattner1ec72732006-09-14 21:11:37 +00001920
Dan Gohmanea859be2007-06-22 14:59:07 +00001921 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1922 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Chris Lattner1ec72732006-09-14 21:11:37 +00001923 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1924 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1925 }
1926 }
1927
1928
Chris Lattner516b9622006-09-14 20:50:57 +00001929 // See if this is some rotate idiom.
1930 if (SDNode *Rot = MatchRotate(N0, N1))
1931 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001932
Nate Begeman83e75ec2005-09-06 04:43:02 +00001933 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001934}
1935
Chris Lattner516b9622006-09-14 20:50:57 +00001936
1937/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1938static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1939 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001940 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001941 Mask = Op.getOperand(1);
1942 Op = Op.getOperand(0);
1943 } else {
1944 return false;
1945 }
1946 }
1947
1948 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1949 Shift = Op;
1950 return true;
1951 }
1952 return false;
1953}
1954
1955
1956// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1957// idioms for rotate, and if the target supports rotation instructions, generate
1958// a rot[lr].
1959SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1960 // Must be a legal type. Expanded an promoted things won't work with rotates.
1961 MVT::ValueType VT = LHS.getValueType();
1962 if (!TLI.isTypeLegal(VT)) return 0;
1963
1964 // The target must have at least one rotate flavor.
1965 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1966 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1967 if (!HasROTL && !HasROTR) return 0;
1968
1969 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1970 SDOperand LHSShift; // The shift.
1971 SDOperand LHSMask; // AND value if any.
1972 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1973 return 0; // Not part of a rotate.
1974
1975 SDOperand RHSShift; // The shift.
1976 SDOperand RHSMask; // AND value if any.
1977 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1978 return 0; // Not part of a rotate.
1979
1980 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1981 return 0; // Not shifting the same value.
1982
1983 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1984 return 0; // Shifts must disagree.
1985
1986 // Canonicalize shl to left side in a shl/srl pair.
1987 if (RHSShift.getOpcode() == ISD::SHL) {
1988 std::swap(LHS, RHS);
1989 std::swap(LHSShift, RHSShift);
1990 std::swap(LHSMask , RHSMask );
1991 }
1992
1993 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Scott Michelc9dc1142007-04-02 21:36:32 +00001994 SDOperand LHSShiftArg = LHSShift.getOperand(0);
1995 SDOperand LHSShiftAmt = LHSShift.getOperand(1);
1996 SDOperand RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00001997
1998 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1999 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00002000 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
2001 RHSShiftAmt.getOpcode() == ISD::Constant) {
2002 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getValue();
2003 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getValue();
Chris Lattner516b9622006-09-14 20:50:57 +00002004 if ((LShVal + RShVal) != OpSizeInBits)
2005 return 0;
2006
2007 SDOperand Rot;
2008 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002009 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002010 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002011 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002012
2013 // If there is an AND of either shifted operand, apply it to the result.
2014 if (LHSMask.Val || RHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00002015 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Chris Lattner516b9622006-09-14 20:50:57 +00002016
2017 if (LHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00002018 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2019 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002020 }
2021 if (RHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00002022 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2023 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002024 }
2025
2026 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
2027 }
2028
2029 return Rot.Val;
2030 }
2031
2032 // If there is a mask here, and we have a variable shift, we can't be sure
2033 // that we're masking out the right stuff.
2034 if (LHSMask.Val || RHSMask.Val)
2035 return 0;
2036
2037 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2038 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002039 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2040 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002041 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002042 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002043 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002044 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002045 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00002046 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002047 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002048 }
Chris Lattner516b9622006-09-14 20:50:57 +00002049 }
2050 }
2051
2052 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2053 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002054 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2055 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002056 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002057 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002058 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002059 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002060 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00002061 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002062 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002063 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002064 }
2065 }
2066
2067 // Look for sign/zext/any-extended cases:
2068 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2069 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2070 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
2071 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2072 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2073 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
2074 SDOperand LExtOp0 = LHSShiftAmt.getOperand(0);
2075 SDOperand RExtOp0 = RHSShiftAmt.getOperand(0);
2076 if (RExtOp0.getOpcode() == ISD::SUB &&
2077 RExtOp0.getOperand(1) == LExtOp0) {
2078 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2079 // (rotr x, y)
2080 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2081 // (rotl x, (sub 32, y))
2082 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002083 if (SUBC->getAPIntValue() == OpSizeInBits) {
Scott Michelc9dc1142007-04-02 21:36:32 +00002084 if (HasROTL)
2085 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2086 else
2087 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
2088 }
2089 }
2090 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2091 RExtOp0 == LExtOp0.getOperand(1)) {
2092 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2093 // (rotl x, y)
2094 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2095 // (rotr x, (sub 32, y))
2096 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002097 if (SUBC->getAPIntValue() == OpSizeInBits) {
Scott Michelc9dc1142007-04-02 21:36:32 +00002098 if (HasROTL)
2099 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, RHSShiftAmt).Val;
2100 else
2101 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2102 }
2103 }
Chris Lattner516b9622006-09-14 20:50:57 +00002104 }
2105 }
2106
2107 return 0;
2108}
2109
2110
Nate Begeman83e75ec2005-09-06 04:43:02 +00002111SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002112 SDOperand N0 = N->getOperand(0);
2113 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002114 SDOperand LHS, RHS, CC;
2115 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2116 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002117 MVT::ValueType VT = N0.getValueType();
2118
Dan Gohman7f321562007-06-25 16:23:39 +00002119 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00002120 if (MVT::isVector(VT)) {
2121 SDOperand FoldedVOp = SimplifyVBinOp(N);
2122 if (FoldedVOp.Val) return FoldedVOp;
2123 }
Dan Gohman7f321562007-06-25 16:23:39 +00002124
Evan Cheng26471c42008-03-25 20:08:07 +00002125 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
2126 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
2127 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00002128 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002129 if (N0.getOpcode() == ISD::UNDEF)
2130 return N0;
2131 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002132 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002133 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002134 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002135 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00002136 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002137 if (N0C && !N1C)
2138 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002139 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002140 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002141 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002142 // reassociate xor
2143 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
2144 if (RXOR.Val != 0)
2145 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002146 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00002147 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Nate Begeman646d7e22005-09-02 21:18:40 +00002148 bool isInt = MVT::isInteger(LHS.getValueType());
2149 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2150 isInt);
2151 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002152 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002153 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002154 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002155 assert(0 && "Unhandled SetCC Equivalent!");
2156 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002157 }
Chris Lattner61c5ff42007-09-10 21:39:07 +00002158 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002159 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Chris Lattner61c5ff42007-09-10 21:39:07 +00002160 N0.Val->hasOneUse() && isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
2161 SDOperand V = N0.getOperand(0);
2162 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002163 DAG.getConstant(1, V.getValueType()));
Chris Lattner61c5ff42007-09-10 21:39:07 +00002164 AddToWorkList(V.Val);
2165 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2166 }
2167
Nate Begeman99801192005-09-07 23:25:52 +00002168 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Dan Gohman002e5d02008-03-13 22:13:53 +00002169 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002170 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002171 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002172 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2173 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002174 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2175 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002176 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002177 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002178 }
2179 }
Nate Begeman99801192005-09-07 23:25:52 +00002180 // fold !(x or y) -> (!x and !y) iff x or y are constants
2181 if (N1C && N1C->isAllOnesValue() &&
2182 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002183 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002184 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2185 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002186 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2187 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002188 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002189 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002190 }
2191 }
Nate Begeman223df222005-09-08 20:18:10 +00002192 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2193 if (N1C && N0.getOpcode() == ISD::XOR) {
2194 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2195 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2196 if (N00C)
2197 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00002198 DAG.getConstant(N1C->getAPIntValue()^
2199 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002200 if (N01C)
2201 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
Dan Gohman002e5d02008-03-13 22:13:53 +00002202 DAG.getConstant(N1C->getAPIntValue()^
2203 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002204 }
2205 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002206 if (N0 == N1) {
2207 if (!MVT::isVector(VT)) {
2208 return DAG.getConstant(0, VT);
2209 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
2210 // Produce a vector of zeros.
Dan Gohman51eaa862007-06-14 22:58:02 +00002211 SDOperand El = DAG.getConstant(0, MVT::getVectorElementType(VT));
Chris Lattner4fbdd592006-03-28 19:11:05 +00002212 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002213 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002214 }
2215 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002216
2217 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2218 if (N0.getOpcode() == N1.getOpcode()) {
2219 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
2220 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002221 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002222
Chris Lattner3e104b12006-04-08 04:15:24 +00002223 // Simplify the expression using non-local knowledge.
2224 if (!MVT::isVector(VT) &&
2225 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002226 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002227
Nate Begeman83e75ec2005-09-06 04:43:02 +00002228 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002229}
2230
Chris Lattnere70da202007-12-06 07:33:36 +00002231/// visitShiftByConstant - Handle transforms common to the three shifts, when
2232/// the shift amount is a constant.
2233SDOperand DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
2234 SDNode *LHS = N->getOperand(0).Val;
2235 if (!LHS->hasOneUse()) return SDOperand();
2236
2237 // We want to pull some binops through shifts, so that we have (and (shift))
2238 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2239 // thing happens with address calculations, so it's important to canonicalize
2240 // it.
2241 bool HighBitSet = false; // Can we transform this if the high bit is set?
2242
2243 switch (LHS->getOpcode()) {
2244 default: return SDOperand();
2245 case ISD::OR:
2246 case ISD::XOR:
2247 HighBitSet = false; // We can only transform sra if the high bit is clear.
2248 break;
2249 case ISD::AND:
2250 HighBitSet = true; // We can only transform sra if the high bit is set.
2251 break;
2252 case ISD::ADD:
2253 if (N->getOpcode() != ISD::SHL)
2254 return SDOperand(); // only shl(add) not sr[al](add).
2255 HighBitSet = false; // We can only transform sra if the high bit is clear.
2256 break;
2257 }
2258
2259 // We require the RHS of the binop to be a constant as well.
2260 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
2261 if (!BinOpCst) return SDOperand();
2262
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002263
2264 // FIXME: disable this for unless the input to the binop is a shift by a
2265 // constant. If it is not a shift, it pessimizes some common cases like:
2266 //
2267 //void foo(int *X, int i) { X[i & 1235] = 1; }
2268 //int bar(int *X, int i) { return X[i & 255]; }
2269 SDNode *BinOpLHSVal = LHS->getOperand(0).Val;
2270 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2271 BinOpLHSVal->getOpcode() != ISD::SRA &&
2272 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2273 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
2274 return SDOperand();
2275
Chris Lattnere70da202007-12-06 07:33:36 +00002276 MVT::ValueType VT = N->getValueType(0);
2277
2278 // If this is a signed shift right, and the high bit is modified
2279 // by the logical operation, do not perform the transformation.
2280 // The highBitSet boolean indicates the value of the high bit of
2281 // the constant which would cause it to be modified for this
2282 // operation.
2283 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00002284 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2285 if (BinOpRHSSignSet != HighBitSet)
Chris Lattnere70da202007-12-06 07:33:36 +00002286 return SDOperand();
2287 }
2288
2289 // Fold the constants, shifting the binop RHS by the shift amount.
2290 SDOperand NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
2291 LHS->getOperand(1), N->getOperand(1));
2292
2293 // Create the new shift.
2294 SDOperand NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
2295 N->getOperand(1));
2296
2297 // Create the new binop.
2298 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2299}
2300
2301
Nate Begeman83e75ec2005-09-06 04:43:02 +00002302SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002303 SDOperand N0 = N->getOperand(0);
2304 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002305 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2306 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002307 MVT::ValueType VT = N0.getValueType();
2308 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2309
2310 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002311 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002312 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002313 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002314 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002315 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002316 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002317 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002318 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002319 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002320 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002321 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002322 // if (shl x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002323 if (DAG.MaskedValueIsZero(SDOperand(N, 0),
2324 APInt::getAllOnesValue(MVT::getSizeInBits(VT))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002325 return DAG.getConstant(0, VT);
Chris Lattner61a4c072007-04-18 03:06:49 +00002326 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002327 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002328 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002329 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002330 N0.getOperand(1).getOpcode() == ISD::Constant) {
2331 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002332 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002333 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002334 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002335 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002336 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002337 }
2338 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2339 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002340 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002341 N0.getOperand(1).getOpcode() == ISD::Constant) {
2342 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002343 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002344 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
2345 DAG.getConstant(~0ULL << c1, VT));
2346 if (c2 > c1)
2347 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002348 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002349 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002350 return DAG.getNode(ISD::SRL, VT, Mask,
2351 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002352 }
2353 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002354 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002355 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002356 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnere70da202007-12-06 07:33:36 +00002357
2358 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002359}
2360
Nate Begeman83e75ec2005-09-06 04:43:02 +00002361SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002362 SDOperand N0 = N->getOperand(0);
2363 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002364 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2365 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002366 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002367
2368 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002369 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002370 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002371 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002372 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002373 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002374 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002375 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002376 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002377 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00002378 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002379 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002380 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002381 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002382 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002383 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2384 // sext_inreg.
2385 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
2386 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
2387 MVT::ValueType EVT;
2388 switch (LowBits) {
2389 default: EVT = MVT::Other; break;
2390 case 1: EVT = MVT::i1; break;
2391 case 8: EVT = MVT::i8; break;
2392 case 16: EVT = MVT::i16; break;
2393 case 32: EVT = MVT::i32; break;
2394 }
2395 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
2396 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2397 DAG.getValueType(EVT));
2398 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002399
2400 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2401 if (N1C && N0.getOpcode() == ISD::SRA) {
2402 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2403 unsigned Sum = N1C->getValue() + C1->getValue();
2404 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
2405 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2406 DAG.getConstant(Sum, N1C->getValueType(0)));
2407 }
2408 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00002409
2410 // fold sra (shl X, m), result_size - n
2411 // -> (sign_extend (trunc (shl X, result_size - n - m))) for
Christopher Lambb9b04282008-03-20 04:31:39 +00002412 // result_size - n != m.
2413 // If truncate is free for the target sext(shl) is likely to result in better
2414 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002415 if (N0.getOpcode() == ISD::SHL) {
2416 // Get the two constanst of the shifts, CN0 = m, CN = n.
2417 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2418 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002419 // Determine what the truncate's result bitsize and type would be.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002420 unsigned VTValSize = MVT::getSizeInBits(VT);
2421 MVT::ValueType TruncVT = MVT::getIntegerType(VTValSize - N1C->getValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00002422 // Determine the residual right-shift amount.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002423 unsigned ShiftAmt = N1C->getValue() - N01C->getValue();
Christopher Lambb9b04282008-03-20 04:31:39 +00002424
2425 // If the shift is not a no-op (in which case this should be just a sign
2426 // extend already), the truncated to type is legal, sign_extend is legal
2427 // on that type, and the the truncate to that type is both legal and free,
2428 // perform the transform.
2429 if (ShiftAmt &&
2430 TLI.isTypeLegal(TruncVT) &&
2431 TLI.isOperationLegal(ISD::SIGN_EXTEND, TruncVT) &&
2432 TLI.isOperationLegal(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00002433 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002434
2435 SDOperand Amt = DAG.getConstant(ShiftAmt, TLI.getShiftAmountTy());
2436 SDOperand Shift = DAG.getNode(ISD::SRL, VT, N0.getOperand(0), Amt);
2437 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, TruncVT, Shift);
2438 return DAG.getNode(ISD::SIGN_EXTEND, N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00002439 }
2440 }
2441 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002442
Chris Lattnera8504462006-05-08 20:51:54 +00002443 // Simplify, based on bits shifted out of the LHS.
2444 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2445 return SDOperand(N, 0);
2446
2447
Nate Begeman1d4d4142005-09-01 00:19:25 +00002448 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002449 if (DAG.SignBitIsZero(N0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002450 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002451
2452 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002453}
2454
Nate Begeman83e75ec2005-09-06 04:43:02 +00002455SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002456 SDOperand N0 = N->getOperand(0);
2457 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002458 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2459 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002460 MVT::ValueType VT = N0.getValueType();
2461 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2462
2463 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002464 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002465 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002466 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002467 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002468 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002469 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002470 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002471 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002472 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002473 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002474 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002475 // if (srl x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002476 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0),
2477 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002478 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002479
Nate Begeman1d4d4142005-09-01 00:19:25 +00002480 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002481 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002482 N0.getOperand(1).getOpcode() == ISD::Constant) {
2483 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002484 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002485 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002486 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002487 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002488 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002489 }
Chris Lattner350bec02006-04-02 06:11:11 +00002490
Chris Lattner06afe072006-05-05 22:53:17 +00002491 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2492 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2493 // Shifting in all undef bits?
2494 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
2495 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
2496 return DAG.getNode(ISD::UNDEF, VT);
2497
2498 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
2499 AddToWorkList(SmallShift.Val);
2500 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2501 }
2502
Chris Lattner3657ffe2006-10-12 20:23:19 +00002503 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2504 // bit, which is unmodified by sra.
2505 if (N1C && N1C->getValue()+1 == MVT::getSizeInBits(VT)) {
2506 if (N0.getOpcode() == ISD::SRA)
2507 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2508 }
2509
Chris Lattner350bec02006-04-02 06:11:11 +00002510 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2511 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Dan Gohman002e5d02008-03-13 22:13:53 +00002512 N1C->getAPIntValue() == Log2_32(MVT::getSizeInBits(VT))) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00002513 APInt KnownZero, KnownOne;
2514 APInt Mask = APInt::getAllOnesValue(MVT::getSizeInBits(VT));
Dan Gohmanea859be2007-06-22 14:59:07 +00002515 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002516
2517 // If any of the input bits are KnownOne, then the input couldn't be all
2518 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002519 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Chris Lattner350bec02006-04-02 06:11:11 +00002520
2521 // If all of the bits input the to ctlz node are known to be zero, then
2522 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002523 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00002524 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2525
2526 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2527 if ((UnknownBits & (UnknownBits-1)) == 0) {
2528 // Okay, we know that only that the single bit specified by UnknownBits
2529 // could be set on input to the CTLZ node. If this bit is set, the SRL
2530 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2531 // to an SRL,XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002532 unsigned ShAmt = UnknownBits.countTrailingZeros();
Chris Lattner350bec02006-04-02 06:11:11 +00002533 SDOperand Op = N0.getOperand(0);
2534 if (ShAmt) {
2535 Op = DAG.getNode(ISD::SRL, VT, Op,
2536 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
2537 AddToWorkList(Op.Val);
2538 }
2539 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2540 }
2541 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002542
2543 // fold operands of srl based on knowledge that the low bits are not
2544 // demanded.
2545 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2546 return SDOperand(N, 0);
2547
Chris Lattnere70da202007-12-06 07:33:36 +00002548 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002549}
2550
Nate Begeman83e75ec2005-09-06 04:43:02 +00002551SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002552 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002553 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002554
2555 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002556 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002557 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002558 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002559}
2560
Nate Begeman83e75ec2005-09-06 04:43:02 +00002561SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002562 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002563 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002564
2565 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002566 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002567 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002568 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002569}
2570
Nate Begeman83e75ec2005-09-06 04:43:02 +00002571SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002572 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002573 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002574
2575 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002576 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002577 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002578 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002579}
2580
Nate Begeman452d7be2005-09-16 00:54:12 +00002581SDOperand DAGCombiner::visitSELECT(SDNode *N) {
2582 SDOperand N0 = N->getOperand(0);
2583 SDOperand N1 = N->getOperand(1);
2584 SDOperand N2 = N->getOperand(2);
2585 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2586 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2587 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2588 MVT::ValueType VT = N->getValueType(0);
Evan Cheng571c4782007-08-18 05:57:05 +00002589 MVT::ValueType VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002590
Nate Begeman452d7be2005-09-16 00:54:12 +00002591 // fold select C, X, X -> X
2592 if (N1 == N2)
2593 return N1;
2594 // fold select true, X, Y -> X
2595 if (N0C && !N0C->isNullValue())
2596 return N1;
2597 // fold select false, X, Y -> Y
2598 if (N0C && N0C->isNullValue())
2599 return N2;
2600 // fold select C, 1, X -> C | X
Dan Gohman002e5d02008-03-13 22:13:53 +00002601 if (MVT::i1 == VT && N1C && N1C->getAPIntValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002602 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Cheng571c4782007-08-18 05:57:05 +00002603 // fold select C, 0, 1 -> ~C
2604 if (MVT::isInteger(VT) && MVT::isInteger(VT0) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00002605 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Evan Cheng571c4782007-08-18 05:57:05 +00002606 SDOperand XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
2607 if (VT == VT0)
2608 return XORNode;
2609 AddToWorkList(XORNode.Val);
2610 if (MVT::getSizeInBits(VT) > MVT::getSizeInBits(VT0))
2611 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2612 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2613 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002614 // fold select C, 0, X -> ~C & X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002615 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
2616 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002617 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002618 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2619 }
2620 // fold select C, X, 1 -> ~C | X
Dan Gohman002e5d02008-03-13 22:13:53 +00002621 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002622 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002623 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002624 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2625 }
2626 // fold select C, X, 0 -> C & X
2627 // FIXME: this should check for C type == X type, not i1?
2628 if (MVT::i1 == VT && N2C && N2C->isNullValue())
2629 return DAG.getNode(ISD::AND, VT, N0, N1);
2630 // fold X ? X : Y --> X ? 1 : Y --> X | Y
2631 if (MVT::i1 == VT && N0 == N1)
2632 return DAG.getNode(ISD::OR, VT, N0, N2);
2633 // fold X ? Y : X --> X ? Y : 0 --> X & Y
2634 if (MVT::i1 == VT && N0 == N2)
2635 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002636
Chris Lattner40c62d52005-10-18 06:04:22 +00002637 // If we can fold this based on the true/false value, do so.
2638 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00002639 return SDOperand(N, 0); // Don't revisit N.
2640
Nate Begeman44728a72005-09-19 22:34:01 +00002641 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002642 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002643 // FIXME:
2644 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2645 // having to say they don't support SELECT_CC on every type the DAG knows
2646 // about, since there is no way to mark an opcode illegal at all value types
2647 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2648 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2649 N1, N2, N0.getOperand(2));
2650 else
2651 return SimplifySelect(N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002652 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002653 return SDOperand();
2654}
2655
2656SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00002657 SDOperand N0 = N->getOperand(0);
2658 SDOperand N1 = N->getOperand(1);
2659 SDOperand N2 = N->getOperand(2);
2660 SDOperand N3 = N->getOperand(3);
2661 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002662 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2663
Nate Begeman44728a72005-09-19 22:34:01 +00002664 // fold select_cc lhs, rhs, x, x, cc -> x
2665 if (N2 == N3)
2666 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002667
Chris Lattner5f42a242006-09-20 06:19:26 +00002668 // Determine if the condition we're dealing with is constant
Scott Michel5b8f82e2008-03-10 15:42:14 +00002669 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002670 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002671
2672 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002673 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00002674 return N2; // cond always true -> true val
2675 else
2676 return N3; // cond always false -> false val
2677 }
2678
2679 // Fold to a simpler select_cc
2680 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2681 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2682 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2683 SCC.getOperand(2));
2684
Chris Lattner40c62d52005-10-18 06:04:22 +00002685 // If we can fold this based on the true/false value, do so.
2686 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00002687 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002688
Nate Begeman44728a72005-09-19 22:34:01 +00002689 // fold select_cc into other things, such as min/max/abs
2690 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002691}
2692
2693SDOperand DAGCombiner::visitSETCC(SDNode *N) {
2694 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2695 cast<CondCodeSDNode>(N->getOperand(2))->get());
2696}
2697
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002698// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2699// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2700// transformation. Returns true if extension are possible and the above
2701// mentioned transformation is profitable.
2702static bool ExtendUsesToFormExtLoad(SDNode *N, SDOperand N0,
2703 unsigned ExtOpc,
2704 SmallVector<SDNode*, 4> &ExtendNodes,
2705 TargetLowering &TLI) {
2706 bool HasCopyToRegUses = false;
2707 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
2708 for (SDNode::use_iterator UI = N0.Val->use_begin(), UE = N0.Val->use_end();
2709 UI != UE; ++UI) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00002710 SDNode *User = UI->getUser();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002711 if (User == N)
2712 continue;
2713 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2714 if (User->getOpcode() == ISD::SETCC) {
2715 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2716 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2717 // Sign bits will be lost after a zext.
2718 return false;
2719 bool Add = false;
2720 for (unsigned i = 0; i != 2; ++i) {
2721 SDOperand UseOp = User->getOperand(i);
2722 if (UseOp == N0)
2723 continue;
2724 if (!isa<ConstantSDNode>(UseOp))
2725 return false;
2726 Add = true;
2727 }
2728 if (Add)
2729 ExtendNodes.push_back(User);
2730 } else {
2731 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2732 SDOperand UseOp = User->getOperand(i);
2733 if (UseOp == N0) {
2734 // If truncate from extended type to original load type is free
2735 // on this target, then it's ok to extend a CopyToReg.
2736 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2737 HasCopyToRegUses = true;
2738 else
2739 return false;
2740 }
2741 }
2742 }
2743 }
2744
2745 if (HasCopyToRegUses) {
2746 bool BothLiveOut = false;
2747 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2748 UI != UE; ++UI) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00002749 SDNode *User = UI->getUser();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002750 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2751 SDOperand UseOp = User->getOperand(i);
2752 if (UseOp.Val == N && UseOp.ResNo == 0) {
2753 BothLiveOut = true;
2754 break;
2755 }
2756 }
2757 }
2758 if (BothLiveOut)
2759 // Both unextended and extended values are live out. There had better be
2760 // good a reason for the transformation.
2761 return ExtendNodes.size();
2762 }
2763 return true;
2764}
2765
Nate Begeman83e75ec2005-09-06 04:43:02 +00002766SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002767 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002768 MVT::ValueType VT = N->getValueType(0);
2769
Nate Begeman1d4d4142005-09-01 00:19:25 +00002770 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002771 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002772 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002773
Nate Begeman1d4d4142005-09-01 00:19:25 +00002774 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002775 // fold (sext (aext x)) -> (sext x)
2776 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002777 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002778
Chris Lattner22558872007-02-26 03:13:59 +00002779 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002780 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2781 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Evan Chengc88138f2007-03-22 01:54:19 +00002782 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002783 if (NarrowLoad.Val) {
2784 if (NarrowLoad.Val != N0.Val)
2785 CombineTo(N0.Val, NarrowLoad);
2786 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2787 }
Evan Chengc88138f2007-03-22 01:54:19 +00002788
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002789 // See if the value being truncated is already sign extended. If so, just
2790 // eliminate the trunc/sext pair.
Chris Lattner6007b842006-09-21 06:00:20 +00002791 SDOperand Op = N0.getOperand(0);
Chris Lattner22558872007-02-26 03:13:59 +00002792 unsigned OpBits = MVT::getSizeInBits(Op.getValueType());
2793 unsigned MidBits = MVT::getSizeInBits(N0.getValueType());
2794 unsigned DestBits = MVT::getSizeInBits(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +00002795 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002796
2797 if (OpBits == DestBits) {
2798 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2799 // bits, it is already ready.
2800 if (NumSignBits > DestBits-MidBits)
2801 return Op;
2802 } else if (OpBits < DestBits) {
2803 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2804 // bits, just sext from i32.
2805 if (NumSignBits > OpBits-MidBits)
2806 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2807 } else {
2808 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2809 // bits, just truncate to i32.
2810 if (NumSignBits > OpBits-MidBits)
2811 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002812 }
Chris Lattner22558872007-02-26 03:13:59 +00002813
2814 // fold (sext (truncate x)) -> (sextinreg x).
2815 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2816 N0.getValueType())) {
2817 if (Op.getValueType() < VT)
2818 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2819 else if (Op.getValueType() > VT)
2820 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2821 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2822 DAG.getValueType(N0.getValueType()));
2823 }
Chris Lattner6007b842006-09-21 06:00:20 +00002824 }
Chris Lattner310b5782006-05-06 23:06:26 +00002825
Evan Cheng110dec22005-12-14 02:19:23 +00002826 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002827 if (ISD::isNON_EXTLoad(N0.Val) &&
Evan Chengc5484282006-10-04 00:56:09 +00002828 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002829 bool DoXform = true;
2830 SmallVector<SDNode*, 4> SetCCs;
2831 if (!N0.hasOneUse())
2832 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
2833 if (DoXform) {
2834 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2835 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2836 LN0->getBasePtr(), LN0->getSrcValue(),
2837 LN0->getSrcValueOffset(),
2838 N0.getValueType(),
2839 LN0->isVolatile(),
2840 LN0->getAlignment());
2841 CombineTo(N, ExtLoad);
2842 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2843 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2844 // Extend SetCC uses if necessary.
2845 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2846 SDNode *SetCC = SetCCs[i];
2847 SmallVector<SDOperand, 4> Ops;
2848 for (unsigned j = 0; j != 2; ++j) {
2849 SDOperand SOp = SetCC->getOperand(j);
2850 if (SOp == Trunc)
2851 Ops.push_back(ExtLoad);
2852 else
2853 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
2854 }
2855 Ops.push_back(SetCC->getOperand(2));
2856 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2857 &Ops[0], Ops.size()));
2858 }
2859 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2860 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002861 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002862
2863 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2864 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002865 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2866 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002867 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002868 MVT::ValueType EVT = LN0->getMemoryVT();
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002869 if (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
2870 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2871 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002872 LN0->getSrcValueOffset(), EVT,
2873 LN0->isVolatile(),
2874 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002875 CombineTo(N, ExtLoad);
2876 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2877 ExtLoad.getValue(1));
2878 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2879 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002880 }
2881
Chris Lattner20a35c32007-04-11 05:32:27 +00002882 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2883 if (N0.getOpcode() == ISD::SETCC) {
2884 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002885 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2886 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2887 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2888 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002889 }
2890
Dan Gohman8f0ad582008-04-28 16:58:24 +00002891 // fold (sext x) -> (zext x) if the sign bit is known zero.
Dan Gohman187db7b2008-04-28 18:47:17 +00002892 if ((!AfterLegalize || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
2893 DAG.SignBitIsZero(N0))
Dan Gohman8f0ad582008-04-28 16:58:24 +00002894 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2895
Nate Begeman83e75ec2005-09-06 04:43:02 +00002896 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002897}
2898
Nate Begeman83e75ec2005-09-06 04:43:02 +00002899SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002900 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002901 MVT::ValueType VT = N->getValueType(0);
2902
Nate Begeman1d4d4142005-09-01 00:19:25 +00002903 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002904 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002905 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002906 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002907 // fold (zext (aext x)) -> (zext x)
2908 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002909 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002910
Evan Chengc88138f2007-03-22 01:54:19 +00002911 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2912 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002913 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002914 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002915 if (NarrowLoad.Val) {
2916 if (NarrowLoad.Val != N0.Val)
2917 CombineTo(N0.Val, NarrowLoad);
2918 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2919 }
Evan Chengc88138f2007-03-22 01:54:19 +00002920 }
2921
Chris Lattner6007b842006-09-21 06:00:20 +00002922 // fold (zext (truncate x)) -> (and x, mask)
2923 if (N0.getOpcode() == ISD::TRUNCATE &&
2924 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2925 SDOperand Op = N0.getOperand(0);
2926 if (Op.getValueType() < VT) {
2927 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2928 } else if (Op.getValueType() > VT) {
2929 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2930 }
2931 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2932 }
2933
Chris Lattner111c2282006-09-21 06:14:31 +00002934 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2935 if (N0.getOpcode() == ISD::AND &&
2936 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2937 N0.getOperand(1).getOpcode() == ISD::Constant) {
2938 SDOperand X = N0.getOperand(0).getOperand(0);
2939 if (X.getValueType() < VT) {
2940 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2941 } else if (X.getValueType() > VT) {
2942 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2943 }
Dan Gohman220a8232008-03-03 23:51:38 +00002944 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
2945 Mask.zext(MVT::getSizeInBits(VT));
Chris Lattner111c2282006-09-21 06:14:31 +00002946 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2947 }
2948
Evan Cheng110dec22005-12-14 02:19:23 +00002949 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002950 if (ISD::isNON_EXTLoad(N0.Val) &&
Evan Chengc5484282006-10-04 00:56:09 +00002951 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002952 bool DoXform = true;
2953 SmallVector<SDNode*, 4> SetCCs;
2954 if (!N0.hasOneUse())
2955 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
2956 if (DoXform) {
2957 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2958 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2959 LN0->getBasePtr(), LN0->getSrcValue(),
2960 LN0->getSrcValueOffset(),
2961 N0.getValueType(),
2962 LN0->isVolatile(),
2963 LN0->getAlignment());
2964 CombineTo(N, ExtLoad);
2965 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2966 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2967 // Extend SetCC uses if necessary.
2968 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2969 SDNode *SetCC = SetCCs[i];
2970 SmallVector<SDOperand, 4> Ops;
2971 for (unsigned j = 0; j != 2; ++j) {
2972 SDOperand SOp = SetCC->getOperand(j);
2973 if (SOp == Trunc)
2974 Ops.push_back(ExtLoad);
2975 else
Evan Chengde1631b2007-10-30 20:11:21 +00002976 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002977 }
2978 Ops.push_back(SetCC->getOperand(2));
2979 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2980 &Ops[0], Ops.size()));
2981 }
2982 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2983 }
Evan Cheng110dec22005-12-14 02:19:23 +00002984 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002985
2986 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2987 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002988 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2989 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002990 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002991 MVT::ValueType EVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002992 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2993 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002994 LN0->getSrcValueOffset(), EVT,
2995 LN0->isVolatile(),
2996 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002997 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002998 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2999 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003000 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003001 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003002
3003 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3004 if (N0.getOpcode() == ISD::SETCC) {
3005 SDOperand SCC =
3006 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3007 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003008 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
3009 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003010 }
3011
Nate Begeman83e75ec2005-09-06 04:43:02 +00003012 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003013}
3014
Chris Lattner5ffc0662006-05-05 05:58:59 +00003015SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
3016 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00003017 MVT::ValueType VT = N->getValueType(0);
3018
3019 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003020 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00003021 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
3022 // fold (aext (aext x)) -> (aext x)
3023 // fold (aext (zext x)) -> (zext x)
3024 // fold (aext (sext x)) -> (sext x)
3025 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3026 N0.getOpcode() == ISD::ZERO_EXTEND ||
3027 N0.getOpcode() == ISD::SIGN_EXTEND)
3028 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
3029
Evan Chengc88138f2007-03-22 01:54:19 +00003030 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3031 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3032 if (N0.getOpcode() == ISD::TRUNCATE) {
3033 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00003034 if (NarrowLoad.Val) {
3035 if (NarrowLoad.Val != N0.Val)
3036 CombineTo(N0.Val, NarrowLoad);
3037 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
3038 }
Evan Chengc88138f2007-03-22 01:54:19 +00003039 }
3040
Chris Lattner84750582006-09-20 06:29:17 +00003041 // fold (aext (truncate x))
3042 if (N0.getOpcode() == ISD::TRUNCATE) {
3043 SDOperand TruncOp = N0.getOperand(0);
3044 if (TruncOp.getValueType() == VT)
3045 return TruncOp; // x iff x size == zext size.
3046 if (TruncOp.getValueType() > VT)
3047 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
3048 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
3049 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00003050
3051 // fold (aext (and (trunc x), cst)) -> (and x, cst).
3052 if (N0.getOpcode() == ISD::AND &&
3053 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3054 N0.getOperand(1).getOpcode() == ISD::Constant) {
3055 SDOperand X = N0.getOperand(0).getOperand(0);
3056 if (X.getValueType() < VT) {
3057 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
3058 } else if (X.getValueType() > VT) {
3059 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3060 }
Dan Gohman220a8232008-03-03 23:51:38 +00003061 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
3062 Mask.zext(MVT::getSizeInBits(VT));
Chris Lattner0e4b9222006-09-21 06:40:43 +00003063 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3064 }
3065
Chris Lattner5ffc0662006-05-05 05:58:59 +00003066 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00003067 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00003068 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003069 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3070 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3071 LN0->getBasePtr(), LN0->getSrcValue(),
3072 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003073 N0.getValueType(),
3074 LN0->isVolatile(),
3075 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003076 CombineTo(N, ExtLoad);
3077 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3078 ExtLoad.getValue(1));
3079 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3080 }
3081
3082 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3083 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3084 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003085 if (N0.getOpcode() == ISD::LOAD &&
3086 !ISD::isNON_EXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003087 N0.hasOneUse()) {
3088 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003089 MVT::ValueType EVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00003090 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
3091 LN0->getChain(), LN0->getBasePtr(),
3092 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003093 LN0->getSrcValueOffset(), EVT,
3094 LN0->isVolatile(),
3095 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003096 CombineTo(N, ExtLoad);
3097 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3098 ExtLoad.getValue(1));
3099 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3100 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003101
3102 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3103 if (N0.getOpcode() == ISD::SETCC) {
3104 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003105 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3106 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003107 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Chris Lattner1eba01e2007-04-11 06:50:51 +00003108 if (SCC.Val)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003109 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003110 }
3111
Chris Lattner5ffc0662006-05-05 05:58:59 +00003112 return SDOperand();
3113}
3114
Chris Lattner2b4c2792007-10-13 06:35:54 +00003115/// GetDemandedBits - See if the specified operand can be simplified with the
3116/// knowledge that only the bits specified by Mask are used. If so, return the
3117/// simpler operand, otherwise return a null SDOperand.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003118SDOperand DAGCombiner::GetDemandedBits(SDOperand V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003119 switch (V.getOpcode()) {
3120 default: break;
3121 case ISD::OR:
3122 case ISD::XOR:
3123 // If the LHS or RHS don't contribute bits to the or, drop them.
3124 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3125 return V.getOperand(1);
3126 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3127 return V.getOperand(0);
3128 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003129 case ISD::SRL:
3130 // Only look at single-use SRLs.
3131 if (!V.Val->hasOneUse())
3132 break;
3133 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3134 // See if we can recursively simplify the LHS.
3135 unsigned Amt = RHSC->getValue();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003136 APInt NewMask = Mask << Amt;
3137 SDOperand SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Chris Lattnere33544c2007-10-13 06:58:48 +00003138 if (SimplifyLHS.Val) {
3139 return DAG.getNode(ISD::SRL, V.getValueType(),
3140 SimplifyLHS, V.getOperand(1));
3141 }
3142 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003143 }
3144 return SDOperand();
3145}
3146
Evan Chengc88138f2007-03-22 01:54:19 +00003147/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3148/// bits and then truncated to a narrower type and where N is a multiple
3149/// of number of bits of the narrower type, transform it to a narrower load
3150/// from address + N / num of bits of new type. If the result is to be
3151/// extended, also fold the extension to form a extending load.
3152SDOperand DAGCombiner::ReduceLoadWidth(SDNode *N) {
3153 unsigned Opc = N->getOpcode();
3154 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
3155 SDOperand N0 = N->getOperand(0);
3156 MVT::ValueType VT = N->getValueType(0);
3157 MVT::ValueType EVT = N->getValueType(0);
3158
Evan Chenge177e302007-03-23 22:13:36 +00003159 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3160 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003161 if (Opc == ISD::SIGN_EXTEND_INREG) {
3162 ExtType = ISD::SEXTLOAD;
3163 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00003164 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
3165 return SDOperand();
Evan Chengc88138f2007-03-22 01:54:19 +00003166 }
3167
3168 unsigned EVTBits = MVT::getSizeInBits(EVT);
3169 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00003170 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00003171 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3172 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3173 ShAmt = N01->getValue();
3174 // Is the shift amount a multiple of size of VT?
3175 if ((ShAmt & (EVTBits-1)) == 0) {
3176 N0 = N0.getOperand(0);
3177 if (MVT::getSizeInBits(N0.getValueType()) <= EVTBits)
3178 return SDOperand();
Evan Chengb37b80c2007-03-23 20:55:21 +00003179 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00003180 }
3181 }
3182 }
3183
3184 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
3185 // Do not allow folding to i1 here. i1 is implicitly stored in memory in
3186 // zero extended form: by shrinking the load, we lose track of the fact
3187 // that it is already zero extended.
3188 // FIXME: This should be reevaluated.
3189 VT != MVT::i1) {
3190 assert(MVT::getSizeInBits(N0.getValueType()) > EVTBits &&
3191 "Cannot truncate to larger type!");
3192 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3193 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00003194 // For big endian targets, we need to adjust the offset to the pointer to
3195 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003196 if (TLI.isBigEndian()) {
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003197 unsigned LVTStoreBits = MVT::getStoreSizeInBits(N0.getValueType());
3198 unsigned EVTStoreBits = MVT::getStoreSizeInBits(EVT);
3199 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3200 }
Evan Chengdae54ce2007-03-24 00:02:43 +00003201 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003202 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Evan Chengc88138f2007-03-22 01:54:19 +00003203 SDOperand NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
3204 DAG.getConstant(PtrOff, PtrType));
3205 AddToWorkList(NewPtr.Val);
3206 SDOperand Load = (ExtType == ISD::NON_EXTLOAD)
3207 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003208 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Duncan Sandsdc846502007-10-28 12:59:45 +00003209 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003210 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003211 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00003212 LN0->isVolatile(), NewAlign);
Evan Chengc88138f2007-03-22 01:54:19 +00003213 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00003214 if (CombineSRL) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003215 WorkListRemover DeadNodes(*this);
3216 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3217 &DeadNodes);
Evan Chengb37b80c2007-03-23 20:55:21 +00003218 CombineTo(N->getOperand(0).Val, Load);
3219 } else
3220 CombineTo(N0.Val, Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00003221 if (ShAmt) {
3222 if (Opc == ISD::SIGN_EXTEND_INREG)
3223 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
3224 else
3225 return DAG.getNode(Opc, VT, Load);
3226 }
Evan Chengc88138f2007-03-22 01:54:19 +00003227 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3228 }
3229
3230 return SDOperand();
3231}
3232
Chris Lattner5ffc0662006-05-05 05:58:59 +00003233
Nate Begeman83e75ec2005-09-06 04:43:02 +00003234SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003235 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003236 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003237 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003238 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003239 unsigned VTBits = MVT::getSizeInBits(VT);
Nate Begeman07ed4172005-10-10 21:26:48 +00003240 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003241
Nate Begeman1d4d4142005-09-01 00:19:25 +00003242 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003243 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003244 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003245
Chris Lattner541a24f2006-05-06 22:43:44 +00003246 // If the input is already sign extended, just drop the extension.
Dan Gohmanea859be2007-06-22 14:59:07 +00003247 if (DAG.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003248 return N0;
3249
Nate Begeman646d7e22005-09-02 21:18:40 +00003250 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3251 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3252 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003253 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003254 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003255
Chris Lattner95a5e052007-04-17 19:03:21 +00003256 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003257 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Nate Begemande996292006-02-03 22:24:05 +00003258 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003259
Chris Lattner95a5e052007-04-17 19:03:21 +00003260 // fold operands of sext_in_reg based on knowledge that the top bits are not
3261 // demanded.
3262 if (SimplifyDemandedBits(SDOperand(N, 0)))
3263 return SDOperand(N, 0);
3264
Evan Chengc88138f2007-03-22 01:54:19 +00003265 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3266 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
3267 SDOperand NarrowLoad = ReduceLoadWidth(N);
3268 if (NarrowLoad.Val)
3269 return NarrowLoad;
3270
Chris Lattner4b37e872006-05-08 21:18:59 +00003271 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3272 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3273 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3274 if (N0.getOpcode() == ISD::SRL) {
3275 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
3276 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
3277 // We can turn this into an SRA iff the input to the SRL is already sign
3278 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003279 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Chris Lattner4b37e872006-05-08 21:18:59 +00003280 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
3281 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3282 }
3283 }
Evan Chengc88138f2007-03-22 01:54:19 +00003284
Nate Begemanded49632005-10-13 03:11:28 +00003285 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00003286 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003287 ISD::isUNINDEXEDLoad(N0.Val) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003288 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00003289 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003290 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3291 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3292 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003293 LN0->getSrcValueOffset(), EVT,
3294 LN0->isVolatile(),
3295 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003296 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003297 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003298 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003299 }
3300 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00003301 if (ISD::isZEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
3302 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003303 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00003304 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003305 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3306 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3307 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003308 LN0->getSrcValueOffset(), EVT,
3309 LN0->isVolatile(),
3310 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003311 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003312 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003313 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003314 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003315 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003316}
3317
Nate Begeman83e75ec2005-09-06 04:43:02 +00003318SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003319 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003320 MVT::ValueType VT = N->getValueType(0);
3321
3322 // noop truncate
3323 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003324 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003325 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003326 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003327 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003328 // fold (truncate (truncate x)) -> (truncate x)
3329 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003330 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003331 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003332 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3333 N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner32ba1aa2006-11-20 18:05:46 +00003334 if (N0.getOperand(0).getValueType() < VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00003335 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003336 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Chris Lattner32ba1aa2006-11-20 18:05:46 +00003337 else if (N0.getOperand(0).getValueType() > VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00003338 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003339 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003340 else
3341 // if the source and dest are the same type, we can drop both the extend
3342 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003343 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003344 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003345
Chris Lattner2b4c2792007-10-13 06:35:54 +00003346 // See if we can simplify the input to this truncate through knowledge that
3347 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3348 // -> trunc y
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003349 SDOperand Shorter =
3350 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
3351 MVT::getSizeInBits(VT)));
Chris Lattner2b4c2792007-10-13 06:35:54 +00003352 if (Shorter.Val)
3353 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3354
Nate Begeman3df4d522005-10-12 20:40:40 +00003355 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003356 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003357 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003358}
3359
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003360static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
3361 SDOperand Elt = N->getOperand(i);
3362 if (Elt.getOpcode() != ISD::MERGE_VALUES)
3363 return Elt.Val;
3364 return Elt.getOperand(Elt.ResNo).Val;
3365}
3366
3367/// CombineConsecutiveLoads - build_pair (load, load) -> load
3368/// if load locations are consecutive.
3369SDOperand DAGCombiner::CombineConsecutiveLoads(SDNode *N, MVT::ValueType VT) {
3370 assert(N->getOpcode() == ISD::BUILD_PAIR);
3371
3372 SDNode *LD1 = getBuildPairElt(N, 0);
3373 if (!ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse())
3374 return SDOperand();
3375 MVT::ValueType LD1VT = LD1->getValueType(0);
3376 SDNode *LD2 = getBuildPairElt(N, 1);
3377 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3378 if (ISD::isNON_EXTLoad(LD2) &&
3379 LD2->hasOneUse() &&
3380 TLI.isConsecutiveLoad(LD2, LD1, MVT::getSizeInBits(LD1VT)/8, 1, MFI)) {
3381 LoadSDNode *LD = cast<LoadSDNode>(LD1);
3382 unsigned Align = LD->getAlignment();
3383 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
3384 getABITypeAlignment(MVT::getTypeForValueType(VT));
3385 if ((!AfterLegalize || TLI.isTypeLegal(VT)) &&
3386 TLI.isOperationLegal(ISD::LOAD, VT) && NewAlign <= Align)
3387 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(),
3388 LD->getSrcValue(), LD->getSrcValueOffset(),
3389 LD->isVolatile(), Align);
3390 }
3391 return SDOperand();
3392}
3393
Chris Lattner94683772005-12-23 05:30:37 +00003394SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3395 SDOperand N0 = N->getOperand(0);
3396 MVT::ValueType VT = N->getValueType(0);
3397
Dan Gohman7f321562007-06-25 16:23:39 +00003398 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3399 // Only do this before legalize, since afterward the target may be depending
3400 // on the bitconvert.
3401 // First check to see if this is all constant.
3402 if (!AfterLegalize &&
3403 N0.getOpcode() == ISD::BUILD_VECTOR && N0.Val->hasOneUse() &&
3404 MVT::isVector(VT)) {
3405 bool isSimple = true;
3406 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3407 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3408 N0.getOperand(i).getOpcode() != ISD::Constant &&
3409 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3410 isSimple = false;
3411 break;
3412 }
3413
3414 MVT::ValueType DestEltVT = MVT::getVectorElementType(N->getValueType(0));
3415 assert(!MVT::isVector(DestEltVT) &&
3416 "Element type of vector ValueType must not be vector!");
3417 if (isSimple) {
3418 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.Val, DestEltVT);
3419 }
3420 }
3421
Chris Lattner94683772005-12-23 05:30:37 +00003422 // If the input is a constant, let getNode() fold it.
3423 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
3424 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3425 if (Res.Val != N) return Res;
3426 }
3427
Chris Lattnerc8547d82005-12-23 05:37:50 +00003428 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3429 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003430
Chris Lattner57104102005-12-23 05:44:41 +00003431 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003432 // If the resultant load doesn't need a higher alignment than the original!
3433 if (ISD::isNormalLoad(N0.Val) && N0.hasOneUse() &&
Evan Cheng59d5b682007-05-07 21:27:48 +00003434 TLI.isOperationLegal(ISD::LOAD, VT)) {
Evan Cheng466685d2006-10-09 20:57:25 +00003435 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng59d5b682007-05-07 21:27:48 +00003436 unsigned Align = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00003437 getABITypeAlignment(MVT::getTypeForValueType(VT));
Evan Cheng59d5b682007-05-07 21:27:48 +00003438 unsigned OrigAlign = LN0->getAlignment();
3439 if (Align <= OrigAlign) {
3440 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
3441 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003442 LN0->isVolatile(), Align);
Evan Cheng59d5b682007-05-07 21:27:48 +00003443 AddToWorkList(N);
3444 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
3445 Load.getValue(1));
3446 return Load;
3447 }
Chris Lattner57104102005-12-23 05:44:41 +00003448 }
3449
Chris Lattner3bd39d42008-01-27 17:42:27 +00003450 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3451 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3452 // This often reduces constant pool loads.
3453 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
3454 N0.Val->hasOneUse() && MVT::isInteger(VT) && !MVT::isVector(VT)) {
3455 SDOperand NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3456 AddToWorkList(NewConv.Val);
3457
Dan Gohman220a8232008-03-03 23:51:38 +00003458 APInt SignBit = APInt::getSignBit(MVT::getSizeInBits(VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00003459 if (N0.getOpcode() == ISD::FNEG)
3460 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3461 assert(N0.getOpcode() == ISD::FABS);
3462 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3463 }
3464
3465 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3466 // Note that we don't handle copysign(x,cst) because this can always be folded
3467 // to an fneg or fabs.
3468 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00003469 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
3470 MVT::isInteger(VT) && !MVT::isVector(VT)) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003471 unsigned OrigXWidth = MVT::getSizeInBits(N0.getOperand(1).getValueType());
3472 SDOperand X = DAG.getNode(ISD::BIT_CONVERT, MVT::getIntegerType(OrigXWidth),
3473 N0.getOperand(1));
3474 AddToWorkList(X.Val);
3475
3476 // If X has a different width than the result/lhs, sext it or truncate it.
3477 unsigned VTWidth = MVT::getSizeInBits(VT);
3478 if (OrigXWidth < VTWidth) {
3479 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
3480 AddToWorkList(X.Val);
3481 } else if (OrigXWidth > VTWidth) {
3482 // To get the sign bit in the right place, we have to shift it right
3483 // before truncating.
3484 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3485 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
3486 AddToWorkList(X.Val);
3487 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3488 AddToWorkList(X.Val);
3489 }
3490
Dan Gohman220a8232008-03-03 23:51:38 +00003491 APInt SignBit = APInt::getSignBit(MVT::getSizeInBits(VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00003492 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
3493 AddToWorkList(X.Val);
3494
3495 SDOperand Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3496 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
3497 AddToWorkList(Cst.Val);
3498
3499 return DAG.getNode(ISD::OR, VT, X, Cst);
3500 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003501
3502 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
3503 if (N0.getOpcode() == ISD::BUILD_PAIR) {
3504 SDOperand CombineLD = CombineConsecutiveLoads(N0.Val, VT);
3505 if (CombineLD.Val)
3506 return CombineLD;
3507 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003508
Chris Lattner94683772005-12-23 05:30:37 +00003509 return SDOperand();
3510}
3511
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003512SDOperand DAGCombiner::visitBUILD_PAIR(SDNode *N) {
3513 MVT::ValueType VT = N->getValueType(0);
3514 return CombineConsecutiveLoads(N, VT);
3515}
3516
Dan Gohman7f321562007-06-25 16:23:39 +00003517/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003518/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3519/// destination element value type.
3520SDOperand DAGCombiner::
Dan Gohman7f321562007-06-25 16:23:39 +00003521ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003522 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
3523
3524 // If this is already the right type, we're done.
3525 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
3526
3527 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
3528 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
3529
3530 // If this is a conversion of N elements of one type to N elements of another
3531 // type, convert each element. This handles FP<->INT cases.
3532 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003533 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003534 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003535 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00003536 AddToWorkList(Ops.back().Val);
3537 }
Dan Gohman7f321562007-06-25 16:23:39 +00003538 MVT::ValueType VT =
3539 MVT::getVectorType(DstEltVT,
3540 MVT::getVectorNumElements(BV->getValueType(0)));
3541 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003542 }
3543
3544 // Otherwise, we're growing or shrinking the elements. To avoid having to
3545 // handle annoying details of growing/shrinking FP values, we convert them to
3546 // int first.
3547 if (MVT::isFloatingPoint(SrcEltVT)) {
3548 // Convert the input float vector to a int vector where the elements are the
3549 // same sizes.
3550 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
3551 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00003552 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003553 SrcEltVT = IntVT;
3554 }
3555
3556 // Now we know the input is an integer vector. If the output is a FP type,
3557 // convert to integer first, then to FP of the right size.
3558 if (MVT::isFloatingPoint(DstEltVT)) {
3559 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
3560 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00003561 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003562
3563 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003564 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003565 }
3566
3567 // Okay, we know the src/dst types are both integers of differing types.
3568 // Handling growing first.
3569 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
3570 if (SrcBitSize < DstBitSize) {
3571 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3572
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003573 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003574 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003575 i += NumInputsPerOutput) {
3576 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00003577 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003578 bool EltIsUndef = true;
3579 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3580 // Shift the previously computed bits over.
3581 NewBits <<= SrcBitSize;
3582 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
3583 if (Op.getOpcode() == ISD::UNDEF) continue;
3584 EltIsUndef = false;
3585
Dan Gohman220a8232008-03-03 23:51:38 +00003586 NewBits |=
3587 APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003588 }
3589
3590 if (EltIsUndef)
3591 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3592 else
3593 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3594 }
3595
Evan Chengefec7512008-02-18 23:04:32 +00003596 MVT::ValueType VT = MVT::getVectorType(DstEltVT, Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00003597 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003598 }
3599
3600 // Finally, this must be the case where we are shrinking elements: each input
3601 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00003602 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00003603 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Evan Chengefec7512008-02-18 23:04:32 +00003604 MVT::ValueType VT = MVT::getVectorType(DstEltVT,
3605 NumOutputsPerInput * BV->getNumOperands());
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003606 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003607 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003608 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3609 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3610 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3611 continue;
3612 }
Dan Gohman220a8232008-03-03 23:51:38 +00003613 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getAPIntValue();
Chris Lattner6258fb22006-04-02 02:53:43 +00003614 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohman220a8232008-03-03 23:51:38 +00003615 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003616 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohman220a8232008-03-03 23:51:38 +00003617 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00003618 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
3619 return DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00003620 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003621 }
3622
3623 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00003624 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00003625 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3626 }
Dan Gohman7f321562007-06-25 16:23:39 +00003627 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003628}
3629
3630
3631
Chris Lattner01b3d732005-09-28 22:28:18 +00003632SDOperand DAGCombiner::visitFADD(SDNode *N) {
3633 SDOperand N0 = N->getOperand(0);
3634 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003635 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3636 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003637 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003638
Dan Gohman7f321562007-06-25 16:23:39 +00003639 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003640 if (MVT::isVector(VT)) {
3641 SDOperand FoldedVOp = SimplifyVBinOp(N);
3642 if (FoldedVOp.Val) return FoldedVOp;
3643 }
Dan Gohman7f321562007-06-25 16:23:39 +00003644
Nate Begemana0e221d2005-10-18 00:28:13 +00003645 // fold (fadd c1, c2) -> c1+c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003646 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003647 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003648 // canonicalize constant to RHS
3649 if (N0CFP && !N1CFP)
3650 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003651 // fold (A + (-B)) -> A-B
Chris Lattner0254e702008-02-26 07:04:54 +00003652 if (isNegatibleForFree(N1, AfterLegalize) == 2)
3653 return DAG.getNode(ISD::FSUB, VT, N0,
3654 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner01b3d732005-09-28 22:28:18 +00003655 // fold ((-A) + B) -> B-A
Chris Lattner0254e702008-02-26 07:04:54 +00003656 if (isNegatibleForFree(N0, AfterLegalize) == 2)
3657 return DAG.getNode(ISD::FSUB, VT, N1,
3658 GetNegatedExpression(N0, DAG, AfterLegalize));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003659
3660 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3661 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
3662 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3663 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3664 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3665
Chris Lattner01b3d732005-09-28 22:28:18 +00003666 return SDOperand();
3667}
3668
3669SDOperand DAGCombiner::visitFSUB(SDNode *N) {
3670 SDOperand N0 = N->getOperand(0);
3671 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003672 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3673 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003674 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003675
Dan Gohman7f321562007-06-25 16:23:39 +00003676 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003677 if (MVT::isVector(VT)) {
3678 SDOperand FoldedVOp = SimplifyVBinOp(N);
3679 if (FoldedVOp.Val) return FoldedVOp;
3680 }
Dan Gohman7f321562007-06-25 16:23:39 +00003681
Nate Begemana0e221d2005-10-18 00:28:13 +00003682 // fold (fsub c1, c2) -> c1-c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003683 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003684 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003685 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003686 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Chris Lattner0254e702008-02-26 07:04:54 +00003687 if (isNegatibleForFree(N1, AfterLegalize))
3688 return GetNegatedExpression(N1, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00003689 return DAG.getNode(ISD::FNEG, VT, N1);
3690 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003691 // fold (A-(-B)) -> A+B
Chris Lattner0254e702008-02-26 07:04:54 +00003692 if (isNegatibleForFree(N1, AfterLegalize))
3693 return DAG.getNode(ISD::FADD, VT, N0,
3694 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003695
Chris Lattner01b3d732005-09-28 22:28:18 +00003696 return SDOperand();
3697}
3698
3699SDOperand DAGCombiner::visitFMUL(SDNode *N) {
3700 SDOperand N0 = N->getOperand(0);
3701 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003702 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3703 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003704 MVT::ValueType VT = N->getValueType(0);
3705
Dan Gohman7f321562007-06-25 16:23:39 +00003706 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003707 if (MVT::isVector(VT)) {
3708 SDOperand FoldedVOp = SimplifyVBinOp(N);
3709 if (FoldedVOp.Val) return FoldedVOp;
3710 }
Dan Gohman7f321562007-06-25 16:23:39 +00003711
Nate Begeman11af4ea2005-10-17 20:40:11 +00003712 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003713 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003714 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003715 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003716 if (N0CFP && !N1CFP)
3717 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003718 // fold (fmul X, 2.0) -> (fadd X, X)
3719 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3720 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003721 // fold (fmul X, -1.0) -> (fneg X)
3722 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3723 return DAG.getNode(ISD::FNEG, VT, N0);
3724
3725 // -X * -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003726 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3727 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003728 // Both can be negated for free, check to see if at least one is cheaper
3729 // negated.
3730 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003731 return DAG.getNode(ISD::FMUL, VT,
3732 GetNegatedExpression(N0, DAG, AfterLegalize),
3733 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003734 }
3735 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003736
3737 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3738 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
3739 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3740 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3741 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3742
Chris Lattner01b3d732005-09-28 22:28:18 +00003743 return SDOperand();
3744}
3745
3746SDOperand DAGCombiner::visitFDIV(SDNode *N) {
3747 SDOperand N0 = N->getOperand(0);
3748 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003749 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3750 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003751 MVT::ValueType VT = N->getValueType(0);
3752
Dan Gohman7f321562007-06-25 16:23:39 +00003753 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003754 if (MVT::isVector(VT)) {
3755 SDOperand FoldedVOp = SimplifyVBinOp(N);
3756 if (FoldedVOp.Val) return FoldedVOp;
3757 }
Dan Gohman7f321562007-06-25 16:23:39 +00003758
Nate Begemana148d982006-01-18 22:35:16 +00003759 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003760 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003761 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003762
3763
3764 // -X / -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003765 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3766 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003767 // Both can be negated for free, check to see if at least one is cheaper
3768 // negated.
3769 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003770 return DAG.getNode(ISD::FDIV, VT,
3771 GetNegatedExpression(N0, DAG, AfterLegalize),
3772 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003773 }
3774 }
3775
Chris Lattner01b3d732005-09-28 22:28:18 +00003776 return SDOperand();
3777}
3778
3779SDOperand DAGCombiner::visitFREM(SDNode *N) {
3780 SDOperand N0 = N->getOperand(0);
3781 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003782 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3783 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003784 MVT::ValueType VT = N->getValueType(0);
3785
Nate Begemana148d982006-01-18 22:35:16 +00003786 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003787 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003788 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003789
Chris Lattner01b3d732005-09-28 22:28:18 +00003790 return SDOperand();
3791}
3792
Chris Lattner12d83032006-03-05 05:30:57 +00003793SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3794 SDOperand N0 = N->getOperand(0);
3795 SDOperand N1 = N->getOperand(1);
3796 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3797 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3798 MVT::ValueType VT = N->getValueType(0);
3799
Dale Johannesendb44bf82007-10-16 23:38:29 +00003800 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00003801 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3802
3803 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003804 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003805 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3806 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dale Johannesen87503a62007-08-25 22:10:57 +00003807 if (!V.isNegative())
Chris Lattner12d83032006-03-05 05:30:57 +00003808 return DAG.getNode(ISD::FABS, VT, N0);
3809 else
3810 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3811 }
3812
3813 // copysign(fabs(x), y) -> copysign(x, y)
3814 // copysign(fneg(x), y) -> copysign(x, y)
3815 // copysign(copysign(x,z), y) -> copysign(x, y)
3816 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3817 N0.getOpcode() == ISD::FCOPYSIGN)
3818 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3819
3820 // copysign(x, abs(y)) -> abs(x)
3821 if (N1.getOpcode() == ISD::FABS)
3822 return DAG.getNode(ISD::FABS, VT, N0);
3823
3824 // copysign(x, copysign(y,z)) -> copysign(x, z)
3825 if (N1.getOpcode() == ISD::FCOPYSIGN)
3826 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3827
3828 // copysign(x, fp_extend(y)) -> copysign(x, y)
3829 // copysign(x, fp_round(y)) -> copysign(x, y)
3830 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3831 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3832
3833 return SDOperand();
3834}
3835
3836
Chris Lattner01b3d732005-09-28 22:28:18 +00003837
Nate Begeman83e75ec2005-09-06 04:43:02 +00003838SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003839 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003840 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003841 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003842
3843 // fold (sint_to_fp c1) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003844 if (N0C && N0.getValueType() != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003845 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003846 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003847}
3848
Nate Begeman83e75ec2005-09-06 04:43:02 +00003849SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003850 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003851 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003852 MVT::ValueType VT = N->getValueType(0);
3853
Nate Begeman1d4d4142005-09-01 00:19:25 +00003854 // fold (uint_to_fp c1) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003855 if (N0C && N0.getValueType() != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003856 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003857 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003858}
3859
Nate Begeman83e75ec2005-09-06 04:43:02 +00003860SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003861 SDOperand N0 = N->getOperand(0);
3862 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3863 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003864
3865 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003866 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003867 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003868 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003869}
3870
Nate Begeman83e75ec2005-09-06 04:43:02 +00003871SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003872 SDOperand N0 = N->getOperand(0);
3873 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3874 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003875
3876 // fold (fp_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00003877 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003878 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003879 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003880}
3881
Nate Begeman83e75ec2005-09-06 04:43:02 +00003882SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003883 SDOperand N0 = N->getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003884 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003885 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3886 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003887
3888 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003889 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner0bd48932008-01-17 07:00:52 +00003890 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003891
3892 // fold (fp_round (fp_extend x)) -> x
3893 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3894 return N0.getOperand(0);
3895
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00003896 // fold (fp_round (fp_round x)) -> (fp_round x)
3897 if (N0.getOpcode() == ISD::FP_ROUND) {
3898 // This is a value preserving truncation if both round's are.
3899 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
3900 N0.Val->getConstantOperandVal(1) == 1;
3901 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
3902 DAG.getIntPtrConstant(IsTrunc));
3903 }
3904
Chris Lattner79dbea52006-03-13 06:26:26 +00003905 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
3906 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
Chris Lattner0bd48932008-01-17 07:00:52 +00003907 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003908 AddToWorkList(Tmp.Val);
3909 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
3910 }
3911
Nate Begeman83e75ec2005-09-06 04:43:02 +00003912 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003913}
3914
Nate Begeman83e75ec2005-09-06 04:43:02 +00003915SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003916 SDOperand N0 = N->getOperand(0);
3917 MVT::ValueType VT = N->getValueType(0);
3918 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00003919 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003920
Nate Begeman1d4d4142005-09-01 00:19:25 +00003921 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003922 if (N0CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003923 SDOperand Round = DAG.getConstantFP(N0CFP->getValueAPF(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003924 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003925 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003926 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003927}
3928
Nate Begeman83e75ec2005-09-06 04:43:02 +00003929SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003930 SDOperand N0 = N->getOperand(0);
3931 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3932 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003933
Chris Lattner5938bef2007-12-29 06:55:23 +00003934 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003935 if (N->hasOneUse() &&
3936 N->use_begin()->getSDOperand().getOpcode() == ISD::FP_ROUND)
Chris Lattner5938bef2007-12-29 06:55:23 +00003937 return SDOperand();
Chris Lattner0bd48932008-01-17 07:00:52 +00003938
Nate Begeman1d4d4142005-09-01 00:19:25 +00003939 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003940 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003941 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003942
3943 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
3944 // value of X.
3945 if (N0.getOpcode() == ISD::FP_ROUND && N0.Val->getConstantOperandVal(1) == 1){
3946 SDOperand In = N0.getOperand(0);
3947 if (In.getValueType() == VT) return In;
3948 if (VT < In.getValueType())
3949 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
3950 return DAG.getNode(ISD::FP_EXTEND, VT, In);
3951 }
3952
3953 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Dale Johannesenb6210fc2007-10-19 20:29:00 +00003954 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00003955 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003956 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3957 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3958 LN0->getBasePtr(), LN0->getSrcValue(),
3959 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003960 N0.getValueType(),
3961 LN0->isVolatile(),
3962 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00003963 CombineTo(N, ExtLoad);
Chris Lattner0bd48932008-01-17 07:00:52 +00003964 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad,
3965 DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00003966 ExtLoad.getValue(1));
3967 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3968 }
3969
3970
Nate Begeman83e75ec2005-09-06 04:43:02 +00003971 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003972}
3973
Nate Begeman83e75ec2005-09-06 04:43:02 +00003974SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003975 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003976
Chris Lattner0254e702008-02-26 07:04:54 +00003977 if (isNegatibleForFree(N0, AfterLegalize))
3978 return GetNegatedExpression(N0, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00003979
Chris Lattner3bd39d42008-01-27 17:42:27 +00003980 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
3981 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00003982 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
3983 MVT::isInteger(N0.getOperand(0).getValueType()) &&
3984 !MVT::isVector(N0.getOperand(0).getValueType())) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003985 SDOperand Int = N0.getOperand(0);
3986 MVT::ValueType IntVT = Int.getValueType();
3987 if (MVT::isInteger(IntVT) && !MVT::isVector(IntVT)) {
3988 Int = DAG.getNode(ISD::XOR, IntVT, Int,
3989 DAG.getConstant(MVT::getIntVTSignBit(IntVT), IntVT));
3990 AddToWorkList(Int.Val);
3991 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
3992 }
3993 }
3994
Nate Begeman83e75ec2005-09-06 04:43:02 +00003995 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003996}
3997
Nate Begeman83e75ec2005-09-06 04:43:02 +00003998SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003999 SDOperand N0 = N->getOperand(0);
4000 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4001 MVT::ValueType VT = N->getValueType(0);
4002
Nate Begeman1d4d4142005-09-01 00:19:25 +00004003 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00004004 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004005 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004006 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004007 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00004008 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004009 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004010 // fold (fabs (fcopysign x, y)) -> (fabs x)
4011 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
4012 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
4013
Chris Lattner3bd39d42008-01-27 17:42:27 +00004014 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
4015 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00004016 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
4017 MVT::isInteger(N0.getOperand(0).getValueType()) &&
4018 !MVT::isVector(N0.getOperand(0).getValueType())) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004019 SDOperand Int = N0.getOperand(0);
4020 MVT::ValueType IntVT = Int.getValueType();
4021 if (MVT::isInteger(IntVT) && !MVT::isVector(IntVT)) {
4022 Int = DAG.getNode(ISD::AND, IntVT, Int,
4023 DAG.getConstant(~MVT::getIntVTSignBit(IntVT), IntVT));
4024 AddToWorkList(Int.Val);
4025 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4026 }
4027 }
4028
Nate Begeman83e75ec2005-09-06 04:43:02 +00004029 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004030}
4031
Nate Begeman44728a72005-09-19 22:34:01 +00004032SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
4033 SDOperand Chain = N->getOperand(0);
4034 SDOperand N1 = N->getOperand(1);
4035 SDOperand N2 = N->getOperand(2);
4036 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4037
4038 // never taken branch, fold to chain
4039 if (N1C && N1C->isNullValue())
4040 return Chain;
4041 // unconditional branch
Dan Gohman002e5d02008-03-13 22:13:53 +00004042 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00004043 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004044 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
4045 // on the target.
4046 if (N1.getOpcode() == ISD::SETCC &&
4047 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
4048 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
4049 N1.getOperand(0), N1.getOperand(1), N2);
4050 }
Nate Begeman44728a72005-09-19 22:34:01 +00004051 return SDOperand();
4052}
4053
Chris Lattner3ea0b472005-10-05 06:47:48 +00004054// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
4055//
Nate Begeman44728a72005-09-19 22:34:01 +00004056SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00004057 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
4058 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
4059
4060 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00004061 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00004062 if (Simp.Val) AddToWorkList(Simp.Val);
4063
Nate Begemane17daeb2005-10-05 21:43:42 +00004064 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
4065
4066 // fold br_cc true, dest -> br dest (unconditional branch)
Dan Gohman002e5d02008-03-13 22:13:53 +00004067 if (SCCC && !SCCC->isNullValue())
Nate Begemane17daeb2005-10-05 21:43:42 +00004068 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
4069 N->getOperand(4));
4070 // fold br_cc false, dest -> unconditional fall through
4071 if (SCCC && SCCC->isNullValue())
4072 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00004073
Nate Begemane17daeb2005-10-05 21:43:42 +00004074 // fold to a simpler setcc
4075 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
4076 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
4077 Simp.getOperand(2), Simp.getOperand(0),
4078 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00004079 return SDOperand();
4080}
4081
Chris Lattner448f2192006-11-11 00:39:41 +00004082
4083/// CombineToPreIndexedLoadStore - Try turning a load / store and a
4084/// pre-indexed load / store when the base pointer is a add or subtract
4085/// and it has other uses besides the load / store. After the
4086/// transformation, the new indexed load / store has effectively folded
4087/// the add / subtract in and all of its other uses are redirected to the
4088/// new load / store.
4089bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
4090 if (!AfterLegalize)
4091 return false;
4092
4093 bool isLoad = true;
4094 SDOperand Ptr;
4095 MVT::ValueType VT;
4096 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004097 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004098 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004099 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00004100 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00004101 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
4102 return false;
4103 Ptr = LD->getBasePtr();
4104 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004105 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004106 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004107 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004108 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
4109 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
4110 return false;
4111 Ptr = ST->getBasePtr();
4112 isLoad = false;
4113 } else
4114 return false;
4115
Chris Lattner9f1794e2006-11-11 00:56:29 +00004116 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4117 // out. There is no reason to make this a preinc/predec.
4118 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
4119 Ptr.Val->hasOneUse())
4120 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004121
Chris Lattner9f1794e2006-11-11 00:56:29 +00004122 // Ask the target to do addressing mode selection.
4123 SDOperand BasePtr;
4124 SDOperand Offset;
4125 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4126 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4127 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00004128 // Don't create a indexed load / store with zero offset.
4129 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004130 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004131 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004132
Chris Lattner41e53fd2006-11-11 01:00:15 +00004133 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00004134 // 1) The new base ptr is a frame index.
4135 // 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 +00004136 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00004137 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00004138 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00004139 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00004140
Chris Lattner41e53fd2006-11-11 01:00:15 +00004141 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4142 // (plus the implicit offset) to a register to preinc anyway.
4143 if (isa<FrameIndexSDNode>(BasePtr))
4144 return false;
4145
4146 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004147 if (!isLoad) {
4148 SDOperand Val = cast<StoreSDNode>(N)->getValue();
Evan Cheng917be682008-03-04 00:41:45 +00004149 if (Val == BasePtr || BasePtr.Val->isPredecessorOf(Val.Val))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004150 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004151 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004152
Evan Chengc843abe2007-05-24 02:35:39 +00004153 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004154 bool RealUse = false;
4155 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
4156 E = Ptr.Val->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004157 SDNode *Use = I->getUser();
Chris Lattner9f1794e2006-11-11 00:56:29 +00004158 if (Use == N)
4159 continue;
Evan Cheng917be682008-03-04 00:41:45 +00004160 if (Use->isPredecessorOf(N))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004161 return false;
4162
4163 if (!((Use->getOpcode() == ISD::LOAD &&
4164 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004165 (Use->getOpcode() == ISD::STORE &&
4166 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004167 RealUse = true;
4168 }
4169 if (!RealUse)
4170 return false;
4171
4172 SDOperand Result;
4173 if (isLoad)
4174 Result = DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM);
4175 else
4176 Result = DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4177 ++PreIndexedNodes;
4178 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004179 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004180 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4181 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004182 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004183 if (isLoad) {
4184 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004185 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004186 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004187 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004188 } else {
4189 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004190 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004191 }
4192
Chris Lattner9f1794e2006-11-11 00:56:29 +00004193 // Finally, since the node is now dead, remove it from the graph.
4194 DAG.DeleteNode(N);
4195
4196 // Replace the uses of Ptr with uses of the updated base value.
4197 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004198 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004199 removeFromWorkList(Ptr.Val);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004200 DAG.DeleteNode(Ptr.Val);
4201
4202 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004203}
4204
4205/// CombineToPostIndexedLoadStore - Try combine a load / store with a
4206/// add / sub of the base pointer node into a post-indexed load / store.
4207/// The transformation folded the add / subtract into the new indexed
4208/// load / store effectively and all of its uses are redirected to the
4209/// new load / store.
4210bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
4211 if (!AfterLegalize)
4212 return false;
4213
4214 bool isLoad = true;
4215 SDOperand Ptr;
4216 MVT::ValueType VT;
4217 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004218 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004219 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004220 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004221 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4222 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4223 return false;
4224 Ptr = LD->getBasePtr();
4225 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004226 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004227 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004228 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004229 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4230 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4231 return false;
4232 Ptr = ST->getBasePtr();
4233 isLoad = false;
4234 } else
4235 return false;
4236
Evan Chengcc470212006-11-16 00:08:20 +00004237 if (Ptr.Val->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004238 return false;
4239
4240 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
4241 E = Ptr.Val->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004242 SDNode *Op = I->getUser();
Chris Lattner9f1794e2006-11-11 00:56:29 +00004243 if (Op == N ||
4244 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4245 continue;
4246
4247 SDOperand BasePtr;
4248 SDOperand Offset;
4249 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4250 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4251 if (Ptr == Offset)
4252 std::swap(BasePtr, Offset);
4253 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00004254 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00004255 // Don't create a indexed load / store with zero offset.
4256 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004257 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004258 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004259
Chris Lattner9f1794e2006-11-11 00:56:29 +00004260 // Try turning it into a post-indexed load / store except when
4261 // 1) All uses are load / store ops that use it as base ptr.
4262 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4263 // nor a successor of N. Otherwise, if Op is folded that would
4264 // create a cycle.
4265
4266 // Check for #1.
4267 bool TryNext = false;
4268 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
4269 EE = BasePtr.Val->use_end(); II != EE; ++II) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004270 SDNode *Use = II->getUser();
Chris Lattner9f1794e2006-11-11 00:56:29 +00004271 if (Use == Ptr.Val)
Chris Lattner448f2192006-11-11 00:39:41 +00004272 continue;
4273
Chris Lattner9f1794e2006-11-11 00:56:29 +00004274 // If all the uses are load / store addresses, then don't do the
4275 // transformation.
4276 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4277 bool RealUse = false;
4278 for (SDNode::use_iterator III = Use->use_begin(),
4279 EEE = Use->use_end(); III != EEE; ++III) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004280 SDNode *UseUse = III->getUser();
Chris Lattner9f1794e2006-11-11 00:56:29 +00004281 if (!((UseUse->getOpcode() == ISD::LOAD &&
4282 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004283 (UseUse->getOpcode() == ISD::STORE &&
4284 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004285 RealUse = true;
4286 }
Chris Lattner448f2192006-11-11 00:39:41 +00004287
Chris Lattner9f1794e2006-11-11 00:56:29 +00004288 if (!RealUse) {
4289 TryNext = true;
4290 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004291 }
4292 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004293 }
4294 if (TryNext)
4295 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004296
Chris Lattner9f1794e2006-11-11 00:56:29 +00004297 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00004298 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Chris Lattner9f1794e2006-11-11 00:56:29 +00004299 SDOperand Result = isLoad
4300 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
4301 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4302 ++PostIndexedNodes;
4303 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004304 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004305 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4306 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004307 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004308 if (isLoad) {
4309 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004310 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004311 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004312 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004313 } else {
4314 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004315 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00004316 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004317
Chris Lattner9f1794e2006-11-11 00:56:29 +00004318 // Finally, since the node is now dead, remove it from the graph.
4319 DAG.DeleteNode(N);
4320
4321 // Replace the uses of Use with uses of the updated base value.
4322 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
4323 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004324 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004325 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004326 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004327 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004328 }
4329 }
4330 }
4331 return false;
4332}
4333
Chris Lattner00161a62008-01-25 07:20:16 +00004334/// InferAlignment - If we can infer some alignment information from this
4335/// pointer, return it.
4336static unsigned InferAlignment(SDOperand Ptr, SelectionDAG &DAG) {
4337 // If this is a direct reference to a stack slot, use information about the
4338 // stack slot's alignment.
Chris Lattner1329cb82008-01-26 19:45:50 +00004339 int FrameIdx = 1 << 31;
4340 int64_t FrameOffset = 0;
Chris Lattner00161a62008-01-25 07:20:16 +00004341 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1329cb82008-01-26 19:45:50 +00004342 FrameIdx = FI->getIndex();
4343 } else if (Ptr.getOpcode() == ISD::ADD &&
4344 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4345 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4346 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4347 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner00161a62008-01-25 07:20:16 +00004348 }
Chris Lattner1329cb82008-01-26 19:45:50 +00004349
4350 if (FrameIdx != (1 << 31)) {
4351 // FIXME: Handle FI+CST.
4352 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4353 if (MFI.isFixedObjectIndex(FrameIdx)) {
4354 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx);
4355
4356 // The alignment of the frame index can be determined from its offset from
4357 // the incoming frame position. If the frame object is at offset 32 and
4358 // the stack is guaranteed to be 16-byte aligned, then we know that the
4359 // object is 16-byte aligned.
4360 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4361 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4362
4363 // Finally, the frame object itself may have a known alignment. Factor
4364 // the alignment + offset into a new alignment. For example, if we know
4365 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4366 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4367 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4368 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4369 FrameOffset);
4370 return std::max(Align, FIInfoAlign);
4371 }
4372 }
Chris Lattner00161a62008-01-25 07:20:16 +00004373
4374 return 0;
4375}
Chris Lattner448f2192006-11-11 00:39:41 +00004376
Chris Lattner01a22022005-10-10 22:04:48 +00004377SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004378 LoadSDNode *LD = cast<LoadSDNode>(N);
4379 SDOperand Chain = LD->getChain();
4380 SDOperand Ptr = LD->getBasePtr();
Chris Lattner00161a62008-01-25 07:20:16 +00004381
4382 // Try to infer better alignment information than the load already has.
4383 if (LD->isUnindexed()) {
4384 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4385 if (Align > LD->getAlignment())
4386 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4387 Chain, Ptr, LD->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004388 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004389 LD->isVolatile(), Align);
4390 }
4391 }
4392
Evan Cheng45a7ca92007-05-01 00:38:21 +00004393
4394 // If load is not volatile and there are no uses of the loaded value (and
4395 // the updated indexed value in case of indexed loads), change uses of the
4396 // chain value into uses of the chain input (i.e. delete the dead load).
4397 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00004398 if (N->getValueType(1) == MVT::Other) {
4399 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004400 if (N->hasNUsesOfValue(0, 0)) {
4401 // It's not safe to use the two value CombineTo variant here. e.g.
4402 // v1, chain2 = load chain1, loc
4403 // v2, chain3 = load chain2, loc
4404 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004405 // Now we replace use of chain2 with chain1. This makes the second load
4406 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004407 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004408 DOUT << "\nWith chain: "; DEBUG(Chain.Val->dump(&DAG));
4409 DOUT << "\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004410 WorkListRemover DeadNodes(*this);
4411 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Chain, &DeadNodes);
Chris Lattner125991a2008-01-24 07:57:06 +00004412 if (N->use_empty()) {
4413 removeFromWorkList(N);
4414 DAG.DeleteNode(N);
4415 }
Evan Cheng02c42852008-01-16 23:11:54 +00004416 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
4417 }
Evan Cheng498f5592007-05-01 08:53:39 +00004418 } else {
4419 // Indexed loads.
4420 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4421 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Evan Cheng02c42852008-01-16 23:11:54 +00004422 SDOperand Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
4423 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
4424 DOUT << "\nWith: "; DEBUG(Undef.Val->dump(&DAG));
4425 DOUT << " and 2 other values\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004426 WorkListRemover DeadNodes(*this);
4427 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Undef, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004428 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1),
Chris Lattner4626b252008-01-17 07:20:38 +00004429 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004430 &DeadNodes);
4431 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004432 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00004433 DAG.DeleteNode(N);
4434 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004435 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004436 }
4437 }
Chris Lattner01a22022005-10-10 22:04:48 +00004438
4439 // If this load is directly stored, replace the load value with the stored
4440 // value.
4441 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004442 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohmanb061c4b2008-03-31 20:32:52 +00004443 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
4444 !LD->isVolatile()) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004445 if (ISD::isNON_TRUNCStore(Chain.Val)) {
4446 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4447 if (PrevST->getBasePtr() == Ptr &&
4448 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004449 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004450 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004451 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004452
Jim Laskey7ca56af2006-10-11 13:47:09 +00004453 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004454 // Walk up chain skipping non-aliasing memory nodes.
4455 SDOperand BetterChain = FindBetterChain(N, Chain);
4456
Jim Laskey6ff23e52006-10-04 16:53:27 +00004457 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004458 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004459 SDOperand ReplLoad;
4460
Jim Laskey279f0532006-09-25 16:29:54 +00004461 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004462 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4463 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004464 LD->getSrcValue(), LD->getSrcValueOffset(),
4465 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004466 } else {
4467 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4468 LD->getValueType(0),
4469 BetterChain, Ptr, LD->getSrcValue(),
4470 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004471 LD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004472 LD->isVolatile(),
4473 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004474 }
Jim Laskey279f0532006-09-25 16:29:54 +00004475
Jim Laskey6ff23e52006-10-04 16:53:27 +00004476 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00004477 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
4478 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004479
Jim Laskey274062c2006-10-13 23:32:28 +00004480 // Replace uses with load result and token factor. Don't add users
4481 // to work list.
4482 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004483 }
4484 }
4485
Evan Cheng7fc033a2006-11-03 03:06:21 +00004486 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004487 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00004488 return SDOperand(N, 0);
4489
Chris Lattner01a22022005-10-10 22:04:48 +00004490 return SDOperand();
4491}
4492
Chris Lattner07649d92008-01-08 23:08:06 +00004493
Chris Lattner87514ca2005-10-10 22:31:19 +00004494SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004495 StoreSDNode *ST = cast<StoreSDNode>(N);
4496 SDOperand Chain = ST->getChain();
4497 SDOperand Value = ST->getValue();
4498 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004499
Chris Lattner00161a62008-01-25 07:20:16 +00004500 // Try to infer better alignment information than the store already has.
4501 if (ST->isUnindexed()) {
4502 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4503 if (Align > ST->getAlignment())
4504 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004505 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004506 ST->isVolatile(), Align);
4507 }
4508 }
4509
Evan Cheng59d5b682007-05-07 21:27:48 +00004510 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004511 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004512 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004513 ST->isUnindexed()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004514 unsigned Align = ST->getAlignment();
4515 MVT::ValueType SVT = Value.getOperand(0).getValueType();
4516 unsigned OrigAlign = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00004517 getABITypeAlignment(MVT::getTypeForValueType(SVT));
Evan Chengc2cd2b22007-05-07 21:36:06 +00004518 if (Align <= OrigAlign && TLI.isOperationLegal(ISD::STORE, SVT))
Evan Cheng59d5b682007-05-07 21:27:48 +00004519 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004520 ST->getSrcValueOffset(), ST->isVolatile(), Align);
Jim Laskey279f0532006-09-25 16:29:54 +00004521 }
4522
Nate Begeman2cbba892006-12-11 02:23:46 +00004523 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004524 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Evan Cheng25ece662006-12-11 17:25:19 +00004525 if (Value.getOpcode() != ISD::TargetConstantFP) {
4526 SDOperand Tmp;
Chris Lattner62be1a72006-12-12 04:16:14 +00004527 switch (CFP->getValueType(0)) {
4528 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004529 case MVT::f80: // We don't do this for these yet.
4530 case MVT::f128:
4531 case MVT::ppcf128:
4532 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004533 case MVT::f32:
4534 if (!AfterLegalize || TLI.isTypeLegal(MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004535 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
4536 convertToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004537 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004538 ST->getSrcValueOffset(), ST->isVolatile(),
4539 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004540 }
4541 break;
4542 case MVT::f64:
4543 if (!AfterLegalize || TLI.isTypeLegal(MVT::i64)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004544 Tmp = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
4545 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004546 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004547 ST->getSrcValueOffset(), ST->isVolatile(),
4548 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004549 } else if (TLI.isTypeLegal(MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004550 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004551 // argument passing. Since this is so common, custom legalize the
4552 // 64-bit integer store into two 32-bit stores.
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004553 uint64_t Val = CFP->getValueAPF().convertToAPInt().getZExtValue();
Chris Lattner62be1a72006-12-12 04:16:14 +00004554 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4555 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00004556 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00004557
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004558 int SVOffset = ST->getSrcValueOffset();
4559 unsigned Alignment = ST->getAlignment();
4560 bool isVolatile = ST->isVolatile();
4561
Chris Lattner62be1a72006-12-12 04:16:14 +00004562 SDOperand St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004563 ST->getSrcValueOffset(),
4564 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004565 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4566 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004567 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004568 Alignment = MinAlign(Alignment, 4U);
Chris Lattner62be1a72006-12-12 04:16:14 +00004569 SDOperand St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004570 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004571 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4572 }
4573 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004574 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004575 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004576 }
4577
Jim Laskey279f0532006-09-25 16:29:54 +00004578 if (CombinerAA) {
4579 // Walk up chain skipping non-aliasing memory nodes.
4580 SDOperand BetterChain = FindBetterChain(N, Chain);
4581
Jim Laskey6ff23e52006-10-04 16:53:27 +00004582 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004583 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004584 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004585 SDOperand ReplStore;
4586 if (ST->isTruncatingStore()) {
4587 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004588 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004589 ST->getMemoryVT(),
Chris Lattner4626b252008-01-17 07:20:38 +00004590 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004591 } else {
4592 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004593 ST->getSrcValue(), ST->getSrcValueOffset(),
4594 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004595 }
4596
Jim Laskey279f0532006-09-25 16:29:54 +00004597 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00004598 SDOperand Token =
4599 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4600
4601 // Don't add users to work list.
4602 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004603 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004604 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004605
Evan Cheng33dbedc2006-11-05 09:31:14 +00004606 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004607 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00004608 return SDOperand(N, 0);
4609
Chris Lattner3c872852007-12-29 06:26:16 +00004610 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00004611 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Chris Lattner2b4c2792007-10-13 06:35:54 +00004612 MVT::isInteger(Value.getValueType())) {
4613 // See if we can simplify the input to this truncstore with knowledge that
4614 // only the low bits are being used. For example:
4615 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
4616 SDOperand Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004617 GetDemandedBits(Value,
4618 APInt::getLowBitsSet(Value.getValueSizeInBits(),
4619 MVT::getSizeInBits(ST->getMemoryVT())));
Chris Lattner2b4c2792007-10-13 06:35:54 +00004620 AddToWorkList(Value.Val);
4621 if (Shorter.Val)
4622 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004623 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner2b4c2792007-10-13 06:35:54 +00004624 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004625
4626 // Otherwise, see if we can simplify the operation with
4627 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00004628 if (SimplifyDemandedBits(Value,
4629 APInt::getLowBitsSet(
4630 Value.getValueSizeInBits(),
4631 MVT::getSizeInBits(ST->getMemoryVT()))))
Chris Lattnere33544c2007-10-13 06:58:48 +00004632 return SDOperand(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004633 }
4634
Chris Lattner3c872852007-12-29 06:26:16 +00004635 // If this is a load followed by a store to the same location, then the store
4636 // is dead/noop.
4637 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004638 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004639 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00004640 // There can't be any side effects between the load and store, such as
4641 // a call or store.
Chris Lattner572dee72008-01-16 05:49:24 +00004642 Chain.reachesChainWithoutSideEffects(SDOperand(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00004643 // The store is dead, remove it.
4644 return Chain;
4645 }
4646 }
4647
Chris Lattnerddf89562008-01-17 19:59:44 +00004648 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4649 // truncating store. We can do this even if this is already a truncstore.
4650 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
4651 && TLI.isTypeLegal(Value.getOperand(0).getValueType()) &&
4652 Value.Val->hasOneUse() && ST->isUnindexed() &&
4653 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004654 ST->getMemoryVT())) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004655 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004656 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnerddf89562008-01-17 19:59:44 +00004657 ST->isVolatile(), ST->getAlignment());
4658 }
4659
Chris Lattner87514ca2005-10-10 22:31:19 +00004660 return SDOperand();
4661}
4662
Chris Lattnerca242442006-03-19 01:27:56 +00004663SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4664 SDOperand InVec = N->getOperand(0);
4665 SDOperand InVal = N->getOperand(1);
4666 SDOperand EltNo = N->getOperand(2);
4667
4668 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4669 // vector with the inserted element.
4670 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
4671 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004672 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004673 if (Elt < Ops.size())
4674 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004675 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4676 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004677 }
4678
4679 return SDOperand();
4680}
4681
Evan Cheng513da432007-10-06 08:19:55 +00004682SDOperand DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004683 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
4684 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
4685 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
4686
4687 // Perform only after legalization to ensure build_vector / vector_shuffle
4688 // optimizations have already been done.
4689 if (!AfterLegalize) return SDOperand();
4690
Evan Cheng513da432007-10-06 08:19:55 +00004691 SDOperand InVec = N->getOperand(0);
4692 SDOperand EltNo = N->getOperand(1);
4693
Evan Cheng513da432007-10-06 08:19:55 +00004694 if (isa<ConstantSDNode>(EltNo)) {
4695 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
4696 bool NewLoad = false;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004697 MVT::ValueType VT = InVec.getValueType();
4698 MVT::ValueType EVT = MVT::getVectorElementType(VT);
4699 MVT::ValueType LVT = EVT;
4700 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
4701 MVT::ValueType BCVT = InVec.getOperand(0).getValueType();
4702 if (!MVT::isVector(BCVT)
4703 || (MVT::getSizeInBits(EVT) >
4704 MVT::getSizeInBits(MVT::getVectorElementType(BCVT))))
4705 return SDOperand();
4706 InVec = InVec.getOperand(0);
4707 EVT = MVT::getVectorElementType(BCVT);
4708 NewLoad = true;
4709 }
Evan Cheng513da432007-10-06 08:19:55 +00004710
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004711 LoadSDNode *LN0 = NULL;
4712 if (ISD::isNormalLoad(InVec.Val))
4713 LN0 = cast<LoadSDNode>(InVec);
4714 else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4715 InVec.getOperand(0).getValueType() == EVT &&
4716 ISD::isNormalLoad(InVec.getOperand(0).Val)) {
4717 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4718 } else if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE) {
4719 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
4720 // =>
4721 // (load $addr+1*size)
4722 unsigned Idx = cast<ConstantSDNode>(InVec.getOperand(2).
4723 getOperand(Elt))->getValue();
4724 unsigned NumElems = InVec.getOperand(2).getNumOperands();
4725 InVec = (Idx < NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
4726 if (InVec.getOpcode() == ISD::BIT_CONVERT)
4727 InVec = InVec.getOperand(0);
4728 if (ISD::isNormalLoad(InVec.Val)) {
4729 LN0 = cast<LoadSDNode>(InVec);
4730 Elt = (Idx < NumElems) ? Idx : Idx - NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00004731 }
4732 }
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004733 if (!LN0 || !LN0->hasOneUse())
4734 return SDOperand();
4735
4736 unsigned Align = LN0->getAlignment();
4737 if (NewLoad) {
4738 // Check the resultant load doesn't need a higher alignment than the
4739 // original load.
4740 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
4741 getABITypeAlignment(MVT::getTypeForValueType(LVT));
4742 if (!TLI.isOperationLegal(ISD::LOAD, LVT) || NewAlign > Align)
4743 return SDOperand();
4744 Align = NewAlign;
4745 }
4746
4747 SDOperand NewPtr = LN0->getBasePtr();
4748 if (Elt) {
4749 unsigned PtrOff = MVT::getSizeInBits(LVT) * Elt / 8;
4750 MVT::ValueType PtrType = NewPtr.getValueType();
4751 if (TLI.isBigEndian())
4752 PtrOff = MVT::getSizeInBits(VT) / 8 - PtrOff;
4753 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
4754 DAG.getConstant(PtrOff, PtrType));
4755 }
4756 return DAG.getLoad(LVT, LN0->getChain(), NewPtr,
4757 LN0->getSrcValue(), LN0->getSrcValueOffset(),
4758 LN0->isVolatile(), Align);
Evan Cheng513da432007-10-06 08:19:55 +00004759 }
4760 return SDOperand();
4761}
4762
4763
Dan Gohman7f321562007-06-25 16:23:39 +00004764SDOperand DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
4765 unsigned NumInScalars = N->getNumOperands();
4766 MVT::ValueType VT = N->getValueType(0);
4767 unsigned NumElts = MVT::getVectorNumElements(VT);
4768 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattnerca242442006-03-19 01:27:56 +00004769
Dan Gohman7f321562007-06-25 16:23:39 +00004770 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
4771 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
4772 // at most two distinct vectors, turn this into a shuffle node.
Chris Lattnerd7648c82006-03-28 20:28:38 +00004773 SDOperand VecIn1, VecIn2;
4774 for (unsigned i = 0; i != NumInScalars; ++i) {
4775 // Ignore undef inputs.
4776 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4777
Dan Gohman7f321562007-06-25 16:23:39 +00004778 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00004779 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00004780 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00004781 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
4782 VecIn1 = VecIn2 = SDOperand(0, 0);
4783 break;
4784 }
4785
Dan Gohman7f321562007-06-25 16:23:39 +00004786 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00004787 // we can't make a shuffle.
4788 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004789 if (ExtractedFromVec.getValueType() != VT) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004790 VecIn1 = VecIn2 = SDOperand(0, 0);
4791 break;
4792 }
4793
4794 // Otherwise, remember this. We allow up to two distinct input vectors.
4795 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
4796 continue;
4797
4798 if (VecIn1.Val == 0) {
4799 VecIn1 = ExtractedFromVec;
4800 } else if (VecIn2.Val == 0) {
4801 VecIn2 = ExtractedFromVec;
4802 } else {
4803 // Too many inputs.
4804 VecIn1 = VecIn2 = SDOperand(0, 0);
4805 break;
4806 }
4807 }
4808
4809 // If everything is good, we can make a shuffle operation.
4810 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004811 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004812 for (unsigned i = 0; i != NumInScalars; ++i) {
4813 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00004814 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004815 continue;
4816 }
4817
4818 SDOperand Extract = N->getOperand(i);
4819
4820 // If extracting from the first vector, just use the index directly.
4821 if (Extract.getOperand(0) == VecIn1) {
4822 BuildVecIndices.push_back(Extract.getOperand(1));
4823 continue;
4824 }
4825
4826 // Otherwise, use InIdx + VecSize
4827 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004828 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004829 }
4830
4831 // Add count and size info.
Chris Lattner0bd48932008-01-17 07:00:52 +00004832 MVT::ValueType BuildVecVT = MVT::getVectorType(TLI.getPointerTy(), NumElts);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004833
Dan Gohman7f321562007-06-25 16:23:39 +00004834 // Return the new VECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004835 SDOperand Ops[5];
4836 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00004837 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004838 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00004839 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004840 // Use an undef build_vector as input for the second operand.
Chris Lattnercef896e2006-03-28 22:19:47 +00004841 std::vector<SDOperand> UnOps(NumInScalars,
4842 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00004843 EltType));
4844 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004845 &UnOps[0], UnOps.size());
4846 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00004847 }
Dan Gohman7f321562007-06-25 16:23:39 +00004848 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004849 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004850 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004851 }
4852
4853 return SDOperand();
4854}
4855
Dan Gohman7f321562007-06-25 16:23:39 +00004856SDOperand DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
4857 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
4858 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
4859 // inputs come from at most two distinct vectors, turn this into a shuffle
4860 // node.
4861
4862 // If we only have one input vector, we don't need to do any concatenation.
4863 if (N->getNumOperands() == 1) {
4864 return N->getOperand(0);
4865 }
4866
4867 return SDOperand();
4868}
4869
Chris Lattner66445d32006-03-28 22:11:53 +00004870SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004871 SDOperand ShufMask = N->getOperand(2);
4872 unsigned NumElts = ShufMask.getNumOperands();
4873
4874 // If the shuffle mask is an identity operation on the LHS, return the LHS.
4875 bool isIdentity = true;
4876 for (unsigned i = 0; i != NumElts; ++i) {
4877 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4878 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
4879 isIdentity = false;
4880 break;
4881 }
4882 }
4883 if (isIdentity) return N->getOperand(0);
4884
4885 // If the shuffle mask is an identity operation on the RHS, return the RHS.
4886 isIdentity = true;
4887 for (unsigned i = 0; i != NumElts; ++i) {
4888 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4889 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
4890 isIdentity = false;
4891 break;
4892 }
4893 }
4894 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00004895
4896 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
4897 // needed at all.
4898 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00004899 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004900 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00004901 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004902 for (unsigned i = 0; i != NumElts; ++i)
4903 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
4904 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
4905 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00004906 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00004907 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00004908 BaseIdx = Idx;
4909 } else {
4910 if (BaseIdx != Idx)
4911 isSplat = false;
4912 if (VecNum != V) {
4913 isUnary = false;
4914 break;
4915 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00004916 }
4917 }
4918
4919 SDOperand N0 = N->getOperand(0);
4920 SDOperand N1 = N->getOperand(1);
4921 // Normalize unary shuffle so the RHS is undef.
4922 if (isUnary && VecNum == 1)
4923 std::swap(N0, N1);
4924
Evan Cheng917ec982006-07-21 08:25:53 +00004925 // If it is a splat, check if the argument vector is a build_vector with
4926 // all scalar elements the same.
4927 if (isSplat) {
4928 SDNode *V = N0.Val;
Evan Cheng917ec982006-07-21 08:25:53 +00004929
Dan Gohman7f321562007-06-25 16:23:39 +00004930 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00004931 // not the number of vector elements, look through it. Be careful not to
4932 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00004933 if (V->getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng59569222006-10-16 22:49:37 +00004934 SDOperand ConvInput = V->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004935 if (MVT::getVectorNumElements(ConvInput.getValueType()) == NumElts)
Evan Cheng59569222006-10-16 22:49:37 +00004936 V = ConvInput.Val;
4937 }
4938
Dan Gohman7f321562007-06-25 16:23:39 +00004939 if (V->getOpcode() == ISD::BUILD_VECTOR) {
4940 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00004941 if (NumElems > BaseIdx) {
4942 SDOperand Base;
4943 bool AllSame = true;
4944 for (unsigned i = 0; i != NumElems; ++i) {
4945 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
4946 Base = V->getOperand(i);
4947 break;
4948 }
4949 }
4950 // Splat of <u, u, u, u>, return <u, u, u, u>
4951 if (!Base.Val)
4952 return N0;
4953 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00004954 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00004955 AllSame = false;
4956 break;
4957 }
4958 }
4959 // Splat of <x, x, x, x>, return <x, x, x, x>
4960 if (AllSame)
4961 return N0;
4962 }
4963 }
4964 }
4965
Evan Chenge7bec0d2006-07-20 22:44:41 +00004966 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
4967 // into an undef.
4968 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00004969 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
4970 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004971 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00004972 for (unsigned i = 0; i != NumElts; ++i) {
4973 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
4974 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
4975 MappedOps.push_back(ShufMask.getOperand(i));
4976 } else {
4977 unsigned NewIdx =
4978 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
4979 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
4980 }
4981 }
Dan Gohman7f321562007-06-25 16:23:39 +00004982 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004983 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00004984 AddToWorkList(ShufMask.Val);
Dan Gohman7f321562007-06-25 16:23:39 +00004985 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
4986 N0,
4987 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
4988 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00004989 }
Dan Gohman7f321562007-06-25 16:23:39 +00004990
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004991 return SDOperand();
4992}
4993
Evan Cheng44f1f092006-04-20 08:56:16 +00004994/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00004995/// an AND to a vector_shuffle with the destination vector and a zero vector.
4996/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00004997/// vector_shuffle V, Zero, <0, 4, 2, 4>
4998SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
4999 SDOperand LHS = N->getOperand(0);
5000 SDOperand RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00005001 if (N->getOpcode() == ISD::AND) {
5002 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00005003 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005004 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Evan Cheng44f1f092006-04-20 08:56:16 +00005005 std::vector<SDOperand> IdxOps;
5006 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00005007 unsigned NumElts = NumOps;
5008 MVT::ValueType EVT = MVT::getVectorElementType(RHS.getValueType());
Evan Cheng44f1f092006-04-20 08:56:16 +00005009 for (unsigned i = 0; i != NumElts; ++i) {
5010 SDOperand Elt = RHS.getOperand(i);
5011 if (!isa<ConstantSDNode>(Elt))
5012 return SDOperand();
5013 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
5014 IdxOps.push_back(DAG.getConstant(i, EVT));
5015 else if (cast<ConstantSDNode>(Elt)->isNullValue())
5016 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
5017 else
5018 return SDOperand();
5019 }
5020
5021 // Let's see if the target supports this vector_shuffle.
5022 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
5023 return SDOperand();
5024
Dan Gohman7f321562007-06-25 16:23:39 +00005025 // Return the new VECTOR_SHUFFLE node.
5026 MVT::ValueType VT = MVT::getVectorType(EVT, NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00005027 std::vector<SDOperand> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005028 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00005029 Ops.push_back(LHS);
5030 AddToWorkList(LHS.Val);
5031 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00005032 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005033 &ZeroOps[0], ZeroOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00005034 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005035 &IdxOps[0], IdxOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00005036 SDOperand Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005037 &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00005038 if (VT != LHS.getValueType()) {
5039 Result = DAG.getNode(ISD::BIT_CONVERT, LHS.getValueType(), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00005040 }
5041 return Result;
5042 }
5043 }
5044 return SDOperand();
5045}
5046
Dan Gohman7f321562007-06-25 16:23:39 +00005047/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
5048SDOperand DAGCombiner::SimplifyVBinOp(SDNode *N) {
5049 // After legalize, the target may be depending on adds and other
5050 // binary ops to provide legal ways to construct constants or other
5051 // things. Simplifying them may result in a loss of legality.
5052 if (AfterLegalize) return SDOperand();
5053
5054 MVT::ValueType VT = N->getValueType(0);
Dan Gohman05d92fe2007-07-13 20:03:40 +00005055 assert(MVT::isVector(VT) && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00005056
5057 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattneredab1b92006-04-02 03:25:57 +00005058 SDOperand LHS = N->getOperand(0);
5059 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00005060 SDOperand Shuffle = XformToShuffleWithZero(N);
5061 if (Shuffle.Val) return Shuffle;
5062
Dan Gohman7f321562007-06-25 16:23:39 +00005063 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00005064 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00005065 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
5066 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005067 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005068 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Chris Lattneredab1b92006-04-02 03:25:57 +00005069 SDOperand LHSOp = LHS.getOperand(i);
5070 SDOperand RHSOp = RHS.getOperand(i);
5071 // If these two elements can't be folded, bail out.
5072 if ((LHSOp.getOpcode() != ISD::UNDEF &&
5073 LHSOp.getOpcode() != ISD::Constant &&
5074 LHSOp.getOpcode() != ISD::ConstantFP) ||
5075 (RHSOp.getOpcode() != ISD::UNDEF &&
5076 RHSOp.getOpcode() != ISD::Constant &&
5077 RHSOp.getOpcode() != ISD::ConstantFP))
5078 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00005079 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00005080 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
5081 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00005082 if ((RHSOp.getOpcode() == ISD::Constant &&
5083 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
5084 (RHSOp.getOpcode() == ISD::ConstantFP &&
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00005085 cast<ConstantFPSDNode>(RHSOp.Val)->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00005086 break;
5087 }
Dan Gohman7f321562007-06-25 16:23:39 +00005088 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00005089 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00005090 assert((Ops.back().getOpcode() == ISD::UNDEF ||
5091 Ops.back().getOpcode() == ISD::Constant ||
5092 Ops.back().getOpcode() == ISD::ConstantFP) &&
5093 "Scalar binop didn't fold!");
5094 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005095
Dan Gohman7f321562007-06-25 16:23:39 +00005096 if (Ops.size() == LHS.getNumOperands()) {
5097 MVT::ValueType VT = LHS.getValueType();
5098 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005099 }
Chris Lattneredab1b92006-04-02 03:25:57 +00005100 }
5101
5102 return SDOperand();
5103}
5104
Nate Begeman44728a72005-09-19 22:34:01 +00005105SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00005106 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
5107
5108 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
5109 cast<CondCodeSDNode>(N0.getOperand(2))->get());
5110 // If we got a simplified select_cc node back from SimplifySelectCC, then
5111 // break it down into a new SETCC node, and a new SELECT node, and then return
5112 // the SELECT node, since we were called with a SELECT node.
5113 if (SCC.Val) {
5114 // Check to see if we got a select_cc back (to turn into setcc/select).
5115 // Otherwise, just return whatever node we got back, like fabs.
5116 if (SCC.getOpcode() == ISD::SELECT_CC) {
5117 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
5118 SCC.getOperand(0), SCC.getOperand(1),
5119 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00005120 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005121 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
5122 SCC.getOperand(3), SETCC);
5123 }
5124 return SCC;
5125 }
Nate Begeman44728a72005-09-19 22:34:01 +00005126 return SDOperand();
5127}
5128
Chris Lattner40c62d52005-10-18 06:04:22 +00005129/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
5130/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00005131/// select. Callers of this should assume that TheSelect is deleted if this
5132/// returns true. As such, they should return the appropriate thing (e.g. the
5133/// node) back to the top-level of the DAG combiner loop to avoid it being
5134/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00005135///
5136bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
5137 SDOperand RHS) {
5138
5139 // If this is a select from two identical things, try to pull the operation
5140 // through the select.
5141 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00005142 // If this is a load and the token chain is identical, replace the select
5143 // of two loads with a load through a select of the address to load from.
5144 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
5145 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00005146 if (LHS.getOpcode() == ISD::LOAD &&
Chris Lattner40c62d52005-10-18 06:04:22 +00005147 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00005148 LHS.getOperand(0) == RHS.getOperand(0)) {
5149 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5150 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5151
5152 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005153 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00005154 // FIXME: this conflates two src values, discarding one. This is not
5155 // the right thing to do, but nothing uses srcvalues now. When they do,
5156 // turn SrcValue into a list of locations.
5157 SDOperand Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005158 if (TheSelect->getOpcode() == ISD::SELECT) {
5159 // Check that the condition doesn't reach either load. If so, folding
5160 // this will induce a cycle into the DAG.
Evan Cheng917be682008-03-04 00:41:45 +00005161 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5162 !RLD->isPredecessorOf(TheSelect->getOperand(0).Val)) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005163 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
5164 TheSelect->getOperand(0), LLD->getBasePtr(),
5165 RLD->getBasePtr());
5166 }
5167 } else {
5168 // Check that the condition doesn't reach either load. If so, folding
5169 // this will induce a cycle into the DAG.
Evan Cheng917be682008-03-04 00:41:45 +00005170 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5171 !RLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5172 !LLD->isPredecessorOf(TheSelect->getOperand(1).Val) &&
5173 !RLD->isPredecessorOf(TheSelect->getOperand(1).Val)) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005174 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00005175 TheSelect->getOperand(0),
5176 TheSelect->getOperand(1),
5177 LLD->getBasePtr(), RLD->getBasePtr(),
5178 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005179 }
Evan Cheng466685d2006-10-09 20:57:25 +00005180 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005181
5182 if (Addr.Val) {
5183 SDOperand Load;
5184 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
5185 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
5186 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005187 LLD->getSrcValueOffset(),
5188 LLD->isVolatile(),
5189 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005190 else {
5191 Load = DAG.getExtLoad(LLD->getExtensionType(),
5192 TheSelect->getValueType(0),
5193 LLD->getChain(), Addr, LLD->getSrcValue(),
5194 LLD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005195 LLD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005196 LLD->isVolatile(),
5197 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005198 }
5199 // Users of the select now use the result of the load.
5200 CombineTo(TheSelect, Load);
5201
5202 // Users of the old loads now use the new load's chain. We know the
5203 // old-load value is dead now.
5204 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
5205 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
5206 return true;
5207 }
Evan Chengc5484282006-10-04 00:56:09 +00005208 }
Chris Lattner40c62d52005-10-18 06:04:22 +00005209 }
5210 }
5211
5212 return false;
5213}
5214
Nate Begeman44728a72005-09-19 22:34:01 +00005215SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
5216 SDOperand N2, SDOperand N3,
Chris Lattner1eba01e2007-04-11 06:50:51 +00005217 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00005218
5219 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00005220 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
5221 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
5222 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
5223
5224 // Determine if the condition we're dealing with is constant
Scott Michel5b8f82e2008-03-10 15:42:14 +00005225 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00005226 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005227 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
5228
5229 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00005230 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005231 return N2;
5232 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00005233 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005234 return N3;
5235
5236 // Check to see if we can simplify the select into an fabs node
5237 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5238 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00005239 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005240 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5241 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5242 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5243 N2 == N3.getOperand(0))
5244 return DAG.getNode(ISD::FABS, VT, N0);
5245
5246 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5247 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5248 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5249 N2.getOperand(0) == N3)
5250 return DAG.getNode(ISD::FABS, VT, N3);
5251 }
5252 }
5253
5254 // Check to see if we can perform the "gzip trick", transforming
5255 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00005256 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00005257 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00005258 MVT::isInteger(N2.getValueType()) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005259 (N1C->isNullValue() || // (a < 0) ? b : 0
5260 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00005261 MVT::ValueType XType = N0.getValueType();
5262 MVT::ValueType AType = N2.getValueType();
5263 if (XType >= AType) {
5264 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00005265 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00005266 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
5267 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Nate Begemanf845b452005-10-08 00:29:44 +00005268 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
5269 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5270 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00005271 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005272 if (XType > AType) {
5273 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005274 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005275 }
5276 return DAG.getNode(ISD::AND, AType, Shift, N2);
5277 }
5278 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5279 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5280 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00005281 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005282 if (XType > AType) {
5283 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005284 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005285 }
5286 return DAG.getNode(ISD::AND, AType, Shift, N2);
5287 }
5288 }
Nate Begeman07ed4172005-10-10 21:26:48 +00005289
5290 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00005291 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Nate Begeman07ed4172005-10-10 21:26:48 +00005292 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00005293
5294 // If the caller doesn't want us to simplify this into a zext of a compare,
5295 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00005296 if (NotExtCompare && N2C->getAPIntValue() == 1)
Chris Lattner1eba01e2007-04-11 06:50:51 +00005297 return SDOperand();
5298
Nate Begeman07ed4172005-10-10 21:26:48 +00005299 // Get a SetCC of the condition
5300 // FIXME: Should probably make sure that setcc is legal if we ever have a
5301 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00005302 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00005303 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00005304 if (AfterLegalize) {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005305 SCC = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Chris Lattner555d8d62006-12-07 22:36:47 +00005306 if (N2.getValueType() < SCC.getValueType())
5307 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5308 else
5309 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005310 } else {
5311 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00005312 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005313 }
Chris Lattner5750df92006-03-01 04:03:14 +00005314 AddToWorkList(SCC.Val);
5315 AddToWorkList(Temp.Val);
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005316
Dan Gohman002e5d02008-03-13 22:13:53 +00005317 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005318 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00005319 // shl setcc result by log2 n2c
5320 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00005321 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Nate Begeman07ed4172005-10-10 21:26:48 +00005322 TLI.getShiftAmountTy()));
5323 }
5324
Nate Begemanf845b452005-10-08 00:29:44 +00005325 // Check to see if this is the equivalent of setcc
5326 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5327 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00005328 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005329 MVT::ValueType XType = N0.getValueType();
Scott Michel5b8f82e2008-03-10 15:42:14 +00005330 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(N0))) {
5331 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00005332 if (Res.getValueType() != VT)
5333 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5334 return Res;
5335 }
5336
5337 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5338 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
5339 TLI.isOperationLegal(ISD::CTLZ, XType)) {
5340 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
5341 return DAG.getNode(ISD::SRL, XType, Ctlz,
5342 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
5343 TLI.getShiftAmountTy()));
5344 }
5345 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5346 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
5347 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
5348 N0);
5349 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
5350 DAG.getConstant(~0ULL, XType));
5351 return DAG.getNode(ISD::SRL, XType,
5352 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
5353 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5354 TLI.getShiftAmountTy()));
5355 }
5356 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5357 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
5358 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
5359 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5360 TLI.getShiftAmountTy()));
5361 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5362 }
5363 }
5364
5365 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5366 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5367 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005368 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
5369 N2.getOperand(0) == N1 && MVT::isInteger(N0.getValueType())) {
5370 MVT::ValueType XType = N0.getValueType();
5371 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5372 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5373 TLI.getShiftAmountTy()));
5374 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
5375 AddToWorkList(Shift.Val);
5376 AddToWorkList(Add.Val);
5377 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5378 }
5379 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5380 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5381 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5382 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5383 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Nate Begemanf845b452005-10-08 00:29:44 +00005384 MVT::ValueType XType = N0.getValueType();
5385 if (SubC->isNullValue() && MVT::isInteger(XType)) {
5386 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5387 DAG.getConstant(MVT::getSizeInBits(XType)-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005388 TLI.getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00005389 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005390 AddToWorkList(Shift.Val);
5391 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005392 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5393 }
5394 }
5395 }
Chris Lattner1982ef22007-04-11 05:11:38 +00005396
Nate Begeman44728a72005-09-19 22:34:01 +00005397 return SDOperand();
5398}
5399
Evan Chengfa1eb272007-02-08 22:13:59 +00005400/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Nate Begeman452d7be2005-09-16 00:54:12 +00005401SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00005402 SDOperand N1, ISD::CondCode Cond,
5403 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00005404 TargetLowering::DAGCombinerInfo
5405 DagCombineInfo(DAG, !AfterLegalize, false, this);
5406 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00005407}
5408
Nate Begeman69575232005-10-20 02:15:44 +00005409/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5410/// return a DAG expression to select that will generate the same value by
5411/// multiplying by a magic number. See:
5412/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5413SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005414 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005415 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
5416
Andrew Lenharth232c9102006-06-12 16:07:18 +00005417 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005418 ii != ee; ++ii)
5419 AddToWorkList(*ii);
5420 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005421}
5422
5423/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5424/// return a DAG expression to select that will generate the same value by
5425/// multiplying by a magic number. See:
5426/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5427SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005428 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005429 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00005430
Andrew Lenharth232c9102006-06-12 16:07:18 +00005431 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005432 ii != ee; ++ii)
5433 AddToWorkList(*ii);
5434 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005435}
5436
Jim Laskey71382342006-10-07 23:37:56 +00005437/// FindBaseOffset - Return true if base is known not to alias with anything
5438/// but itself. Provides base object and offset as results.
5439static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
5440 // Assume it is a primitive operation.
5441 Base = Ptr; Offset = 0;
5442
5443 // If it's an adding a simple constant then integrate the offset.
5444 if (Base.getOpcode() == ISD::ADD) {
5445 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5446 Base = Base.getOperand(0);
5447 Offset += C->getValue();
5448 }
5449 }
5450
5451 // If it's any of the following then it can't alias with anything but itself.
5452 return isa<FrameIndexSDNode>(Base) ||
5453 isa<ConstantPoolSDNode>(Base) ||
5454 isa<GlobalAddressSDNode>(Base);
5455}
5456
5457/// isAlias - Return true if there is any possibility that the two addresses
5458/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00005459bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
5460 const Value *SrcValue1, int SrcValueOffset1,
5461 SDOperand Ptr2, int64_t Size2,
5462 const Value *SrcValue2, int SrcValueOffset2)
5463{
Jim Laskey71382342006-10-07 23:37:56 +00005464 // If they are the same then they must be aliases.
5465 if (Ptr1 == Ptr2) return true;
5466
5467 // Gather base node and offset information.
5468 SDOperand Base1, Base2;
5469 int64_t Offset1, Offset2;
5470 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5471 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5472
5473 // If they have a same base address then...
5474 if (Base1 == Base2) {
5475 // Check to see if the addresses overlap.
5476 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5477 }
5478
Jim Laskey096c22e2006-10-18 12:29:57 +00005479 // If we know both bases then they can't alias.
5480 if (KnownBase1 && KnownBase2) return false;
5481
Jim Laskey07a27092006-10-18 19:08:31 +00005482 if (CombinerGlobalAA) {
5483 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00005484 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5485 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5486 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00005487 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00005488 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00005489 if (AAResult == AliasAnalysis::NoAlias)
5490 return false;
5491 }
Jim Laskey096c22e2006-10-18 12:29:57 +00005492
5493 // Otherwise we have to assume they alias.
5494 return true;
Jim Laskey71382342006-10-07 23:37:56 +00005495}
5496
5497/// FindAliasInfo - Extracts the relevant alias information from the memory
5498/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00005499bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00005500 SDOperand &Ptr, int64_t &Size,
5501 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005502 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5503 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005504 Size = MVT::getSizeInBits(LD->getMemoryVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005505 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005506 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00005507 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005508 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005509 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005510 Size = MVT::getSizeInBits(ST->getMemoryVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005511 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005512 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00005513 } else {
Jim Laskey71382342006-10-07 23:37:56 +00005514 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005515 }
5516
5517 return false;
5518}
5519
Jim Laskey6ff23e52006-10-04 16:53:27 +00005520/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5521/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00005522void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005523 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005524 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005525 std::set<SDNode *> Visited; // Visited node set.
5526
Jim Laskey279f0532006-09-25 16:29:54 +00005527 // Get alias information for node.
5528 SDOperand Ptr;
5529 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005530 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005531 int SrcValueOffset;
5532 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005533
Jim Laskey6ff23e52006-10-04 16:53:27 +00005534 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005535 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005536
Jim Laskeybc588b82006-10-05 15:07:25 +00005537 // Look at each chain and determine if it is an alias. If so, add it to the
5538 // aliases list. If not, then continue up the chain looking for the next
5539 // candidate.
5540 while (!Chains.empty()) {
5541 SDOperand Chain = Chains.back();
5542 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005543
Jim Laskeybc588b82006-10-05 15:07:25 +00005544 // Don't bother if we've been before.
5545 if (Visited.find(Chain.Val) != Visited.end()) continue;
5546 Visited.insert(Chain.Val);
5547
5548 switch (Chain.getOpcode()) {
5549 case ISD::EntryToken:
5550 // Entry token is ideal chain operand, but handled in FindBetterChain.
5551 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005552
Jim Laskeybc588b82006-10-05 15:07:25 +00005553 case ISD::LOAD:
5554 case ISD::STORE: {
5555 // Get alias information for Chain.
5556 SDOperand OpPtr;
5557 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005558 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005559 int OpSrcValueOffset;
5560 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
5561 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005562
5563 // If chain is alias then stop here.
5564 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005565 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5566 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005567 Aliases.push_back(Chain);
5568 } else {
5569 // Look further up the chain.
5570 Chains.push_back(Chain.getOperand(0));
5571 // Clean up old chain.
5572 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00005573 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005574 break;
5575 }
5576
5577 case ISD::TokenFactor:
5578 // We have to check each of the operands of the token factor, so we queue
5579 // then up. Adding the operands to the queue (stack) in reverse order
5580 // maintains the original order and increases the likelihood that getNode
5581 // will find a matching token factor (CSE.)
5582 for (unsigned n = Chain.getNumOperands(); n;)
5583 Chains.push_back(Chain.getOperand(--n));
5584 // Eliminate the token factor if we can.
5585 AddToWorkList(Chain.Val);
5586 break;
5587
5588 default:
5589 // For all other instructions we will just have to take what we can get.
5590 Aliases.push_back(Chain);
5591 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005592 }
5593 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005594}
5595
5596/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5597/// for a better chain (aliasing node.)
5598SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
5599 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005600
Jim Laskey6ff23e52006-10-04 16:53:27 +00005601 // Accumulate all the aliases to this node.
5602 GatherAllAliases(N, OldChain, Aliases);
5603
5604 if (Aliases.size() == 0) {
5605 // If no operands then chain to entry token.
5606 return DAG.getEntryNode();
5607 } else if (Aliases.size() == 1) {
5608 // If a single operand then chain to it. We don't need to revisit it.
5609 return Aliases[0];
5610 }
5611
5612 // Construct a custom tailored token factor.
5613 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5614 &Aliases[0], Aliases.size());
5615
5616 // Make sure the old chain gets cleaned up.
5617 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
5618
5619 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005620}
5621
Nate Begeman1d4d4142005-09-01 00:19:25 +00005622// SelectionDAG::Combine - This is the entry point for the file.
5623//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005624void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Chris Lattner938ab022007-01-16 04:55:25 +00005625 if (!RunningAfterLegalize && ViewDAGCombine1)
5626 viewGraph();
5627 if (RunningAfterLegalize && ViewDAGCombine2)
5628 viewGraph();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005629 /// run - This is the main entry point to this class.
5630 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005631 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005632}