blob: ad8a2279f9f3493c5f19e7a2a0dce852fb4fafc7 [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Nate Begeman1d4d4142005-09-01 00:19:25 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
Nate Begeman1d4d4142005-09-01 00:19:25 +000012//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "dagcombine"
Nate Begeman1d4d4142005-09-01 00:19:25 +000016#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner00161a62008-01-25 07:20:16 +000017#include "llvm/CodeGen/MachineFunction.h"
18#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000019#include "llvm/Analysis/AliasAnalysis.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000020#include "llvm/Target/TargetData.h"
Chris Lattner1329cb82008-01-26 19:45:50 +000021#include "llvm/Target/TargetFrameInfo.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000022#include "llvm/Target/TargetLowering.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000023#include "llvm/Target/TargetMachine.h"
Chris Lattnerddae4bd2007-01-08 23:04:05 +000024#include "llvm/Target/TargetOptions.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000025#include "llvm/ADT/SmallPtrSet.h"
26#include "llvm/ADT/Statistic.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000028#include "llvm/Support/CommandLine.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/MathExtras.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000031#include <algorithm>
Dan Gohmanee335e32008-05-23 20:40:06 +000032#include <set>
Nate Begeman1d4d4142005-09-01 00:19:25 +000033using namespace llvm;
34
Chris Lattnercd3245a2006-12-19 22:41:21 +000035STATISTIC(NodesCombined , "Number of dag nodes combined");
36STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
37STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
38
Nate Begeman1d4d4142005-09-01 00:19:25 +000039namespace {
Chris Lattner938ab022007-01-16 04:55:25 +000040#ifndef NDEBUG
41 static cl::opt<bool>
42 ViewDAGCombine1("view-dag-combine1-dags", cl::Hidden,
43 cl::desc("Pop up a window to show dags before the first "
44 "dag combine pass"));
45 static cl::opt<bool>
46 ViewDAGCombine2("view-dag-combine2-dags", cl::Hidden,
47 cl::desc("Pop up a window to show dags before the second "
48 "dag combine pass"));
49#else
50 static const bool ViewDAGCombine1 = false;
51 static const bool ViewDAGCombine2 = false;
52#endif
53
Jim Laskey71382342006-10-07 23:37:56 +000054 static cl::opt<bool>
55 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000056 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000057
Jim Laskey07a27092006-10-18 19:08:31 +000058 static cl::opt<bool>
59 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
60 cl::desc("Include global information in alias analysis"));
61
Jim Laskeybc588b82006-10-05 15:07:25 +000062//------------------------------ DAGCombiner ---------------------------------//
63
Jim Laskey71382342006-10-07 23:37:56 +000064 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000065 SelectionDAG &DAG;
66 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000067 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000068
69 // Worklist of all of the nodes that need to be simplified.
70 std::vector<SDNode*> WorkList;
71
Jim Laskeyc7c3f112006-10-16 20:52:31 +000072 // AA - Used for DAG load/store alias analysis.
73 AliasAnalysis &AA;
74
Nate Begeman1d4d4142005-09-01 00:19:25 +000075 /// AddUsersToWorkList - When an instruction is simplified, add all users of
76 /// the instruction to the work lists because they might get more simplified
77 /// now.
78 ///
79 void AddUsersToWorkList(SDNode *N) {
80 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000081 UI != UE; ++UI)
Roman Levensteindc1adac2008-04-07 10:06:32 +000082 AddToWorkList(UI->getUser());
Nate Begeman1d4d4142005-09-01 00:19:25 +000083 }
84
Dan Gohman389079b2007-10-08 17:57:15 +000085 /// visit - call the node-specific routine that knows how to fold each
86 /// particular type of node.
87 SDOperand visit(SDNode *N);
88
Chris Lattner24664722006-03-01 04:53:38 +000089 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +000090 /// AddToWorkList - Add to the work list making sure it's instance is at the
91 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +000092 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000093 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +000094 WorkList.push_back(N);
95 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000096
Chris Lattnerf8dc0612008-02-03 06:49:24 +000097 /// removeFromWorkList - remove all instances of N from the worklist.
98 ///
99 void removeFromWorkList(SDNode *N) {
100 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
101 WorkList.end());
Chris Lattner01a22022005-10-10 22:04:48 +0000102 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000103
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000104 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo,
105 bool AddTo = true);
106
Jim Laskey274062c2006-10-13 23:32:28 +0000107 SDOperand CombineTo(SDNode *N, SDOperand Res, bool AddTo = true) {
108 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000109 }
110
Jim Laskey274062c2006-10-13 23:32:28 +0000111 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1,
112 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +0000113 SDOperand To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000114 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000115 }
Chris Lattner0bd48932008-01-17 07:00:52 +0000116
Chris Lattner24664722006-03-01 04:53:38 +0000117 private:
118
Chris Lattner012f2412006-02-17 21:58:01 +0000119 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000120 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000121 /// propagation. If so, return true.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000122 bool SimplifyDemandedBits(SDOperand Op) {
123 APInt Demanded = APInt::getAllOnesValue(Op.getValueSizeInBits());
124 return SimplifyDemandedBits(Op, Demanded);
125 }
126
127 bool SimplifyDemandedBits(SDOperand Op, const APInt &Demanded);
Chris Lattner87514ca2005-10-10 22:31:19 +0000128
Chris Lattner448f2192006-11-11 00:39:41 +0000129 bool CombineToPreIndexedLoadStore(SDNode *N);
130 bool CombineToPostIndexedLoadStore(SDNode *N);
131
132
Dan Gohman389079b2007-10-08 17:57:15 +0000133 /// combine - call the node-specific routine that knows how to fold each
134 /// particular type of node. If that doesn't do anything, try the
135 /// target-specific DAG combines.
136 SDOperand combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000137
138 // Visitation implementation - Implement dag node combining for different
139 // node types. The semantics are as follows:
140 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000141 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000142 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000143 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000144 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000145 SDOperand visitTokenFactor(SDNode *N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000146 SDOperand visitMERGE_VALUES(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000147 SDOperand visitADD(SDNode *N);
148 SDOperand visitSUB(SDNode *N);
Chris Lattner91153682007-03-04 20:03:15 +0000149 SDOperand visitADDC(SDNode *N);
150 SDOperand visitADDE(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000151 SDOperand visitMUL(SDNode *N);
152 SDOperand visitSDIV(SDNode *N);
153 SDOperand visitUDIV(SDNode *N);
154 SDOperand visitSREM(SDNode *N);
155 SDOperand visitUREM(SDNode *N);
156 SDOperand visitMULHU(SDNode *N);
157 SDOperand visitMULHS(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +0000158 SDOperand visitSMUL_LOHI(SDNode *N);
159 SDOperand visitUMUL_LOHI(SDNode *N);
160 SDOperand visitSDIVREM(SDNode *N);
161 SDOperand visitUDIVREM(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000162 SDOperand visitAND(SDNode *N);
163 SDOperand visitOR(SDNode *N);
164 SDOperand visitXOR(SDNode *N);
Dan Gohman7f321562007-06-25 16:23:39 +0000165 SDOperand SimplifyVBinOp(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000166 SDOperand visitSHL(SDNode *N);
167 SDOperand visitSRA(SDNode *N);
168 SDOperand visitSRL(SDNode *N);
169 SDOperand visitCTLZ(SDNode *N);
170 SDOperand visitCTTZ(SDNode *N);
171 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000172 SDOperand visitSELECT(SDNode *N);
173 SDOperand visitSELECT_CC(SDNode *N);
174 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000175 SDOperand visitSIGN_EXTEND(SDNode *N);
176 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000177 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000178 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
179 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000180 SDOperand visitBIT_CONVERT(SDNode *N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +0000181 SDOperand visitBUILD_PAIR(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000182 SDOperand visitFADD(SDNode *N);
183 SDOperand visitFSUB(SDNode *N);
184 SDOperand visitFMUL(SDNode *N);
185 SDOperand visitFDIV(SDNode *N);
186 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000187 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000188 SDOperand visitSINT_TO_FP(SDNode *N);
189 SDOperand visitUINT_TO_FP(SDNode *N);
190 SDOperand visitFP_TO_SINT(SDNode *N);
191 SDOperand visitFP_TO_UINT(SDNode *N);
192 SDOperand visitFP_ROUND(SDNode *N);
193 SDOperand visitFP_ROUND_INREG(SDNode *N);
194 SDOperand visitFP_EXTEND(SDNode *N);
195 SDOperand visitFNEG(SDNode *N);
196 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000197 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000198 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000199 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000200 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000201 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
Evan Cheng513da432007-10-06 08:19:55 +0000202 SDOperand visitEXTRACT_VECTOR_ELT(SDNode *N);
Dan Gohman7f321562007-06-25 16:23:39 +0000203 SDOperand visitBUILD_VECTOR(SDNode *N);
204 SDOperand visitCONCAT_VECTORS(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000205 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000206
Evan Cheng44f1f092006-04-20 08:56:16 +0000207 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000208 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
209
Chris Lattnere70da202007-12-06 07:33:36 +0000210 SDOperand visitShiftByConstant(SDNode *N, unsigned Amt);
211
Chris Lattner40c62d52005-10-18 06:04:22 +0000212 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000213 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000214 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
215 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
Chris Lattner1eba01e2007-04-11 06:50:51 +0000216 SDOperand N3, ISD::CondCode CC,
217 bool NotExtCompare = false);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000218 SDOperand SimplifySetCC(MVT VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000219 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner5eee4272008-01-26 01:09:19 +0000220 SDOperand SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
221 unsigned HiOp);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000222 SDOperand CombineConsecutiveLoads(SDNode *N, MVT VT);
223 SDOperand ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, MVT);
Nate Begeman69575232005-10-20 02:15:44 +0000224 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000225 SDOperand BuildUDIV(SDNode *N);
226 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Evan Chengc88138f2007-03-22 01:54:19 +0000227 SDOperand ReduceLoadWidth(SDNode *N);
Jim Laskey279f0532006-09-25 16:29:54 +0000228
Dan Gohman2e68b6f2008-02-25 21:11:39 +0000229 SDOperand GetDemandedBits(SDOperand V, const APInt &Mask);
Chris Lattner2b4c2792007-10-13 06:35:54 +0000230
Jim Laskey6ff23e52006-10-04 16:53:27 +0000231 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
232 /// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +0000233 void GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000234 SmallVector<SDOperand, 8> &Aliases);
235
Jim Laskey096c22e2006-10-18 12:29:57 +0000236 /// isAlias - Return true if there is any possibility that the two addresses
237 /// overlap.
238 bool isAlias(SDOperand Ptr1, int64_t Size1,
239 const Value *SrcValue1, int SrcValueOffset1,
240 SDOperand Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000241 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000242
Jim Laskey7ca56af2006-10-11 13:47:09 +0000243 /// FindAliasInfo - Extracts the relevant alias information from the memory
244 /// node. Returns true if the operand was a load.
245 bool FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +0000246 SDOperand &Ptr, int64_t &Size,
247 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000248
Jim Laskey279f0532006-09-25 16:29:54 +0000249 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000250 /// looking for a better chain (aliasing node.)
Jim Laskey279f0532006-09-25 16:29:54 +0000251 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
252
Nate Begeman1d4d4142005-09-01 00:19:25 +0000253public:
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000254 DAGCombiner(SelectionDAG &D, AliasAnalysis &A)
255 : DAG(D),
256 TLI(D.getTargetLoweringInfo()),
257 AfterLegalize(false),
258 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000259
260 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000261 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000262 };
263}
264
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000265
266namespace {
267/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
268/// nodes from the worklist.
269class VISIBILITY_HIDDEN WorkListRemover :
270 public SelectionDAG::DAGUpdateListener {
271 DAGCombiner &DC;
272public:
Dan Gohmanb5660dc2008-02-20 16:44:09 +0000273 explicit WorkListRemover(DAGCombiner &dc) : DC(dc) {}
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000274
Duncan Sandsedfcf592008-06-11 11:42:12 +0000275 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000276 DC.removeFromWorkList(N);
277 }
278
279 virtual void NodeUpdated(SDNode *N) {
280 // Ignore updates.
281 }
282};
283}
284
Chris Lattner24664722006-03-01 04:53:38 +0000285//===----------------------------------------------------------------------===//
286// TargetLowering::DAGCombinerInfo implementation
287//===----------------------------------------------------------------------===//
288
289void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
290 ((DAGCombiner*)DC)->AddToWorkList(N);
291}
292
293SDOperand TargetLowering::DAGCombinerInfo::
294CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000295 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000296}
297
298SDOperand TargetLowering::DAGCombinerInfo::
299CombineTo(SDNode *N, SDOperand Res) {
300 return ((DAGCombiner*)DC)->CombineTo(N, Res);
301}
302
303
304SDOperand TargetLowering::DAGCombinerInfo::
305CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
306 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
307}
308
309
Chris Lattner24664722006-03-01 04:53:38 +0000310//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000311// Helper Functions
312//===----------------------------------------------------------------------===//
313
314/// isNegatibleForFree - Return 1 if we can compute the negated form of the
315/// specified expression for the same cost as the expression itself, or 2 if we
316/// can compute the negated form more cheaply than the expression itself.
Chris Lattner0254e702008-02-26 07:04:54 +0000317static char isNegatibleForFree(SDOperand Op, bool AfterLegalize,
318 unsigned Depth = 0) {
Dale Johannesendb44bf82007-10-16 23:38:29 +0000319 // No compile time optimizations on this type.
320 if (Op.getValueType() == MVT::ppcf128)
321 return 0;
322
Chris Lattner29446522007-05-14 22:04:50 +0000323 // fneg is removable even if it has multiple uses.
324 if (Op.getOpcode() == ISD::FNEG) return 2;
325
326 // Don't allow anything with multiple uses.
327 if (!Op.hasOneUse()) return 0;
328
Chris Lattner3adf9512007-05-25 02:19:06 +0000329 // Don't recurse exponentially.
330 if (Depth > 6) return 0;
331
Chris Lattner29446522007-05-14 22:04:50 +0000332 switch (Op.getOpcode()) {
333 default: return false;
334 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000335 // Don't invert constant FP values after legalize. The negated constant
336 // isn't necessarily legal.
337 return AfterLegalize ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000338 case ISD::FADD:
339 // FIXME: determine better conditions for this xform.
340 if (!UnsafeFPMath) return 0;
341
342 // -(A+B) -> -A - B
Chris Lattner0254e702008-02-26 07:04:54 +0000343 if (char V = isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000344 return V;
345 // -(A+B) -> -B - A
Chris Lattner0254e702008-02-26 07:04:54 +0000346 return isNegatibleForFree(Op.getOperand(1), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000347 case ISD::FSUB:
348 // We can't turn -(A-B) into B-A when we honor signed zeros.
349 if (!UnsafeFPMath) return 0;
350
351 // -(A-B) -> B-A
352 return 1;
353
354 case ISD::FMUL:
355 case ISD::FDIV:
356 if (HonorSignDependentRoundingFPMath()) return 0;
357
358 // -(X*Y) -> (-X * Y) or (X*-Y)
Chris Lattner0254e702008-02-26 07:04:54 +0000359 if (char V = isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000360 return V;
361
Chris Lattner0254e702008-02-26 07:04:54 +0000362 return isNegatibleForFree(Op.getOperand(1), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000363
364 case ISD::FP_EXTEND:
365 case ISD::FP_ROUND:
366 case ISD::FSIN:
Chris Lattner0254e702008-02-26 07:04:54 +0000367 return isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000368 }
369}
370
371/// GetNegatedExpression - If isNegatibleForFree returns true, this function
372/// returns the newly negated expression.
Chris Lattner3adf9512007-05-25 02:19:06 +0000373static SDOperand GetNegatedExpression(SDOperand Op, SelectionDAG &DAG,
Chris Lattner0254e702008-02-26 07:04:54 +0000374 bool AfterLegalize, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000375 // fneg is removable even if it has multiple uses.
376 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
377
378 // Don't allow anything with multiple uses.
379 assert(Op.hasOneUse() && "Unknown reuse!");
380
Chris Lattner3adf9512007-05-25 02:19:06 +0000381 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000382 switch (Op.getOpcode()) {
383 default: assert(0 && "Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000384 case ISD::ConstantFP: {
385 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
386 V.changeSign();
387 return DAG.getConstantFP(V, Op.getValueType());
388 }
Chris Lattner29446522007-05-14 22:04:50 +0000389 case ISD::FADD:
390 // FIXME: determine better conditions for this xform.
391 assert(UnsafeFPMath);
392
393 // -(A+B) -> -A - B
Chris Lattner0254e702008-02-26 07:04:54 +0000394 if (isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000395 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000396 GetNegatedExpression(Op.getOperand(0), DAG,
397 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000398 Op.getOperand(1));
399 // -(A+B) -> -B - A
400 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000401 GetNegatedExpression(Op.getOperand(1), DAG,
402 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000403 Op.getOperand(0));
404 case ISD::FSUB:
405 // We can't turn -(A-B) into B-A when we honor signed zeros.
406 assert(UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000407
408 // -(0-B) -> B
409 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000410 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000411 return Op.getOperand(1);
Chris Lattner29446522007-05-14 22:04:50 +0000412
413 // -(A-B) -> B-A
414 return DAG.getNode(ISD::FSUB, Op.getValueType(), Op.getOperand(1),
415 Op.getOperand(0));
416
417 case ISD::FMUL:
418 case ISD::FDIV:
419 assert(!HonorSignDependentRoundingFPMath());
420
421 // -(X*Y) -> -X * Y
Chris Lattneraeecb6c2008-02-26 17:09:59 +0000422 if (isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000423 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000424 GetNegatedExpression(Op.getOperand(0), DAG,
425 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000426 Op.getOperand(1));
427
428 // -(X*Y) -> X * -Y
429 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
430 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000431 GetNegatedExpression(Op.getOperand(1), DAG,
432 AfterLegalize, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000433
434 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000435 case ISD::FSIN:
436 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000437 GetNegatedExpression(Op.getOperand(0), DAG,
438 AfterLegalize, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000439 case ISD::FP_ROUND:
440 return DAG.getNode(ISD::FP_ROUND, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000441 GetNegatedExpression(Op.getOperand(0), DAG,
442 AfterLegalize, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000443 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000444 }
445}
Chris Lattner24664722006-03-01 04:53:38 +0000446
447
Nate Begeman4ebd8052005-09-01 23:24:04 +0000448// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
449// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000450// Also, set the incoming LHS, RHS, and CC references to the appropriate
451// nodes based on the type of node we are checking. This simplifies life a
452// bit for the callers.
453static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
454 SDOperand &CC) {
455 if (N.getOpcode() == ISD::SETCC) {
456 LHS = N.getOperand(0);
457 RHS = N.getOperand(1);
458 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000459 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000460 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000461 if (N.getOpcode() == ISD::SELECT_CC &&
462 N.getOperand(2).getOpcode() == ISD::Constant &&
463 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000464 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000465 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
466 LHS = N.getOperand(0);
467 RHS = N.getOperand(1);
468 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000469 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000470 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000471 return false;
472}
473
Nate Begeman99801192005-09-07 23:25:52 +0000474// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
475// one use. If this is true, it allows the users to invert the operation for
476// free when it is profitable to do so.
477static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000478 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000479 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000480 return true;
481 return false;
482}
483
Nate Begemancd4d58c2006-02-03 06:46:56 +0000484SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
Duncan Sands83ec4b62008-06-06 12:08:01 +0000485 MVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000486 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
487 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
488 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
489 if (isa<ConstantSDNode>(N1)) {
490 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000491 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000492 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
493 } else if (N0.hasOneUse()) {
494 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000495 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000496 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
497 }
498 }
499 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
500 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
501 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
502 if (isa<ConstantSDNode>(N0)) {
503 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000504 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000505 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
506 } else if (N1.hasOneUse()) {
507 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000508 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000509 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
510 }
511 }
512 return SDOperand();
513}
514
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000515SDOperand DAGCombiner::CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo,
516 bool AddTo) {
517 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
518 ++NodesCombined;
519 DOUT << "\nReplacing.1 "; DEBUG(N->dump(&DAG));
520 DOUT << "\nWith: "; DEBUG(To[0].Val->dump(&DAG));
521 DOUT << " and " << NumTo-1 << " other values\n";
522 WorkListRemover DeadNodes(*this);
523 DAG.ReplaceAllUsesWith(N, To, &DeadNodes);
524
525 if (AddTo) {
526 // Push the new nodes and any users onto the worklist
527 for (unsigned i = 0, e = NumTo; i != e; ++i) {
528 AddToWorkList(To[i].Val);
529 AddUsersToWorkList(To[i].Val);
530 }
531 }
532
533 // Nodes can be reintroduced into the worklist. Make sure we do not
534 // process a node that has been replaced.
535 removeFromWorkList(N);
536
537 // Finally, since the node is now dead, remove it from the graph.
538 DAG.DeleteNode(N);
539 return SDOperand(N, 0);
540}
541
542/// SimplifyDemandedBits - Check the specified integer node value to see if
543/// it can be simplified or if things it uses can be simplified by bit
544/// propagation. If so, return true.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000545bool DAGCombiner::SimplifyDemandedBits(SDOperand Op, const APInt &Demanded) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000546 TargetLowering::TargetLoweringOpt TLO(DAG, AfterLegalize);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000547 APInt KnownZero, KnownOne;
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000548 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
549 return false;
550
551 // Revisit the node.
552 AddToWorkList(Op.Val);
553
554 // Replace the old value with the new one.
555 ++NodesCombined;
556 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.Val->dump(&DAG));
557 DOUT << "\nWith: "; DEBUG(TLO.New.Val->dump(&DAG));
558 DOUT << '\n';
559
560 // Replace all uses. If any nodes become isomorphic to other nodes and
561 // are deleted, make sure to remove them from our worklist.
562 WorkListRemover DeadNodes(*this);
563 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
564
565 // Push the new node and any (possibly new) users onto the worklist.
566 AddToWorkList(TLO.New.Val);
567 AddUsersToWorkList(TLO.New.Val);
568
569 // Finally, if the node is now dead, remove it from the graph. The node
570 // may not be dead if the replacement process recursively simplified to
571 // something else needing this node.
572 if (TLO.Old.Val->use_empty()) {
573 removeFromWorkList(TLO.Old.Val);
574
575 // If the operands of this node are only used by the node, they will now
576 // be dead. Make sure to visit them first to delete dead nodes early.
577 for (unsigned i = 0, e = TLO.Old.Val->getNumOperands(); i != e; ++i)
578 if (TLO.Old.Val->getOperand(i).Val->hasOneUse())
579 AddToWorkList(TLO.Old.Val->getOperand(i).Val);
580
581 DAG.DeleteNode(TLO.Old.Val);
582 }
583 return true;
584}
585
Chris Lattner29446522007-05-14 22:04:50 +0000586//===----------------------------------------------------------------------===//
587// Main DAG Combiner implementation
588//===----------------------------------------------------------------------===//
589
Nate Begeman4ebd8052005-09-01 23:24:04 +0000590void DAGCombiner::Run(bool RunningAfterLegalize) {
591 // set the instance variable, so that the various visit routines may use it.
592 AfterLegalize = RunningAfterLegalize;
593
Nate Begeman646d7e22005-09-02 21:18:40 +0000594 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000595 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
596 E = DAG.allnodes_end(); I != E; ++I)
597 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000598
Chris Lattner95038592005-10-05 06:35:28 +0000599 // Create a dummy node (which is not added to allnodes), that adds a reference
600 // to the root node, preventing it from being deleted, and tracking any
601 // changes of the root.
602 HandleSDNode Dummy(DAG.getRoot());
603
Jim Laskey26f7fa72006-10-17 19:33:52 +0000604 // The root of the dag may dangle to deleted nodes until the dag combiner is
605 // done. Set it to null to avoid confusion.
606 DAG.setRoot(SDOperand());
Chris Lattner24664722006-03-01 04:53:38 +0000607
Nate Begeman1d4d4142005-09-01 00:19:25 +0000608 // while the worklist isn't empty, inspect the node on the end of it and
609 // try and combine it.
610 while (!WorkList.empty()) {
611 SDNode *N = WorkList.back();
612 WorkList.pop_back();
613
614 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000615 // N is deleted from the DAG, since they too may now be dead or may have a
616 // reduced number of uses, allowing other xforms.
617 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000618 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000619 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000620
Chris Lattner95038592005-10-05 06:35:28 +0000621 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000622 continue;
623 }
624
Dan Gohman389079b2007-10-08 17:57:15 +0000625 SDOperand RV = combine(N);
Chris Lattner24664722006-03-01 04:53:38 +0000626
Chris Lattner50d8e492008-01-25 23:34:24 +0000627 if (RV.Val == 0)
628 continue;
629
630 ++NodesCombined;
Chris Lattner5eee4272008-01-26 01:09:19 +0000631
Chris Lattner50d8e492008-01-25 23:34:24 +0000632 // If we get back the same node we passed in, rather than a new node or
633 // zero, we know that the node must have defined multiple values and
634 // CombineTo was used. Since CombineTo takes care of the worklist
635 // mechanics for us, we have no work to do in this case.
636 if (RV.Val == N)
637 continue;
638
639 assert(N->getOpcode() != ISD::DELETED_NODE &&
640 RV.Val->getOpcode() != ISD::DELETED_NODE &&
641 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +0000642
Chris Lattner50d8e492008-01-25 23:34:24 +0000643 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
644 DOUT << "\nWith: "; DEBUG(RV.Val->dump(&DAG));
645 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000646 WorkListRemover DeadNodes(*this);
Chris Lattner50d8e492008-01-25 23:34:24 +0000647 if (N->getNumValues() == RV.Val->getNumValues())
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000648 DAG.ReplaceAllUsesWith(N, RV.Val, &DeadNodes);
Chris Lattner50d8e492008-01-25 23:34:24 +0000649 else {
Chris Lattner5eee4272008-01-26 01:09:19 +0000650 assert(N->getValueType(0) == RV.getValueType() &&
651 N->getNumValues() == 1 && "Type mismatch");
Chris Lattner50d8e492008-01-25 23:34:24 +0000652 SDOperand OpV = RV;
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000653 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000654 }
Chris Lattner50d8e492008-01-25 23:34:24 +0000655
656 // Push the new node and any users onto the worklist
657 AddToWorkList(RV.Val);
658 AddUsersToWorkList(RV.Val);
659
660 // Add any uses of the old node to the worklist in case this node is the
661 // last one that uses them. They may become dead after this node is
662 // deleted.
663 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
664 AddToWorkList(N->getOperand(i).Val);
665
666 // Nodes can be reintroduced into the worklist. Make sure we do not
667 // process a node that has been replaced.
668 removeFromWorkList(N);
Chris Lattner50d8e492008-01-25 23:34:24 +0000669
670 // Finally, since the node is now dead, remove it from the graph.
671 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000672 }
Chris Lattner95038592005-10-05 06:35:28 +0000673
674 // If the root changed (e.g. it was a dead load, update the root).
675 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000676}
677
Nate Begeman83e75ec2005-09-06 04:43:02 +0000678SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000679 switch(N->getOpcode()) {
680 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000681 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000682 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000683 case ISD::ADD: return visitADD(N);
684 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000685 case ISD::ADDC: return visitADDC(N);
686 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000687 case ISD::MUL: return visitMUL(N);
688 case ISD::SDIV: return visitSDIV(N);
689 case ISD::UDIV: return visitUDIV(N);
690 case ISD::SREM: return visitSREM(N);
691 case ISD::UREM: return visitUREM(N);
692 case ISD::MULHU: return visitMULHU(N);
693 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000694 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
695 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
696 case ISD::SDIVREM: return visitSDIVREM(N);
697 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000698 case ISD::AND: return visitAND(N);
699 case ISD::OR: return visitOR(N);
700 case ISD::XOR: return visitXOR(N);
701 case ISD::SHL: return visitSHL(N);
702 case ISD::SRA: return visitSRA(N);
703 case ISD::SRL: return visitSRL(N);
704 case ISD::CTLZ: return visitCTLZ(N);
705 case ISD::CTTZ: return visitCTTZ(N);
706 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000707 case ISD::SELECT: return visitSELECT(N);
708 case ISD::SELECT_CC: return visitSELECT_CC(N);
709 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000710 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
711 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000712 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000713 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
714 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000715 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +0000716 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000717 case ISD::FADD: return visitFADD(N);
718 case ISD::FSUB: return visitFSUB(N);
719 case ISD::FMUL: return visitFMUL(N);
720 case ISD::FDIV: return visitFDIV(N);
721 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000722 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000723 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
724 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
725 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
726 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
727 case ISD::FP_ROUND: return visitFP_ROUND(N);
728 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
729 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
730 case ISD::FNEG: return visitFNEG(N);
731 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000732 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000733 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000734 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000735 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000736 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +0000737 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000738 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
739 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000740 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000741 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000742 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000743}
744
Dan Gohman389079b2007-10-08 17:57:15 +0000745SDOperand DAGCombiner::combine(SDNode *N) {
746
747 SDOperand RV = visit(N);
748
749 // If nothing happened, try a target-specific DAG combine.
750 if (RV.Val == 0) {
751 assert(N->getOpcode() != ISD::DELETED_NODE &&
752 "Node was deleted but visit returned NULL!");
753
754 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
755 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
756
757 // Expose the DAG combiner to the target combiner impls.
758 TargetLowering::DAGCombinerInfo
759 DagCombineInfo(DAG, !AfterLegalize, false, this);
760
761 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
762 }
763 }
764
Evan Cheng08b11732008-03-22 01:55:50 +0000765 // If N is a commutative binary node, try commuting it to enable more
766 // sdisel CSE.
767 if (RV.Val == 0 &&
768 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
769 N->getNumValues() == 1) {
770 SDOperand N0 = N->getOperand(0);
771 SDOperand N1 = N->getOperand(1);
772 // Constant operands are canonicalized to RHS.
773 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
774 SDOperand Ops[] = { N1, N0 };
775 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
776 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +0000777 if (CSENode)
Evan Cheng08b11732008-03-22 01:55:50 +0000778 return SDOperand(CSENode, 0);
779 }
780 }
781
Dan Gohman389079b2007-10-08 17:57:15 +0000782 return RV;
783}
784
Chris Lattner6270f682006-10-08 22:57:01 +0000785/// getInputChainForNode - Given a node, return its input chain if it has one,
786/// otherwise return a null sd operand.
787static SDOperand getInputChainForNode(SDNode *N) {
788 if (unsigned NumOps = N->getNumOperands()) {
789 if (N->getOperand(0).getValueType() == MVT::Other)
790 return N->getOperand(0);
791 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
792 return N->getOperand(NumOps-1);
793 for (unsigned i = 1; i < NumOps-1; ++i)
794 if (N->getOperand(i).getValueType() == MVT::Other)
795 return N->getOperand(i);
796 }
797 return SDOperand(0, 0);
798}
799
Nate Begeman83e75ec2005-09-06 04:43:02 +0000800SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000801 // If N has two operands, where one has an input chain equal to the other,
802 // the 'other' chain is redundant.
803 if (N->getNumOperands() == 2) {
804 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
805 return N->getOperand(0);
806 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
807 return N->getOperand(1);
808 }
809
Chris Lattnerc76d4412007-05-16 06:37:59 +0000810 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
811 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
812 SmallPtrSet<SDNode*, 16> SeenOps;
813 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000814
815 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000816 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000817
Jim Laskey71382342006-10-07 23:37:56 +0000818 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000819 // encountered.
820 for (unsigned i = 0; i < TFs.size(); ++i) {
821 SDNode *TF = TFs[i];
822
Jim Laskey6ff23e52006-10-04 16:53:27 +0000823 // Check each of the operands.
824 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
825 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000826
Jim Laskey6ff23e52006-10-04 16:53:27 +0000827 switch (Op.getOpcode()) {
828 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000829 // Entry tokens don't need to be added to the list. They are
830 // rededundant.
831 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000832 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000833
Jim Laskey6ff23e52006-10-04 16:53:27 +0000834 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000835 if ((CombinerAA || Op.hasOneUse()) &&
836 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000837 // Queue up for processing.
838 TFs.push_back(Op.Val);
839 // Clean up in case the token factor is removed.
840 AddToWorkList(Op.Val);
841 Changed = true;
842 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000843 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000844 // Fall thru
845
846 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000847 // Only add if it isn't already in the list.
848 if (SeenOps.insert(Op.Val))
Jim Laskeybc588b82006-10-05 15:07:25 +0000849 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000850 else
851 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000852 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000853 }
854 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000855 }
856
857 SDOperand Result;
858
859 // If we've change things around then replace token factor.
860 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +0000861 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000862 // The entry token is the only possible outcome.
863 Result = DAG.getEntryNode();
864 } else {
865 // New and improved token factor.
866 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000867 }
Jim Laskey274062c2006-10-13 23:32:28 +0000868
869 // Don't add users to work list.
870 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000871 }
Jim Laskey279f0532006-09-25 16:29:54 +0000872
Jim Laskey6ff23e52006-10-04 16:53:27 +0000873 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000874}
875
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000876/// MERGE_VALUES can always be eliminated.
877SDOperand DAGCombiner::visitMERGE_VALUES(SDNode *N) {
878 WorkListRemover DeadNodes(*this);
879 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
880 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, i), N->getOperand(i),
881 &DeadNodes);
882 removeFromWorkList(N);
883 DAG.DeleteNode(N);
884 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
885}
886
887
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000888static
889SDOperand combineShlAddConstant(SDOperand N0, SDOperand N1, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000890 MVT VT = N0.getValueType();
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000891 SDOperand N00 = N0.getOperand(0);
892 SDOperand N01 = N0.getOperand(1);
893 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
894 if (N01C && N00.getOpcode() == ISD::ADD && N00.Val->hasOneUse() &&
895 isa<ConstantSDNode>(N00.getOperand(1))) {
896 N0 = DAG.getNode(ISD::ADD, VT,
897 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
898 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
899 return DAG.getNode(ISD::ADD, VT, N0, N1);
900 }
901 return SDOperand();
902}
903
Evan Chengb13cdbd2007-06-21 07:39:16 +0000904static
905SDOperand combineSelectAndUse(SDNode *N, SDOperand Slct, SDOperand OtherOp,
906 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000907 MVT VT = N->getValueType(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000908 unsigned Opc = N->getOpcode();
909 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
910 SDOperand LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
911 SDOperand RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
912 ISD::CondCode CC = ISD::SETCC_INVALID;
913 if (isSlctCC)
914 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
915 else {
916 SDOperand CCOp = Slct.getOperand(0);
917 if (CCOp.getOpcode() == ISD::SETCC)
918 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
919 }
920
921 bool DoXform = false;
922 bool InvCC = false;
923 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
924 "Bad input!");
925 if (LHS.getOpcode() == ISD::Constant &&
926 cast<ConstantSDNode>(LHS)->isNullValue())
927 DoXform = true;
928 else if (CC != ISD::SETCC_INVALID &&
929 RHS.getOpcode() == ISD::Constant &&
930 cast<ConstantSDNode>(RHS)->isNullValue()) {
931 std::swap(LHS, RHS);
Chris Lattner4626b252008-01-17 07:20:38 +0000932 SDOperand Op0 = Slct.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000933 bool isInt = (isSlctCC ? Op0.getValueType() :
934 Op0.getOperand(0).getValueType()).isInteger();
Evan Chengb13cdbd2007-06-21 07:39:16 +0000935 CC = ISD::getSetCCInverse(CC, isInt);
936 DoXform = true;
937 InvCC = true;
938 }
939
940 if (DoXform) {
941 SDOperand Result = DAG.getNode(Opc, VT, OtherOp, RHS);
942 if (isSlctCC)
943 return DAG.getSelectCC(OtherOp, Result,
944 Slct.getOperand(0), Slct.getOperand(1), CC);
945 SDOperand CCOp = Slct.getOperand(0);
946 if (InvCC)
947 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
948 CCOp.getOperand(1), CC);
949 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
950 }
951 return SDOperand();
952}
953
Nate Begeman83e75ec2005-09-06 04:43:02 +0000954SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000955 SDOperand N0 = N->getOperand(0);
956 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000957 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
958 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000959 MVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000960
961 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +0000962 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +0000963 SDOperand FoldedVOp = SimplifyVBinOp(N);
964 if (FoldedVOp.Val) return FoldedVOp;
965 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000966
Dan Gohman613e0d82007-07-03 14:03:57 +0000967 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +0000968 if (N0.getOpcode() == ISD::UNDEF)
969 return N0;
970 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +0000971 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000972 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000973 if (N0C && N1C)
Dan Gohman002e5d02008-03-13 22:13:53 +0000974 return DAG.getConstant(N0C->getAPIntValue() + N1C->getAPIntValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000975 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000976 if (N0C && !N1C)
977 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000978 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000979 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000980 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000981 // fold ((c1-A)+c2) -> (c1+c2)-A
982 if (N1C && N0.getOpcode() == ISD::SUB)
983 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
984 return DAG.getNode(ISD::SUB, VT,
Dan Gohman002e5d02008-03-13 22:13:53 +0000985 DAG.getConstant(N1C->getAPIntValue()+
986 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000987 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000988 // reassociate add
989 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
990 if (RADD.Val != 0)
991 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000992 // fold ((0-A) + B) -> B-A
993 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
994 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000995 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000996 // fold (A + (0-B)) -> A-B
997 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
998 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000999 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001000 // fold (A+(B-A)) -> B
1001 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001002 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +00001003
Duncan Sands83ec4b62008-06-06 12:08:01 +00001004 if (!VT.isVector() && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001005 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +00001006
1007 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001008 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001009 APInt LHSZero, LHSOne;
1010 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001011 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001012 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001013 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001014 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +00001015
1016 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1017 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1018 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1019 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1020 return DAG.getNode(ISD::OR, VT, N0, N1);
1021 }
1022 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001023
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001024 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
1025 if (N0.getOpcode() == ISD::SHL && N0.Val->hasOneUse()) {
1026 SDOperand Result = combineShlAddConstant(N0, N1, DAG);
1027 if (Result.Val) return Result;
1028 }
1029 if (N1.getOpcode() == ISD::SHL && N1.Val->hasOneUse()) {
1030 SDOperand Result = combineShlAddConstant(N1, N0, DAG);
1031 if (Result.Val) return Result;
1032 }
1033
Evan Chengb13cdbd2007-06-21 07:39:16 +00001034 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
1035 if (N0.getOpcode() == ISD::SELECT && N0.Val->hasOneUse()) {
1036 SDOperand Result = combineSelectAndUse(N, N0, N1, DAG);
1037 if (Result.Val) return Result;
1038 }
1039 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1040 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1041 if (Result.Val) return Result;
1042 }
1043
Nate Begeman83e75ec2005-09-06 04:43:02 +00001044 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001045}
1046
Chris Lattner91153682007-03-04 20:03:15 +00001047SDOperand DAGCombiner::visitADDC(SDNode *N) {
1048 SDOperand N0 = N->getOperand(0);
1049 SDOperand N1 = N->getOperand(1);
1050 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1051 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001052 MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001053
1054 // If the flag result is dead, turn this into an ADD.
1055 if (N->hasNUsesOfValue(0, 1))
1056 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +00001057 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +00001058
1059 // canonicalize constant to RHS.
Chris Lattnerbcf24842007-03-04 20:08:45 +00001060 if (N0C && !N1C) {
1061 SDOperand Ops[] = { N1, N0 };
1062 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
1063 }
Chris Lattner91153682007-03-04 20:03:15 +00001064
Chris Lattnerb6541762007-03-04 20:40:38 +00001065 // fold (addc x, 0) -> x + no carry out
1066 if (N1C && N1C->isNullValue())
1067 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1068
1069 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001070 APInt LHSZero, LHSOne;
1071 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001072 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001073 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001074 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001075 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001076
1077 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1078 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1079 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1080 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1081 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
1082 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1083 }
Chris Lattner91153682007-03-04 20:03:15 +00001084
1085 return SDOperand();
1086}
1087
1088SDOperand DAGCombiner::visitADDE(SDNode *N) {
1089 SDOperand N0 = N->getOperand(0);
1090 SDOperand N1 = N->getOperand(1);
Chris Lattnerb6541762007-03-04 20:40:38 +00001091 SDOperand CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001092 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1093 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001094 //MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001095
1096 // canonicalize constant to RHS
Chris Lattnerbcf24842007-03-04 20:08:45 +00001097 if (N0C && !N1C) {
Chris Lattnerb6541762007-03-04 20:40:38 +00001098 SDOperand Ops[] = { N1, N0, CarryIn };
Chris Lattnerbcf24842007-03-04 20:08:45 +00001099 return DAG.getNode(ISD::ADDE, N->getVTList(), Ops, 3);
1100 }
Chris Lattner91153682007-03-04 20:03:15 +00001101
Chris Lattnerb6541762007-03-04 20:40:38 +00001102 // fold (adde x, y, false) -> (addc x, y)
1103 if (CarryIn.getOpcode() == ISD::CARRY_FALSE) {
1104 SDOperand Ops[] = { N1, N0 };
1105 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
1106 }
Chris Lattner91153682007-03-04 20:03:15 +00001107
1108 return SDOperand();
1109}
1110
1111
1112
Nate Begeman83e75ec2005-09-06 04:43:02 +00001113SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001114 SDOperand N0 = N->getOperand(0);
1115 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001116 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1117 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001118 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001119
Dan Gohman7f321562007-06-25 16:23:39 +00001120 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001121 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001122 SDOperand FoldedVOp = SimplifyVBinOp(N);
1123 if (FoldedVOp.Val) return FoldedVOp;
1124 }
Dan Gohman7f321562007-06-25 16:23:39 +00001125
Chris Lattner854077d2005-10-17 01:07:11 +00001126 // fold (sub x, x) -> 0
Evan Chengc8e3b142008-03-12 07:02:50 +00001127 if (N0 == N1)
Chris Lattner854077d2005-10-17 01:07:11 +00001128 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001129 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001130 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001131 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +00001132 // fold (sub x, c) -> (add x, -c)
1133 if (N1C)
Dan Gohman002e5d02008-03-13 22:13:53 +00001134 return DAG.getNode(ISD::ADD, VT, N0,
1135 DAG.getConstant(-N1C->getAPIntValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001136 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001137 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001138 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001139 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001140 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001141 return N0.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +00001142 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
1143 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1144 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1145 if (Result.Val) return Result;
1146 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001147 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001148 if (N0.getOpcode() == ISD::UNDEF)
1149 return N0;
1150 if (N1.getOpcode() == ISD::UNDEF)
1151 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001152
Nate Begeman83e75ec2005-09-06 04:43:02 +00001153 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001154}
1155
Nate Begeman83e75ec2005-09-06 04:43:02 +00001156SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001157 SDOperand N0 = N->getOperand(0);
1158 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001159 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1160 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001161 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001162
Dan Gohman7f321562007-06-25 16:23:39 +00001163 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001164 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001165 SDOperand FoldedVOp = SimplifyVBinOp(N);
1166 if (FoldedVOp.Val) return FoldedVOp;
1167 }
Dan Gohman7f321562007-06-25 16:23:39 +00001168
Dan Gohman613e0d82007-07-03 14:03:57 +00001169 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001170 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001171 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001172 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001173 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001174 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001175 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001176 if (N0C && !N1C)
1177 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001178 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001179 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001180 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001181 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001182 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001183 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001184 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001185 if (N1C && N1C->getAPIntValue().isPowerOf2())
Chris Lattner3e6099b2005-10-30 06:41:49 +00001186 return DAG.getNode(ISD::SHL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001187 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001188 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001189 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
1190 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
1191 // FIXME: If the input is something that is easily negated (e.g. a
1192 // single-use add), we should put the negate there.
1193 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1194 DAG.getNode(ISD::SHL, VT, N0,
1195 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
1196 TLI.getShiftAmountTy())));
1197 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001198
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001199 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1200 if (N1C && N0.getOpcode() == ISD::SHL &&
1201 isa<ConstantSDNode>(N0.getOperand(1))) {
1202 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +00001203 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001204 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1205 }
1206
1207 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1208 // use.
1209 {
1210 SDOperand Sh(0,0), Y(0,0);
1211 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1212 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
1213 N0.Val->hasOneUse()) {
1214 Sh = N0; Y = N1;
1215 } else if (N1.getOpcode() == ISD::SHL &&
1216 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
1217 Sh = N1; Y = N0;
1218 }
1219 if (Sh.Val) {
1220 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
1221 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1222 }
1223 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001224 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
1225 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1226 isa<ConstantSDNode>(N0.getOperand(1))) {
1227 return DAG.getNode(ISD::ADD, VT,
1228 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1229 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1230 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001231
Nate Begemancd4d58c2006-02-03 06:46:56 +00001232 // reassociate mul
1233 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
1234 if (RMUL.Val != 0)
1235 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001236
Nate Begeman83e75ec2005-09-06 04:43:02 +00001237 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001238}
1239
Nate Begeman83e75ec2005-09-06 04:43:02 +00001240SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001241 SDOperand N0 = N->getOperand(0);
1242 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001243 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1244 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001245 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001246
Dan Gohman7f321562007-06-25 16:23:39 +00001247 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001248 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001249 SDOperand FoldedVOp = SimplifyVBinOp(N);
1250 if (FoldedVOp.Val) return FoldedVOp;
1251 }
Dan Gohman7f321562007-06-25 16:23:39 +00001252
Nate Begeman1d4d4142005-09-01 00:19:25 +00001253 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001254 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001255 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001256 // fold (sdiv X, 1) -> X
1257 if (N1C && N1C->getSignExtended() == 1LL)
1258 return N0;
1259 // fold (sdiv X, -1) -> 0-X
1260 if (N1C && N1C->isAllOnesValue())
1261 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001262 // If we know the sign bits of both operands are zero, strength reduce to a
1263 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001264 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001265 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattnerf32aac32008-01-27 23:32:17 +00001266 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
1267 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001268 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman002e5d02008-03-13 22:13:53 +00001269 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001270 (isPowerOf2_64(N1C->getSignExtended()) ||
1271 isPowerOf2_64(-N1C->getSignExtended()))) {
1272 // If dividing by powers of two is cheap, then don't perform the following
1273 // fold.
1274 if (TLI.isPow2DivCheap())
1275 return SDOperand();
1276 int64_t pow2 = N1C->getSignExtended();
1277 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001278 unsigned lg2 = Log2_64(abs2);
1279 // Splat the sign bit into the register
1280 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001281 DAG.getConstant(VT.getSizeInBits()-1,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001282 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00001283 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +00001284 // Add (N0 < 0) ? abs2 - 1 : 0;
1285 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001286 DAG.getConstant(VT.getSizeInBits()-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001287 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +00001288 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +00001289 AddToWorkList(SRL.Val);
1290 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +00001291 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
1292 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001293 // If we're dividing by a positive value, we're done. Otherwise, we must
1294 // negate the result.
1295 if (pow2 > 0)
1296 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +00001297 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001298 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1299 }
Nate Begeman69575232005-10-20 02:15:44 +00001300 // if integer divide is expensive and we satisfy the requirements, emit an
1301 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001302 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001303 !TLI.isIntDivCheap()) {
1304 SDOperand Op = BuildSDIV(N);
1305 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001306 }
Dan Gohman7f321562007-06-25 16:23:39 +00001307
Dan Gohman613e0d82007-07-03 14:03:57 +00001308 // undef / X -> 0
1309 if (N0.getOpcode() == ISD::UNDEF)
1310 return DAG.getConstant(0, VT);
1311 // X / undef -> undef
1312 if (N1.getOpcode() == ISD::UNDEF)
1313 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001314
Nate Begeman83e75ec2005-09-06 04:43:02 +00001315 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001316}
1317
Nate Begeman83e75ec2005-09-06 04:43:02 +00001318SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001319 SDOperand N0 = N->getOperand(0);
1320 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001321 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1322 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001323 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001324
Dan Gohman7f321562007-06-25 16:23:39 +00001325 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001326 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001327 SDOperand FoldedVOp = SimplifyVBinOp(N);
1328 if (FoldedVOp.Val) return FoldedVOp;
1329 }
Dan Gohman7f321562007-06-25 16:23:39 +00001330
Nate Begeman1d4d4142005-09-01 00:19:25 +00001331 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001332 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001333 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001334 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001335 if (N1C && N1C->getAPIntValue().isPowerOf2())
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001336 return DAG.getNode(ISD::SRL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001337 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001338 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001339 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1340 if (N1.getOpcode() == ISD::SHL) {
1341 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001342 if (SHC->getAPIntValue().isPowerOf2()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001343 MVT ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +00001344 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001345 DAG.getConstant(SHC->getAPIntValue()
1346 .logBase2(),
Nate Begemanc031e332006-02-05 07:36:48 +00001347 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001348 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001349 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001350 }
1351 }
1352 }
Nate Begeman69575232005-10-20 02:15:44 +00001353 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001354 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Chris Lattnere9936d12005-10-22 18:50:15 +00001355 SDOperand Op = BuildUDIV(N);
1356 if (Op.Val) return Op;
1357 }
Dan Gohman7f321562007-06-25 16:23:39 +00001358
Dan Gohman613e0d82007-07-03 14:03:57 +00001359 // undef / X -> 0
1360 if (N0.getOpcode() == ISD::UNDEF)
1361 return DAG.getConstant(0, VT);
1362 // X / undef -> undef
1363 if (N1.getOpcode() == ISD::UNDEF)
1364 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001365
Nate Begeman83e75ec2005-09-06 04:43:02 +00001366 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001367}
1368
Nate Begeman83e75ec2005-09-06 04:43:02 +00001369SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001370 SDOperand N0 = N->getOperand(0);
1371 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001372 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1373 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001374 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001375
1376 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001377 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001378 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001379 // If we know the sign bits of both operands are zero, strength reduce to a
1380 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00001381 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001382 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattneree339f42008-01-27 23:21:58 +00001383 return DAG.getNode(ISD::UREM, VT, N0, N1);
1384 }
Chris Lattner26d29902006-10-12 20:58:32 +00001385
Dan Gohman77003042007-11-26 23:46:11 +00001386 // If X/C can be simplified by the division-by-constant logic, lower
1387 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001388 if (N1C && !N1C->isNullValue()) {
1389 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
Chris Lattner5eee4272008-01-26 01:09:19 +00001390 AddToWorkList(Div.Val);
Dan Gohman77003042007-11-26 23:46:11 +00001391 SDOperand OptimizedDiv = combine(Div.Val);
1392 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1393 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1394 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1395 AddToWorkList(Mul.Val);
1396 return Sub;
1397 }
Chris Lattner26d29902006-10-12 20:58:32 +00001398 }
1399
Dan Gohman613e0d82007-07-03 14:03:57 +00001400 // undef % X -> 0
1401 if (N0.getOpcode() == ISD::UNDEF)
1402 return DAG.getConstant(0, VT);
1403 // X % undef -> undef
1404 if (N1.getOpcode() == ISD::UNDEF)
1405 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001406
Nate Begeman83e75ec2005-09-06 04:43:02 +00001407 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001408}
1409
Nate Begeman83e75ec2005-09-06 04:43:02 +00001410SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001411 SDOperand N0 = N->getOperand(0);
1412 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001413 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1414 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001415 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001416
1417 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001418 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001419 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001420 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001421 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
1422 return DAG.getNode(ISD::AND, VT, N0,
1423 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001424 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1425 if (N1.getOpcode() == ISD::SHL) {
1426 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001427 if (SHC->getAPIntValue().isPowerOf2()) {
1428 SDOperand Add =
1429 DAG.getNode(ISD::ADD, VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001430 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00001431 VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001432 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001433 return DAG.getNode(ISD::AND, VT, N0, Add);
1434 }
1435 }
1436 }
Chris Lattner26d29902006-10-12 20:58:32 +00001437
Dan Gohman77003042007-11-26 23:46:11 +00001438 // If X/C can be simplified by the division-by-constant logic, lower
1439 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001440 if (N1C && !N1C->isNullValue()) {
1441 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
Dan Gohman77003042007-11-26 23:46:11 +00001442 SDOperand OptimizedDiv = combine(Div.Val);
1443 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1444 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1445 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1446 AddToWorkList(Mul.Val);
1447 return Sub;
1448 }
Chris Lattner26d29902006-10-12 20:58:32 +00001449 }
1450
Dan Gohman613e0d82007-07-03 14:03:57 +00001451 // undef % X -> 0
1452 if (N0.getOpcode() == ISD::UNDEF)
1453 return DAG.getConstant(0, VT);
1454 // X % undef -> undef
1455 if (N1.getOpcode() == ISD::UNDEF)
1456 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001457
Nate Begeman83e75ec2005-09-06 04:43:02 +00001458 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001459}
1460
Nate Begeman83e75ec2005-09-06 04:43:02 +00001461SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001462 SDOperand N0 = N->getOperand(0);
1463 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001464 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001465 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001466
1467 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001468 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001469 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001470 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001471 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001472 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001473 DAG.getConstant(N0.getValueType().getSizeInBits()-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001474 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001475 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001476 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001477 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001478
Nate Begeman83e75ec2005-09-06 04:43:02 +00001479 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001480}
1481
Nate Begeman83e75ec2005-09-06 04:43:02 +00001482SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001483 SDOperand N0 = N->getOperand(0);
1484 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001485 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001486 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001487
1488 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001489 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001490 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001491 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00001492 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001493 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001494 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001495 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001496 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001497
Nate Begeman83e75ec2005-09-06 04:43:02 +00001498 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001499}
1500
Dan Gohman389079b2007-10-08 17:57:15 +00001501/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1502/// compute two values. LoOp and HiOp give the opcodes for the two computations
1503/// that are being performed. Return true if a simplification was made.
1504///
Chris Lattner5eee4272008-01-26 01:09:19 +00001505SDOperand DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1506 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001507 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001508 bool HiExists = N->hasAnyUseOfValue(1);
1509 if (!HiExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001510 (!AfterLegalize ||
1511 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001512 SDOperand Res = DAG.getNode(LoOp, N->getValueType(0), N->op_begin(),
1513 N->getNumOperands());
1514 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001515 }
1516
1517 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001518 bool LoExists = N->hasAnyUseOfValue(0);
1519 if (!LoExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001520 (!AfterLegalize ||
1521 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001522 SDOperand Res = DAG.getNode(HiOp, N->getValueType(1), N->op_begin(),
1523 N->getNumOperands());
1524 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001525 }
1526
Evan Cheng44711942007-11-08 09:25:29 +00001527 // If both halves are used, return as it is.
1528 if (LoExists && HiExists)
Chris Lattner5eee4272008-01-26 01:09:19 +00001529 return SDOperand();
Evan Cheng44711942007-11-08 09:25:29 +00001530
1531 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001532 if (LoExists) {
1533 SDOperand Lo = DAG.getNode(LoOp, N->getValueType(0),
1534 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001535 AddToWorkList(Lo.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001536 SDOperand LoOpt = combine(Lo.Val);
Chris Lattner5eee4272008-01-26 01:09:19 +00001537 if (LoOpt.Val && LoOpt.Val != Lo.Val &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001538 (!AfterLegalize ||
1539 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001540 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001541 }
1542
Evan Cheng44711942007-11-08 09:25:29 +00001543 if (HiExists) {
1544 SDOperand Hi = DAG.getNode(HiOp, N->getValueType(1),
1545 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001546 AddToWorkList(Hi.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001547 SDOperand HiOpt = combine(Hi.Val);
1548 if (HiOpt.Val && HiOpt != Hi &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001549 (!AfterLegalize ||
1550 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001551 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001552 }
Chris Lattner5eee4272008-01-26 01:09:19 +00001553 return SDOperand();
Dan Gohman389079b2007-10-08 17:57:15 +00001554}
1555
1556SDOperand DAGCombiner::visitSMUL_LOHI(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001557 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
1558 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001559
1560 return SDOperand();
1561}
1562
1563SDOperand DAGCombiner::visitUMUL_LOHI(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001564 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
1565 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001566
1567 return SDOperand();
1568}
1569
1570SDOperand DAGCombiner::visitSDIVREM(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001571 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
1572 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001573
1574 return SDOperand();
1575}
1576
1577SDOperand DAGCombiner::visitUDIVREM(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001578 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
1579 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001580
1581 return SDOperand();
1582}
1583
Chris Lattner35e5c142006-05-05 05:51:50 +00001584/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1585/// two operands of the same opcode, try to simplify it.
1586SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1587 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001588 MVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00001589 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1590
Chris Lattner540121f2006-05-05 06:31:05 +00001591 // For each of OP in AND/OR/XOR:
1592 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1593 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1594 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001595 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001596 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001597 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001598 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1599 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1600 N0.getOperand(0).getValueType(),
1601 N0.getOperand(0), N1.getOperand(0));
1602 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001603 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001604 }
1605
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001606 // For each of OP in SHL/SRL/SRA/AND...
1607 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1608 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1609 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001610 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001611 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001612 N0.getOperand(1) == N1.getOperand(1)) {
1613 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1614 N0.getOperand(0).getValueType(),
1615 N0.getOperand(0), N1.getOperand(0));
1616 AddToWorkList(ORNode.Val);
1617 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1618 }
1619
1620 return SDOperand();
1621}
1622
Nate Begeman83e75ec2005-09-06 04:43:02 +00001623SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001624 SDOperand N0 = N->getOperand(0);
1625 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001626 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001627 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1628 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001629 MVT VT = N1.getValueType();
1630 unsigned BitWidth = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001631
Dan Gohman7f321562007-06-25 16:23:39 +00001632 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001633 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001634 SDOperand FoldedVOp = SimplifyVBinOp(N);
1635 if (FoldedVOp.Val) return FoldedVOp;
1636 }
Dan Gohman7f321562007-06-25 16:23:39 +00001637
Dan Gohman613e0d82007-07-03 14:03:57 +00001638 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001639 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001640 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001641 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001642 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001643 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001644 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001645 if (N0C && !N1C)
1646 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001647 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001648 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001649 return N0;
1650 // if (and x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001651 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0),
1652 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001653 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001654 // reassociate and
1655 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1656 if (RAND.Val != 0)
1657 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001658 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001659 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001660 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001661 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001662 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001663 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1664 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001665 SDOperand N0Op0 = N0.getOperand(0);
1666 APInt Mask = ~N1C->getAPIntValue();
1667 Mask.trunc(N0Op0.getValueSizeInBits());
1668 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001669 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001670 N0Op0);
Chris Lattner1ec05d12006-03-01 21:47:21 +00001671
1672 // Replace uses of the AND with uses of the Zero extend node.
1673 CombineTo(N, Zext);
1674
Chris Lattner3603cd62006-02-02 07:17:31 +00001675 // We actually want to replace all uses of the any_extend with the
1676 // zero_extend, to avoid duplicating things. This will later cause this
1677 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001678 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001679 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001680 }
1681 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001682 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1683 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1684 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1685 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1686
1687 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001688 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001689 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001690 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001691 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001692 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001693 return DAG.getSetCC(VT, ORNode, LR, Op1);
1694 }
1695 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1696 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1697 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001698 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001699 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1700 }
1701 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1702 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1703 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001704 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001705 return DAG.getSetCC(VT, ORNode, LR, Op1);
1706 }
1707 }
1708 // canonicalize equivalent to ll == rl
1709 if (LL == RR && LR == RL) {
1710 Op1 = ISD::getSetCCSwappedOperands(Op1);
1711 std::swap(RL, RR);
1712 }
1713 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001714 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001715 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1716 if (Result != ISD::SETCC_INVALID)
1717 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1718 }
1719 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001720
1721 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1722 if (N0.getOpcode() == N1.getOpcode()) {
1723 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1724 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001725 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001726
Nate Begemande996292006-02-03 22:24:05 +00001727 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1728 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001729 if (!VT.isVector() &&
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001730 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001731 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001732 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Cheng83060c52007-03-07 08:07:03 +00001733 if (ISD::isEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001734 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001735 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001736 // If we zero all the possible extended bits, then we can turn this into
1737 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001738 unsigned BitWidth = N1.getValueSizeInBits();
1739 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001740 BitWidth - EVT.getSizeInBits())) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001741 ((!AfterLegalize && !LN0->isVolatile()) ||
1742 TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001743 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1744 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001745 LN0->getSrcValueOffset(), EVT,
1746 LN0->isVolatile(),
1747 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001748 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001749 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001750 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001751 }
1752 }
1753 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00001754 if (ISD::isSEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
1755 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001756 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001757 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001758 // If we zero all the possible extended bits, then we can turn this into
1759 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001760 unsigned BitWidth = N1.getValueSizeInBits();
1761 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001762 BitWidth - EVT.getSizeInBits())) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001763 ((!AfterLegalize && !LN0->isVolatile()) ||
1764 TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001765 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1766 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001767 LN0->getSrcValueOffset(), EVT,
1768 LN0->isVolatile(),
1769 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001770 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001771 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001772 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001773 }
1774 }
Chris Lattner15045b62006-02-28 06:35:35 +00001775
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001776 // fold (and (load x), 255) -> (zextload x, i8)
1777 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001778 if (N1C && N0.getOpcode() == ISD::LOAD) {
1779 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1780 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001781 LN0->isUnindexed() && N0.hasOneUse() &&
1782 // Do not change the width of a volatile load.
1783 !LN0->isVolatile()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00001784 MVT EVT = MVT::Other;
1785 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
1786 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue()))
1787 EVT = MVT::getIntegerVT(ActiveBits);
1788
1789 MVT LoadedVT = LN0->getMemoryVT();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001790 // Do not generate loads of extended integer types since these can be
1791 // expensive (and would be wrong if the type is not byte sized).
1792 if (EVT != MVT::Other && LoadedVT.bitsGT(EVT) && EVT.isSimple() &&
1793 EVT.isByteSized() && // Exclude MVT::i1, which is simple.
Evan Cheng466685d2006-10-09 20:57:25 +00001794 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001795 MVT PtrType = N0.getOperand(1).getValueType();
Evan Cheng466685d2006-10-09 20:57:25 +00001796 // For big endian targets, we need to add an offset to the pointer to
1797 // load the correct bytes. For little endian systems, we merely need to
1798 // read fewer bytes from the same pointer.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001799 unsigned LVTStoreBytes = LoadedVT.getStoreSizeInBits()/8;
1800 unsigned EVTStoreBytes = EVT.getStoreSizeInBits()/8;
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001801 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001802 unsigned Alignment = LN0->getAlignment();
Evan Cheng466685d2006-10-09 20:57:25 +00001803 SDOperand NewPtr = LN0->getBasePtr();
Duncan Sands0753fc12008-02-11 10:37:04 +00001804 if (TLI.isBigEndian()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001805 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1806 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001807 Alignment = MinAlign(Alignment, PtrOff);
1808 }
Evan Cheng466685d2006-10-09 20:57:25 +00001809 AddToWorkList(NewPtr.Val);
1810 SDOperand Load =
1811 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001812 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00001813 LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001814 AddToWorkList(N);
1815 CombineTo(N0.Val, Load, Load.getValue(1));
1816 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1817 }
Chris Lattner15045b62006-02-28 06:35:35 +00001818 }
1819 }
1820
Nate Begeman83e75ec2005-09-06 04:43:02 +00001821 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001822}
1823
Nate Begeman83e75ec2005-09-06 04:43:02 +00001824SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001825 SDOperand N0 = N->getOperand(0);
1826 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001827 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001828 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1829 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001830 MVT VT = N1.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001831
Dan Gohman7f321562007-06-25 16:23:39 +00001832 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001833 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001834 SDOperand FoldedVOp = SimplifyVBinOp(N);
1835 if (FoldedVOp.Val) return FoldedVOp;
1836 }
Dan Gohman7f321562007-06-25 16:23:39 +00001837
Dan Gohman613e0d82007-07-03 14:03:57 +00001838 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001839 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001840 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001841 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001842 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001843 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001844 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001845 if (N0C && !N1C)
1846 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001847 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001848 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001849 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001850 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001851 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001852 return N1;
1853 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001854 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001855 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001856 // reassociate or
1857 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1858 if (ROR.Val != 0)
1859 return ROR;
1860 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1861 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001862 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001863 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1864 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1865 N1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001866 DAG.getConstant(N1C->getAPIntValue() |
1867 C1->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001868 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001869 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1870 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1871 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1872 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1873
1874 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001875 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001876 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1877 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001878 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001879 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1880 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001881 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001882 return DAG.getSetCC(VT, ORNode, LR, Op1);
1883 }
1884 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1885 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1886 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1887 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1888 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001889 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001890 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1891 }
1892 }
1893 // canonicalize equivalent to ll == rl
1894 if (LL == RR && LR == RL) {
1895 Op1 = ISD::getSetCCSwappedOperands(Op1);
1896 std::swap(RL, RR);
1897 }
1898 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001899 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001900 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1901 if (Result != ISD::SETCC_INVALID)
1902 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1903 }
1904 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001905
1906 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1907 if (N0.getOpcode() == N1.getOpcode()) {
1908 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1909 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001910 }
Chris Lattner516b9622006-09-14 20:50:57 +00001911
Chris Lattner1ec72732006-09-14 21:11:37 +00001912 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1913 if (N0.getOpcode() == ISD::AND &&
1914 N1.getOpcode() == ISD::AND &&
1915 N0.getOperand(1).getOpcode() == ISD::Constant &&
1916 N1.getOperand(1).getOpcode() == ISD::Constant &&
1917 // Don't increase # computations.
1918 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1919 // We can only do this xform if we know that bits from X that are set in C2
1920 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001921 const APInt &LHSMask =
1922 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1923 const APInt &RHSMask =
1924 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Chris Lattner1ec72732006-09-14 21:11:37 +00001925
Dan Gohmanea859be2007-06-22 14:59:07 +00001926 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1927 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Chris Lattner1ec72732006-09-14 21:11:37 +00001928 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1929 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1930 }
1931 }
1932
1933
Chris Lattner516b9622006-09-14 20:50:57 +00001934 // See if this is some rotate idiom.
1935 if (SDNode *Rot = MatchRotate(N0, N1))
1936 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001937
Nate Begeman83e75ec2005-09-06 04:43:02 +00001938 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001939}
1940
Chris Lattner516b9622006-09-14 20:50:57 +00001941
1942/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1943static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1944 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001945 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001946 Mask = Op.getOperand(1);
1947 Op = Op.getOperand(0);
1948 } else {
1949 return false;
1950 }
1951 }
1952
1953 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1954 Shift = Op;
1955 return true;
1956 }
1957 return false;
1958}
1959
1960
1961// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1962// idioms for rotate, and if the target supports rotation instructions, generate
1963// a rot[lr].
1964SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001965 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001966 MVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00001967 if (!TLI.isTypeLegal(VT)) return 0;
1968
1969 // The target must have at least one rotate flavor.
1970 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1971 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1972 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001973
Chris Lattner516b9622006-09-14 20:50:57 +00001974 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1975 SDOperand LHSShift; // The shift.
1976 SDOperand LHSMask; // AND value if any.
1977 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1978 return 0; // Not part of a rotate.
1979
1980 SDOperand RHSShift; // The shift.
1981 SDOperand RHSMask; // AND value if any.
1982 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1983 return 0; // Not part of a rotate.
1984
1985 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1986 return 0; // Not shifting the same value.
1987
1988 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1989 return 0; // Shifts must disagree.
1990
1991 // Canonicalize shl to left side in a shl/srl pair.
1992 if (RHSShift.getOpcode() == ISD::SHL) {
1993 std::swap(LHS, RHS);
1994 std::swap(LHSShift, RHSShift);
1995 std::swap(LHSMask , RHSMask );
1996 }
1997
Duncan Sands83ec4b62008-06-06 12:08:01 +00001998 unsigned OpSizeInBits = VT.getSizeInBits();
Scott Michelc9dc1142007-04-02 21:36:32 +00001999 SDOperand LHSShiftArg = LHSShift.getOperand(0);
2000 SDOperand LHSShiftAmt = LHSShift.getOperand(1);
2001 SDOperand RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00002002
2003 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
2004 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00002005 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
2006 RHSShiftAmt.getOpcode() == ISD::Constant) {
2007 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getValue();
2008 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getValue();
Chris Lattner516b9622006-09-14 20:50:57 +00002009 if ((LShVal + RShVal) != OpSizeInBits)
2010 return 0;
2011
2012 SDOperand Rot;
2013 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002014 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002015 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002016 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002017
2018 // If there is an AND of either shifted operand, apply it to the result.
2019 if (LHSMask.Val || RHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00002020 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Chris Lattner516b9622006-09-14 20:50:57 +00002021
2022 if (LHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00002023 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2024 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002025 }
2026 if (RHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00002027 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2028 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002029 }
2030
2031 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
2032 }
2033
2034 return Rot.Val;
2035 }
2036
2037 // If there is a mask here, and we have a variable shift, we can't be sure
2038 // that we're masking out the right stuff.
2039 if (LHSMask.Val || RHSMask.Val)
2040 return 0;
2041
2042 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2043 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002044 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2045 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002046 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002047 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002048 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002049 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002050 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00002051 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002052 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002053 }
Chris Lattner516b9622006-09-14 20:50:57 +00002054 }
2055 }
2056
2057 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2058 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002059 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2060 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002061 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002062 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002063 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002064 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002065 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00002066 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002067 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002068 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002069 }
2070 }
2071
2072 // Look for sign/zext/any-extended cases:
2073 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2074 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2075 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
2076 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2077 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2078 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
2079 SDOperand LExtOp0 = LHSShiftAmt.getOperand(0);
2080 SDOperand RExtOp0 = RHSShiftAmt.getOperand(0);
2081 if (RExtOp0.getOpcode() == ISD::SUB &&
2082 RExtOp0.getOperand(1) == LExtOp0) {
2083 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2084 // (rotr x, y)
2085 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2086 // (rotl x, (sub 32, y))
2087 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002088 if (SUBC->getAPIntValue() == OpSizeInBits) {
Scott Michelc9dc1142007-04-02 21:36:32 +00002089 if (HasROTL)
2090 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2091 else
2092 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
2093 }
2094 }
2095 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2096 RExtOp0 == LExtOp0.getOperand(1)) {
2097 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2098 // (rotl x, y)
2099 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2100 // (rotr x, (sub 32, y))
2101 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002102 if (SUBC->getAPIntValue() == OpSizeInBits) {
Scott Michelc9dc1142007-04-02 21:36:32 +00002103 if (HasROTL)
2104 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, RHSShiftAmt).Val;
2105 else
2106 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2107 }
2108 }
Chris Lattner516b9622006-09-14 20:50:57 +00002109 }
2110 }
2111
2112 return 0;
2113}
2114
2115
Nate Begeman83e75ec2005-09-06 04:43:02 +00002116SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002117 SDOperand N0 = N->getOperand(0);
2118 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002119 SDOperand LHS, RHS, CC;
2120 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2121 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002122 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002123
Dan Gohman7f321562007-06-25 16:23:39 +00002124 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002125 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00002126 SDOperand FoldedVOp = SimplifyVBinOp(N);
2127 if (FoldedVOp.Val) return FoldedVOp;
2128 }
Dan Gohman7f321562007-06-25 16:23:39 +00002129
Evan Cheng26471c42008-03-25 20:08:07 +00002130 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
2131 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
2132 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00002133 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002134 if (N0.getOpcode() == ISD::UNDEF)
2135 return N0;
2136 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002137 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002138 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002139 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002140 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00002141 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002142 if (N0C && !N1C)
2143 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002144 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002145 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002146 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002147 // reassociate xor
2148 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
2149 if (RXOR.Val != 0)
2150 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002151 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00002152 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002153 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00002154 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2155 isInt);
2156 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002157 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002158 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002159 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002160 assert(0 && "Unhandled SetCC Equivalent!");
2161 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002162 }
Chris Lattner61c5ff42007-09-10 21:39:07 +00002163 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002164 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Chris Lattner61c5ff42007-09-10 21:39:07 +00002165 N0.Val->hasOneUse() && isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
2166 SDOperand V = N0.getOperand(0);
2167 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002168 DAG.getConstant(1, V.getValueType()));
Chris Lattner61c5ff42007-09-10 21:39:07 +00002169 AddToWorkList(V.Val);
2170 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2171 }
2172
Nate Begeman99801192005-09-07 23:25:52 +00002173 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Dan Gohman002e5d02008-03-13 22:13:53 +00002174 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002175 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002176 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002177 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2178 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002179 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2180 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002181 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002182 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002183 }
2184 }
Nate Begeman99801192005-09-07 23:25:52 +00002185 // fold !(x or y) -> (!x and !y) iff x or y are constants
2186 if (N1C && N1C->isAllOnesValue() &&
2187 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002188 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002189 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2190 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002191 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2192 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002193 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002194 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002195 }
2196 }
Nate Begeman223df222005-09-08 20:18:10 +00002197 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2198 if (N1C && N0.getOpcode() == ISD::XOR) {
2199 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2200 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2201 if (N00C)
2202 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00002203 DAG.getConstant(N1C->getAPIntValue()^
2204 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002205 if (N01C)
2206 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
Dan Gohman002e5d02008-03-13 22:13:53 +00002207 DAG.getConstant(N1C->getAPIntValue()^
2208 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002209 }
2210 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002211 if (N0 == N1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002212 if (!VT.isVector()) {
Chris Lattner4fbdd592006-03-28 19:11:05 +00002213 return DAG.getConstant(0, VT);
2214 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
2215 // Produce a vector of zeros.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002216 SDOperand El = DAG.getConstant(0, VT.getVectorElementType());
2217 std::vector<SDOperand> Ops(VT.getVectorNumElements(), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002218 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002219 }
2220 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002221
2222 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2223 if (N0.getOpcode() == N1.getOpcode()) {
2224 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
2225 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002226 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002227
Chris Lattner3e104b12006-04-08 04:15:24 +00002228 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002229 if (!VT.isVector() &&
Chris Lattner3e104b12006-04-08 04:15:24 +00002230 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002231 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002232
Nate Begeman83e75ec2005-09-06 04:43:02 +00002233 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002234}
2235
Chris Lattnere70da202007-12-06 07:33:36 +00002236/// visitShiftByConstant - Handle transforms common to the three shifts, when
2237/// the shift amount is a constant.
2238SDOperand DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
2239 SDNode *LHS = N->getOperand(0).Val;
2240 if (!LHS->hasOneUse()) return SDOperand();
2241
2242 // We want to pull some binops through shifts, so that we have (and (shift))
2243 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2244 // thing happens with address calculations, so it's important to canonicalize
2245 // it.
2246 bool HighBitSet = false; // Can we transform this if the high bit is set?
2247
2248 switch (LHS->getOpcode()) {
2249 default: return SDOperand();
2250 case ISD::OR:
2251 case ISD::XOR:
2252 HighBitSet = false; // We can only transform sra if the high bit is clear.
2253 break;
2254 case ISD::AND:
2255 HighBitSet = true; // We can only transform sra if the high bit is set.
2256 break;
2257 case ISD::ADD:
2258 if (N->getOpcode() != ISD::SHL)
2259 return SDOperand(); // only shl(add) not sr[al](add).
2260 HighBitSet = false; // We can only transform sra if the high bit is clear.
2261 break;
2262 }
2263
2264 // We require the RHS of the binop to be a constant as well.
2265 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
2266 if (!BinOpCst) return SDOperand();
2267
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002268
2269 // FIXME: disable this for unless the input to the binop is a shift by a
2270 // constant. If it is not a shift, it pessimizes some common cases like:
2271 //
2272 //void foo(int *X, int i) { X[i & 1235] = 1; }
2273 //int bar(int *X, int i) { return X[i & 255]; }
2274 SDNode *BinOpLHSVal = LHS->getOperand(0).Val;
2275 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2276 BinOpLHSVal->getOpcode() != ISD::SRA &&
2277 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2278 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
2279 return SDOperand();
2280
Duncan Sands83ec4b62008-06-06 12:08:01 +00002281 MVT VT = N->getValueType(0);
Chris Lattnere70da202007-12-06 07:33:36 +00002282
2283 // If this is a signed shift right, and the high bit is modified
2284 // by the logical operation, do not perform the transformation.
2285 // The highBitSet boolean indicates the value of the high bit of
2286 // the constant which would cause it to be modified for this
2287 // operation.
2288 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00002289 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2290 if (BinOpRHSSignSet != HighBitSet)
Chris Lattnere70da202007-12-06 07:33:36 +00002291 return SDOperand();
2292 }
2293
2294 // Fold the constants, shifting the binop RHS by the shift amount.
2295 SDOperand NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
2296 LHS->getOperand(1), N->getOperand(1));
2297
2298 // Create the new shift.
2299 SDOperand NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
2300 N->getOperand(1));
2301
2302 // Create the new binop.
2303 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2304}
2305
2306
Nate Begeman83e75ec2005-09-06 04:43:02 +00002307SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002308 SDOperand N0 = N->getOperand(0);
2309 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002310 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2311 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002312 MVT VT = N0.getValueType();
2313 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002314
2315 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002316 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002317 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002318 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002319 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002320 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002321 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002322 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002323 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002324 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002325 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002326 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002327 // if (shl x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002328 if (DAG.MaskedValueIsZero(SDOperand(N, 0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002329 APInt::getAllOnesValue(VT.getSizeInBits())))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002330 return DAG.getConstant(0, VT);
Chris Lattner61a4c072007-04-18 03:06:49 +00002331 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002332 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002333 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002334 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002335 N0.getOperand(1).getOpcode() == ISD::Constant) {
2336 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002337 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002338 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002339 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002340 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002341 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002342 }
2343 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2344 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002345 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002346 N0.getOperand(1).getOpcode() == ISD::Constant) {
2347 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002348 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002349 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
2350 DAG.getConstant(~0ULL << c1, VT));
2351 if (c2 > c1)
2352 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002353 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002354 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002355 return DAG.getNode(ISD::SRL, VT, Mask,
2356 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002357 }
2358 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002359 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002360 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002361 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnere70da202007-12-06 07:33:36 +00002362
2363 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002364}
2365
Nate Begeman83e75ec2005-09-06 04:43:02 +00002366SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002367 SDOperand N0 = N->getOperand(0);
2368 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002369 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2370 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002371 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002372
2373 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002374 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002375 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002376 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002377 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002378 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002379 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002380 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002381 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002382 // fold (sra x, c >= size(x)) -> undef
Duncan Sands83ec4b62008-06-06 12:08:01 +00002383 if (N1C && N1C->getValue() >= VT.getSizeInBits())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002384 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002385 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002386 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002387 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002388 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2389 // sext_inreg.
2390 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002391 unsigned LowBits = VT.getSizeInBits() - (unsigned)N1C->getValue();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002392 MVT EVT = MVT::getIntegerVT(LowBits);
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002393 if (EVT.isSimple() && // TODO: remove when apint codegen support lands.
2394 (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT)))
Nate Begemanfb7217b2006-02-17 19:54:08 +00002395 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2396 DAG.getValueType(EVT));
2397 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002398
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002399 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2400 if (N1C && N0.getOpcode() == ISD::SRA) {
2401 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2402 unsigned Sum = N1C->getValue() + C1->getValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002403 if (Sum >= VT.getSizeInBits()) Sum = VT.getSizeInBits()-1;
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002404 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2405 DAG.getConstant(Sum, N1C->getValueType(0)));
2406 }
2407 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00002408
2409 // fold sra (shl X, m), result_size - n
2410 // -> (sign_extend (trunc (shl X, result_size - n - m))) for
Christopher Lambb9b04282008-03-20 04:31:39 +00002411 // result_size - n != m.
2412 // If truncate is free for the target sext(shl) is likely to result in better
2413 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002414 if (N0.getOpcode() == ISD::SHL) {
2415 // Get the two constanst of the shifts, CN0 = m, CN = n.
2416 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2417 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002418 // Determine what the truncate's result bitsize and type would be.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002419 unsigned VTValSize = VT.getSizeInBits();
2420 MVT TruncVT =
2421 MVT::getIntegerVT(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();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002424
Christopher Lambb9b04282008-03-20 04:31:39 +00002425 // 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 &&
Christopher Lambb9b04282008-03-20 04:31:39 +00002430 TLI.isOperationLegal(ISD::SIGN_EXTEND, TruncVT) &&
2431 TLI.isOperationLegal(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00002432 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002433
2434 SDOperand Amt = DAG.getConstant(ShiftAmt, TLI.getShiftAmountTy());
2435 SDOperand Shift = DAG.getNode(ISD::SRL, VT, N0.getOperand(0), Amt);
2436 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, TruncVT, Shift);
2437 return DAG.getNode(ISD::SIGN_EXTEND, N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00002438 }
2439 }
2440 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002441
Chris Lattnera8504462006-05-08 20:51:54 +00002442 // Simplify, based on bits shifted out of the LHS.
2443 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2444 return SDOperand(N, 0);
2445
2446
Nate Begeman1d4d4142005-09-01 00:19:25 +00002447 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002448 if (DAG.SignBitIsZero(N0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002449 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002450
2451 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002452}
2453
Nate Begeman83e75ec2005-09-06 04:43:02 +00002454SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002455 SDOperand N0 = N->getOperand(0);
2456 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002457 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2458 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002459 MVT VT = N0.getValueType();
2460 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002461
2462 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002463 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002464 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002465 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002466 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002467 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002468 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002469 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002470 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002471 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002472 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002473 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002474 // if (srl x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002475 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0),
2476 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002477 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002478
Nate Begeman1d4d4142005-09-01 00:19:25 +00002479 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002480 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002481 N0.getOperand(1).getOpcode() == ISD::Constant) {
2482 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002483 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002484 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002485 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002486 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002487 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002488 }
Chris Lattner350bec02006-04-02 06:11:11 +00002489
Chris Lattner06afe072006-05-05 22:53:17 +00002490 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2491 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2492 // Shifting in all undef bits?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002493 MVT SmallVT = N0.getOperand(0).getValueType();
2494 if (N1C->getValue() >= SmallVT.getSizeInBits())
Chris Lattner06afe072006-05-05 22:53:17 +00002495 return DAG.getNode(ISD::UNDEF, VT);
2496
2497 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
2498 AddToWorkList(SmallShift.Val);
2499 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2500 }
2501
Chris Lattner3657ffe2006-10-12 20:23:19 +00002502 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2503 // bit, which is unmodified by sra.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002504 if (N1C && N1C->getValue()+1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00002505 if (N0.getOpcode() == ISD::SRA)
2506 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2507 }
2508
Chris Lattner350bec02006-04-02 06:11:11 +00002509 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2510 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002511 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00002512 APInt KnownZero, KnownOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002513 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00002514 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002515
2516 // If any of the input bits are KnownOne, then the input couldn't be all
2517 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002518 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Chris Lattner350bec02006-04-02 06:11:11 +00002519
2520 // If all of the bits input the to ctlz node are known to be zero, then
2521 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002522 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00002523 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2524
2525 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2526 if ((UnknownBits & (UnknownBits-1)) == 0) {
2527 // Okay, we know that only that the single bit specified by UnknownBits
2528 // could be set on input to the CTLZ node. If this bit is set, the SRL
2529 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2530 // to an SRL,XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002531 unsigned ShAmt = UnknownBits.countTrailingZeros();
Chris Lattner350bec02006-04-02 06:11:11 +00002532 SDOperand Op = N0.getOperand(0);
2533 if (ShAmt) {
2534 Op = DAG.getNode(ISD::SRL, VT, Op,
2535 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
2536 AddToWorkList(Op.Val);
2537 }
2538 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2539 }
2540 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002541
2542 // fold operands of srl based on knowledge that the low bits are not
2543 // demanded.
2544 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2545 return SDOperand(N, 0);
2546
Chris Lattnere70da202007-12-06 07:33:36 +00002547 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002548}
2549
Nate Begeman83e75ec2005-09-06 04:43:02 +00002550SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002551 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002552 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002553
2554 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002555 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002556 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002557 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002558}
2559
Nate Begeman83e75ec2005-09-06 04:43:02 +00002560SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002561 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002562 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002563
2564 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002565 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002566 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002567 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002568}
2569
Nate Begeman83e75ec2005-09-06 04:43:02 +00002570SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002571 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002572 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002573
2574 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002575 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002576 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002577 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002578}
2579
Nate Begeman452d7be2005-09-16 00:54:12 +00002580SDOperand DAGCombiner::visitSELECT(SDNode *N) {
2581 SDOperand N0 = N->getOperand(0);
2582 SDOperand N1 = N->getOperand(1);
2583 SDOperand N2 = N->getOperand(2);
2584 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2585 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2586 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002587 MVT VT = N->getValueType(0);
2588 MVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002589
Nate Begeman452d7be2005-09-16 00:54:12 +00002590 // fold select C, X, X -> X
2591 if (N1 == N2)
2592 return N1;
2593 // fold select true, X, Y -> X
2594 if (N0C && !N0C->isNullValue())
2595 return N1;
2596 // fold select false, X, Y -> Y
2597 if (N0C && N0C->isNullValue())
2598 return N2;
2599 // fold select C, 1, X -> C | X
Duncan Sands83ec4b62008-06-06 12:08:01 +00002600 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002601 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Cheng571c4782007-08-18 05:57:05 +00002602 // fold select C, 0, 1 -> ~C
Duncan Sands83ec4b62008-06-06 12:08:01 +00002603 if (VT.isInteger() && VT0.isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00002604 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Evan Cheng571c4782007-08-18 05:57:05 +00002605 SDOperand XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
2606 if (VT == VT0)
2607 return XORNode;
2608 AddToWorkList(XORNode.Val);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002609 if (VT.bitsGT(VT0))
Evan Cheng571c4782007-08-18 05:57:05 +00002610 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2611 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2612 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002613 // fold select C, 0, X -> ~C & X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002614 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
2615 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002616 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002617 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2618 }
2619 // fold select C, X, 1 -> ~C | X
Dan Gohman002e5d02008-03-13 22:13:53 +00002620 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002621 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002622 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002623 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2624 }
2625 // fold select C, X, 0 -> C & X
2626 // FIXME: this should check for C type == X type, not i1?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002627 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Nate Begeman452d7be2005-09-16 00:54:12 +00002628 return DAG.getNode(ISD::AND, VT, N0, N1);
2629 // fold X ? X : Y --> X ? 1 : Y --> X | Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002630 if (VT == MVT::i1 && N0 == N1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002631 return DAG.getNode(ISD::OR, VT, N0, N2);
2632 // fold X ? Y : X --> X ? Y : 0 --> X & Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002633 if (VT == MVT::i1 && N0 == N2)
Nate Begeman452d7be2005-09-16 00:54:12 +00002634 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002635
Chris Lattner40c62d52005-10-18 06:04:22 +00002636 // If we can fold this based on the true/false value, do so.
2637 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00002638 return SDOperand(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002639
Nate Begeman44728a72005-09-19 22:34:01 +00002640 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002641 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002642 // FIXME:
2643 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2644 // having to say they don't support SELECT_CC on every type the DAG knows
2645 // about, since there is no way to mark an opcode illegal at all value types
2646 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2647 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2648 N1, N2, N0.getOperand(2));
2649 else
2650 return SimplifySelect(N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002651 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002652 return SDOperand();
2653}
2654
2655SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00002656 SDOperand N0 = N->getOperand(0);
2657 SDOperand N1 = N->getOperand(1);
2658 SDOperand N2 = N->getOperand(2);
2659 SDOperand N3 = N->getOperand(3);
2660 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002661 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2662
Nate Begeman44728a72005-09-19 22:34:01 +00002663 // fold select_cc lhs, rhs, x, x, cc -> x
2664 if (N2 == N3)
2665 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002666
Chris Lattner5f42a242006-09-20 06:19:26 +00002667 // Determine if the condition we're dealing with is constant
Scott Michel5b8f82e2008-03-10 15:42:14 +00002668 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002669 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002670
2671 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002672 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00002673 return N2; // cond always true -> true val
2674 else
2675 return N3; // cond always false -> false val
2676 }
2677
2678 // Fold to a simpler select_cc
2679 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2680 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2681 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2682 SCC.getOperand(2));
2683
Chris Lattner40c62d52005-10-18 06:04:22 +00002684 // If we can fold this based on the true/false value, do so.
2685 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00002686 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002687
Nate Begeman44728a72005-09-19 22:34:01 +00002688 // fold select_cc into other things, such as min/max/abs
2689 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002690}
2691
2692SDOperand DAGCombiner::visitSETCC(SDNode *N) {
2693 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2694 cast<CondCodeSDNode>(N->getOperand(2))->get());
2695}
2696
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002697// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2698// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2699// transformation. Returns true if extension are possible and the above
2700// mentioned transformation is profitable.
2701static bool ExtendUsesToFormExtLoad(SDNode *N, SDOperand N0,
2702 unsigned ExtOpc,
2703 SmallVector<SDNode*, 4> &ExtendNodes,
2704 TargetLowering &TLI) {
2705 bool HasCopyToRegUses = false;
2706 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
2707 for (SDNode::use_iterator UI = N0.Val->use_begin(), UE = N0.Val->use_end();
2708 UI != UE; ++UI) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00002709 SDNode *User = UI->getUser();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002710 if (User == N)
2711 continue;
2712 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2713 if (User->getOpcode() == ISD::SETCC) {
2714 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2715 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2716 // Sign bits will be lost after a zext.
2717 return false;
2718 bool Add = false;
2719 for (unsigned i = 0; i != 2; ++i) {
2720 SDOperand UseOp = User->getOperand(i);
2721 if (UseOp == N0)
2722 continue;
2723 if (!isa<ConstantSDNode>(UseOp))
2724 return false;
2725 Add = true;
2726 }
2727 if (Add)
2728 ExtendNodes.push_back(User);
2729 } else {
2730 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2731 SDOperand UseOp = User->getOperand(i);
2732 if (UseOp == N0) {
2733 // If truncate from extended type to original load type is free
2734 // on this target, then it's ok to extend a CopyToReg.
2735 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2736 HasCopyToRegUses = true;
2737 else
2738 return false;
2739 }
2740 }
2741 }
2742 }
2743
2744 if (HasCopyToRegUses) {
2745 bool BothLiveOut = false;
2746 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2747 UI != UE; ++UI) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00002748 SDNode *User = UI->getUser();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002749 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2750 SDOperand UseOp = User->getOperand(i);
2751 if (UseOp.Val == N && UseOp.ResNo == 0) {
2752 BothLiveOut = true;
2753 break;
2754 }
2755 }
2756 }
2757 if (BothLiveOut)
2758 // Both unextended and extended values are live out. There had better be
2759 // good a reason for the transformation.
2760 return ExtendNodes.size();
2761 }
2762 return true;
2763}
2764
Nate Begeman83e75ec2005-09-06 04:43:02 +00002765SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002766 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002767 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002768
Nate Begeman1d4d4142005-09-01 00:19:25 +00002769 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002770 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002771 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002772
Nate Begeman1d4d4142005-09-01 00:19:25 +00002773 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002774 // fold (sext (aext x)) -> (sext x)
2775 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002776 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002777
Chris Lattner22558872007-02-26 03:13:59 +00002778 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002779 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2780 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Evan Chengc88138f2007-03-22 01:54:19 +00002781 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002782 if (NarrowLoad.Val) {
2783 if (NarrowLoad.Val != N0.Val)
2784 CombineTo(N0.Val, NarrowLoad);
2785 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2786 }
Evan Chengc88138f2007-03-22 01:54:19 +00002787
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002788 // See if the value being truncated is already sign extended. If so, just
2789 // eliminate the trunc/sext pair.
Chris Lattner6007b842006-09-21 06:00:20 +00002790 SDOperand Op = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002791 unsigned OpBits = Op.getValueType().getSizeInBits();
2792 unsigned MidBits = N0.getValueType().getSizeInBits();
2793 unsigned DestBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002794 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002795
2796 if (OpBits == DestBits) {
2797 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2798 // bits, it is already ready.
2799 if (NumSignBits > DestBits-MidBits)
2800 return Op;
2801 } else if (OpBits < DestBits) {
2802 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2803 // bits, just sext from i32.
2804 if (NumSignBits > OpBits-MidBits)
2805 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2806 } else {
2807 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2808 // bits, just truncate to i32.
2809 if (NumSignBits > OpBits-MidBits)
2810 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002811 }
Chris Lattner22558872007-02-26 03:13:59 +00002812
2813 // fold (sext (truncate x)) -> (sextinreg x).
2814 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2815 N0.getValueType())) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00002816 if (Op.getValueType().bitsLT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002817 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002818 else if (Op.getValueType().bitsGT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002819 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2820 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2821 DAG.getValueType(N0.getValueType()));
2822 }
Chris Lattner6007b842006-09-21 06:00:20 +00002823 }
Chris Lattner310b5782006-05-06 23:06:26 +00002824
Evan Cheng110dec22005-12-14 02:19:23 +00002825 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002826 if (ISD::isNON_EXTLoad(N0.Val) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002827 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
2828 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);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002868 MVT EVT = LN0->getMemoryVT();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002869 if ((!AfterLegalize && !LN0->isVolatile()) ||
2870 TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002871 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2872 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002873 LN0->getSrcValueOffset(), EVT,
2874 LN0->isVolatile(),
2875 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002876 CombineTo(N, ExtLoad);
2877 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2878 ExtLoad.getValue(1));
2879 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2880 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002881 }
2882
Chris Lattner20a35c32007-04-11 05:32:27 +00002883 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2884 if (N0.getOpcode() == ISD::SETCC) {
2885 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002886 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2887 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2888 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2889 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002890 }
2891
Dan Gohman8f0ad582008-04-28 16:58:24 +00002892 // fold (sext x) -> (zext x) if the sign bit is known zero.
Dan Gohman187db7b2008-04-28 18:47:17 +00002893 if ((!AfterLegalize || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
2894 DAG.SignBitIsZero(N0))
Dan Gohman8f0ad582008-04-28 16:58:24 +00002895 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2896
Nate Begeman83e75ec2005-09-06 04:43:02 +00002897 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002898}
2899
Nate Begeman83e75ec2005-09-06 04:43:02 +00002900SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002901 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002902 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002903
Nate Begeman1d4d4142005-09-01 00:19:25 +00002904 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002905 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002906 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002907 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002908 // fold (zext (aext x)) -> (zext x)
2909 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002910 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002911
Evan Chengc88138f2007-03-22 01:54:19 +00002912 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2913 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002914 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002915 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002916 if (NarrowLoad.Val) {
2917 if (NarrowLoad.Val != N0.Val)
2918 CombineTo(N0.Val, NarrowLoad);
2919 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2920 }
Evan Chengc88138f2007-03-22 01:54:19 +00002921 }
2922
Chris Lattner6007b842006-09-21 06:00:20 +00002923 // fold (zext (truncate x)) -> (and x, mask)
2924 if (N0.getOpcode() == ISD::TRUNCATE &&
2925 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2926 SDOperand Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002927 if (Op.getValueType().bitsLT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00002928 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002929 } else if (Op.getValueType().bitsGT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00002930 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2931 }
2932 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2933 }
2934
Chris Lattner111c2282006-09-21 06:14:31 +00002935 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2936 if (N0.getOpcode() == ISD::AND &&
2937 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2938 N0.getOperand(1).getOpcode() == ISD::Constant) {
2939 SDOperand X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002940 if (X.getValueType().bitsLT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00002941 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002942 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00002943 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2944 }
Dan Gohman220a8232008-03-03 23:51:38 +00002945 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002946 Mask.zext(VT.getSizeInBits());
Chris Lattner111c2282006-09-21 06:14:31 +00002947 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2948 }
2949
Evan Cheng110dec22005-12-14 02:19:23 +00002950 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002951 if (ISD::isNON_EXTLoad(N0.Val) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002952 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
2953 TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002954 bool DoXform = true;
2955 SmallVector<SDNode*, 4> SetCCs;
2956 if (!N0.hasOneUse())
2957 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
2958 if (DoXform) {
2959 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2960 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2961 LN0->getBasePtr(), LN0->getSrcValue(),
2962 LN0->getSrcValueOffset(),
2963 N0.getValueType(),
2964 LN0->isVolatile(),
2965 LN0->getAlignment());
2966 CombineTo(N, ExtLoad);
2967 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2968 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2969 // Extend SetCC uses if necessary.
2970 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2971 SDNode *SetCC = SetCCs[i];
2972 SmallVector<SDOperand, 4> Ops;
2973 for (unsigned j = 0; j != 2; ++j) {
2974 SDOperand SOp = SetCC->getOperand(j);
2975 if (SOp == Trunc)
2976 Ops.push_back(ExtLoad);
2977 else
Evan Chengde1631b2007-10-30 20:11:21 +00002978 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002979 }
2980 Ops.push_back(SetCC->getOperand(2));
2981 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2982 &Ops[0], Ops.size()));
2983 }
2984 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2985 }
Evan Cheng110dec22005-12-14 02:19:23 +00002986 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002987
2988 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2989 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002990 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2991 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002992 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002993 MVT EVT = LN0->getMemoryVT();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002994 if ((!AfterLegalize && !LN0->isVolatile()) ||
2995 TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT)) {
2996 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2997 LN0->getBasePtr(), LN0->getSrcValue(),
2998 LN0->getSrcValueOffset(), EVT,
2999 LN0->isVolatile(),
3000 LN0->getAlignment());
3001 CombineTo(N, ExtLoad);
3002 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3003 ExtLoad.getValue(1));
3004 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3005 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003006 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003007
3008 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3009 if (N0.getOpcode() == ISD::SETCC) {
3010 SDOperand SCC =
3011 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3012 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003013 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
3014 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003015 }
3016
Nate Begeman83e75ec2005-09-06 04:43:02 +00003017 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003018}
3019
Chris Lattner5ffc0662006-05-05 05:58:59 +00003020SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
3021 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003022 MVT VT = N->getValueType(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00003023
3024 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003025 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00003026 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
3027 // fold (aext (aext x)) -> (aext x)
3028 // fold (aext (zext x)) -> (zext x)
3029 // fold (aext (sext x)) -> (sext x)
3030 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3031 N0.getOpcode() == ISD::ZERO_EXTEND ||
3032 N0.getOpcode() == ISD::SIGN_EXTEND)
3033 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
3034
Evan Chengc88138f2007-03-22 01:54:19 +00003035 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3036 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3037 if (N0.getOpcode() == ISD::TRUNCATE) {
3038 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00003039 if (NarrowLoad.Val) {
3040 if (NarrowLoad.Val != N0.Val)
3041 CombineTo(N0.Val, NarrowLoad);
3042 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
3043 }
Evan Chengc88138f2007-03-22 01:54:19 +00003044 }
3045
Chris Lattner84750582006-09-20 06:29:17 +00003046 // fold (aext (truncate x))
3047 if (N0.getOpcode() == ISD::TRUNCATE) {
3048 SDOperand TruncOp = N0.getOperand(0);
3049 if (TruncOp.getValueType() == VT)
3050 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003051 if (TruncOp.getValueType().bitsGT(VT))
Chris Lattner84750582006-09-20 06:29:17 +00003052 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
3053 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
3054 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00003055
3056 // fold (aext (and (trunc x), cst)) -> (and x, cst).
3057 if (N0.getOpcode() == ISD::AND &&
3058 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3059 N0.getOperand(1).getOpcode() == ISD::Constant) {
3060 SDOperand X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003061 if (X.getValueType().bitsLT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003062 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003063 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003064 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3065 }
Dan Gohman220a8232008-03-03 23:51:38 +00003066 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003067 Mask.zext(VT.getSizeInBits());
Chris Lattner0e4b9222006-09-21 06:40:43 +00003068 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3069 }
3070
Chris Lattner5ffc0662006-05-05 05:58:59 +00003071 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00003072 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003073 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3074 TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003075 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3076 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3077 LN0->getBasePtr(), LN0->getSrcValue(),
3078 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003079 N0.getValueType(),
3080 LN0->isVolatile(),
3081 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003082 CombineTo(N, ExtLoad);
3083 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3084 ExtLoad.getValue(1));
3085 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3086 }
3087
3088 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3089 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3090 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003091 if (N0.getOpcode() == ISD::LOAD &&
3092 !ISD::isNON_EXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003093 N0.hasOneUse()) {
3094 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003095 MVT EVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00003096 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
3097 LN0->getChain(), LN0->getBasePtr(),
3098 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003099 LN0->getSrcValueOffset(), EVT,
3100 LN0->isVolatile(),
3101 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003102 CombineTo(N, ExtLoad);
3103 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3104 ExtLoad.getValue(1));
3105 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3106 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003107
3108 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3109 if (N0.getOpcode() == ISD::SETCC) {
3110 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003111 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3112 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003113 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Chris Lattner1eba01e2007-04-11 06:50:51 +00003114 if (SCC.Val)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003115 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003116 }
3117
Chris Lattner5ffc0662006-05-05 05:58:59 +00003118 return SDOperand();
3119}
3120
Chris Lattner2b4c2792007-10-13 06:35:54 +00003121/// GetDemandedBits - See if the specified operand can be simplified with the
3122/// knowledge that only the bits specified by Mask are used. If so, return the
3123/// simpler operand, otherwise return a null SDOperand.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003124SDOperand DAGCombiner::GetDemandedBits(SDOperand V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003125 switch (V.getOpcode()) {
3126 default: break;
3127 case ISD::OR:
3128 case ISD::XOR:
3129 // If the LHS or RHS don't contribute bits to the or, drop them.
3130 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3131 return V.getOperand(1);
3132 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3133 return V.getOperand(0);
3134 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003135 case ISD::SRL:
3136 // Only look at single-use SRLs.
3137 if (!V.Val->hasOneUse())
3138 break;
3139 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3140 // See if we can recursively simplify the LHS.
3141 unsigned Amt = RHSC->getValue();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003142 APInt NewMask = Mask << Amt;
3143 SDOperand SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Chris Lattnere33544c2007-10-13 06:58:48 +00003144 if (SimplifyLHS.Val) {
3145 return DAG.getNode(ISD::SRL, V.getValueType(),
3146 SimplifyLHS, V.getOperand(1));
3147 }
3148 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003149 }
3150 return SDOperand();
3151}
3152
Evan Chengc88138f2007-03-22 01:54:19 +00003153/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3154/// bits and then truncated to a narrower type and where N is a multiple
3155/// of number of bits of the narrower type, transform it to a narrower load
3156/// from address + N / num of bits of new type. If the result is to be
3157/// extended, also fold the extension to form a extending load.
3158SDOperand DAGCombiner::ReduceLoadWidth(SDNode *N) {
3159 unsigned Opc = N->getOpcode();
3160 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
3161 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003162 MVT VT = N->getValueType(0);
3163 MVT EVT = N->getValueType(0);
Evan Chengc88138f2007-03-22 01:54:19 +00003164
Evan Chenge177e302007-03-23 22:13:36 +00003165 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3166 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003167 if (Opc == ISD::SIGN_EXTEND_INREG) {
3168 ExtType = ISD::SEXTLOAD;
3169 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00003170 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
3171 return SDOperand();
Evan Chengc88138f2007-03-22 01:54:19 +00003172 }
3173
Duncan Sands83ec4b62008-06-06 12:08:01 +00003174 unsigned EVTBits = EVT.getSizeInBits();
Evan Chengc88138f2007-03-22 01:54:19 +00003175 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00003176 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00003177 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3178 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3179 ShAmt = N01->getValue();
3180 // Is the shift amount a multiple of size of VT?
3181 if ((ShAmt & (EVTBits-1)) == 0) {
3182 N0 = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003183 if (N0.getValueType().getSizeInBits() <= EVTBits)
Evan Chengc88138f2007-03-22 01:54:19 +00003184 return SDOperand();
Evan Chengb37b80c2007-03-23 20:55:21 +00003185 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00003186 }
3187 }
3188 }
3189
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003190 // Do not generate loads of extended integer types since these can be
3191 // expensive (and would be wrong if the type is not byte sized).
3192 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() && VT.isSimple() &&
3193 VT.isByteSized() && // Exclude MVT::i1, which is simple.
3194 // Do not change the width of a volatile load.
3195 !cast<LoadSDNode>(N0)->isVolatile()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003196 assert(N0.getValueType().getSizeInBits() > EVTBits &&
Evan Chengc88138f2007-03-22 01:54:19 +00003197 "Cannot truncate to larger type!");
3198 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003199 MVT PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00003200 // For big endian targets, we need to adjust the offset to the pointer to
3201 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003202 if (TLI.isBigEndian()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003203 unsigned LVTStoreBits = N0.getValueType().getStoreSizeInBits();
3204 unsigned EVTStoreBits = EVT.getStoreSizeInBits();
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003205 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3206 }
Evan Chengdae54ce2007-03-24 00:02:43 +00003207 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003208 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Evan Chengc88138f2007-03-22 01:54:19 +00003209 SDOperand NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
3210 DAG.getConstant(PtrOff, PtrType));
3211 AddToWorkList(NewPtr.Val);
3212 SDOperand Load = (ExtType == ISD::NON_EXTLOAD)
3213 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003214 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Duncan Sandsdc846502007-10-28 12:59:45 +00003215 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003216 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003217 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00003218 LN0->isVolatile(), NewAlign);
Evan Chengc88138f2007-03-22 01:54:19 +00003219 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00003220 if (CombineSRL) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003221 WorkListRemover DeadNodes(*this);
3222 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3223 &DeadNodes);
Evan Chengb37b80c2007-03-23 20:55:21 +00003224 CombineTo(N->getOperand(0).Val, Load);
3225 } else
3226 CombineTo(N0.Val, Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00003227 if (ShAmt) {
3228 if (Opc == ISD::SIGN_EXTEND_INREG)
3229 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
3230 else
3231 return DAG.getNode(Opc, VT, Load);
3232 }
Evan Chengc88138f2007-03-22 01:54:19 +00003233 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3234 }
3235
3236 return SDOperand();
3237}
3238
Chris Lattner5ffc0662006-05-05 05:58:59 +00003239
Nate Begeman83e75ec2005-09-06 04:43:02 +00003240SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003241 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003242 SDOperand N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003243 MVT VT = N->getValueType(0);
3244 MVT EVT = cast<VTSDNode>(N1)->getVT();
3245 unsigned VTBits = VT.getSizeInBits();
3246 unsigned EVTBits = EVT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003247
Nate Begeman1d4d4142005-09-01 00:19:25 +00003248 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003249 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003250 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003251
Chris Lattner541a24f2006-05-06 22:43:44 +00003252 // If the input is already sign extended, just drop the extension.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003253 if (DAG.ComputeNumSignBits(N0) >= VT.getSizeInBits()-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003254 return N0;
3255
Nate Begeman646d7e22005-09-02 21:18:40 +00003256 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3257 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00003258 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003259 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003260 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003261
Chris Lattner95a5e052007-04-17 19:03:21 +00003262 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003263 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Nate Begemande996292006-02-03 22:24:05 +00003264 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003265
Chris Lattner95a5e052007-04-17 19:03:21 +00003266 // fold operands of sext_in_reg based on knowledge that the top bits are not
3267 // demanded.
3268 if (SimplifyDemandedBits(SDOperand(N, 0)))
3269 return SDOperand(N, 0);
3270
Evan Chengc88138f2007-03-22 01:54:19 +00003271 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3272 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
3273 SDOperand NarrowLoad = ReduceLoadWidth(N);
3274 if (NarrowLoad.Val)
3275 return NarrowLoad;
3276
Chris Lattner4b37e872006-05-08 21:18:59 +00003277 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3278 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3279 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3280 if (N0.getOpcode() == ISD::SRL) {
3281 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00003282 if (ShAmt->getValue()+EVTBits <= VT.getSizeInBits()) {
Chris Lattner4b37e872006-05-08 21:18:59 +00003283 // We can turn this into an SRA iff the input to the SRL is already sign
3284 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003285 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00003286 if (VT.getSizeInBits()-(ShAmt->getValue()+EVTBits) < InSignBits)
Chris Lattner4b37e872006-05-08 21:18:59 +00003287 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3288 }
3289 }
Evan Chengc88138f2007-03-22 01:54:19 +00003290
Nate Begemanded49632005-10-13 03:11:28 +00003291 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00003292 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003293 ISD::isUNINDEXEDLoad(N0.Val) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003294 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003295 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3296 TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003297 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3298 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3299 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003300 LN0->getSrcValueOffset(), EVT,
3301 LN0->isVolatile(),
3302 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003303 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003304 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003305 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003306 }
3307 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00003308 if (ISD::isZEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
3309 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003310 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003311 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3312 TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003313 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3314 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3315 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003316 LN0->getSrcValueOffset(), EVT,
3317 LN0->isVolatile(),
3318 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003319 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003320 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003321 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003322 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003323 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003324}
3325
Nate Begeman83e75ec2005-09-06 04:43:02 +00003326SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003327 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003328 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003329
3330 // noop truncate
3331 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003332 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003333 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003334 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003335 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003336 // fold (truncate (truncate x)) -> (truncate x)
3337 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003338 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003339 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003340 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3341 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00003342 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003343 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003344 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00003345 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003346 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003347 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003348 else
3349 // if the source and dest are the same type, we can drop both the extend
3350 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003351 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003352 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003353
Chris Lattner2b4c2792007-10-13 06:35:54 +00003354 // See if we can simplify the input to this truncate through knowledge that
3355 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3356 // -> trunc y
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003357 SDOperand Shorter =
3358 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00003359 VT.getSizeInBits()));
Chris Lattner2b4c2792007-10-13 06:35:54 +00003360 if (Shorter.Val)
3361 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3362
Nate Begeman3df4d522005-10-12 20:40:40 +00003363 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003364 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003365 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003366}
3367
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003368static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
3369 SDOperand Elt = N->getOperand(i);
3370 if (Elt.getOpcode() != ISD::MERGE_VALUES)
3371 return Elt.Val;
3372 return Elt.getOperand(Elt.ResNo).Val;
3373}
3374
3375/// CombineConsecutiveLoads - build_pair (load, load) -> load
3376/// if load locations are consecutive.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003377SDOperand DAGCombiner::CombineConsecutiveLoads(SDNode *N, MVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003378 assert(N->getOpcode() == ISD::BUILD_PAIR);
3379
3380 SDNode *LD1 = getBuildPairElt(N, 0);
3381 if (!ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse())
3382 return SDOperand();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003383 MVT LD1VT = LD1->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003384 SDNode *LD2 = getBuildPairElt(N, 1);
3385 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3386 if (ISD::isNON_EXTLoad(LD2) &&
3387 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003388 // If both are volatile this would reduce the number of volatile loads.
3389 // If one is volatile it might be ok, but play conservative and bail out.
3390 !cast<LoadSDNode>(LD1)->isVolatile() &&
3391 !cast<LoadSDNode>(LD2)->isVolatile() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003392 TLI.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1, MFI)) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003393 LoadSDNode *LD = cast<LoadSDNode>(LD1);
3394 unsigned Align = LD->getAlignment();
3395 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003396 getABITypeAlignment(VT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003397 if (NewAlign <= Align &&
3398 (!AfterLegalize || TLI.isOperationLegal(ISD::LOAD, VT)))
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003399 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(),
3400 LD->getSrcValue(), LD->getSrcValueOffset(),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003401 false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003402 }
3403 return SDOperand();
3404}
3405
Chris Lattner94683772005-12-23 05:30:37 +00003406SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3407 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003408 MVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00003409
Dan Gohman7f321562007-06-25 16:23:39 +00003410 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3411 // Only do this before legalize, since afterward the target may be depending
3412 // on the bitconvert.
3413 // First check to see if this is all constant.
3414 if (!AfterLegalize &&
3415 N0.getOpcode() == ISD::BUILD_VECTOR && N0.Val->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003416 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003417 bool isSimple = true;
3418 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3419 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3420 N0.getOperand(i).getOpcode() != ISD::Constant &&
3421 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3422 isSimple = false;
3423 break;
3424 }
3425
Duncan Sands83ec4b62008-06-06 12:08:01 +00003426 MVT DestEltVT = N->getValueType(0).getVectorElementType();
3427 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00003428 "Element type of vector ValueType must not be vector!");
3429 if (isSimple) {
3430 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.Val, DestEltVT);
3431 }
3432 }
3433
Chris Lattner94683772005-12-23 05:30:37 +00003434 // If the input is a constant, let getNode() fold it.
3435 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
3436 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3437 if (Res.Val != N) return Res;
3438 }
3439
Chris Lattnerc8547d82005-12-23 05:37:50 +00003440 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3441 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003442
Chris Lattner57104102005-12-23 05:44:41 +00003443 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003444 // If the resultant load doesn't need a higher alignment than the original!
3445 if (ISD::isNormalLoad(N0.Val) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003446 // Do not change the width of a volatile load.
3447 !cast<LoadSDNode>(N0)->isVolatile() &&
3448 (!AfterLegalize || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003449 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng59d5b682007-05-07 21:27:48 +00003450 unsigned Align = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003451 getABITypeAlignment(VT.getTypeForMVT());
Evan Cheng59d5b682007-05-07 21:27:48 +00003452 unsigned OrigAlign = LN0->getAlignment();
3453 if (Align <= OrigAlign) {
3454 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
3455 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003456 LN0->isVolatile(), Align);
Evan Cheng59d5b682007-05-07 21:27:48 +00003457 AddToWorkList(N);
3458 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
3459 Load.getValue(1));
3460 return Load;
3461 }
Chris Lattner57104102005-12-23 05:44:41 +00003462 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003463
Chris Lattner3bd39d42008-01-27 17:42:27 +00003464 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3465 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3466 // This often reduces constant pool loads.
3467 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003468 N0.Val->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003469 SDOperand NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3470 AddToWorkList(NewConv.Val);
3471
Duncan Sands83ec4b62008-06-06 12:08:01 +00003472 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003473 if (N0.getOpcode() == ISD::FNEG)
3474 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3475 assert(N0.getOpcode() == ISD::FABS);
3476 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3477 }
3478
3479 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3480 // Note that we don't handle copysign(x,cst) because this can always be folded
3481 // to an fneg or fabs.
3482 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00003483 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003484 VT.isInteger() && !VT.isVector()) {
3485 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
3486 SDOperand X = DAG.getNode(ISD::BIT_CONVERT,
3487 MVT::getIntegerVT(OrigXWidth),
Chris Lattner3bd39d42008-01-27 17:42:27 +00003488 N0.getOperand(1));
3489 AddToWorkList(X.Val);
3490
3491 // If X has a different width than the result/lhs, sext it or truncate it.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003492 unsigned VTWidth = VT.getSizeInBits();
Chris Lattner3bd39d42008-01-27 17:42:27 +00003493 if (OrigXWidth < VTWidth) {
3494 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
3495 AddToWorkList(X.Val);
3496 } else if (OrigXWidth > VTWidth) {
3497 // To get the sign bit in the right place, we have to shift it right
3498 // before truncating.
3499 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3500 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
3501 AddToWorkList(X.Val);
3502 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3503 AddToWorkList(X.Val);
3504 }
3505
Duncan Sands83ec4b62008-06-06 12:08:01 +00003506 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003507 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
3508 AddToWorkList(X.Val);
3509
3510 SDOperand Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3511 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
3512 AddToWorkList(Cst.Val);
3513
3514 return DAG.getNode(ISD::OR, VT, X, Cst);
3515 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003516
3517 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
3518 if (N0.getOpcode() == ISD::BUILD_PAIR) {
3519 SDOperand CombineLD = CombineConsecutiveLoads(N0.Val, VT);
3520 if (CombineLD.Val)
3521 return CombineLD;
3522 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003523
Chris Lattner94683772005-12-23 05:30:37 +00003524 return SDOperand();
3525}
3526
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003527SDOperand DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003528 MVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003529 return CombineConsecutiveLoads(N, VT);
3530}
3531
Dan Gohman7f321562007-06-25 16:23:39 +00003532/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003533/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3534/// destination element value type.
3535SDOperand DAGCombiner::
Duncan Sands83ec4b62008-06-06 12:08:01 +00003536ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT DstEltVT) {
3537 MVT SrcEltVT = BV->getOperand(0).getValueType();
Chris Lattner6258fb22006-04-02 02:53:43 +00003538
3539 // If this is already the right type, we're done.
3540 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
3541
Duncan Sands83ec4b62008-06-06 12:08:01 +00003542 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
3543 unsigned DstBitSize = DstEltVT.getSizeInBits();
Chris Lattner6258fb22006-04-02 02:53:43 +00003544
3545 // If this is a conversion of N elements of one type to N elements of another
3546 // type, convert each element. This handles FP<->INT cases.
3547 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003548 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003549 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003550 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00003551 AddToWorkList(Ops.back().Val);
3552 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003553 MVT VT = MVT::getVectorVT(DstEltVT,
3554 BV->getValueType(0).getVectorNumElements());
Dan Gohman7f321562007-06-25 16:23:39 +00003555 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003556 }
3557
3558 // Otherwise, we're growing or shrinking the elements. To avoid having to
3559 // handle annoying details of growing/shrinking FP values, we convert them to
3560 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003561 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003562 // Convert the input float vector to a int vector where the elements are the
3563 // same sizes.
3564 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003565 MVT IntVT = MVT::getIntegerVT(SrcEltVT.getSizeInBits());
Dan Gohman7f321562007-06-25 16:23:39 +00003566 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003567 SrcEltVT = IntVT;
3568 }
3569
3570 // Now we know the input is an integer vector. If the output is a FP type,
3571 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003572 if (DstEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003573 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003574 MVT TmpVT = MVT::getIntegerVT(DstEltVT.getSizeInBits());
Dan Gohman7f321562007-06-25 16:23:39 +00003575 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003576
3577 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003578 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003579 }
3580
3581 // Okay, we know the src/dst types are both integers of differing types.
3582 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003583 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00003584 if (SrcBitSize < DstBitSize) {
3585 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3586
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003587 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003588 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003589 i += NumInputsPerOutput) {
3590 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00003591 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003592 bool EltIsUndef = true;
3593 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3594 // Shift the previously computed bits over.
3595 NewBits <<= SrcBitSize;
3596 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
3597 if (Op.getOpcode() == ISD::UNDEF) continue;
3598 EltIsUndef = false;
3599
Dan Gohman220a8232008-03-03 23:51:38 +00003600 NewBits |=
3601 APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003602 }
3603
3604 if (EltIsUndef)
3605 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3606 else
3607 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3608 }
3609
Duncan Sands83ec4b62008-06-06 12:08:01 +00003610 MVT VT = MVT::getVectorVT(DstEltVT, Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00003611 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003612 }
3613
3614 // Finally, this must be the case where we are shrinking elements: each input
3615 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00003616 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00003617 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003618 MVT VT = MVT::getVectorVT(DstEltVT, NumOutputsPerInput*BV->getNumOperands());
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003619 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003620 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003621 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3622 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3623 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3624 continue;
3625 }
Dan Gohman220a8232008-03-03 23:51:38 +00003626 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getAPIntValue();
Chris Lattner6258fb22006-04-02 02:53:43 +00003627 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohman220a8232008-03-03 23:51:38 +00003628 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003629 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohman220a8232008-03-03 23:51:38 +00003630 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00003631 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
3632 return DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00003633 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003634 }
3635
3636 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00003637 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00003638 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3639 }
Dan Gohman7f321562007-06-25 16:23:39 +00003640 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003641}
3642
3643
3644
Chris Lattner01b3d732005-09-28 22:28:18 +00003645SDOperand DAGCombiner::visitFADD(SDNode *N) {
3646 SDOperand N0 = N->getOperand(0);
3647 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003648 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3649 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003650 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003651
Dan Gohman7f321562007-06-25 16:23:39 +00003652 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003653 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00003654 SDOperand FoldedVOp = SimplifyVBinOp(N);
3655 if (FoldedVOp.Val) return FoldedVOp;
3656 }
Dan Gohman7f321562007-06-25 16:23:39 +00003657
Nate Begemana0e221d2005-10-18 00:28:13 +00003658 // fold (fadd c1, c2) -> c1+c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003659 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003660 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003661 // canonicalize constant to RHS
3662 if (N0CFP && !N1CFP)
3663 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003664 // fold (A + (-B)) -> A-B
Chris Lattner0254e702008-02-26 07:04:54 +00003665 if (isNegatibleForFree(N1, AfterLegalize) == 2)
3666 return DAG.getNode(ISD::FSUB, VT, N0,
3667 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner01b3d732005-09-28 22:28:18 +00003668 // fold ((-A) + B) -> B-A
Chris Lattner0254e702008-02-26 07:04:54 +00003669 if (isNegatibleForFree(N0, AfterLegalize) == 2)
3670 return DAG.getNode(ISD::FSUB, VT, N1,
3671 GetNegatedExpression(N0, DAG, AfterLegalize));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003672
3673 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3674 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
3675 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3676 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3677 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3678
Chris Lattner01b3d732005-09-28 22:28:18 +00003679 return SDOperand();
3680}
3681
3682SDOperand DAGCombiner::visitFSUB(SDNode *N) {
3683 SDOperand N0 = N->getOperand(0);
3684 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003685 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3686 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003687 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003688
Dan Gohman7f321562007-06-25 16:23:39 +00003689 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003690 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00003691 SDOperand FoldedVOp = SimplifyVBinOp(N);
3692 if (FoldedVOp.Val) return FoldedVOp;
3693 }
Dan Gohman7f321562007-06-25 16:23:39 +00003694
Nate Begemana0e221d2005-10-18 00:28:13 +00003695 // fold (fsub c1, c2) -> c1-c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003696 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003697 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003698 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003699 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Chris Lattner0254e702008-02-26 07:04:54 +00003700 if (isNegatibleForFree(N1, AfterLegalize))
3701 return GetNegatedExpression(N1, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00003702 return DAG.getNode(ISD::FNEG, VT, N1);
3703 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003704 // fold (A-(-B)) -> A+B
Chris Lattner0254e702008-02-26 07:04:54 +00003705 if (isNegatibleForFree(N1, AfterLegalize))
3706 return DAG.getNode(ISD::FADD, VT, N0,
3707 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003708
Chris Lattner01b3d732005-09-28 22:28:18 +00003709 return SDOperand();
3710}
3711
3712SDOperand DAGCombiner::visitFMUL(SDNode *N) {
3713 SDOperand N0 = N->getOperand(0);
3714 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003715 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3716 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003717 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003718
Dan Gohman7f321562007-06-25 16:23:39 +00003719 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003720 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00003721 SDOperand FoldedVOp = SimplifyVBinOp(N);
3722 if (FoldedVOp.Val) return FoldedVOp;
3723 }
Dan Gohman7f321562007-06-25 16:23:39 +00003724
Nate Begeman11af4ea2005-10-17 20:40:11 +00003725 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003726 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003727 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003728 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003729 if (N0CFP && !N1CFP)
3730 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003731 // fold (fmul X, 2.0) -> (fadd X, X)
3732 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3733 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003734 // fold (fmul X, -1.0) -> (fneg X)
3735 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3736 return DAG.getNode(ISD::FNEG, VT, N0);
3737
3738 // -X * -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003739 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3740 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003741 // Both can be negated for free, check to see if at least one is cheaper
3742 // negated.
3743 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003744 return DAG.getNode(ISD::FMUL, VT,
3745 GetNegatedExpression(N0, DAG, AfterLegalize),
3746 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003747 }
3748 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003749
3750 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3751 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
3752 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3753 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3754 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3755
Chris Lattner01b3d732005-09-28 22:28:18 +00003756 return SDOperand();
3757}
3758
3759SDOperand DAGCombiner::visitFDIV(SDNode *N) {
3760 SDOperand N0 = N->getOperand(0);
3761 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003762 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3763 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003764 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003765
Dan Gohman7f321562007-06-25 16:23:39 +00003766 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003767 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00003768 SDOperand FoldedVOp = SimplifyVBinOp(N);
3769 if (FoldedVOp.Val) return FoldedVOp;
3770 }
Dan Gohman7f321562007-06-25 16:23:39 +00003771
Nate Begemana148d982006-01-18 22:35:16 +00003772 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003773 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003774 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003775
3776
3777 // -X / -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003778 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3779 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003780 // Both can be negated for free, check to see if at least one is cheaper
3781 // negated.
3782 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003783 return DAG.getNode(ISD::FDIV, VT,
3784 GetNegatedExpression(N0, DAG, AfterLegalize),
3785 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003786 }
3787 }
3788
Chris Lattner01b3d732005-09-28 22:28:18 +00003789 return SDOperand();
3790}
3791
3792SDOperand DAGCombiner::visitFREM(SDNode *N) {
3793 SDOperand N0 = N->getOperand(0);
3794 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003795 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3796 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003797 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003798
Nate Begemana148d982006-01-18 22:35:16 +00003799 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003800 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003801 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003802
Chris Lattner01b3d732005-09-28 22:28:18 +00003803 return SDOperand();
3804}
3805
Chris Lattner12d83032006-03-05 05:30:57 +00003806SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3807 SDOperand N0 = N->getOperand(0);
3808 SDOperand N1 = N->getOperand(1);
3809 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3810 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003811 MVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00003812
Dale Johannesendb44bf82007-10-16 23:38:29 +00003813 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00003814 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3815
3816 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003817 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003818 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3819 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dale Johannesen87503a62007-08-25 22:10:57 +00003820 if (!V.isNegative())
Chris Lattner12d83032006-03-05 05:30:57 +00003821 return DAG.getNode(ISD::FABS, VT, N0);
3822 else
3823 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3824 }
3825
3826 // copysign(fabs(x), y) -> copysign(x, y)
3827 // copysign(fneg(x), y) -> copysign(x, y)
3828 // copysign(copysign(x,z), y) -> copysign(x, y)
3829 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3830 N0.getOpcode() == ISD::FCOPYSIGN)
3831 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3832
3833 // copysign(x, abs(y)) -> abs(x)
3834 if (N1.getOpcode() == ISD::FABS)
3835 return DAG.getNode(ISD::FABS, VT, N0);
3836
3837 // copysign(x, copysign(y,z)) -> copysign(x, z)
3838 if (N1.getOpcode() == ISD::FCOPYSIGN)
3839 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3840
3841 // copysign(x, fp_extend(y)) -> copysign(x, y)
3842 // copysign(x, fp_round(y)) -> copysign(x, y)
3843 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3844 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3845
3846 return SDOperand();
3847}
3848
3849
Chris Lattner01b3d732005-09-28 22:28:18 +00003850
Nate Begeman83e75ec2005-09-06 04:43:02 +00003851SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003852 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003853 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003854 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003855
3856 // fold (sint_to_fp c1) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003857 if (N0C && N0.getValueType() != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003858 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003859 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003860}
3861
Nate Begeman83e75ec2005-09-06 04:43:02 +00003862SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003863 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003864 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003865 MVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00003866
Nate Begeman1d4d4142005-09-01 00:19:25 +00003867 // fold (uint_to_fp c1) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003868 if (N0C && N0.getValueType() != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003869 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003870 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003871}
3872
Nate Begeman83e75ec2005-09-06 04:43:02 +00003873SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003874 SDOperand N0 = N->getOperand(0);
3875 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003876 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003877
3878 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003879 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003880 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003881 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003882}
3883
Nate Begeman83e75ec2005-09-06 04:43:02 +00003884SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003885 SDOperand N0 = N->getOperand(0);
3886 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003887 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003888
3889 // fold (fp_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00003890 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003891 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003892 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003893}
3894
Nate Begeman83e75ec2005-09-06 04:43:02 +00003895SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003896 SDOperand N0 = N->getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003897 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003898 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003899 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003900
3901 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003902 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner0bd48932008-01-17 07:00:52 +00003903 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003904
3905 // fold (fp_round (fp_extend x)) -> x
3906 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3907 return N0.getOperand(0);
3908
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00003909 // fold (fp_round (fp_round x)) -> (fp_round x)
3910 if (N0.getOpcode() == ISD::FP_ROUND) {
3911 // This is a value preserving truncation if both round's are.
3912 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
3913 N0.Val->getConstantOperandVal(1) == 1;
3914 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
3915 DAG.getIntPtrConstant(IsTrunc));
3916 }
3917
Chris Lattner79dbea52006-03-13 06:26:26 +00003918 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
3919 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
Chris Lattner0bd48932008-01-17 07:00:52 +00003920 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003921 AddToWorkList(Tmp.Val);
3922 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
3923 }
3924
Nate Begeman83e75ec2005-09-06 04:43:02 +00003925 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003926}
3927
Nate Begeman83e75ec2005-09-06 04:43:02 +00003928SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003929 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003930 MVT VT = N->getValueType(0);
3931 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00003932 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003933
Nate Begeman1d4d4142005-09-01 00:19:25 +00003934 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003935 if (N0CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003936 SDOperand Round = DAG.getConstantFP(N0CFP->getValueAPF(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003937 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003938 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003939 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003940}
3941
Nate Begeman83e75ec2005-09-06 04:43:02 +00003942SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003943 SDOperand N0 = N->getOperand(0);
3944 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003945 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003946
Chris Lattner5938bef2007-12-29 06:55:23 +00003947 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003948 if (N->hasOneUse() &&
3949 N->use_begin()->getSDOperand().getOpcode() == ISD::FP_ROUND)
Chris Lattner5938bef2007-12-29 06:55:23 +00003950 return SDOperand();
Chris Lattner0bd48932008-01-17 07:00:52 +00003951
Nate Begeman1d4d4142005-09-01 00:19:25 +00003952 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003953 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003954 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003955
3956 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
3957 // value of X.
3958 if (N0.getOpcode() == ISD::FP_ROUND && N0.Val->getConstantOperandVal(1) == 1){
3959 SDOperand In = N0.getOperand(0);
3960 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00003961 if (VT.bitsLT(In.getValueType()))
Chris Lattner0bd48932008-01-17 07:00:52 +00003962 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
3963 return DAG.getNode(ISD::FP_EXTEND, VT, In);
3964 }
3965
3966 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Dale Johannesenb6210fc2007-10-19 20:29:00 +00003967 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003968 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3969 TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003970 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3971 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3972 LN0->getBasePtr(), LN0->getSrcValue(),
3973 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003974 N0.getValueType(),
3975 LN0->isVolatile(),
3976 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00003977 CombineTo(N, ExtLoad);
Chris Lattner0bd48932008-01-17 07:00:52 +00003978 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad,
3979 DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00003980 ExtLoad.getValue(1));
3981 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3982 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003983
Nate Begeman83e75ec2005-09-06 04:43:02 +00003984 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003985}
3986
Nate Begeman83e75ec2005-09-06 04:43:02 +00003987SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003988 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003989
Chris Lattner0254e702008-02-26 07:04:54 +00003990 if (isNegatibleForFree(N0, AfterLegalize))
3991 return GetNegatedExpression(N0, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00003992
Chris Lattner3bd39d42008-01-27 17:42:27 +00003993 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
3994 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00003995 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003996 N0.getOperand(0).getValueType().isInteger() &&
3997 !N0.getOperand(0).getValueType().isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003998 SDOperand Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003999 MVT IntVT = Int.getValueType();
4000 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004001 Int = DAG.getNode(ISD::XOR, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004002 DAG.getConstant(IntVT.getIntegerVTSignBit(), IntVT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00004003 AddToWorkList(Int.Val);
4004 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4005 }
4006 }
4007
Nate Begeman83e75ec2005-09-06 04:43:02 +00004008 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004009}
4010
Nate Begeman83e75ec2005-09-06 04:43:02 +00004011SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00004012 SDOperand N0 = N->getOperand(0);
4013 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004014 MVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00004015
Nate Begeman1d4d4142005-09-01 00:19:25 +00004016 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00004017 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004018 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004019 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004020 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00004021 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004022 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004023 // fold (fabs (fcopysign x, y)) -> (fabs x)
4024 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
4025 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
4026
Chris Lattner3bd39d42008-01-27 17:42:27 +00004027 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
4028 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00004029 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004030 N0.getOperand(0).getValueType().isInteger() &&
4031 !N0.getOperand(0).getValueType().isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004032 SDOperand Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004033 MVT IntVT = Int.getValueType();
4034 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004035 Int = DAG.getNode(ISD::AND, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004036 DAG.getConstant(~IntVT.getIntegerVTSignBit(), IntVT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00004037 AddToWorkList(Int.Val);
4038 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4039 }
4040 }
4041
Nate Begeman83e75ec2005-09-06 04:43:02 +00004042 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004043}
4044
Nate Begeman44728a72005-09-19 22:34:01 +00004045SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
4046 SDOperand Chain = N->getOperand(0);
4047 SDOperand N1 = N->getOperand(1);
4048 SDOperand N2 = N->getOperand(2);
4049 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4050
4051 // never taken branch, fold to chain
4052 if (N1C && N1C->isNullValue())
4053 return Chain;
4054 // unconditional branch
Dan Gohman002e5d02008-03-13 22:13:53 +00004055 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00004056 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004057 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
4058 // on the target.
4059 if (N1.getOpcode() == ISD::SETCC &&
4060 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
4061 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
4062 N1.getOperand(0), N1.getOperand(1), N2);
4063 }
Nate Begeman44728a72005-09-19 22:34:01 +00004064 return SDOperand();
4065}
4066
Chris Lattner3ea0b472005-10-05 06:47:48 +00004067// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
4068//
Nate Begeman44728a72005-09-19 22:34:01 +00004069SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00004070 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
4071 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
4072
Duncan Sands8eab8a22008-06-09 11:32:28 +00004073 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00004074 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00004075 if (Simp.Val) AddToWorkList(Simp.Val);
4076
Nate Begemane17daeb2005-10-05 21:43:42 +00004077 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
4078
4079 // fold br_cc true, dest -> br dest (unconditional branch)
Dan Gohman002e5d02008-03-13 22:13:53 +00004080 if (SCCC && !SCCC->isNullValue())
Nate Begemane17daeb2005-10-05 21:43:42 +00004081 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
4082 N->getOperand(4));
4083 // fold br_cc false, dest -> unconditional fall through
4084 if (SCCC && SCCC->isNullValue())
4085 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00004086
Nate Begemane17daeb2005-10-05 21:43:42 +00004087 // fold to a simpler setcc
4088 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
4089 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
4090 Simp.getOperand(2), Simp.getOperand(0),
4091 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00004092 return SDOperand();
4093}
4094
Chris Lattner448f2192006-11-11 00:39:41 +00004095
4096/// CombineToPreIndexedLoadStore - Try turning a load / store and a
4097/// pre-indexed load / store when the base pointer is a add or subtract
4098/// and it has other uses besides the load / store. After the
4099/// transformation, the new indexed load / store has effectively folded
4100/// the add / subtract in and all of its other uses are redirected to the
4101/// new load / store.
4102bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
4103 if (!AfterLegalize)
4104 return false;
4105
4106 bool isLoad = true;
4107 SDOperand Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004108 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004109 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004110 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004111 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004112 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00004113 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00004114 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
4115 return false;
4116 Ptr = LD->getBasePtr();
4117 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004118 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004119 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004120 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004121 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
4122 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
4123 return false;
4124 Ptr = ST->getBasePtr();
4125 isLoad = false;
4126 } else
4127 return false;
4128
Chris Lattner9f1794e2006-11-11 00:56:29 +00004129 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4130 // out. There is no reason to make this a preinc/predec.
4131 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
4132 Ptr.Val->hasOneUse())
4133 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004134
Chris Lattner9f1794e2006-11-11 00:56:29 +00004135 // Ask the target to do addressing mode selection.
4136 SDOperand BasePtr;
4137 SDOperand Offset;
4138 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4139 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4140 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00004141 // Don't create a indexed load / store with zero offset.
4142 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004143 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004144 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004145
Chris Lattner41e53fd2006-11-11 01:00:15 +00004146 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00004147 // 1) The new base ptr is a frame index.
4148 // 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 +00004149 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00004150 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00004151 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00004152 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00004153
Chris Lattner41e53fd2006-11-11 01:00:15 +00004154 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4155 // (plus the implicit offset) to a register to preinc anyway.
4156 if (isa<FrameIndexSDNode>(BasePtr))
4157 return false;
4158
4159 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004160 if (!isLoad) {
4161 SDOperand Val = cast<StoreSDNode>(N)->getValue();
Evan Cheng917be682008-03-04 00:41:45 +00004162 if (Val == BasePtr || BasePtr.Val->isPredecessorOf(Val.Val))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004163 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004164 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004165
Evan Chengc843abe2007-05-24 02:35:39 +00004166 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004167 bool RealUse = false;
4168 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
4169 E = Ptr.Val->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004170 SDNode *Use = I->getUser();
Chris Lattner9f1794e2006-11-11 00:56:29 +00004171 if (Use == N)
4172 continue;
Evan Cheng917be682008-03-04 00:41:45 +00004173 if (Use->isPredecessorOf(N))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004174 return false;
4175
4176 if (!((Use->getOpcode() == ISD::LOAD &&
4177 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004178 (Use->getOpcode() == ISD::STORE &&
4179 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004180 RealUse = true;
4181 }
4182 if (!RealUse)
4183 return false;
4184
4185 SDOperand Result;
4186 if (isLoad)
4187 Result = DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM);
4188 else
4189 Result = DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4190 ++PreIndexedNodes;
4191 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004192 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004193 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4194 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004195 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004196 if (isLoad) {
4197 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004198 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004199 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004200 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004201 } else {
4202 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004203 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004204 }
4205
Chris Lattner9f1794e2006-11-11 00:56:29 +00004206 // Finally, since the node is now dead, remove it from the graph.
4207 DAG.DeleteNode(N);
4208
4209 // Replace the uses of Ptr with uses of the updated base value.
4210 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004211 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004212 removeFromWorkList(Ptr.Val);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004213 DAG.DeleteNode(Ptr.Val);
4214
4215 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004216}
4217
4218/// CombineToPostIndexedLoadStore - Try combine a load / store with a
4219/// add / sub of the base pointer node into a post-indexed load / store.
4220/// The transformation folded the add / subtract into the new indexed
4221/// load / store effectively and all of its uses are redirected to the
4222/// new load / store.
4223bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
4224 if (!AfterLegalize)
4225 return false;
4226
4227 bool isLoad = true;
4228 SDOperand Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004229 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004230 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004231 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004232 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004233 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004234 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4235 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4236 return false;
4237 Ptr = LD->getBasePtr();
4238 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004239 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004240 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004241 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004242 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4243 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4244 return false;
4245 Ptr = ST->getBasePtr();
4246 isLoad = false;
4247 } else
4248 return false;
4249
Evan Chengcc470212006-11-16 00:08:20 +00004250 if (Ptr.Val->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004251 return false;
4252
4253 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
4254 E = Ptr.Val->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004255 SDNode *Op = I->getUser();
Chris Lattner9f1794e2006-11-11 00:56:29 +00004256 if (Op == N ||
4257 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4258 continue;
4259
4260 SDOperand BasePtr;
4261 SDOperand Offset;
4262 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4263 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4264 if (Ptr == Offset)
4265 std::swap(BasePtr, Offset);
4266 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00004267 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00004268 // Don't create a indexed load / store with zero offset.
4269 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004270 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004271 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004272
Chris Lattner9f1794e2006-11-11 00:56:29 +00004273 // Try turning it into a post-indexed load / store except when
4274 // 1) All uses are load / store ops that use it as base ptr.
4275 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4276 // nor a successor of N. Otherwise, if Op is folded that would
4277 // create a cycle.
4278
4279 // Check for #1.
4280 bool TryNext = false;
4281 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
4282 EE = BasePtr.Val->use_end(); II != EE; ++II) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004283 SDNode *Use = II->getUser();
Chris Lattner9f1794e2006-11-11 00:56:29 +00004284 if (Use == Ptr.Val)
Chris Lattner448f2192006-11-11 00:39:41 +00004285 continue;
4286
Chris Lattner9f1794e2006-11-11 00:56:29 +00004287 // If all the uses are load / store addresses, then don't do the
4288 // transformation.
4289 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4290 bool RealUse = false;
4291 for (SDNode::use_iterator III = Use->use_begin(),
4292 EEE = Use->use_end(); III != EEE; ++III) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004293 SDNode *UseUse = III->getUser();
Chris Lattner9f1794e2006-11-11 00:56:29 +00004294 if (!((UseUse->getOpcode() == ISD::LOAD &&
4295 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004296 (UseUse->getOpcode() == ISD::STORE &&
4297 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004298 RealUse = true;
4299 }
Chris Lattner448f2192006-11-11 00:39:41 +00004300
Chris Lattner9f1794e2006-11-11 00:56:29 +00004301 if (!RealUse) {
4302 TryNext = true;
4303 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004304 }
4305 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004306 }
4307 if (TryNext)
4308 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004309
Chris Lattner9f1794e2006-11-11 00:56:29 +00004310 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00004311 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Chris Lattner9f1794e2006-11-11 00:56:29 +00004312 SDOperand Result = isLoad
4313 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
4314 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4315 ++PostIndexedNodes;
4316 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004317 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004318 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4319 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004320 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004321 if (isLoad) {
4322 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004323 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004324 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004325 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004326 } else {
4327 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004328 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00004329 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004330
Chris Lattner9f1794e2006-11-11 00:56:29 +00004331 // Finally, since the node is now dead, remove it from the graph.
4332 DAG.DeleteNode(N);
4333
4334 // Replace the uses of Use with uses of the updated base value.
4335 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
4336 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004337 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004338 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004339 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004340 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004341 }
4342 }
4343 }
4344 return false;
4345}
4346
Chris Lattner00161a62008-01-25 07:20:16 +00004347/// InferAlignment - If we can infer some alignment information from this
4348/// pointer, return it.
4349static unsigned InferAlignment(SDOperand Ptr, SelectionDAG &DAG) {
4350 // If this is a direct reference to a stack slot, use information about the
4351 // stack slot's alignment.
Chris Lattner1329cb82008-01-26 19:45:50 +00004352 int FrameIdx = 1 << 31;
4353 int64_t FrameOffset = 0;
Chris Lattner00161a62008-01-25 07:20:16 +00004354 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1329cb82008-01-26 19:45:50 +00004355 FrameIdx = FI->getIndex();
4356 } else if (Ptr.getOpcode() == ISD::ADD &&
4357 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4358 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4359 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4360 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner00161a62008-01-25 07:20:16 +00004361 }
Chris Lattner1329cb82008-01-26 19:45:50 +00004362
4363 if (FrameIdx != (1 << 31)) {
4364 // FIXME: Handle FI+CST.
4365 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4366 if (MFI.isFixedObjectIndex(FrameIdx)) {
4367 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx);
4368
4369 // The alignment of the frame index can be determined from its offset from
4370 // the incoming frame position. If the frame object is at offset 32 and
4371 // the stack is guaranteed to be 16-byte aligned, then we know that the
4372 // object is 16-byte aligned.
4373 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4374 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4375
4376 // Finally, the frame object itself may have a known alignment. Factor
4377 // the alignment + offset into a new alignment. For example, if we know
4378 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4379 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4380 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4381 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4382 FrameOffset);
4383 return std::max(Align, FIInfoAlign);
4384 }
4385 }
Chris Lattner00161a62008-01-25 07:20:16 +00004386
4387 return 0;
4388}
Chris Lattner448f2192006-11-11 00:39:41 +00004389
Chris Lattner01a22022005-10-10 22:04:48 +00004390SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004391 LoadSDNode *LD = cast<LoadSDNode>(N);
4392 SDOperand Chain = LD->getChain();
4393 SDOperand Ptr = LD->getBasePtr();
Chris Lattner00161a62008-01-25 07:20:16 +00004394
4395 // Try to infer better alignment information than the load already has.
4396 if (LD->isUnindexed()) {
4397 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4398 if (Align > LD->getAlignment())
4399 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4400 Chain, Ptr, LD->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004401 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004402 LD->isVolatile(), Align);
4403 }
4404 }
4405
Evan Cheng45a7ca92007-05-01 00:38:21 +00004406
4407 // If load is not volatile and there are no uses of the loaded value (and
4408 // the updated indexed value in case of indexed loads), change uses of the
4409 // chain value into uses of the chain input (i.e. delete the dead load).
4410 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00004411 if (N->getValueType(1) == MVT::Other) {
4412 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004413 if (N->hasNUsesOfValue(0, 0)) {
4414 // It's not safe to use the two value CombineTo variant here. e.g.
4415 // v1, chain2 = load chain1, loc
4416 // v2, chain3 = load chain2, loc
4417 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004418 // Now we replace use of chain2 with chain1. This makes the second load
4419 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004420 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004421 DOUT << "\nWith chain: "; DEBUG(Chain.Val->dump(&DAG));
4422 DOUT << "\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004423 WorkListRemover DeadNodes(*this);
4424 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Chain, &DeadNodes);
Chris Lattner125991a2008-01-24 07:57:06 +00004425 if (N->use_empty()) {
4426 removeFromWorkList(N);
4427 DAG.DeleteNode(N);
4428 }
Evan Cheng02c42852008-01-16 23:11:54 +00004429 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
4430 }
Evan Cheng498f5592007-05-01 08:53:39 +00004431 } else {
4432 // Indexed loads.
4433 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4434 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Evan Cheng02c42852008-01-16 23:11:54 +00004435 SDOperand Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
4436 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
4437 DOUT << "\nWith: "; DEBUG(Undef.Val->dump(&DAG));
4438 DOUT << " and 2 other values\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004439 WorkListRemover DeadNodes(*this);
4440 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Undef, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004441 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1),
Chris Lattner4626b252008-01-17 07:20:38 +00004442 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004443 &DeadNodes);
4444 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004445 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00004446 DAG.DeleteNode(N);
4447 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004448 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004449 }
4450 }
Chris Lattner01a22022005-10-10 22:04:48 +00004451
4452 // If this load is directly stored, replace the load value with the stored
4453 // value.
4454 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004455 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohmanb061c4b2008-03-31 20:32:52 +00004456 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
4457 !LD->isVolatile()) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004458 if (ISD::isNON_TRUNCStore(Chain.Val)) {
4459 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4460 if (PrevST->getBasePtr() == Ptr &&
4461 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004462 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004463 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004464 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004465
Jim Laskey7ca56af2006-10-11 13:47:09 +00004466 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004467 // Walk up chain skipping non-aliasing memory nodes.
4468 SDOperand BetterChain = FindBetterChain(N, Chain);
4469
Jim Laskey6ff23e52006-10-04 16:53:27 +00004470 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004471 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004472 SDOperand ReplLoad;
4473
Jim Laskey279f0532006-09-25 16:29:54 +00004474 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004475 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4476 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004477 LD->getSrcValue(), LD->getSrcValueOffset(),
4478 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004479 } else {
4480 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4481 LD->getValueType(0),
4482 BetterChain, Ptr, LD->getSrcValue(),
4483 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004484 LD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004485 LD->isVolatile(),
4486 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004487 }
Jim Laskey279f0532006-09-25 16:29:54 +00004488
Jim Laskey6ff23e52006-10-04 16:53:27 +00004489 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00004490 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
4491 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004492
Jim Laskey274062c2006-10-13 23:32:28 +00004493 // Replace uses with load result and token factor. Don't add users
4494 // to work list.
4495 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004496 }
4497 }
4498
Evan Cheng7fc033a2006-11-03 03:06:21 +00004499 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004500 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00004501 return SDOperand(N, 0);
4502
Chris Lattner01a22022005-10-10 22:04:48 +00004503 return SDOperand();
4504}
4505
Chris Lattner07649d92008-01-08 23:08:06 +00004506
Chris Lattner87514ca2005-10-10 22:31:19 +00004507SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004508 StoreSDNode *ST = cast<StoreSDNode>(N);
4509 SDOperand Chain = ST->getChain();
4510 SDOperand Value = ST->getValue();
4511 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004512
Chris Lattner00161a62008-01-25 07:20:16 +00004513 // Try to infer better alignment information than the store already has.
4514 if (ST->isUnindexed()) {
4515 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4516 if (Align > ST->getAlignment())
4517 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004518 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004519 ST->isVolatile(), Align);
4520 }
4521 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004522
Evan Cheng59d5b682007-05-07 21:27:48 +00004523 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004524 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004525 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004526 ST->isUnindexed()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004527 unsigned Align = ST->getAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004528 MVT SVT = Value.getOperand(0).getValueType();
Evan Cheng59d5b682007-05-07 21:27:48 +00004529 unsigned OrigAlign = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004530 getABITypeAlignment(SVT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004531 if (Align <= OrigAlign &&
4532 ((!AfterLegalize && !ST->isVolatile()) ||
4533 TLI.isOperationLegal(ISD::STORE, SVT)))
Evan Cheng59d5b682007-05-07 21:27:48 +00004534 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004535 ST->getSrcValueOffset(), ST->isVolatile(), Align);
Jim Laskey279f0532006-09-25 16:29:54 +00004536 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004537
Nate Begeman2cbba892006-12-11 02:23:46 +00004538 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004539 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004540 // NOTE: If the original store is volatile, this transform must not increase
4541 // the number of stores. For example, on x86-32 an f64 can be stored in one
4542 // processor operation but an i64 (which is not legal) requires two. So the
4543 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00004544 if (Value.getOpcode() != ISD::TargetConstantFP) {
4545 SDOperand Tmp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004546 switch (CFP->getValueType(0).getSimpleVT()) {
Chris Lattner62be1a72006-12-12 04:16:14 +00004547 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004548 case MVT::f80: // We don't do this for these yet.
4549 case MVT::f128:
4550 case MVT::ppcf128:
4551 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004552 case MVT::f32:
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004553 if ((!AfterLegalize && !ST->isVolatile()) ||
4554 TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004555 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
4556 convertToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004557 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004558 ST->getSrcValueOffset(), ST->isVolatile(),
4559 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004560 }
4561 break;
4562 case MVT::f64:
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004563 if ((!AfterLegalize && !ST->isVolatile()) ||
4564 TLI.isOperationLegal(ISD::STORE, MVT::i64)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004565 Tmp = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
4566 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004567 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004568 ST->getSrcValueOffset(), ST->isVolatile(),
4569 ST->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004570 } else if (!ST->isVolatile() &&
4571 TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004572 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004573 // argument passing. Since this is so common, custom legalize the
4574 // 64-bit integer store into two 32-bit stores.
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004575 uint64_t Val = CFP->getValueAPF().convertToAPInt().getZExtValue();
Chris Lattner62be1a72006-12-12 04:16:14 +00004576 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4577 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00004578 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00004579
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004580 int SVOffset = ST->getSrcValueOffset();
4581 unsigned Alignment = ST->getAlignment();
4582 bool isVolatile = ST->isVolatile();
4583
Chris Lattner62be1a72006-12-12 04:16:14 +00004584 SDOperand St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004585 ST->getSrcValueOffset(),
4586 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004587 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4588 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004589 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004590 Alignment = MinAlign(Alignment, 4U);
Chris Lattner62be1a72006-12-12 04:16:14 +00004591 SDOperand St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004592 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004593 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4594 }
4595 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004596 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004597 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004598 }
4599
Jim Laskey279f0532006-09-25 16:29:54 +00004600 if (CombinerAA) {
4601 // Walk up chain skipping non-aliasing memory nodes.
4602 SDOperand BetterChain = FindBetterChain(N, Chain);
4603
Jim Laskey6ff23e52006-10-04 16:53:27 +00004604 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004605 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004606 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004607 SDOperand ReplStore;
4608 if (ST->isTruncatingStore()) {
4609 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004610 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004611 ST->getMemoryVT(),
Chris Lattner4626b252008-01-17 07:20:38 +00004612 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004613 } else {
4614 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004615 ST->getSrcValue(), ST->getSrcValueOffset(),
4616 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004617 }
4618
Jim Laskey279f0532006-09-25 16:29:54 +00004619 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00004620 SDOperand Token =
4621 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4622
4623 // Don't add users to work list.
4624 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004625 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004626 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004627
Evan Cheng33dbedc2006-11-05 09:31:14 +00004628 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004629 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00004630 return SDOperand(N, 0);
4631
Chris Lattner3c872852007-12-29 06:26:16 +00004632 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00004633 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004634 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00004635 // See if we can simplify the input to this truncstore with knowledge that
4636 // only the low bits are being used. For example:
4637 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
4638 SDOperand Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004639 GetDemandedBits(Value,
4640 APInt::getLowBitsSet(Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004641 ST->getMemoryVT().getSizeInBits()));
Chris Lattner2b4c2792007-10-13 06:35:54 +00004642 AddToWorkList(Value.Val);
4643 if (Shorter.Val)
4644 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004645 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner2b4c2792007-10-13 06:35:54 +00004646 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004647
4648 // Otherwise, see if we can simplify the operation with
4649 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00004650 if (SimplifyDemandedBits(Value,
4651 APInt::getLowBitsSet(
4652 Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004653 ST->getMemoryVT().getSizeInBits())))
Chris Lattnere33544c2007-10-13 06:58:48 +00004654 return SDOperand(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004655 }
4656
Chris Lattner3c872852007-12-29 06:26:16 +00004657 // If this is a load followed by a store to the same location, then the store
4658 // is dead/noop.
4659 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004660 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004661 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00004662 // There can't be any side effects between the load and store, such as
4663 // a call or store.
Chris Lattner572dee72008-01-16 05:49:24 +00004664 Chain.reachesChainWithoutSideEffects(SDOperand(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00004665 // The store is dead, remove it.
4666 return Chain;
4667 }
4668 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004669
Chris Lattnerddf89562008-01-17 19:59:44 +00004670 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4671 // truncating store. We can do this even if this is already a truncstore.
4672 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004673 && Value.Val->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004674 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004675 ST->getMemoryVT())) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004676 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004677 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnerddf89562008-01-17 19:59:44 +00004678 ST->isVolatile(), ST->getAlignment());
4679 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004680
Chris Lattner87514ca2005-10-10 22:31:19 +00004681 return SDOperand();
4682}
4683
Chris Lattnerca242442006-03-19 01:27:56 +00004684SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4685 SDOperand InVec = N->getOperand(0);
4686 SDOperand InVal = N->getOperand(1);
4687 SDOperand EltNo = N->getOperand(2);
4688
4689 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4690 // vector with the inserted element.
4691 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
4692 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004693 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004694 if (Elt < Ops.size())
4695 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004696 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4697 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004698 }
4699
4700 return SDOperand();
4701}
4702
Evan Cheng513da432007-10-06 08:19:55 +00004703SDOperand DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004704 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
4705 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
4706 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
4707
4708 // Perform only after legalization to ensure build_vector / vector_shuffle
4709 // optimizations have already been done.
4710 if (!AfterLegalize) return SDOperand();
4711
Evan Cheng513da432007-10-06 08:19:55 +00004712 SDOperand InVec = N->getOperand(0);
4713 SDOperand EltNo = N->getOperand(1);
4714
Evan Cheng513da432007-10-06 08:19:55 +00004715 if (isa<ConstantSDNode>(EltNo)) {
4716 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
4717 bool NewLoad = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004718 MVT VT = InVec.getValueType();
4719 MVT EVT = VT.getVectorElementType();
4720 MVT LVT = EVT;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004721 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004722 MVT BCVT = InVec.getOperand(0).getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00004723 if (!BCVT.isVector() || EVT.bitsGT(BCVT.getVectorElementType()))
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004724 return SDOperand();
4725 InVec = InVec.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004726 EVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004727 NewLoad = true;
4728 }
Evan Cheng513da432007-10-06 08:19:55 +00004729
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004730 LoadSDNode *LN0 = NULL;
4731 if (ISD::isNormalLoad(InVec.Val))
4732 LN0 = cast<LoadSDNode>(InVec);
4733 else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4734 InVec.getOperand(0).getValueType() == EVT &&
4735 ISD::isNormalLoad(InVec.getOperand(0).Val)) {
4736 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4737 } else if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE) {
4738 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
4739 // =>
4740 // (load $addr+1*size)
4741 unsigned Idx = cast<ConstantSDNode>(InVec.getOperand(2).
4742 getOperand(Elt))->getValue();
4743 unsigned NumElems = InVec.getOperand(2).getNumOperands();
4744 InVec = (Idx < NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
4745 if (InVec.getOpcode() == ISD::BIT_CONVERT)
4746 InVec = InVec.getOperand(0);
4747 if (ISD::isNormalLoad(InVec.Val)) {
4748 LN0 = cast<LoadSDNode>(InVec);
4749 Elt = (Idx < NumElems) ? Idx : Idx - NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00004750 }
4751 }
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004752 if (!LN0 || !LN0->hasOneUse())
4753 return SDOperand();
4754
4755 unsigned Align = LN0->getAlignment();
4756 if (NewLoad) {
4757 // Check the resultant load doesn't need a higher alignment than the
4758 // original load.
4759 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004760 getABITypeAlignment(LVT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004761 if (NewAlign > Align ||
4762 (AfterLegalize && !TLI.isOperationLegal(ISD::LOAD, LVT)))
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004763 return SDOperand();
4764 Align = NewAlign;
4765 }
4766
4767 SDOperand NewPtr = LN0->getBasePtr();
4768 if (Elt) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004769 unsigned PtrOff = LVT.getSizeInBits() * Elt / 8;
4770 MVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004771 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00004772 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004773 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
4774 DAG.getConstant(PtrOff, PtrType));
4775 }
4776 return DAG.getLoad(LVT, LN0->getChain(), NewPtr,
4777 LN0->getSrcValue(), LN0->getSrcValueOffset(),
4778 LN0->isVolatile(), Align);
Evan Cheng513da432007-10-06 08:19:55 +00004779 }
4780 return SDOperand();
4781}
4782
4783
Dan Gohman7f321562007-06-25 16:23:39 +00004784SDOperand DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
4785 unsigned NumInScalars = N->getNumOperands();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004786 MVT VT = N->getValueType(0);
4787 unsigned NumElts = VT.getVectorNumElements();
4788 MVT EltType = VT.getVectorElementType();
Chris Lattnerca242442006-03-19 01:27:56 +00004789
Dan Gohman7f321562007-06-25 16:23:39 +00004790 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
4791 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
4792 // at most two distinct vectors, turn this into a shuffle node.
Chris Lattnerd7648c82006-03-28 20:28:38 +00004793 SDOperand VecIn1, VecIn2;
4794 for (unsigned i = 0; i != NumInScalars; ++i) {
4795 // Ignore undef inputs.
4796 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4797
Dan Gohman7f321562007-06-25 16:23:39 +00004798 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00004799 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00004800 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00004801 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
4802 VecIn1 = VecIn2 = SDOperand(0, 0);
4803 break;
4804 }
4805
Dan Gohman7f321562007-06-25 16:23:39 +00004806 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00004807 // we can't make a shuffle.
4808 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004809 if (ExtractedFromVec.getValueType() != VT) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004810 VecIn1 = VecIn2 = SDOperand(0, 0);
4811 break;
4812 }
4813
4814 // Otherwise, remember this. We allow up to two distinct input vectors.
4815 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
4816 continue;
4817
4818 if (VecIn1.Val == 0) {
4819 VecIn1 = ExtractedFromVec;
4820 } else if (VecIn2.Val == 0) {
4821 VecIn2 = ExtractedFromVec;
4822 } else {
4823 // Too many inputs.
4824 VecIn1 = VecIn2 = SDOperand(0, 0);
4825 break;
4826 }
4827 }
4828
4829 // If everything is good, we can make a shuffle operation.
4830 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004831 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004832 for (unsigned i = 0; i != NumInScalars; ++i) {
4833 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00004834 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004835 continue;
4836 }
4837
4838 SDOperand Extract = N->getOperand(i);
4839
4840 // If extracting from the first vector, just use the index directly.
4841 if (Extract.getOperand(0) == VecIn1) {
4842 BuildVecIndices.push_back(Extract.getOperand(1));
4843 continue;
4844 }
4845
4846 // Otherwise, use InIdx + VecSize
4847 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004848 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004849 }
4850
4851 // Add count and size info.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004852 MVT BuildVecVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004853
Dan Gohman7f321562007-06-25 16:23:39 +00004854 // Return the new VECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004855 SDOperand Ops[5];
4856 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00004857 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004858 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00004859 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004860 // Use an undef build_vector as input for the second operand.
Chris Lattnercef896e2006-03-28 22:19:47 +00004861 std::vector<SDOperand> UnOps(NumInScalars,
4862 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00004863 EltType));
4864 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004865 &UnOps[0], UnOps.size());
4866 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00004867 }
Dan Gohman7f321562007-06-25 16:23:39 +00004868 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004869 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004870 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004871 }
4872
4873 return SDOperand();
4874}
4875
Dan Gohman7f321562007-06-25 16:23:39 +00004876SDOperand DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
4877 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
4878 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
4879 // inputs come from at most two distinct vectors, turn this into a shuffle
4880 // node.
4881
4882 // If we only have one input vector, we don't need to do any concatenation.
4883 if (N->getNumOperands() == 1) {
4884 return N->getOperand(0);
4885 }
4886
4887 return SDOperand();
4888}
4889
Chris Lattner66445d32006-03-28 22:11:53 +00004890SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004891 SDOperand ShufMask = N->getOperand(2);
4892 unsigned NumElts = ShufMask.getNumOperands();
4893
4894 // If the shuffle mask is an identity operation on the LHS, return the LHS.
4895 bool isIdentity = true;
4896 for (unsigned i = 0; i != NumElts; ++i) {
4897 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4898 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
4899 isIdentity = false;
4900 break;
4901 }
4902 }
4903 if (isIdentity) return N->getOperand(0);
4904
4905 // If the shuffle mask is an identity operation on the RHS, return the RHS.
4906 isIdentity = true;
4907 for (unsigned i = 0; i != NumElts; ++i) {
4908 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4909 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
4910 isIdentity = false;
4911 break;
4912 }
4913 }
4914 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00004915
4916 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
4917 // needed at all.
4918 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00004919 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004920 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00004921 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004922 for (unsigned i = 0; i != NumElts; ++i)
4923 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
4924 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
4925 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00004926 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00004927 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00004928 BaseIdx = Idx;
4929 } else {
4930 if (BaseIdx != Idx)
4931 isSplat = false;
4932 if (VecNum != V) {
4933 isUnary = false;
4934 break;
4935 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00004936 }
4937 }
4938
4939 SDOperand N0 = N->getOperand(0);
4940 SDOperand N1 = N->getOperand(1);
4941 // Normalize unary shuffle so the RHS is undef.
4942 if (isUnary && VecNum == 1)
4943 std::swap(N0, N1);
4944
Evan Cheng917ec982006-07-21 08:25:53 +00004945 // If it is a splat, check if the argument vector is a build_vector with
4946 // all scalar elements the same.
4947 if (isSplat) {
4948 SDNode *V = N0.Val;
Evan Cheng917ec982006-07-21 08:25:53 +00004949
Dan Gohman7f321562007-06-25 16:23:39 +00004950 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00004951 // not the number of vector elements, look through it. Be careful not to
4952 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00004953 if (V->getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng59569222006-10-16 22:49:37 +00004954 SDOperand ConvInput = V->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004955 if (ConvInput.getValueType().getVectorNumElements() == NumElts)
Evan Cheng59569222006-10-16 22:49:37 +00004956 V = ConvInput.Val;
4957 }
4958
Dan Gohman7f321562007-06-25 16:23:39 +00004959 if (V->getOpcode() == ISD::BUILD_VECTOR) {
4960 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00004961 if (NumElems > BaseIdx) {
4962 SDOperand Base;
4963 bool AllSame = true;
4964 for (unsigned i = 0; i != NumElems; ++i) {
4965 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
4966 Base = V->getOperand(i);
4967 break;
4968 }
4969 }
4970 // Splat of <u, u, u, u>, return <u, u, u, u>
4971 if (!Base.Val)
4972 return N0;
4973 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00004974 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00004975 AllSame = false;
4976 break;
4977 }
4978 }
4979 // Splat of <x, x, x, x>, return <x, x, x, x>
4980 if (AllSame)
4981 return N0;
4982 }
4983 }
4984 }
4985
Evan Chenge7bec0d2006-07-20 22:44:41 +00004986 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
4987 // into an undef.
4988 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00004989 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
4990 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004991 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00004992 for (unsigned i = 0; i != NumElts; ++i) {
4993 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
4994 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
4995 MappedOps.push_back(ShufMask.getOperand(i));
4996 } else {
4997 unsigned NewIdx =
4998 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
4999 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
5000 }
5001 }
Dan Gohman7f321562007-06-25 16:23:39 +00005002 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005003 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00005004 AddToWorkList(ShufMask.Val);
Dan Gohman7f321562007-06-25 16:23:39 +00005005 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
5006 N0,
5007 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
5008 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00005009 }
Dan Gohman7f321562007-06-25 16:23:39 +00005010
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005011 return SDOperand();
5012}
5013
Evan Cheng44f1f092006-04-20 08:56:16 +00005014/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00005015/// an AND to a vector_shuffle with the destination vector and a zero vector.
5016/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00005017/// vector_shuffle V, Zero, <0, 4, 2, 4>
5018SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
5019 SDOperand LHS = N->getOperand(0);
5020 SDOperand RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00005021 if (N->getOpcode() == ISD::AND) {
5022 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00005023 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005024 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Evan Cheng44f1f092006-04-20 08:56:16 +00005025 std::vector<SDOperand> IdxOps;
5026 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00005027 unsigned NumElts = NumOps;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005028 MVT EVT = RHS.getValueType().getVectorElementType();
Evan Cheng44f1f092006-04-20 08:56:16 +00005029 for (unsigned i = 0; i != NumElts; ++i) {
5030 SDOperand Elt = RHS.getOperand(i);
5031 if (!isa<ConstantSDNode>(Elt))
5032 return SDOperand();
5033 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
5034 IdxOps.push_back(DAG.getConstant(i, EVT));
5035 else if (cast<ConstantSDNode>(Elt)->isNullValue())
5036 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
5037 else
5038 return SDOperand();
5039 }
5040
5041 // Let's see if the target supports this vector_shuffle.
5042 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
5043 return SDOperand();
5044
Dan Gohman7f321562007-06-25 16:23:39 +00005045 // Return the new VECTOR_SHUFFLE node.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005046 MVT VT = MVT::getVectorVT(EVT, NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00005047 std::vector<SDOperand> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005048 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00005049 Ops.push_back(LHS);
5050 AddToWorkList(LHS.Val);
5051 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00005052 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005053 &ZeroOps[0], ZeroOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00005054 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005055 &IdxOps[0], IdxOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00005056 SDOperand Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005057 &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00005058 if (VT != LHS.getValueType()) {
5059 Result = DAG.getNode(ISD::BIT_CONVERT, LHS.getValueType(), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00005060 }
5061 return Result;
5062 }
5063 }
5064 return SDOperand();
5065}
5066
Dan Gohman7f321562007-06-25 16:23:39 +00005067/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
5068SDOperand DAGCombiner::SimplifyVBinOp(SDNode *N) {
5069 // After legalize, the target may be depending on adds and other
5070 // binary ops to provide legal ways to construct constants or other
5071 // things. Simplifying them may result in a loss of legality.
5072 if (AfterLegalize) return SDOperand();
5073
Duncan Sands83ec4b62008-06-06 12:08:01 +00005074 MVT VT = N->getValueType(0);
5075 assert(VT.isVector() && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00005076
Duncan Sands83ec4b62008-06-06 12:08:01 +00005077 MVT EltType = VT.getVectorElementType();
Chris Lattneredab1b92006-04-02 03:25:57 +00005078 SDOperand LHS = N->getOperand(0);
5079 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00005080 SDOperand Shuffle = XformToShuffleWithZero(N);
5081 if (Shuffle.Val) return Shuffle;
5082
Dan Gohman7f321562007-06-25 16:23:39 +00005083 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00005084 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00005085 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
5086 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005087 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005088 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Chris Lattneredab1b92006-04-02 03:25:57 +00005089 SDOperand LHSOp = LHS.getOperand(i);
5090 SDOperand RHSOp = RHS.getOperand(i);
5091 // If these two elements can't be folded, bail out.
5092 if ((LHSOp.getOpcode() != ISD::UNDEF &&
5093 LHSOp.getOpcode() != ISD::Constant &&
5094 LHSOp.getOpcode() != ISD::ConstantFP) ||
5095 (RHSOp.getOpcode() != ISD::UNDEF &&
5096 RHSOp.getOpcode() != ISD::Constant &&
5097 RHSOp.getOpcode() != ISD::ConstantFP))
5098 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00005099 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00005100 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
5101 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00005102 if ((RHSOp.getOpcode() == ISD::Constant &&
5103 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
5104 (RHSOp.getOpcode() == ISD::ConstantFP &&
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00005105 cast<ConstantFPSDNode>(RHSOp.Val)->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00005106 break;
5107 }
Dan Gohman7f321562007-06-25 16:23:39 +00005108 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00005109 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00005110 assert((Ops.back().getOpcode() == ISD::UNDEF ||
5111 Ops.back().getOpcode() == ISD::Constant ||
5112 Ops.back().getOpcode() == ISD::ConstantFP) &&
5113 "Scalar binop didn't fold!");
5114 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005115
Dan Gohman7f321562007-06-25 16:23:39 +00005116 if (Ops.size() == LHS.getNumOperands()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005117 MVT VT = LHS.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00005118 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005119 }
Chris Lattneredab1b92006-04-02 03:25:57 +00005120 }
5121
5122 return SDOperand();
5123}
5124
Nate Begeman44728a72005-09-19 22:34:01 +00005125SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00005126 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
5127
5128 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
5129 cast<CondCodeSDNode>(N0.getOperand(2))->get());
5130 // If we got a simplified select_cc node back from SimplifySelectCC, then
5131 // break it down into a new SETCC node, and a new SELECT node, and then return
5132 // the SELECT node, since we were called with a SELECT node.
5133 if (SCC.Val) {
5134 // Check to see if we got a select_cc back (to turn into setcc/select).
5135 // Otherwise, just return whatever node we got back, like fabs.
5136 if (SCC.getOpcode() == ISD::SELECT_CC) {
5137 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
5138 SCC.getOperand(0), SCC.getOperand(1),
5139 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00005140 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005141 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
5142 SCC.getOperand(3), SETCC);
5143 }
5144 return SCC;
5145 }
Nate Begeman44728a72005-09-19 22:34:01 +00005146 return SDOperand();
5147}
5148
Chris Lattner40c62d52005-10-18 06:04:22 +00005149/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
5150/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00005151/// select. Callers of this should assume that TheSelect is deleted if this
5152/// returns true. As such, they should return the appropriate thing (e.g. the
5153/// node) back to the top-level of the DAG combiner loop to avoid it being
5154/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00005155///
5156bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
5157 SDOperand RHS) {
5158
5159 // If this is a select from two identical things, try to pull the operation
5160 // through the select.
5161 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00005162 // If this is a load and the token chain is identical, replace the select
5163 // of two loads with a load through a select of the address to load from.
5164 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
5165 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00005166 if (LHS.getOpcode() == ISD::LOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005167 // Do not let this transformation reduce the number of volatile loads.
5168 !cast<LoadSDNode>(LHS)->isVolatile() &&
5169 !cast<LoadSDNode>(RHS)->isVolatile() &&
Chris Lattner40c62d52005-10-18 06:04:22 +00005170 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00005171 LHS.getOperand(0) == RHS.getOperand(0)) {
5172 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5173 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5174
5175 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005176 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00005177 // FIXME: this conflates two src values, discarding one. This is not
5178 // the right thing to do, but nothing uses srcvalues now. When they do,
5179 // turn SrcValue into a list of locations.
5180 SDOperand Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005181 if (TheSelect->getOpcode() == ISD::SELECT) {
5182 // Check that the condition doesn't reach either load. If so, folding
5183 // this will induce a cycle into the DAG.
Evan Cheng917be682008-03-04 00:41:45 +00005184 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5185 !RLD->isPredecessorOf(TheSelect->getOperand(0).Val)) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005186 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
5187 TheSelect->getOperand(0), LLD->getBasePtr(),
5188 RLD->getBasePtr());
5189 }
5190 } else {
5191 // Check that the condition doesn't reach either load. If so, folding
5192 // this will induce a cycle into the DAG.
Evan Cheng917be682008-03-04 00:41:45 +00005193 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5194 !RLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5195 !LLD->isPredecessorOf(TheSelect->getOperand(1).Val) &&
5196 !RLD->isPredecessorOf(TheSelect->getOperand(1).Val)) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005197 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00005198 TheSelect->getOperand(0),
5199 TheSelect->getOperand(1),
5200 LLD->getBasePtr(), RLD->getBasePtr(),
5201 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005202 }
Evan Cheng466685d2006-10-09 20:57:25 +00005203 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005204
5205 if (Addr.Val) {
5206 SDOperand Load;
5207 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
5208 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
5209 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005210 LLD->getSrcValueOffset(),
5211 LLD->isVolatile(),
5212 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005213 else {
5214 Load = DAG.getExtLoad(LLD->getExtensionType(),
5215 TheSelect->getValueType(0),
5216 LLD->getChain(), Addr, LLD->getSrcValue(),
5217 LLD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005218 LLD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005219 LLD->isVolatile(),
5220 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005221 }
5222 // Users of the select now use the result of the load.
5223 CombineTo(TheSelect, Load);
5224
5225 // Users of the old loads now use the new load's chain. We know the
5226 // old-load value is dead now.
5227 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
5228 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
5229 return true;
5230 }
Evan Chengc5484282006-10-04 00:56:09 +00005231 }
Chris Lattner40c62d52005-10-18 06:04:22 +00005232 }
5233 }
5234
5235 return false;
5236}
5237
Nate Begeman44728a72005-09-19 22:34:01 +00005238SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
5239 SDOperand N2, SDOperand N3,
Chris Lattner1eba01e2007-04-11 06:50:51 +00005240 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00005241
Duncan Sands83ec4b62008-06-06 12:08:01 +00005242 MVT VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00005243 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
5244 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
5245 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
5246
5247 // Determine if the condition we're dealing with is constant
Scott Michel5b8f82e2008-03-10 15:42:14 +00005248 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00005249 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005250 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
5251
5252 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00005253 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005254 return N2;
5255 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00005256 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005257 return N3;
5258
5259 // Check to see if we can simplify the select into an fabs node
5260 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5261 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00005262 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005263 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5264 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5265 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5266 N2 == N3.getOperand(0))
5267 return DAG.getNode(ISD::FABS, VT, N0);
5268
5269 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5270 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5271 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5272 N2.getOperand(0) == N3)
5273 return DAG.getNode(ISD::FABS, VT, N3);
5274 }
5275 }
5276
5277 // Check to see if we can perform the "gzip trick", transforming
5278 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00005279 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005280 N0.getValueType().isInteger() &&
5281 N2.getValueType().isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005282 (N1C->isNullValue() || // (a < 0) ? b : 0
5283 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Duncan Sands83ec4b62008-06-06 12:08:01 +00005284 MVT XType = N0.getValueType();
5285 MVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00005286 if (XType.bitsGE(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005287 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00005288 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00005289 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
5290 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005291 ShCtV = XType.getSizeInBits()-ShCtV-1;
Nate Begemanf845b452005-10-08 00:29:44 +00005292 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5293 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00005294 AddToWorkList(Shift.Val);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005295 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005296 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005297 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005298 }
5299 return DAG.getNode(ISD::AND, AType, Shift, N2);
5300 }
5301 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005302 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005303 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00005304 AddToWorkList(Shift.Val);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005305 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005306 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005307 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005308 }
5309 return DAG.getNode(ISD::AND, AType, Shift, N2);
5310 }
5311 }
Nate Begeman07ed4172005-10-10 21:26:48 +00005312
5313 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00005314 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Nate Begeman07ed4172005-10-10 21:26:48 +00005315 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00005316
5317 // If the caller doesn't want us to simplify this into a zext of a compare,
5318 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00005319 if (NotExtCompare && N2C->getAPIntValue() == 1)
Chris Lattner1eba01e2007-04-11 06:50:51 +00005320 return SDOperand();
5321
Nate Begeman07ed4172005-10-10 21:26:48 +00005322 // Get a SetCC of the condition
5323 // FIXME: Should probably make sure that setcc is legal if we ever have a
5324 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00005325 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00005326 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00005327 if (AfterLegalize) {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005328 SCC = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005329 if (N2.getValueType().bitsLT(SCC.getValueType()))
Chris Lattner555d8d62006-12-07 22:36:47 +00005330 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5331 else
5332 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005333 } else {
5334 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00005335 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005336 }
Chris Lattner5750df92006-03-01 04:03:14 +00005337 AddToWorkList(SCC.Val);
5338 AddToWorkList(Temp.Val);
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005339
Dan Gohman002e5d02008-03-13 22:13:53 +00005340 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005341 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00005342 // shl setcc result by log2 n2c
5343 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00005344 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Nate Begeman07ed4172005-10-10 21:26:48 +00005345 TLI.getShiftAmountTy()));
5346 }
5347
Nate Begemanf845b452005-10-08 00:29:44 +00005348 // Check to see if this is the equivalent of setcc
5349 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5350 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00005351 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005352 MVT XType = N0.getValueType();
Scott Michel5b8f82e2008-03-10 15:42:14 +00005353 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(N0))) {
5354 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00005355 if (Res.getValueType() != VT)
5356 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5357 return Res;
5358 }
5359
5360 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5361 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
5362 TLI.isOperationLegal(ISD::CTLZ, XType)) {
5363 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
5364 return DAG.getNode(ISD::SRL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005365 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Nate Begemanf845b452005-10-08 00:29:44 +00005366 TLI.getShiftAmountTy()));
5367 }
5368 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5369 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
5370 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
5371 N0);
5372 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
5373 DAG.getConstant(~0ULL, XType));
5374 return DAG.getNode(ISD::SRL, XType,
5375 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00005376 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005377 TLI.getShiftAmountTy()));
5378 }
5379 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5380 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
5381 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005382 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005383 TLI.getShiftAmountTy()));
5384 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5385 }
5386 }
5387
5388 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5389 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5390 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005391 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005392 N2.getOperand(0) == N1 && N0.getValueType().isInteger()) {
5393 MVT XType = N0.getValueType();
Chris Lattner1982ef22007-04-11 05:11:38 +00005394 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005395 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005396 TLI.getShiftAmountTy()));
5397 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
5398 AddToWorkList(Shift.Val);
5399 AddToWorkList(Add.Val);
5400 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5401 }
5402 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5403 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5404 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5405 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5406 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005407 MVT XType = N0.getValueType();
5408 if (SubC->isNullValue() && XType.isInteger()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005409 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005410 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005411 TLI.getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00005412 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005413 AddToWorkList(Shift.Val);
5414 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005415 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5416 }
5417 }
5418 }
Chris Lattner1982ef22007-04-11 05:11:38 +00005419
Nate Begeman44728a72005-09-19 22:34:01 +00005420 return SDOperand();
5421}
5422
Evan Chengfa1eb272007-02-08 22:13:59 +00005423/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005424SDOperand DAGCombiner::SimplifySetCC(MVT VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00005425 SDOperand N1, ISD::CondCode Cond,
5426 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00005427 TargetLowering::DAGCombinerInfo
5428 DagCombineInfo(DAG, !AfterLegalize, false, this);
5429 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00005430}
5431
Nate Begeman69575232005-10-20 02:15:44 +00005432/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5433/// return a DAG expression to select that will generate the same value by
5434/// multiplying by a magic number. See:
5435/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5436SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005437 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005438 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
5439
Andrew Lenharth232c9102006-06-12 16:07:18 +00005440 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005441 ii != ee; ++ii)
5442 AddToWorkList(*ii);
5443 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005444}
5445
5446/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5447/// return a DAG expression to select that will generate the same value by
5448/// multiplying by a magic number. See:
5449/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5450SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005451 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005452 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00005453
Andrew Lenharth232c9102006-06-12 16:07:18 +00005454 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005455 ii != ee; ++ii)
5456 AddToWorkList(*ii);
5457 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005458}
5459
Jim Laskey71382342006-10-07 23:37:56 +00005460/// FindBaseOffset - Return true if base is known not to alias with anything
5461/// but itself. Provides base object and offset as results.
5462static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
5463 // Assume it is a primitive operation.
5464 Base = Ptr; Offset = 0;
5465
5466 // If it's an adding a simple constant then integrate the offset.
5467 if (Base.getOpcode() == ISD::ADD) {
5468 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5469 Base = Base.getOperand(0);
5470 Offset += C->getValue();
5471 }
5472 }
5473
5474 // If it's any of the following then it can't alias with anything but itself.
5475 return isa<FrameIndexSDNode>(Base) ||
5476 isa<ConstantPoolSDNode>(Base) ||
5477 isa<GlobalAddressSDNode>(Base);
5478}
5479
5480/// isAlias - Return true if there is any possibility that the two addresses
5481/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00005482bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
5483 const Value *SrcValue1, int SrcValueOffset1,
5484 SDOperand Ptr2, int64_t Size2,
5485 const Value *SrcValue2, int SrcValueOffset2)
5486{
Jim Laskey71382342006-10-07 23:37:56 +00005487 // If they are the same then they must be aliases.
5488 if (Ptr1 == Ptr2) return true;
5489
5490 // Gather base node and offset information.
5491 SDOperand Base1, Base2;
5492 int64_t Offset1, Offset2;
5493 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5494 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5495
5496 // If they have a same base address then...
5497 if (Base1 == Base2) {
5498 // Check to see if the addresses overlap.
5499 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5500 }
5501
Jim Laskey096c22e2006-10-18 12:29:57 +00005502 // If we know both bases then they can't alias.
5503 if (KnownBase1 && KnownBase2) return false;
5504
Jim Laskey07a27092006-10-18 19:08:31 +00005505 if (CombinerGlobalAA) {
5506 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00005507 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5508 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5509 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00005510 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00005511 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00005512 if (AAResult == AliasAnalysis::NoAlias)
5513 return false;
5514 }
Jim Laskey096c22e2006-10-18 12:29:57 +00005515
5516 // Otherwise we have to assume they alias.
5517 return true;
Jim Laskey71382342006-10-07 23:37:56 +00005518}
5519
5520/// FindAliasInfo - Extracts the relevant alias information from the memory
5521/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00005522bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00005523 SDOperand &Ptr, int64_t &Size,
5524 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005525 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5526 Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005527 Size = LD->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005528 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005529 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00005530 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005531 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005532 Ptr = ST->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005533 Size = ST->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005534 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005535 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00005536 } else {
Jim Laskey71382342006-10-07 23:37:56 +00005537 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005538 }
5539
5540 return false;
5541}
5542
Jim Laskey6ff23e52006-10-04 16:53:27 +00005543/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5544/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00005545void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005546 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005547 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005548 std::set<SDNode *> Visited; // Visited node set.
5549
Jim Laskey279f0532006-09-25 16:29:54 +00005550 // Get alias information for node.
5551 SDOperand Ptr;
5552 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005553 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005554 int SrcValueOffset;
5555 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005556
Jim Laskey6ff23e52006-10-04 16:53:27 +00005557 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005558 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005559
Jim Laskeybc588b82006-10-05 15:07:25 +00005560 // Look at each chain and determine if it is an alias. If so, add it to the
5561 // aliases list. If not, then continue up the chain looking for the next
5562 // candidate.
5563 while (!Chains.empty()) {
5564 SDOperand Chain = Chains.back();
5565 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005566
Jim Laskeybc588b82006-10-05 15:07:25 +00005567 // Don't bother if we've been before.
5568 if (Visited.find(Chain.Val) != Visited.end()) continue;
5569 Visited.insert(Chain.Val);
5570
5571 switch (Chain.getOpcode()) {
5572 case ISD::EntryToken:
5573 // Entry token is ideal chain operand, but handled in FindBetterChain.
5574 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005575
Jim Laskeybc588b82006-10-05 15:07:25 +00005576 case ISD::LOAD:
5577 case ISD::STORE: {
5578 // Get alias information for Chain.
5579 SDOperand OpPtr;
5580 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005581 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005582 int OpSrcValueOffset;
5583 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
5584 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005585
5586 // If chain is alias then stop here.
5587 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005588 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5589 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005590 Aliases.push_back(Chain);
5591 } else {
5592 // Look further up the chain.
5593 Chains.push_back(Chain.getOperand(0));
5594 // Clean up old chain.
5595 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00005596 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005597 break;
5598 }
5599
5600 case ISD::TokenFactor:
5601 // We have to check each of the operands of the token factor, so we queue
5602 // then up. Adding the operands to the queue (stack) in reverse order
5603 // maintains the original order and increases the likelihood that getNode
5604 // will find a matching token factor (CSE.)
5605 for (unsigned n = Chain.getNumOperands(); n;)
5606 Chains.push_back(Chain.getOperand(--n));
5607 // Eliminate the token factor if we can.
5608 AddToWorkList(Chain.Val);
5609 break;
5610
5611 default:
5612 // For all other instructions we will just have to take what we can get.
5613 Aliases.push_back(Chain);
5614 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005615 }
5616 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005617}
5618
5619/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5620/// for a better chain (aliasing node.)
5621SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
5622 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005623
Jim Laskey6ff23e52006-10-04 16:53:27 +00005624 // Accumulate all the aliases to this node.
5625 GatherAllAliases(N, OldChain, Aliases);
5626
5627 if (Aliases.size() == 0) {
5628 // If no operands then chain to entry token.
5629 return DAG.getEntryNode();
5630 } else if (Aliases.size() == 1) {
5631 // If a single operand then chain to it. We don't need to revisit it.
5632 return Aliases[0];
5633 }
5634
5635 // Construct a custom tailored token factor.
5636 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5637 &Aliases[0], Aliases.size());
5638
5639 // Make sure the old chain gets cleaned up.
5640 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
5641
5642 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005643}
5644
Nate Begeman1d4d4142005-09-01 00:19:25 +00005645// SelectionDAG::Combine - This is the entry point for the file.
5646//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005647void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Chris Lattner938ab022007-01-16 04:55:25 +00005648 if (!RunningAfterLegalize && ViewDAGCombine1)
5649 viewGraph();
5650 if (RunningAfterLegalize && ViewDAGCombine2)
5651 viewGraph();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005652 /// run - This is the main entry point to this class.
5653 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005654 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005655}