blob: 14efb446feade3021fb1d197bca49d6a86b750a3 [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.
Dan Gohmancf8462f2008-06-30 21:04:06 +0000595 WorkList.reserve(DAG.allnodes_size());
Chris Lattnerde202b32005-11-09 23:47:37 +0000596 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
597 E = DAG.allnodes_end(); I != E; ++I)
598 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000599
Chris Lattner95038592005-10-05 06:35:28 +0000600 // Create a dummy node (which is not added to allnodes), that adds a reference
601 // to the root node, preventing it from being deleted, and tracking any
602 // changes of the root.
603 HandleSDNode Dummy(DAG.getRoot());
604
Jim Laskey26f7fa72006-10-17 19:33:52 +0000605 // The root of the dag may dangle to deleted nodes until the dag combiner is
606 // done. Set it to null to avoid confusion.
607 DAG.setRoot(SDOperand());
Chris Lattner24664722006-03-01 04:53:38 +0000608
Nate Begeman1d4d4142005-09-01 00:19:25 +0000609 // while the worklist isn't empty, inspect the node on the end of it and
610 // try and combine it.
611 while (!WorkList.empty()) {
612 SDNode *N = WorkList.back();
613 WorkList.pop_back();
614
615 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000616 // N is deleted from the DAG, since they too may now be dead or may have a
617 // reduced number of uses, allowing other xforms.
618 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000619 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000620 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000621
Chris Lattner95038592005-10-05 06:35:28 +0000622 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000623 continue;
624 }
625
Dan Gohman389079b2007-10-08 17:57:15 +0000626 SDOperand RV = combine(N);
Chris Lattner24664722006-03-01 04:53:38 +0000627
Chris Lattner50d8e492008-01-25 23:34:24 +0000628 if (RV.Val == 0)
629 continue;
630
631 ++NodesCombined;
Chris Lattner5eee4272008-01-26 01:09:19 +0000632
Chris Lattner50d8e492008-01-25 23:34:24 +0000633 // If we get back the same node we passed in, rather than a new node or
634 // zero, we know that the node must have defined multiple values and
635 // CombineTo was used. Since CombineTo takes care of the worklist
636 // mechanics for us, we have no work to do in this case.
637 if (RV.Val == N)
638 continue;
639
640 assert(N->getOpcode() != ISD::DELETED_NODE &&
641 RV.Val->getOpcode() != ISD::DELETED_NODE &&
642 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +0000643
Chris Lattner50d8e492008-01-25 23:34:24 +0000644 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
645 DOUT << "\nWith: "; DEBUG(RV.Val->dump(&DAG));
646 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000647 WorkListRemover DeadNodes(*this);
Chris Lattner50d8e492008-01-25 23:34:24 +0000648 if (N->getNumValues() == RV.Val->getNumValues())
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000649 DAG.ReplaceAllUsesWith(N, RV.Val, &DeadNodes);
Chris Lattner50d8e492008-01-25 23:34:24 +0000650 else {
Chris Lattner5eee4272008-01-26 01:09:19 +0000651 assert(N->getValueType(0) == RV.getValueType() &&
652 N->getNumValues() == 1 && "Type mismatch");
Chris Lattner50d8e492008-01-25 23:34:24 +0000653 SDOperand OpV = RV;
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000654 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000655 }
Chris Lattner50d8e492008-01-25 23:34:24 +0000656
657 // Push the new node and any users onto the worklist
658 AddToWorkList(RV.Val);
659 AddUsersToWorkList(RV.Val);
660
661 // Add any uses of the old node to the worklist in case this node is the
662 // last one that uses them. They may become dead after this node is
663 // deleted.
664 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
665 AddToWorkList(N->getOperand(i).Val);
666
667 // Nodes can be reintroduced into the worklist. Make sure we do not
668 // process a node that has been replaced.
669 removeFromWorkList(N);
Chris Lattner50d8e492008-01-25 23:34:24 +0000670
671 // Finally, since the node is now dead, remove it from the graph.
672 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000673 }
Chris Lattner95038592005-10-05 06:35:28 +0000674
675 // If the root changed (e.g. it was a dead load, update the root).
676 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000677}
678
Nate Begeman83e75ec2005-09-06 04:43:02 +0000679SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000680 switch(N->getOpcode()) {
681 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000682 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000683 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000684 case ISD::ADD: return visitADD(N);
685 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000686 case ISD::ADDC: return visitADDC(N);
687 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000688 case ISD::MUL: return visitMUL(N);
689 case ISD::SDIV: return visitSDIV(N);
690 case ISD::UDIV: return visitUDIV(N);
691 case ISD::SREM: return visitSREM(N);
692 case ISD::UREM: return visitUREM(N);
693 case ISD::MULHU: return visitMULHU(N);
694 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000695 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
696 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
697 case ISD::SDIVREM: return visitSDIVREM(N);
698 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000699 case ISD::AND: return visitAND(N);
700 case ISD::OR: return visitOR(N);
701 case ISD::XOR: return visitXOR(N);
702 case ISD::SHL: return visitSHL(N);
703 case ISD::SRA: return visitSRA(N);
704 case ISD::SRL: return visitSRL(N);
705 case ISD::CTLZ: return visitCTLZ(N);
706 case ISD::CTTZ: return visitCTTZ(N);
707 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000708 case ISD::SELECT: return visitSELECT(N);
709 case ISD::SELECT_CC: return visitSELECT_CC(N);
710 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000711 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
712 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000713 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000714 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
715 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000716 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +0000717 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000718 case ISD::FADD: return visitFADD(N);
719 case ISD::FSUB: return visitFSUB(N);
720 case ISD::FMUL: return visitFMUL(N);
721 case ISD::FDIV: return visitFDIV(N);
722 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000723 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000724 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
725 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
726 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
727 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
728 case ISD::FP_ROUND: return visitFP_ROUND(N);
729 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
730 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
731 case ISD::FNEG: return visitFNEG(N);
732 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000733 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000734 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000735 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000736 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000737 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +0000738 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000739 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
740 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000741 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000742 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000743 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000744}
745
Dan Gohman389079b2007-10-08 17:57:15 +0000746SDOperand DAGCombiner::combine(SDNode *N) {
747
748 SDOperand RV = visit(N);
749
750 // If nothing happened, try a target-specific DAG combine.
751 if (RV.Val == 0) {
752 assert(N->getOpcode() != ISD::DELETED_NODE &&
753 "Node was deleted but visit returned NULL!");
754
755 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
756 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
757
758 // Expose the DAG combiner to the target combiner impls.
759 TargetLowering::DAGCombinerInfo
760 DagCombineInfo(DAG, !AfterLegalize, false, this);
761
762 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
763 }
764 }
765
Evan Cheng08b11732008-03-22 01:55:50 +0000766 // If N is a commutative binary node, try commuting it to enable more
767 // sdisel CSE.
768 if (RV.Val == 0 &&
769 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
770 N->getNumValues() == 1) {
771 SDOperand N0 = N->getOperand(0);
772 SDOperand N1 = N->getOperand(1);
773 // Constant operands are canonicalized to RHS.
774 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
775 SDOperand Ops[] = { N1, N0 };
776 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
777 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +0000778 if (CSENode)
Evan Cheng08b11732008-03-22 01:55:50 +0000779 return SDOperand(CSENode, 0);
780 }
781 }
782
Dan Gohman389079b2007-10-08 17:57:15 +0000783 return RV;
784}
785
Chris Lattner6270f682006-10-08 22:57:01 +0000786/// getInputChainForNode - Given a node, return its input chain if it has one,
787/// otherwise return a null sd operand.
788static SDOperand getInputChainForNode(SDNode *N) {
789 if (unsigned NumOps = N->getNumOperands()) {
790 if (N->getOperand(0).getValueType() == MVT::Other)
791 return N->getOperand(0);
792 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
793 return N->getOperand(NumOps-1);
794 for (unsigned i = 1; i < NumOps-1; ++i)
795 if (N->getOperand(i).getValueType() == MVT::Other)
796 return N->getOperand(i);
797 }
798 return SDOperand(0, 0);
799}
800
Nate Begeman83e75ec2005-09-06 04:43:02 +0000801SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000802 // If N has two operands, where one has an input chain equal to the other,
803 // the 'other' chain is redundant.
804 if (N->getNumOperands() == 2) {
805 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
806 return N->getOperand(0);
807 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
808 return N->getOperand(1);
809 }
810
Chris Lattnerc76d4412007-05-16 06:37:59 +0000811 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
812 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
813 SmallPtrSet<SDNode*, 16> SeenOps;
814 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000815
816 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000817 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000818
Jim Laskey71382342006-10-07 23:37:56 +0000819 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000820 // encountered.
821 for (unsigned i = 0; i < TFs.size(); ++i) {
822 SDNode *TF = TFs[i];
823
Jim Laskey6ff23e52006-10-04 16:53:27 +0000824 // Check each of the operands.
825 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
826 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000827
Jim Laskey6ff23e52006-10-04 16:53:27 +0000828 switch (Op.getOpcode()) {
829 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000830 // Entry tokens don't need to be added to the list. They are
831 // rededundant.
832 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000833 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000834
Jim Laskey6ff23e52006-10-04 16:53:27 +0000835 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000836 if ((CombinerAA || Op.hasOneUse()) &&
837 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000838 // Queue up for processing.
839 TFs.push_back(Op.Val);
840 // Clean up in case the token factor is removed.
841 AddToWorkList(Op.Val);
842 Changed = true;
843 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000844 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000845 // Fall thru
846
847 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000848 // Only add if it isn't already in the list.
849 if (SeenOps.insert(Op.Val))
Jim Laskeybc588b82006-10-05 15:07:25 +0000850 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000851 else
852 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000853 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000854 }
855 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000856 }
857
858 SDOperand Result;
859
860 // If we've change things around then replace token factor.
861 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +0000862 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000863 // The entry token is the only possible outcome.
864 Result = DAG.getEntryNode();
865 } else {
866 // New and improved token factor.
867 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000868 }
Jim Laskey274062c2006-10-13 23:32:28 +0000869
870 // Don't add users to work list.
871 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000872 }
Jim Laskey279f0532006-09-25 16:29:54 +0000873
Jim Laskey6ff23e52006-10-04 16:53:27 +0000874 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000875}
876
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000877/// MERGE_VALUES can always be eliminated.
878SDOperand DAGCombiner::visitMERGE_VALUES(SDNode *N) {
879 WorkListRemover DeadNodes(*this);
880 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
881 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, i), N->getOperand(i),
882 &DeadNodes);
883 removeFromWorkList(N);
884 DAG.DeleteNode(N);
885 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
886}
887
888
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000889static
890SDOperand combineShlAddConstant(SDOperand N0, SDOperand N1, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000891 MVT VT = N0.getValueType();
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000892 SDOperand N00 = N0.getOperand(0);
893 SDOperand N01 = N0.getOperand(1);
894 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
895 if (N01C && N00.getOpcode() == ISD::ADD && N00.Val->hasOneUse() &&
896 isa<ConstantSDNode>(N00.getOperand(1))) {
897 N0 = DAG.getNode(ISD::ADD, VT,
898 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
899 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
900 return DAG.getNode(ISD::ADD, VT, N0, N1);
901 }
902 return SDOperand();
903}
904
Evan Chengb13cdbd2007-06-21 07:39:16 +0000905static
906SDOperand combineSelectAndUse(SDNode *N, SDOperand Slct, SDOperand OtherOp,
907 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000908 MVT VT = N->getValueType(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000909 unsigned Opc = N->getOpcode();
910 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
911 SDOperand LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
912 SDOperand RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
913 ISD::CondCode CC = ISD::SETCC_INVALID;
914 if (isSlctCC)
915 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
916 else {
917 SDOperand CCOp = Slct.getOperand(0);
918 if (CCOp.getOpcode() == ISD::SETCC)
919 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
920 }
921
922 bool DoXform = false;
923 bool InvCC = false;
924 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
925 "Bad input!");
926 if (LHS.getOpcode() == ISD::Constant &&
927 cast<ConstantSDNode>(LHS)->isNullValue())
928 DoXform = true;
929 else if (CC != ISD::SETCC_INVALID &&
930 RHS.getOpcode() == ISD::Constant &&
931 cast<ConstantSDNode>(RHS)->isNullValue()) {
932 std::swap(LHS, RHS);
Chris Lattner4626b252008-01-17 07:20:38 +0000933 SDOperand Op0 = Slct.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000934 bool isInt = (isSlctCC ? Op0.getValueType() :
935 Op0.getOperand(0).getValueType()).isInteger();
Evan Chengb13cdbd2007-06-21 07:39:16 +0000936 CC = ISD::getSetCCInverse(CC, isInt);
937 DoXform = true;
938 InvCC = true;
939 }
940
941 if (DoXform) {
942 SDOperand Result = DAG.getNode(Opc, VT, OtherOp, RHS);
943 if (isSlctCC)
944 return DAG.getSelectCC(OtherOp, Result,
945 Slct.getOperand(0), Slct.getOperand(1), CC);
946 SDOperand CCOp = Slct.getOperand(0);
947 if (InvCC)
948 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
949 CCOp.getOperand(1), CC);
950 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
951 }
952 return SDOperand();
953}
954
Nate Begeman83e75ec2005-09-06 04:43:02 +0000955SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000956 SDOperand N0 = N->getOperand(0);
957 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000958 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
959 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000960 MVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000961
962 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +0000963 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +0000964 SDOperand FoldedVOp = SimplifyVBinOp(N);
965 if (FoldedVOp.Val) return FoldedVOp;
966 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000967
Dan Gohman613e0d82007-07-03 14:03:57 +0000968 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +0000969 if (N0.getOpcode() == ISD::UNDEF)
970 return N0;
971 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +0000972 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000973 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000974 if (N0C && N1C)
Dan Gohman002e5d02008-03-13 22:13:53 +0000975 return DAG.getConstant(N0C->getAPIntValue() + N1C->getAPIntValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000976 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000977 if (N0C && !N1C)
978 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000979 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000980 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000981 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000982 // fold ((c1-A)+c2) -> (c1+c2)-A
983 if (N1C && N0.getOpcode() == ISD::SUB)
984 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
985 return DAG.getNode(ISD::SUB, VT,
Dan Gohman002e5d02008-03-13 22:13:53 +0000986 DAG.getConstant(N1C->getAPIntValue()+
987 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000988 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000989 // reassociate add
990 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
991 if (RADD.Val != 0)
992 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000993 // fold ((0-A) + B) -> B-A
994 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
995 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000996 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000997 // fold (A + (0-B)) -> A-B
998 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
999 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +00001000 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001001 // fold (A+(B-A)) -> B
1002 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001003 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +00001004
Duncan Sands83ec4b62008-06-06 12:08:01 +00001005 if (!VT.isVector() && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001006 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +00001007
1008 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001009 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001010 APInt LHSZero, LHSOne;
1011 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001012 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001013 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001014 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001015 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +00001016
1017 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1018 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1019 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1020 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1021 return DAG.getNode(ISD::OR, VT, N0, N1);
1022 }
1023 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001024
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001025 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
1026 if (N0.getOpcode() == ISD::SHL && N0.Val->hasOneUse()) {
1027 SDOperand Result = combineShlAddConstant(N0, N1, DAG);
1028 if (Result.Val) return Result;
1029 }
1030 if (N1.getOpcode() == ISD::SHL && N1.Val->hasOneUse()) {
1031 SDOperand Result = combineShlAddConstant(N1, N0, DAG);
1032 if (Result.Val) return Result;
1033 }
1034
Evan Chengb13cdbd2007-06-21 07:39:16 +00001035 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
1036 if (N0.getOpcode() == ISD::SELECT && N0.Val->hasOneUse()) {
1037 SDOperand Result = combineSelectAndUse(N, N0, N1, DAG);
1038 if (Result.Val) return Result;
1039 }
1040 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1041 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1042 if (Result.Val) return Result;
1043 }
1044
Nate Begeman83e75ec2005-09-06 04:43:02 +00001045 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001046}
1047
Chris Lattner91153682007-03-04 20:03:15 +00001048SDOperand DAGCombiner::visitADDC(SDNode *N) {
1049 SDOperand N0 = N->getOperand(0);
1050 SDOperand N1 = N->getOperand(1);
1051 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1052 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001053 MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001054
1055 // If the flag result is dead, turn this into an ADD.
1056 if (N->hasNUsesOfValue(0, 1))
1057 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +00001058 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +00001059
1060 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001061 if (N0C && !N1C)
Dan Gohman56867522008-06-21 22:06:07 +00001062 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Chris Lattner91153682007-03-04 20:03:15 +00001063
Chris Lattnerb6541762007-03-04 20:40:38 +00001064 // fold (addc x, 0) -> x + no carry out
1065 if (N1C && N1C->isNullValue())
1066 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1067
1068 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001069 APInt LHSZero, LHSOne;
1070 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001071 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001072 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001073 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001074 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001075
1076 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1077 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1078 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1079 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1080 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
1081 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1082 }
Chris Lattner91153682007-03-04 20:03:15 +00001083
1084 return SDOperand();
1085}
1086
1087SDOperand DAGCombiner::visitADDE(SDNode *N) {
1088 SDOperand N0 = N->getOperand(0);
1089 SDOperand N1 = N->getOperand(1);
Chris Lattnerb6541762007-03-04 20:40:38 +00001090 SDOperand CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001091 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1092 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001093 //MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001094
1095 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001096 if (N0C && !N1C)
Dan Gohman56867522008-06-21 22:06:07 +00001097 return DAG.getNode(ISD::ADDE, N->getVTList(), N1, N0, CarryIn);
Chris Lattner91153682007-03-04 20:03:15 +00001098
Chris Lattnerb6541762007-03-04 20:40:38 +00001099 // fold (adde x, y, false) -> (addc x, y)
Dan Gohman0a4627d2008-06-23 15:29:14 +00001100 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Dan Gohman56867522008-06-21 22:06:07 +00001101 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Chris Lattner91153682007-03-04 20:03:15 +00001102
1103 return SDOperand();
1104}
1105
1106
1107
Nate Begeman83e75ec2005-09-06 04:43:02 +00001108SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001109 SDOperand N0 = N->getOperand(0);
1110 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001111 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1112 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001113 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001114
Dan Gohman7f321562007-06-25 16:23:39 +00001115 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001116 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001117 SDOperand FoldedVOp = SimplifyVBinOp(N);
1118 if (FoldedVOp.Val) return FoldedVOp;
1119 }
Dan Gohman7f321562007-06-25 16:23:39 +00001120
Chris Lattner854077d2005-10-17 01:07:11 +00001121 // fold (sub x, x) -> 0
Evan Chengc8e3b142008-03-12 07:02:50 +00001122 if (N0 == N1)
Chris Lattner854077d2005-10-17 01:07:11 +00001123 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001124 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001125 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001126 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +00001127 // fold (sub x, c) -> (add x, -c)
1128 if (N1C)
Dan Gohman002e5d02008-03-13 22:13:53 +00001129 return DAG.getNode(ISD::ADD, VT, N0,
1130 DAG.getConstant(-N1C->getAPIntValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001131 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001132 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001133 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001134 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001135 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001136 return N0.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +00001137 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
1138 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1139 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1140 if (Result.Val) return Result;
1141 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001142 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001143 if (N0.getOpcode() == ISD::UNDEF)
1144 return N0;
1145 if (N1.getOpcode() == ISD::UNDEF)
1146 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001147
Nate Begeman83e75ec2005-09-06 04:43:02 +00001148 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001149}
1150
Nate Begeman83e75ec2005-09-06 04:43:02 +00001151SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001152 SDOperand N0 = N->getOperand(0);
1153 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001154 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1155 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001156 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001157
Dan Gohman7f321562007-06-25 16:23:39 +00001158 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001159 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001160 SDOperand FoldedVOp = SimplifyVBinOp(N);
1161 if (FoldedVOp.Val) return FoldedVOp;
1162 }
Dan Gohman7f321562007-06-25 16:23:39 +00001163
Dan Gohman613e0d82007-07-03 14:03:57 +00001164 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001165 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001166 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001167 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001168 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001169 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001170 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001171 if (N0C && !N1C)
1172 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001173 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001174 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001175 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001176 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001177 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001178 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001179 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001180 if (N1C && N1C->getAPIntValue().isPowerOf2())
Chris Lattner3e6099b2005-10-30 06:41:49 +00001181 return DAG.getNode(ISD::SHL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001182 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001183 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001184 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
1185 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
1186 // FIXME: If the input is something that is easily negated (e.g. a
1187 // single-use add), we should put the negate there.
1188 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1189 DAG.getNode(ISD::SHL, VT, N0,
1190 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
1191 TLI.getShiftAmountTy())));
1192 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001193
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001194 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1195 if (N1C && N0.getOpcode() == ISD::SHL &&
1196 isa<ConstantSDNode>(N0.getOperand(1))) {
1197 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +00001198 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001199 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1200 }
1201
1202 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1203 // use.
1204 {
1205 SDOperand Sh(0,0), Y(0,0);
1206 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1207 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
1208 N0.Val->hasOneUse()) {
1209 Sh = N0; Y = N1;
1210 } else if (N1.getOpcode() == ISD::SHL &&
1211 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
1212 Sh = N1; Y = N0;
1213 }
1214 if (Sh.Val) {
1215 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
1216 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1217 }
1218 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001219 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
1220 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1221 isa<ConstantSDNode>(N0.getOperand(1))) {
1222 return DAG.getNode(ISD::ADD, VT,
1223 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1224 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1225 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001226
Nate Begemancd4d58c2006-02-03 06:46:56 +00001227 // reassociate mul
1228 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
1229 if (RMUL.Val != 0)
1230 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001231
Nate Begeman83e75ec2005-09-06 04:43:02 +00001232 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001233}
1234
Nate Begeman83e75ec2005-09-06 04:43:02 +00001235SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001236 SDOperand N0 = N->getOperand(0);
1237 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001238 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1239 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001240 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001241
Dan Gohman7f321562007-06-25 16:23:39 +00001242 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001243 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001244 SDOperand FoldedVOp = SimplifyVBinOp(N);
1245 if (FoldedVOp.Val) return FoldedVOp;
1246 }
Dan Gohman7f321562007-06-25 16:23:39 +00001247
Nate Begeman1d4d4142005-09-01 00:19:25 +00001248 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001249 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001250 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001251 // fold (sdiv X, 1) -> X
1252 if (N1C && N1C->getSignExtended() == 1LL)
1253 return N0;
1254 // fold (sdiv X, -1) -> 0-X
1255 if (N1C && N1C->isAllOnesValue())
1256 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001257 // If we know the sign bits of both operands are zero, strength reduce to a
1258 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001259 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001260 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattnerf32aac32008-01-27 23:32:17 +00001261 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
1262 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001263 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman002e5d02008-03-13 22:13:53 +00001264 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001265 (isPowerOf2_64(N1C->getSignExtended()) ||
1266 isPowerOf2_64(-N1C->getSignExtended()))) {
1267 // If dividing by powers of two is cheap, then don't perform the following
1268 // fold.
1269 if (TLI.isPow2DivCheap())
1270 return SDOperand();
1271 int64_t pow2 = N1C->getSignExtended();
1272 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001273 unsigned lg2 = Log2_64(abs2);
1274 // Splat the sign bit into the register
1275 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001276 DAG.getConstant(VT.getSizeInBits()-1,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001277 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00001278 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +00001279 // Add (N0 < 0) ? abs2 - 1 : 0;
1280 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001281 DAG.getConstant(VT.getSizeInBits()-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001282 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +00001283 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +00001284 AddToWorkList(SRL.Val);
1285 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +00001286 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
1287 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001288 // If we're dividing by a positive value, we're done. Otherwise, we must
1289 // negate the result.
1290 if (pow2 > 0)
1291 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +00001292 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001293 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1294 }
Nate Begeman69575232005-10-20 02:15:44 +00001295 // if integer divide is expensive and we satisfy the requirements, emit an
1296 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001297 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001298 !TLI.isIntDivCheap()) {
1299 SDOperand Op = BuildSDIV(N);
1300 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001301 }
Dan Gohman7f321562007-06-25 16:23:39 +00001302
Dan Gohman613e0d82007-07-03 14:03:57 +00001303 // undef / X -> 0
1304 if (N0.getOpcode() == ISD::UNDEF)
1305 return DAG.getConstant(0, VT);
1306 // X / undef -> undef
1307 if (N1.getOpcode() == ISD::UNDEF)
1308 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001309
Nate Begeman83e75ec2005-09-06 04:43:02 +00001310 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001311}
1312
Nate Begeman83e75ec2005-09-06 04:43:02 +00001313SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001314 SDOperand N0 = N->getOperand(0);
1315 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001316 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1317 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001318 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001319
Dan Gohman7f321562007-06-25 16:23:39 +00001320 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001321 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001322 SDOperand FoldedVOp = SimplifyVBinOp(N);
1323 if (FoldedVOp.Val) return FoldedVOp;
1324 }
Dan Gohman7f321562007-06-25 16:23:39 +00001325
Nate Begeman1d4d4142005-09-01 00:19:25 +00001326 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001327 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001328 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001329 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001330 if (N1C && N1C->getAPIntValue().isPowerOf2())
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001331 return DAG.getNode(ISD::SRL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001332 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001333 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001334 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1335 if (N1.getOpcode() == ISD::SHL) {
1336 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001337 if (SHC->getAPIntValue().isPowerOf2()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001338 MVT ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +00001339 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001340 DAG.getConstant(SHC->getAPIntValue()
1341 .logBase2(),
Nate Begemanc031e332006-02-05 07:36:48 +00001342 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001343 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001344 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001345 }
1346 }
1347 }
Nate Begeman69575232005-10-20 02:15:44 +00001348 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001349 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Chris Lattnere9936d12005-10-22 18:50:15 +00001350 SDOperand Op = BuildUDIV(N);
1351 if (Op.Val) return Op;
1352 }
Dan Gohman7f321562007-06-25 16:23:39 +00001353
Dan Gohman613e0d82007-07-03 14:03:57 +00001354 // undef / X -> 0
1355 if (N0.getOpcode() == ISD::UNDEF)
1356 return DAG.getConstant(0, VT);
1357 // X / undef -> undef
1358 if (N1.getOpcode() == ISD::UNDEF)
1359 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001360
Nate Begeman83e75ec2005-09-06 04:43:02 +00001361 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001362}
1363
Nate Begeman83e75ec2005-09-06 04:43:02 +00001364SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001365 SDOperand N0 = N->getOperand(0);
1366 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001367 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1368 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001369 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001370
1371 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001372 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001373 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001374 // If we know the sign bits of both operands are zero, strength reduce to a
1375 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00001376 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001377 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattneree339f42008-01-27 23:21:58 +00001378 return DAG.getNode(ISD::UREM, VT, N0, N1);
1379 }
Chris Lattner26d29902006-10-12 20:58:32 +00001380
Dan Gohman77003042007-11-26 23:46:11 +00001381 // If X/C can be simplified by the division-by-constant logic, lower
1382 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001383 if (N1C && !N1C->isNullValue()) {
1384 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
Chris Lattner5eee4272008-01-26 01:09:19 +00001385 AddToWorkList(Div.Val);
Dan Gohman77003042007-11-26 23:46:11 +00001386 SDOperand OptimizedDiv = combine(Div.Val);
1387 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1388 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1389 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1390 AddToWorkList(Mul.Val);
1391 return Sub;
1392 }
Chris Lattner26d29902006-10-12 20:58:32 +00001393 }
1394
Dan Gohman613e0d82007-07-03 14:03:57 +00001395 // undef % X -> 0
1396 if (N0.getOpcode() == ISD::UNDEF)
1397 return DAG.getConstant(0, VT);
1398 // X % undef -> undef
1399 if (N1.getOpcode() == ISD::UNDEF)
1400 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001401
Nate Begeman83e75ec2005-09-06 04:43:02 +00001402 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001403}
1404
Nate Begeman83e75ec2005-09-06 04:43:02 +00001405SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001406 SDOperand N0 = N->getOperand(0);
1407 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001408 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1409 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001410 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001411
1412 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001413 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001414 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001415 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001416 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
1417 return DAG.getNode(ISD::AND, VT, N0,
1418 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001419 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1420 if (N1.getOpcode() == ISD::SHL) {
1421 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001422 if (SHC->getAPIntValue().isPowerOf2()) {
1423 SDOperand Add =
1424 DAG.getNode(ISD::ADD, VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001425 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00001426 VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001427 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001428 return DAG.getNode(ISD::AND, VT, N0, Add);
1429 }
1430 }
1431 }
Chris Lattner26d29902006-10-12 20:58:32 +00001432
Dan Gohman77003042007-11-26 23:46:11 +00001433 // If X/C can be simplified by the division-by-constant logic, lower
1434 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001435 if (N1C && !N1C->isNullValue()) {
1436 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
Dan Gohman77003042007-11-26 23:46:11 +00001437 SDOperand OptimizedDiv = combine(Div.Val);
1438 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1439 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1440 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1441 AddToWorkList(Mul.Val);
1442 return Sub;
1443 }
Chris Lattner26d29902006-10-12 20:58:32 +00001444 }
1445
Dan Gohman613e0d82007-07-03 14:03:57 +00001446 // undef % X -> 0
1447 if (N0.getOpcode() == ISD::UNDEF)
1448 return DAG.getConstant(0, VT);
1449 // X % undef -> undef
1450 if (N1.getOpcode() == ISD::UNDEF)
1451 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001452
Nate Begeman83e75ec2005-09-06 04:43:02 +00001453 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001454}
1455
Nate Begeman83e75ec2005-09-06 04:43:02 +00001456SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001457 SDOperand N0 = N->getOperand(0);
1458 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001459 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001460 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001461
1462 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001463 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001464 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001465 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001466 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001467 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001468 DAG.getConstant(N0.getValueType().getSizeInBits()-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001469 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001470 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001471 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001472 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001473
Nate Begeman83e75ec2005-09-06 04:43:02 +00001474 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001475}
1476
Nate Begeman83e75ec2005-09-06 04:43:02 +00001477SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001478 SDOperand N0 = N->getOperand(0);
1479 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001480 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001481 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001482
1483 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001484 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001485 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001486 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00001487 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001488 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001489 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001490 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001491 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001492
Nate Begeman83e75ec2005-09-06 04:43:02 +00001493 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001494}
1495
Dan Gohman389079b2007-10-08 17:57:15 +00001496/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1497/// compute two values. LoOp and HiOp give the opcodes for the two computations
1498/// that are being performed. Return true if a simplification was made.
1499///
Chris Lattner5eee4272008-01-26 01:09:19 +00001500SDOperand DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1501 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001502 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001503 bool HiExists = N->hasAnyUseOfValue(1);
1504 if (!HiExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001505 (!AfterLegalize ||
1506 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001507 SDOperand Res = DAG.getNode(LoOp, N->getValueType(0), N->op_begin(),
1508 N->getNumOperands());
1509 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001510 }
1511
1512 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001513 bool LoExists = N->hasAnyUseOfValue(0);
1514 if (!LoExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001515 (!AfterLegalize ||
1516 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001517 SDOperand Res = DAG.getNode(HiOp, N->getValueType(1), N->op_begin(),
1518 N->getNumOperands());
1519 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001520 }
1521
Evan Cheng44711942007-11-08 09:25:29 +00001522 // If both halves are used, return as it is.
1523 if (LoExists && HiExists)
Chris Lattner5eee4272008-01-26 01:09:19 +00001524 return SDOperand();
Evan Cheng44711942007-11-08 09:25:29 +00001525
1526 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001527 if (LoExists) {
1528 SDOperand Lo = DAG.getNode(LoOp, N->getValueType(0),
1529 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001530 AddToWorkList(Lo.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001531 SDOperand LoOpt = combine(Lo.Val);
Chris Lattner5eee4272008-01-26 01:09:19 +00001532 if (LoOpt.Val && LoOpt.Val != Lo.Val &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001533 (!AfterLegalize ||
1534 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001535 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001536 }
1537
Evan Cheng44711942007-11-08 09:25:29 +00001538 if (HiExists) {
1539 SDOperand Hi = DAG.getNode(HiOp, N->getValueType(1),
1540 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001541 AddToWorkList(Hi.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001542 SDOperand HiOpt = combine(Hi.Val);
1543 if (HiOpt.Val && HiOpt != Hi &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001544 (!AfterLegalize ||
1545 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001546 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001547 }
Chris Lattner5eee4272008-01-26 01:09:19 +00001548 return SDOperand();
Dan Gohman389079b2007-10-08 17:57:15 +00001549}
1550
1551SDOperand DAGCombiner::visitSMUL_LOHI(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001552 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
1553 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001554
1555 return SDOperand();
1556}
1557
1558SDOperand DAGCombiner::visitUMUL_LOHI(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001559 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
1560 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001561
1562 return SDOperand();
1563}
1564
1565SDOperand DAGCombiner::visitSDIVREM(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001566 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
1567 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001568
1569 return SDOperand();
1570}
1571
1572SDOperand DAGCombiner::visitUDIVREM(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001573 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
1574 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001575
1576 return SDOperand();
1577}
1578
Chris Lattner35e5c142006-05-05 05:51:50 +00001579/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1580/// two operands of the same opcode, try to simplify it.
1581SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1582 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001583 MVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00001584 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1585
Chris Lattner540121f2006-05-05 06:31:05 +00001586 // For each of OP in AND/OR/XOR:
1587 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1588 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1589 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001590 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001591 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001592 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001593 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1594 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1595 N0.getOperand(0).getValueType(),
1596 N0.getOperand(0), N1.getOperand(0));
1597 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001598 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001599 }
1600
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001601 // For each of OP in SHL/SRL/SRA/AND...
1602 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1603 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1604 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001605 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001606 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001607 N0.getOperand(1) == N1.getOperand(1)) {
1608 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1609 N0.getOperand(0).getValueType(),
1610 N0.getOperand(0), N1.getOperand(0));
1611 AddToWorkList(ORNode.Val);
1612 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1613 }
1614
1615 return SDOperand();
1616}
1617
Nate Begeman83e75ec2005-09-06 04:43:02 +00001618SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001619 SDOperand N0 = N->getOperand(0);
1620 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001621 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001622 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1623 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001624 MVT VT = N1.getValueType();
1625 unsigned BitWidth = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001626
Dan Gohman7f321562007-06-25 16:23:39 +00001627 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001628 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001629 SDOperand FoldedVOp = SimplifyVBinOp(N);
1630 if (FoldedVOp.Val) return FoldedVOp;
1631 }
Dan Gohman7f321562007-06-25 16:23:39 +00001632
Dan Gohman613e0d82007-07-03 14:03:57 +00001633 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001634 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001635 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001636 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001637 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001638 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001639 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001640 if (N0C && !N1C)
1641 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001642 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001643 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001644 return N0;
1645 // if (and x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001646 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0),
1647 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001648 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001649 // reassociate and
1650 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1651 if (RAND.Val != 0)
1652 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001653 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001654 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001655 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001656 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001657 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001658 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1659 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001660 SDOperand N0Op0 = N0.getOperand(0);
1661 APInt Mask = ~N1C->getAPIntValue();
1662 Mask.trunc(N0Op0.getValueSizeInBits());
1663 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001664 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001665 N0Op0);
Chris Lattner1ec05d12006-03-01 21:47:21 +00001666
1667 // Replace uses of the AND with uses of the Zero extend node.
1668 CombineTo(N, Zext);
1669
Chris Lattner3603cd62006-02-02 07:17:31 +00001670 // We actually want to replace all uses of the any_extend with the
1671 // zero_extend, to avoid duplicating things. This will later cause this
1672 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001673 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001674 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001675 }
1676 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001677 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1678 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1679 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1680 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1681
1682 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001683 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001684 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001685 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001686 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001687 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001688 return DAG.getSetCC(VT, ORNode, LR, Op1);
1689 }
1690 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1691 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1692 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001693 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001694 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1695 }
1696 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1697 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1698 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001699 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001700 return DAG.getSetCC(VT, ORNode, LR, Op1);
1701 }
1702 }
1703 // canonicalize equivalent to ll == rl
1704 if (LL == RR && LR == RL) {
1705 Op1 = ISD::getSetCCSwappedOperands(Op1);
1706 std::swap(RL, RR);
1707 }
1708 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001709 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001710 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1711 if (Result != ISD::SETCC_INVALID)
1712 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1713 }
1714 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001715
1716 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1717 if (N0.getOpcode() == N1.getOpcode()) {
1718 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1719 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001720 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001721
Nate Begemande996292006-02-03 22:24:05 +00001722 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1723 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001724 if (!VT.isVector() &&
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001725 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001726 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001727 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Cheng83060c52007-03-07 08:07:03 +00001728 if (ISD::isEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001729 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001730 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001731 // If we zero all the possible extended bits, then we can turn this into
1732 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001733 unsigned BitWidth = N1.getValueSizeInBits();
1734 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001735 BitWidth - EVT.getSizeInBits())) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001736 ((!AfterLegalize && !LN0->isVolatile()) ||
1737 TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001738 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1739 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001740 LN0->getSrcValueOffset(), EVT,
1741 LN0->isVolatile(),
1742 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001743 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001744 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001745 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001746 }
1747 }
1748 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00001749 if (ISD::isSEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
1750 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001751 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001752 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001753 // If we zero all the possible extended bits, then we can turn this into
1754 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001755 unsigned BitWidth = N1.getValueSizeInBits();
1756 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001757 BitWidth - EVT.getSizeInBits())) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001758 ((!AfterLegalize && !LN0->isVolatile()) ||
1759 TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001760 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1761 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001762 LN0->getSrcValueOffset(), EVT,
1763 LN0->isVolatile(),
1764 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001765 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001766 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001767 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001768 }
1769 }
Chris Lattner15045b62006-02-28 06:35:35 +00001770
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001771 // fold (and (load x), 255) -> (zextload x, i8)
1772 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001773 if (N1C && N0.getOpcode() == ISD::LOAD) {
1774 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1775 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001776 LN0->isUnindexed() && N0.hasOneUse() &&
1777 // Do not change the width of a volatile load.
1778 !LN0->isVolatile()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00001779 MVT EVT = MVT::Other;
1780 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
1781 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue()))
1782 EVT = MVT::getIntegerVT(ActiveBits);
1783
1784 MVT LoadedVT = LN0->getMemoryVT();
Duncan Sandsad205a72008-06-16 08:14:38 +00001785 // Do not generate loads of non-round integer types since these can
1786 // be expensive (and would be wrong if the type is not byte sized).
1787 if (EVT != MVT::Other && LoadedVT.bitsGT(EVT) && EVT.isRound() &&
Evan Cheng466685d2006-10-09 20:57:25 +00001788 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001789 MVT PtrType = N0.getOperand(1).getValueType();
Evan Cheng466685d2006-10-09 20:57:25 +00001790 // For big endian targets, we need to add an offset to the pointer to
1791 // load the correct bytes. For little endian systems, we merely need to
1792 // read fewer bytes from the same pointer.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001793 unsigned LVTStoreBytes = LoadedVT.getStoreSizeInBits()/8;
1794 unsigned EVTStoreBytes = EVT.getStoreSizeInBits()/8;
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001795 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001796 unsigned Alignment = LN0->getAlignment();
Evan Cheng466685d2006-10-09 20:57:25 +00001797 SDOperand NewPtr = LN0->getBasePtr();
Duncan Sands0753fc12008-02-11 10:37:04 +00001798 if (TLI.isBigEndian()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001799 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1800 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001801 Alignment = MinAlign(Alignment, PtrOff);
1802 }
Evan Cheng466685d2006-10-09 20:57:25 +00001803 AddToWorkList(NewPtr.Val);
1804 SDOperand Load =
1805 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001806 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00001807 LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001808 AddToWorkList(N);
1809 CombineTo(N0.Val, Load, Load.getValue(1));
1810 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1811 }
Chris Lattner15045b62006-02-28 06:35:35 +00001812 }
1813 }
1814
Nate Begeman83e75ec2005-09-06 04:43:02 +00001815 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001816}
1817
Nate Begeman83e75ec2005-09-06 04:43:02 +00001818SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001819 SDOperand N0 = N->getOperand(0);
1820 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001821 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001822 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1823 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001824 MVT VT = N1.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001825
Dan Gohman7f321562007-06-25 16:23:39 +00001826 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001827 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001828 SDOperand FoldedVOp = SimplifyVBinOp(N);
1829 if (FoldedVOp.Val) return FoldedVOp;
1830 }
Dan Gohman7f321562007-06-25 16:23:39 +00001831
Dan Gohman613e0d82007-07-03 14:03:57 +00001832 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001833 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001834 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001835 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001836 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001837 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001838 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001839 if (N0C && !N1C)
1840 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001841 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001842 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001843 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001844 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001845 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001846 return N1;
1847 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001848 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001849 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001850 // reassociate or
1851 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1852 if (ROR.Val != 0)
1853 return ROR;
1854 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1855 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001856 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001857 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1858 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1859 N1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001860 DAG.getConstant(N1C->getAPIntValue() |
1861 C1->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001862 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001863 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1864 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1865 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1866 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1867
1868 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001869 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001870 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1871 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001872 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001873 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1874 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001875 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001876 return DAG.getSetCC(VT, ORNode, LR, Op1);
1877 }
1878 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1879 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1880 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1881 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1882 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001883 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001884 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1885 }
1886 }
1887 // canonicalize equivalent to ll == rl
1888 if (LL == RR && LR == RL) {
1889 Op1 = ISD::getSetCCSwappedOperands(Op1);
1890 std::swap(RL, RR);
1891 }
1892 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001893 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001894 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1895 if (Result != ISD::SETCC_INVALID)
1896 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1897 }
1898 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001899
1900 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1901 if (N0.getOpcode() == N1.getOpcode()) {
1902 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1903 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001904 }
Chris Lattner516b9622006-09-14 20:50:57 +00001905
Chris Lattner1ec72732006-09-14 21:11:37 +00001906 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1907 if (N0.getOpcode() == ISD::AND &&
1908 N1.getOpcode() == ISD::AND &&
1909 N0.getOperand(1).getOpcode() == ISD::Constant &&
1910 N1.getOperand(1).getOpcode() == ISD::Constant &&
1911 // Don't increase # computations.
1912 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1913 // We can only do this xform if we know that bits from X that are set in C2
1914 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001915 const APInt &LHSMask =
1916 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1917 const APInt &RHSMask =
1918 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Chris Lattner1ec72732006-09-14 21:11:37 +00001919
Dan Gohmanea859be2007-06-22 14:59:07 +00001920 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1921 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Chris Lattner1ec72732006-09-14 21:11:37 +00001922 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1923 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1924 }
1925 }
1926
1927
Chris Lattner516b9622006-09-14 20:50:57 +00001928 // See if this is some rotate idiom.
1929 if (SDNode *Rot = MatchRotate(N0, N1))
1930 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001931
Nate Begeman83e75ec2005-09-06 04:43:02 +00001932 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001933}
1934
Chris Lattner516b9622006-09-14 20:50:57 +00001935
1936/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1937static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1938 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001939 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001940 Mask = Op.getOperand(1);
1941 Op = Op.getOperand(0);
1942 } else {
1943 return false;
1944 }
1945 }
1946
1947 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1948 Shift = Op;
1949 return true;
1950 }
1951 return false;
1952}
1953
1954
1955// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1956// idioms for rotate, and if the target supports rotation instructions, generate
1957// a rot[lr].
1958SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001959 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001960 MVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00001961 if (!TLI.isTypeLegal(VT)) return 0;
1962
1963 // The target must have at least one rotate flavor.
1964 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1965 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1966 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001967
Chris Lattner516b9622006-09-14 20:50:57 +00001968 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1969 SDOperand LHSShift; // The shift.
1970 SDOperand LHSMask; // AND value if any.
1971 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1972 return 0; // Not part of a rotate.
1973
1974 SDOperand RHSShift; // The shift.
1975 SDOperand RHSMask; // AND value if any.
1976 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1977 return 0; // Not part of a rotate.
1978
1979 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1980 return 0; // Not shifting the same value.
1981
1982 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1983 return 0; // Shifts must disagree.
1984
1985 // Canonicalize shl to left side in a shl/srl pair.
1986 if (RHSShift.getOpcode() == ISD::SHL) {
1987 std::swap(LHS, RHS);
1988 std::swap(LHSShift, RHSShift);
1989 std::swap(LHSMask , RHSMask );
1990 }
1991
Duncan Sands83ec4b62008-06-06 12:08:01 +00001992 unsigned OpSizeInBits = VT.getSizeInBits();
Scott Michelc9dc1142007-04-02 21:36:32 +00001993 SDOperand LHSShiftArg = LHSShift.getOperand(0);
1994 SDOperand LHSShiftAmt = LHSShift.getOperand(1);
1995 SDOperand RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00001996
1997 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1998 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00001999 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
2000 RHSShiftAmt.getOpcode() == ISD::Constant) {
2001 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getValue();
2002 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getValue();
Chris Lattner516b9622006-09-14 20:50:57 +00002003 if ((LShVal + RShVal) != OpSizeInBits)
2004 return 0;
2005
2006 SDOperand Rot;
2007 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002008 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002009 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002010 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002011
2012 // If there is an AND of either shifted operand, apply it to the result.
2013 if (LHSMask.Val || RHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00002014 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Chris Lattner516b9622006-09-14 20:50:57 +00002015
2016 if (LHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00002017 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2018 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002019 }
2020 if (RHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00002021 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2022 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002023 }
2024
2025 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
2026 }
2027
2028 return Rot.Val;
2029 }
2030
2031 // If there is a mask here, and we have a variable shift, we can't be sure
2032 // that we're masking out the right stuff.
2033 if (LHSMask.Val || RHSMask.Val)
2034 return 0;
2035
2036 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2037 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002038 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2039 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002040 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002041 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002042 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002043 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002044 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00002045 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002046 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002047 }
Chris Lattner516b9622006-09-14 20:50:57 +00002048 }
2049 }
2050
2051 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2052 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002053 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2054 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002055 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002056 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002057 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002058 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002059 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00002060 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002061 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002062 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002063 }
2064 }
2065
2066 // Look for sign/zext/any-extended cases:
2067 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2068 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2069 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
2070 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2071 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2072 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
2073 SDOperand LExtOp0 = LHSShiftAmt.getOperand(0);
2074 SDOperand RExtOp0 = RHSShiftAmt.getOperand(0);
2075 if (RExtOp0.getOpcode() == ISD::SUB &&
2076 RExtOp0.getOperand(1) == LExtOp0) {
2077 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2078 // (rotr x, y)
2079 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2080 // (rotl x, (sub 32, y))
2081 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002082 if (SUBC->getAPIntValue() == OpSizeInBits) {
Scott Michelc9dc1142007-04-02 21:36:32 +00002083 if (HasROTL)
2084 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2085 else
2086 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
2087 }
2088 }
2089 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2090 RExtOp0 == LExtOp0.getOperand(1)) {
2091 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2092 // (rotl x, y)
2093 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2094 // (rotr x, (sub 32, y))
2095 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002096 if (SUBC->getAPIntValue() == OpSizeInBits) {
Scott Michelc9dc1142007-04-02 21:36:32 +00002097 if (HasROTL)
2098 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, RHSShiftAmt).Val;
2099 else
2100 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2101 }
2102 }
Chris Lattner516b9622006-09-14 20:50:57 +00002103 }
2104 }
2105
2106 return 0;
2107}
2108
2109
Nate Begeman83e75ec2005-09-06 04:43:02 +00002110SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002111 SDOperand N0 = N->getOperand(0);
2112 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002113 SDOperand LHS, RHS, CC;
2114 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2115 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002116 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002117
Dan Gohman7f321562007-06-25 16:23:39 +00002118 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002119 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00002120 SDOperand FoldedVOp = SimplifyVBinOp(N);
2121 if (FoldedVOp.Val) return FoldedVOp;
2122 }
Dan Gohman7f321562007-06-25 16:23:39 +00002123
Evan Cheng26471c42008-03-25 20:08:07 +00002124 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
2125 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
2126 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00002127 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002128 if (N0.getOpcode() == ISD::UNDEF)
2129 return N0;
2130 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002131 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002132 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002133 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002134 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00002135 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002136 if (N0C && !N1C)
2137 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002138 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002139 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002140 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002141 // reassociate xor
2142 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
2143 if (RXOR.Val != 0)
2144 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002145 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00002146 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002147 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00002148 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2149 isInt);
2150 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002151 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002152 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002153 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002154 assert(0 && "Unhandled SetCC Equivalent!");
2155 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002156 }
Chris Lattner61c5ff42007-09-10 21:39:07 +00002157 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002158 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Chris Lattner61c5ff42007-09-10 21:39:07 +00002159 N0.Val->hasOneUse() && isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
2160 SDOperand V = N0.getOperand(0);
2161 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002162 DAG.getConstant(1, V.getValueType()));
Chris Lattner61c5ff42007-09-10 21:39:07 +00002163 AddToWorkList(V.Val);
2164 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2165 }
2166
Nate Begeman99801192005-09-07 23:25:52 +00002167 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Dan Gohman002e5d02008-03-13 22:13:53 +00002168 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002169 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002170 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002171 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2172 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002173 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2174 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002175 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002176 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002177 }
2178 }
Nate Begeman99801192005-09-07 23:25:52 +00002179 // fold !(x or y) -> (!x and !y) iff x or y are constants
2180 if (N1C && N1C->isAllOnesValue() &&
2181 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002182 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002183 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2184 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002185 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2186 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002187 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002188 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002189 }
2190 }
Nate Begeman223df222005-09-08 20:18:10 +00002191 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2192 if (N1C && N0.getOpcode() == ISD::XOR) {
2193 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2194 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2195 if (N00C)
2196 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00002197 DAG.getConstant(N1C->getAPIntValue()^
2198 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002199 if (N01C)
2200 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
Dan Gohman002e5d02008-03-13 22:13:53 +00002201 DAG.getConstant(N1C->getAPIntValue()^
2202 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002203 }
2204 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002205 if (N0 == N1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002206 if (!VT.isVector()) {
Chris Lattner4fbdd592006-03-28 19:11:05 +00002207 return DAG.getConstant(0, VT);
2208 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
2209 // Produce a vector of zeros.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002210 SDOperand El = DAG.getConstant(0, VT.getVectorElementType());
2211 std::vector<SDOperand> Ops(VT.getVectorNumElements(), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002212 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002213 }
2214 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002215
2216 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2217 if (N0.getOpcode() == N1.getOpcode()) {
2218 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
2219 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002220 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002221
Chris Lattner3e104b12006-04-08 04:15:24 +00002222 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002223 if (!VT.isVector() &&
Chris Lattner3e104b12006-04-08 04:15:24 +00002224 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002225 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002226
Nate Begeman83e75ec2005-09-06 04:43:02 +00002227 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002228}
2229
Chris Lattnere70da202007-12-06 07:33:36 +00002230/// visitShiftByConstant - Handle transforms common to the three shifts, when
2231/// the shift amount is a constant.
2232SDOperand DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
2233 SDNode *LHS = N->getOperand(0).Val;
2234 if (!LHS->hasOneUse()) return SDOperand();
2235
2236 // We want to pull some binops through shifts, so that we have (and (shift))
2237 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2238 // thing happens with address calculations, so it's important to canonicalize
2239 // it.
2240 bool HighBitSet = false; // Can we transform this if the high bit is set?
2241
2242 switch (LHS->getOpcode()) {
2243 default: return SDOperand();
2244 case ISD::OR:
2245 case ISD::XOR:
2246 HighBitSet = false; // We can only transform sra if the high bit is clear.
2247 break;
2248 case ISD::AND:
2249 HighBitSet = true; // We can only transform sra if the high bit is set.
2250 break;
2251 case ISD::ADD:
2252 if (N->getOpcode() != ISD::SHL)
2253 return SDOperand(); // only shl(add) not sr[al](add).
2254 HighBitSet = false; // We can only transform sra if the high bit is clear.
2255 break;
2256 }
2257
2258 // We require the RHS of the binop to be a constant as well.
2259 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
2260 if (!BinOpCst) return SDOperand();
2261
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002262
2263 // FIXME: disable this for unless the input to the binop is a shift by a
2264 // constant. If it is not a shift, it pessimizes some common cases like:
2265 //
2266 //void foo(int *X, int i) { X[i & 1235] = 1; }
2267 //int bar(int *X, int i) { return X[i & 255]; }
2268 SDNode *BinOpLHSVal = LHS->getOperand(0).Val;
2269 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2270 BinOpLHSVal->getOpcode() != ISD::SRA &&
2271 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2272 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
2273 return SDOperand();
2274
Duncan Sands83ec4b62008-06-06 12:08:01 +00002275 MVT VT = N->getValueType(0);
Chris Lattnere70da202007-12-06 07:33:36 +00002276
2277 // If this is a signed shift right, and the high bit is modified
2278 // by the logical operation, do not perform the transformation.
2279 // The highBitSet boolean indicates the value of the high bit of
2280 // the constant which would cause it to be modified for this
2281 // operation.
2282 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00002283 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2284 if (BinOpRHSSignSet != HighBitSet)
Chris Lattnere70da202007-12-06 07:33:36 +00002285 return SDOperand();
2286 }
2287
2288 // Fold the constants, shifting the binop RHS by the shift amount.
2289 SDOperand NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
2290 LHS->getOperand(1), N->getOperand(1));
2291
2292 // Create the new shift.
2293 SDOperand NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
2294 N->getOperand(1));
2295
2296 // Create the new binop.
2297 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2298}
2299
2300
Nate Begeman83e75ec2005-09-06 04:43:02 +00002301SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002302 SDOperand N0 = N->getOperand(0);
2303 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002304 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2305 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002306 MVT VT = N0.getValueType();
2307 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002308
2309 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002310 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002311 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002312 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002313 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002314 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002315 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002316 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002317 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002318 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002319 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002320 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002321 // if (shl x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002322 if (DAG.MaskedValueIsZero(SDOperand(N, 0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002323 APInt::getAllOnesValue(VT.getSizeInBits())))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002324 return DAG.getConstant(0, VT);
Chris Lattner61a4c072007-04-18 03:06:49 +00002325 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002326 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002327 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002328 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002329 N0.getOperand(1).getOpcode() == ISD::Constant) {
2330 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002331 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002332 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002333 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002334 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002335 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002336 }
2337 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2338 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002339 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002340 N0.getOperand(1).getOpcode() == ISD::Constant) {
2341 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002342 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002343 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
2344 DAG.getConstant(~0ULL << c1, VT));
2345 if (c2 > c1)
2346 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002347 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002348 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002349 return DAG.getNode(ISD::SRL, VT, Mask,
2350 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002351 }
2352 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002353 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002354 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002355 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnere70da202007-12-06 07:33:36 +00002356
2357 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002358}
2359
Nate Begeman83e75ec2005-09-06 04:43:02 +00002360SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002361 SDOperand N0 = N->getOperand(0);
2362 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002363 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2364 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002365 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002366
2367 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002368 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002369 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002370 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002371 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002372 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002373 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002374 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002375 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002376 // fold (sra x, c >= size(x)) -> undef
Duncan Sands83ec4b62008-06-06 12:08:01 +00002377 if (N1C && N1C->getValue() >= VT.getSizeInBits())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002378 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002379 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002380 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002381 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002382 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2383 // sext_inreg.
2384 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002385 unsigned LowBits = VT.getSizeInBits() - (unsigned)N1C->getValue();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002386 MVT EVT = MVT::getIntegerVT(LowBits);
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002387 if (EVT.isSimple() && // TODO: remove when apint codegen support lands.
2388 (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT)))
Nate Begemanfb7217b2006-02-17 19:54:08 +00002389 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2390 DAG.getValueType(EVT));
2391 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002392
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002393 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2394 if (N1C && N0.getOpcode() == ISD::SRA) {
2395 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2396 unsigned Sum = N1C->getValue() + C1->getValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002397 if (Sum >= VT.getSizeInBits()) Sum = VT.getSizeInBits()-1;
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002398 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2399 DAG.getConstant(Sum, N1C->getValueType(0)));
2400 }
2401 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00002402
2403 // fold sra (shl X, m), result_size - n
2404 // -> (sign_extend (trunc (shl X, result_size - n - m))) for
Christopher Lambb9b04282008-03-20 04:31:39 +00002405 // result_size - n != m.
2406 // If truncate is free for the target sext(shl) is likely to result in better
2407 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002408 if (N0.getOpcode() == ISD::SHL) {
2409 // Get the two constanst of the shifts, CN0 = m, CN = n.
2410 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2411 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002412 // Determine what the truncate's result bitsize and type would be.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002413 unsigned VTValSize = VT.getSizeInBits();
2414 MVT TruncVT =
2415 MVT::getIntegerVT(VTValSize - N1C->getValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00002416 // Determine the residual right-shift amount.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002417 unsigned ShiftAmt = N1C->getValue() - N01C->getValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002418
Christopher Lambb9b04282008-03-20 04:31:39 +00002419 // If the shift is not a no-op (in which case this should be just a sign
2420 // extend already), the truncated to type is legal, sign_extend is legal
2421 // on that type, and the the truncate to that type is both legal and free,
2422 // perform the transform.
2423 if (ShiftAmt &&
Christopher Lambb9b04282008-03-20 04:31:39 +00002424 TLI.isOperationLegal(ISD::SIGN_EXTEND, TruncVT) &&
2425 TLI.isOperationLegal(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00002426 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002427
2428 SDOperand Amt = DAG.getConstant(ShiftAmt, TLI.getShiftAmountTy());
2429 SDOperand Shift = DAG.getNode(ISD::SRL, VT, N0.getOperand(0), Amt);
2430 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, TruncVT, Shift);
2431 return DAG.getNode(ISD::SIGN_EXTEND, N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00002432 }
2433 }
2434 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002435
Chris Lattnera8504462006-05-08 20:51:54 +00002436 // Simplify, based on bits shifted out of the LHS.
2437 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2438 return SDOperand(N, 0);
2439
2440
Nate Begeman1d4d4142005-09-01 00:19:25 +00002441 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002442 if (DAG.SignBitIsZero(N0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002443 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002444
2445 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002446}
2447
Nate Begeman83e75ec2005-09-06 04:43:02 +00002448SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002449 SDOperand N0 = N->getOperand(0);
2450 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002451 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2452 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002453 MVT VT = N0.getValueType();
2454 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002455
2456 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002457 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002458 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002459 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002460 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002461 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002462 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002463 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002464 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002465 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002466 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002467 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002468 // if (srl x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002469 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0),
2470 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002471 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002472
Nate Begeman1d4d4142005-09-01 00:19:25 +00002473 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002474 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002475 N0.getOperand(1).getOpcode() == ISD::Constant) {
2476 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002477 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002478 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002479 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002480 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002481 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002482 }
Chris Lattner350bec02006-04-02 06:11:11 +00002483
Chris Lattner06afe072006-05-05 22:53:17 +00002484 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2485 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2486 // Shifting in all undef bits?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002487 MVT SmallVT = N0.getOperand(0).getValueType();
2488 if (N1C->getValue() >= SmallVT.getSizeInBits())
Chris Lattner06afe072006-05-05 22:53:17 +00002489 return DAG.getNode(ISD::UNDEF, VT);
2490
2491 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
2492 AddToWorkList(SmallShift.Val);
2493 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2494 }
2495
Chris Lattner3657ffe2006-10-12 20:23:19 +00002496 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2497 // bit, which is unmodified by sra.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002498 if (N1C && N1C->getValue()+1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00002499 if (N0.getOpcode() == ISD::SRA)
2500 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2501 }
2502
Chris Lattner350bec02006-04-02 06:11:11 +00002503 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2504 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002505 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00002506 APInt KnownZero, KnownOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002507 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00002508 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002509
2510 // If any of the input bits are KnownOne, then the input couldn't be all
2511 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002512 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Chris Lattner350bec02006-04-02 06:11:11 +00002513
2514 // If all of the bits input the to ctlz node are known to be zero, then
2515 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002516 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00002517 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2518
2519 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2520 if ((UnknownBits & (UnknownBits-1)) == 0) {
2521 // Okay, we know that only that the single bit specified by UnknownBits
2522 // could be set on input to the CTLZ node. If this bit is set, the SRL
2523 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2524 // to an SRL,XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002525 unsigned ShAmt = UnknownBits.countTrailingZeros();
Chris Lattner350bec02006-04-02 06:11:11 +00002526 SDOperand Op = N0.getOperand(0);
2527 if (ShAmt) {
2528 Op = DAG.getNode(ISD::SRL, VT, Op,
2529 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
2530 AddToWorkList(Op.Val);
2531 }
2532 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2533 }
2534 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002535
2536 // fold operands of srl based on knowledge that the low bits are not
2537 // demanded.
2538 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2539 return SDOperand(N, 0);
2540
Chris Lattnere70da202007-12-06 07:33:36 +00002541 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002542}
2543
Nate Begeman83e75ec2005-09-06 04:43:02 +00002544SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002545 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002546 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002547
2548 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002549 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002550 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002551 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002552}
2553
Nate Begeman83e75ec2005-09-06 04:43:02 +00002554SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002555 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002556 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002557
2558 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002559 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002560 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002561 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002562}
2563
Nate Begeman83e75ec2005-09-06 04:43:02 +00002564SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002565 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002566 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002567
2568 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002569 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002570 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002571 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002572}
2573
Nate Begeman452d7be2005-09-16 00:54:12 +00002574SDOperand DAGCombiner::visitSELECT(SDNode *N) {
2575 SDOperand N0 = N->getOperand(0);
2576 SDOperand N1 = N->getOperand(1);
2577 SDOperand N2 = N->getOperand(2);
2578 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2579 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2580 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002581 MVT VT = N->getValueType(0);
2582 MVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002583
Nate Begeman452d7be2005-09-16 00:54:12 +00002584 // fold select C, X, X -> X
2585 if (N1 == N2)
2586 return N1;
2587 // fold select true, X, Y -> X
2588 if (N0C && !N0C->isNullValue())
2589 return N1;
2590 // fold select false, X, Y -> Y
2591 if (N0C && N0C->isNullValue())
2592 return N2;
2593 // fold select C, 1, X -> C | X
Duncan Sands83ec4b62008-06-06 12:08:01 +00002594 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002595 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Cheng571c4782007-08-18 05:57:05 +00002596 // fold select C, 0, 1 -> ~C
Duncan Sands83ec4b62008-06-06 12:08:01 +00002597 if (VT.isInteger() && VT0.isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00002598 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Evan Cheng571c4782007-08-18 05:57:05 +00002599 SDOperand XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
2600 if (VT == VT0)
2601 return XORNode;
2602 AddToWorkList(XORNode.Val);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002603 if (VT.bitsGT(VT0))
Evan Cheng571c4782007-08-18 05:57:05 +00002604 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2605 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2606 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002607 // fold select C, 0, X -> ~C & X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002608 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
2609 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002610 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002611 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2612 }
2613 // fold select C, X, 1 -> ~C | X
Dan Gohman002e5d02008-03-13 22:13:53 +00002614 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002615 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::OR, VT, XORNode, N1);
2618 }
2619 // fold select C, X, 0 -> C & X
2620 // FIXME: this should check for C type == X type, not i1?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002621 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Nate Begeman452d7be2005-09-16 00:54:12 +00002622 return DAG.getNode(ISD::AND, VT, N0, N1);
2623 // fold X ? X : Y --> X ? 1 : Y --> X | Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002624 if (VT == MVT::i1 && N0 == N1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002625 return DAG.getNode(ISD::OR, VT, N0, N2);
2626 // fold X ? Y : X --> X ? Y : 0 --> X & Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002627 if (VT == MVT::i1 && N0 == N2)
Nate Begeman452d7be2005-09-16 00:54:12 +00002628 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002629
Chris Lattner40c62d52005-10-18 06:04:22 +00002630 // If we can fold this based on the true/false value, do so.
2631 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00002632 return SDOperand(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002633
Nate Begeman44728a72005-09-19 22:34:01 +00002634 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002635 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002636 // FIXME:
2637 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2638 // having to say they don't support SELECT_CC on every type the DAG knows
2639 // about, since there is no way to mark an opcode illegal at all value types
2640 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2641 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2642 N1, N2, N0.getOperand(2));
2643 else
2644 return SimplifySelect(N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002645 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002646 return SDOperand();
2647}
2648
2649SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00002650 SDOperand N0 = N->getOperand(0);
2651 SDOperand N1 = N->getOperand(1);
2652 SDOperand N2 = N->getOperand(2);
2653 SDOperand N3 = N->getOperand(3);
2654 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002655 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2656
Nate Begeman44728a72005-09-19 22:34:01 +00002657 // fold select_cc lhs, rhs, x, x, cc -> x
2658 if (N2 == N3)
2659 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002660
Chris Lattner5f42a242006-09-20 06:19:26 +00002661 // Determine if the condition we're dealing with is constant
Scott Michel5b8f82e2008-03-10 15:42:14 +00002662 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002663 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002664
2665 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002666 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00002667 return N2; // cond always true -> true val
2668 else
2669 return N3; // cond always false -> false val
2670 }
2671
2672 // Fold to a simpler select_cc
2673 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2674 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2675 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2676 SCC.getOperand(2));
2677
Chris Lattner40c62d52005-10-18 06:04:22 +00002678 // If we can fold this based on the true/false value, do so.
2679 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00002680 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002681
Nate Begeman44728a72005-09-19 22:34:01 +00002682 // fold select_cc into other things, such as min/max/abs
2683 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002684}
2685
2686SDOperand DAGCombiner::visitSETCC(SDNode *N) {
2687 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2688 cast<CondCodeSDNode>(N->getOperand(2))->get());
2689}
2690
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002691// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2692// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2693// transformation. Returns true if extension are possible and the above
2694// mentioned transformation is profitable.
2695static bool ExtendUsesToFormExtLoad(SDNode *N, SDOperand N0,
2696 unsigned ExtOpc,
2697 SmallVector<SDNode*, 4> &ExtendNodes,
2698 TargetLowering &TLI) {
2699 bool HasCopyToRegUses = false;
2700 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
2701 for (SDNode::use_iterator UI = N0.Val->use_begin(), UE = N0.Val->use_end();
2702 UI != UE; ++UI) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00002703 SDNode *User = UI->getUser();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002704 if (User == N)
2705 continue;
2706 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2707 if (User->getOpcode() == ISD::SETCC) {
2708 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2709 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2710 // Sign bits will be lost after a zext.
2711 return false;
2712 bool Add = false;
2713 for (unsigned i = 0; i != 2; ++i) {
2714 SDOperand UseOp = User->getOperand(i);
2715 if (UseOp == N0)
2716 continue;
2717 if (!isa<ConstantSDNode>(UseOp))
2718 return false;
2719 Add = true;
2720 }
2721 if (Add)
2722 ExtendNodes.push_back(User);
2723 } else {
2724 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2725 SDOperand UseOp = User->getOperand(i);
2726 if (UseOp == N0) {
2727 // If truncate from extended type to original load type is free
2728 // on this target, then it's ok to extend a CopyToReg.
2729 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2730 HasCopyToRegUses = true;
2731 else
2732 return false;
2733 }
2734 }
2735 }
2736 }
2737
2738 if (HasCopyToRegUses) {
2739 bool BothLiveOut = false;
2740 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2741 UI != UE; ++UI) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00002742 SDNode *User = UI->getUser();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002743 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2744 SDOperand UseOp = User->getOperand(i);
2745 if (UseOp.Val == N && UseOp.ResNo == 0) {
2746 BothLiveOut = true;
2747 break;
2748 }
2749 }
2750 }
2751 if (BothLiveOut)
2752 // Both unextended and extended values are live out. There had better be
2753 // good a reason for the transformation.
2754 return ExtendNodes.size();
2755 }
2756 return true;
2757}
2758
Nate Begeman83e75ec2005-09-06 04:43:02 +00002759SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002760 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002761 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002762
Nate Begeman1d4d4142005-09-01 00:19:25 +00002763 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002764 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002765 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002766
Nate Begeman1d4d4142005-09-01 00:19:25 +00002767 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002768 // fold (sext (aext x)) -> (sext x)
2769 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002770 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002771
Chris Lattner22558872007-02-26 03:13:59 +00002772 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002773 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2774 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Evan Chengc88138f2007-03-22 01:54:19 +00002775 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002776 if (NarrowLoad.Val) {
2777 if (NarrowLoad.Val != N0.Val)
2778 CombineTo(N0.Val, NarrowLoad);
2779 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2780 }
Evan Chengc88138f2007-03-22 01:54:19 +00002781
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002782 // See if the value being truncated is already sign extended. If so, just
2783 // eliminate the trunc/sext pair.
Chris Lattner6007b842006-09-21 06:00:20 +00002784 SDOperand Op = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002785 unsigned OpBits = Op.getValueType().getSizeInBits();
2786 unsigned MidBits = N0.getValueType().getSizeInBits();
2787 unsigned DestBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002788 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002789
2790 if (OpBits == DestBits) {
2791 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2792 // bits, it is already ready.
2793 if (NumSignBits > DestBits-MidBits)
2794 return Op;
2795 } else if (OpBits < DestBits) {
2796 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2797 // bits, just sext from i32.
2798 if (NumSignBits > OpBits-MidBits)
2799 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2800 } else {
2801 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2802 // bits, just truncate to i32.
2803 if (NumSignBits > OpBits-MidBits)
2804 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002805 }
Chris Lattner22558872007-02-26 03:13:59 +00002806
2807 // fold (sext (truncate x)) -> (sextinreg x).
2808 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2809 N0.getValueType())) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00002810 if (Op.getValueType().bitsLT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002811 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002812 else if (Op.getValueType().bitsGT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002813 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2814 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2815 DAG.getValueType(N0.getValueType()));
2816 }
Chris Lattner6007b842006-09-21 06:00:20 +00002817 }
Chris Lattner310b5782006-05-06 23:06:26 +00002818
Evan Cheng110dec22005-12-14 02:19:23 +00002819 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002820 if (ISD::isNON_EXTLoad(N0.Val) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002821 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
2822 TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002823 bool DoXform = true;
2824 SmallVector<SDNode*, 4> SetCCs;
2825 if (!N0.hasOneUse())
2826 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
2827 if (DoXform) {
2828 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2829 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2830 LN0->getBasePtr(), LN0->getSrcValue(),
2831 LN0->getSrcValueOffset(),
2832 N0.getValueType(),
2833 LN0->isVolatile(),
2834 LN0->getAlignment());
2835 CombineTo(N, ExtLoad);
2836 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2837 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2838 // Extend SetCC uses if necessary.
2839 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2840 SDNode *SetCC = SetCCs[i];
2841 SmallVector<SDOperand, 4> Ops;
2842 for (unsigned j = 0; j != 2; ++j) {
2843 SDOperand SOp = SetCC->getOperand(j);
2844 if (SOp == Trunc)
2845 Ops.push_back(ExtLoad);
2846 else
2847 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
2848 }
2849 Ops.push_back(SetCC->getOperand(2));
2850 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2851 &Ops[0], Ops.size()));
2852 }
2853 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2854 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002855 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002856
2857 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2858 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002859 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2860 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002861 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002862 MVT EVT = LN0->getMemoryVT();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002863 if ((!AfterLegalize && !LN0->isVolatile()) ||
2864 TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002865 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2866 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002867 LN0->getSrcValueOffset(), EVT,
2868 LN0->isVolatile(),
2869 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002870 CombineTo(N, ExtLoad);
2871 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2872 ExtLoad.getValue(1));
2873 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2874 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002875 }
2876
Chris Lattner20a35c32007-04-11 05:32:27 +00002877 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2878 if (N0.getOpcode() == ISD::SETCC) {
2879 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002880 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2881 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2882 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2883 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002884 }
2885
Dan Gohman8f0ad582008-04-28 16:58:24 +00002886 // fold (sext x) -> (zext x) if the sign bit is known zero.
Dan Gohman187db7b2008-04-28 18:47:17 +00002887 if ((!AfterLegalize || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
2888 DAG.SignBitIsZero(N0))
Dan Gohman8f0ad582008-04-28 16:58:24 +00002889 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2890
Nate Begeman83e75ec2005-09-06 04:43:02 +00002891 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002892}
2893
Nate Begeman83e75ec2005-09-06 04:43:02 +00002894SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002895 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002896 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002897
Nate Begeman1d4d4142005-09-01 00:19:25 +00002898 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002899 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002900 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002901 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002902 // fold (zext (aext x)) -> (zext x)
2903 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002904 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002905
Evan Chengc88138f2007-03-22 01:54:19 +00002906 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2907 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002908 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002909 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002910 if (NarrowLoad.Val) {
2911 if (NarrowLoad.Val != N0.Val)
2912 CombineTo(N0.Val, NarrowLoad);
2913 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2914 }
Evan Chengc88138f2007-03-22 01:54:19 +00002915 }
2916
Chris Lattner6007b842006-09-21 06:00:20 +00002917 // fold (zext (truncate x)) -> (and x, mask)
2918 if (N0.getOpcode() == ISD::TRUNCATE &&
2919 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2920 SDOperand Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002921 if (Op.getValueType().bitsLT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00002922 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002923 } else if (Op.getValueType().bitsGT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00002924 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2925 }
2926 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2927 }
2928
Chris Lattner111c2282006-09-21 06:14:31 +00002929 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2930 if (N0.getOpcode() == ISD::AND &&
2931 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2932 N0.getOperand(1).getOpcode() == ISD::Constant) {
2933 SDOperand X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002934 if (X.getValueType().bitsLT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00002935 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002936 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00002937 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2938 }
Dan Gohman220a8232008-03-03 23:51:38 +00002939 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002940 Mask.zext(VT.getSizeInBits());
Chris Lattner111c2282006-09-21 06:14:31 +00002941 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2942 }
2943
Evan Cheng110dec22005-12-14 02:19:23 +00002944 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002945 if (ISD::isNON_EXTLoad(N0.Val) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002946 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
2947 TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002948 bool DoXform = true;
2949 SmallVector<SDNode*, 4> SetCCs;
2950 if (!N0.hasOneUse())
2951 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
2952 if (DoXform) {
2953 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2954 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2955 LN0->getBasePtr(), LN0->getSrcValue(),
2956 LN0->getSrcValueOffset(),
2957 N0.getValueType(),
2958 LN0->isVolatile(),
2959 LN0->getAlignment());
2960 CombineTo(N, ExtLoad);
2961 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2962 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2963 // Extend SetCC uses if necessary.
2964 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2965 SDNode *SetCC = SetCCs[i];
2966 SmallVector<SDOperand, 4> Ops;
2967 for (unsigned j = 0; j != 2; ++j) {
2968 SDOperand SOp = SetCC->getOperand(j);
2969 if (SOp == Trunc)
2970 Ops.push_back(ExtLoad);
2971 else
Evan Chengde1631b2007-10-30 20:11:21 +00002972 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002973 }
2974 Ops.push_back(SetCC->getOperand(2));
2975 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2976 &Ops[0], Ops.size()));
2977 }
2978 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2979 }
Evan Cheng110dec22005-12-14 02:19:23 +00002980 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002981
2982 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2983 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002984 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2985 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002986 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002987 MVT EVT = LN0->getMemoryVT();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002988 if ((!AfterLegalize && !LN0->isVolatile()) ||
2989 TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT)) {
2990 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2991 LN0->getBasePtr(), LN0->getSrcValue(),
2992 LN0->getSrcValueOffset(), EVT,
2993 LN0->isVolatile(),
2994 LN0->getAlignment());
2995 CombineTo(N, ExtLoad);
2996 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2997 ExtLoad.getValue(1));
2998 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2999 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003000 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003001
3002 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3003 if (N0.getOpcode() == ISD::SETCC) {
3004 SDOperand SCC =
3005 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3006 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003007 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
3008 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003009 }
3010
Nate Begeman83e75ec2005-09-06 04:43:02 +00003011 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003012}
3013
Chris Lattner5ffc0662006-05-05 05:58:59 +00003014SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
3015 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003016 MVT VT = N->getValueType(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00003017
3018 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003019 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00003020 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
3021 // fold (aext (aext x)) -> (aext x)
3022 // fold (aext (zext x)) -> (zext x)
3023 // fold (aext (sext x)) -> (sext x)
3024 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3025 N0.getOpcode() == ISD::ZERO_EXTEND ||
3026 N0.getOpcode() == ISD::SIGN_EXTEND)
3027 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
3028
Evan Chengc88138f2007-03-22 01:54:19 +00003029 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3030 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3031 if (N0.getOpcode() == ISD::TRUNCATE) {
3032 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00003033 if (NarrowLoad.Val) {
3034 if (NarrowLoad.Val != N0.Val)
3035 CombineTo(N0.Val, NarrowLoad);
3036 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
3037 }
Evan Chengc88138f2007-03-22 01:54:19 +00003038 }
3039
Chris Lattner84750582006-09-20 06:29:17 +00003040 // fold (aext (truncate x))
3041 if (N0.getOpcode() == ISD::TRUNCATE) {
3042 SDOperand TruncOp = N0.getOperand(0);
3043 if (TruncOp.getValueType() == VT)
3044 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003045 if (TruncOp.getValueType().bitsGT(VT))
Chris Lattner84750582006-09-20 06:29:17 +00003046 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
3047 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
3048 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00003049
3050 // fold (aext (and (trunc x), cst)) -> (and x, cst).
3051 if (N0.getOpcode() == ISD::AND &&
3052 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3053 N0.getOperand(1).getOpcode() == ISD::Constant) {
3054 SDOperand X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003055 if (X.getValueType().bitsLT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003056 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003057 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003058 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3059 }
Dan Gohman220a8232008-03-03 23:51:38 +00003060 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003061 Mask.zext(VT.getSizeInBits());
Chris Lattner0e4b9222006-09-21 06:40:43 +00003062 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3063 }
3064
Chris Lattner5ffc0662006-05-05 05:58:59 +00003065 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00003066 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003067 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3068 TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003069 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3070 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3071 LN0->getBasePtr(), LN0->getSrcValue(),
3072 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003073 N0.getValueType(),
3074 LN0->isVolatile(),
3075 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003076 CombineTo(N, ExtLoad);
3077 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3078 ExtLoad.getValue(1));
3079 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3080 }
3081
3082 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3083 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3084 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003085 if (N0.getOpcode() == ISD::LOAD &&
3086 !ISD::isNON_EXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003087 N0.hasOneUse()) {
3088 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003089 MVT EVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00003090 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
3091 LN0->getChain(), LN0->getBasePtr(),
3092 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003093 LN0->getSrcValueOffset(), EVT,
3094 LN0->isVolatile(),
3095 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003096 CombineTo(N, ExtLoad);
3097 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3098 ExtLoad.getValue(1));
3099 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3100 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003101
3102 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3103 if (N0.getOpcode() == ISD::SETCC) {
3104 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003105 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3106 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003107 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Chris Lattner1eba01e2007-04-11 06:50:51 +00003108 if (SCC.Val)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003109 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003110 }
3111
Chris Lattner5ffc0662006-05-05 05:58:59 +00003112 return SDOperand();
3113}
3114
Chris Lattner2b4c2792007-10-13 06:35:54 +00003115/// GetDemandedBits - See if the specified operand can be simplified with the
3116/// knowledge that only the bits specified by Mask are used. If so, return the
3117/// simpler operand, otherwise return a null SDOperand.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003118SDOperand DAGCombiner::GetDemandedBits(SDOperand V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003119 switch (V.getOpcode()) {
3120 default: break;
3121 case ISD::OR:
3122 case ISD::XOR:
3123 // If the LHS or RHS don't contribute bits to the or, drop them.
3124 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3125 return V.getOperand(1);
3126 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3127 return V.getOperand(0);
3128 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003129 case ISD::SRL:
3130 // Only look at single-use SRLs.
3131 if (!V.Val->hasOneUse())
3132 break;
3133 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3134 // See if we can recursively simplify the LHS.
3135 unsigned Amt = RHSC->getValue();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003136 APInt NewMask = Mask << Amt;
3137 SDOperand SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Chris Lattnere33544c2007-10-13 06:58:48 +00003138 if (SimplifyLHS.Val) {
3139 return DAG.getNode(ISD::SRL, V.getValueType(),
3140 SimplifyLHS, V.getOperand(1));
3141 }
3142 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003143 }
3144 return SDOperand();
3145}
3146
Evan Chengc88138f2007-03-22 01:54:19 +00003147/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3148/// bits and then truncated to a narrower type and where N is a multiple
3149/// of number of bits of the narrower type, transform it to a narrower load
3150/// from address + N / num of bits of new type. If the result is to be
3151/// extended, also fold the extension to form a extending load.
3152SDOperand DAGCombiner::ReduceLoadWidth(SDNode *N) {
3153 unsigned Opc = N->getOpcode();
3154 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
3155 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003156 MVT VT = N->getValueType(0);
3157 MVT EVT = N->getValueType(0);
Evan Chengc88138f2007-03-22 01:54:19 +00003158
Evan Chenge177e302007-03-23 22:13:36 +00003159 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3160 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003161 if (Opc == ISD::SIGN_EXTEND_INREG) {
3162 ExtType = ISD::SEXTLOAD;
3163 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00003164 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
3165 return SDOperand();
Evan Chengc88138f2007-03-22 01:54:19 +00003166 }
3167
Duncan Sands83ec4b62008-06-06 12:08:01 +00003168 unsigned EVTBits = EVT.getSizeInBits();
Evan Chengc88138f2007-03-22 01:54:19 +00003169 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00003170 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00003171 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3172 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3173 ShAmt = N01->getValue();
3174 // Is the shift amount a multiple of size of VT?
3175 if ((ShAmt & (EVTBits-1)) == 0) {
3176 N0 = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003177 if (N0.getValueType().getSizeInBits() <= EVTBits)
Evan Chengc88138f2007-03-22 01:54:19 +00003178 return SDOperand();
Evan Chengb37b80c2007-03-23 20:55:21 +00003179 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00003180 }
3181 }
3182 }
3183
Duncan Sandsad205a72008-06-16 08:14:38 +00003184 // Do not generate loads of non-round integer types since these can
3185 // be expensive (and would be wrong if the type is not byte sized).
3186 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() && VT.isRound() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003187 // Do not change the width of a volatile load.
3188 !cast<LoadSDNode>(N0)->isVolatile()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003189 assert(N0.getValueType().getSizeInBits() > EVTBits &&
Evan Chengc88138f2007-03-22 01:54:19 +00003190 "Cannot truncate to larger type!");
3191 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003192 MVT PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00003193 // For big endian targets, we need to adjust the offset to the pointer to
3194 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003195 if (TLI.isBigEndian()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003196 unsigned LVTStoreBits = N0.getValueType().getStoreSizeInBits();
3197 unsigned EVTStoreBits = EVT.getStoreSizeInBits();
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003198 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3199 }
Evan Chengdae54ce2007-03-24 00:02:43 +00003200 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003201 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Evan Chengc88138f2007-03-22 01:54:19 +00003202 SDOperand NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
3203 DAG.getConstant(PtrOff, PtrType));
3204 AddToWorkList(NewPtr.Val);
3205 SDOperand Load = (ExtType == ISD::NON_EXTLOAD)
3206 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003207 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Duncan Sandsdc846502007-10-28 12:59:45 +00003208 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003209 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003210 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00003211 LN0->isVolatile(), NewAlign);
Evan Chengc88138f2007-03-22 01:54:19 +00003212 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00003213 if (CombineSRL) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003214 WorkListRemover DeadNodes(*this);
3215 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3216 &DeadNodes);
Evan Chengb37b80c2007-03-23 20:55:21 +00003217 CombineTo(N->getOperand(0).Val, Load);
3218 } else
3219 CombineTo(N0.Val, Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00003220 if (ShAmt) {
3221 if (Opc == ISD::SIGN_EXTEND_INREG)
3222 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
3223 else
3224 return DAG.getNode(Opc, VT, Load);
3225 }
Evan Chengc88138f2007-03-22 01:54:19 +00003226 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3227 }
3228
3229 return SDOperand();
3230}
3231
Chris Lattner5ffc0662006-05-05 05:58:59 +00003232
Nate Begeman83e75ec2005-09-06 04:43:02 +00003233SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003234 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003235 SDOperand N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003236 MVT VT = N->getValueType(0);
3237 MVT EVT = cast<VTSDNode>(N1)->getVT();
3238 unsigned VTBits = VT.getSizeInBits();
3239 unsigned EVTBits = EVT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003240
Nate Begeman1d4d4142005-09-01 00:19:25 +00003241 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003242 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003243 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003244
Chris Lattner541a24f2006-05-06 22:43:44 +00003245 // If the input is already sign extended, just drop the extension.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003246 if (DAG.ComputeNumSignBits(N0) >= VT.getSizeInBits()-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003247 return N0;
3248
Nate Begeman646d7e22005-09-02 21:18:40 +00003249 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3250 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00003251 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003252 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003253 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003254
Chris Lattner95a5e052007-04-17 19:03:21 +00003255 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003256 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Nate Begemande996292006-02-03 22:24:05 +00003257 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003258
Chris Lattner95a5e052007-04-17 19:03:21 +00003259 // fold operands of sext_in_reg based on knowledge that the top bits are not
3260 // demanded.
3261 if (SimplifyDemandedBits(SDOperand(N, 0)))
3262 return SDOperand(N, 0);
3263
Evan Chengc88138f2007-03-22 01:54:19 +00003264 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3265 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
3266 SDOperand NarrowLoad = ReduceLoadWidth(N);
3267 if (NarrowLoad.Val)
3268 return NarrowLoad;
3269
Chris Lattner4b37e872006-05-08 21:18:59 +00003270 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3271 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3272 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3273 if (N0.getOpcode() == ISD::SRL) {
3274 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00003275 if (ShAmt->getValue()+EVTBits <= VT.getSizeInBits()) {
Chris Lattner4b37e872006-05-08 21:18:59 +00003276 // We can turn this into an SRA iff the input to the SRL is already sign
3277 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003278 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00003279 if (VT.getSizeInBits()-(ShAmt->getValue()+EVTBits) < InSignBits)
Chris Lattner4b37e872006-05-08 21:18:59 +00003280 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3281 }
3282 }
Evan Chengc88138f2007-03-22 01:54:19 +00003283
Nate Begemanded49632005-10-13 03:11:28 +00003284 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00003285 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003286 ISD::isUNINDEXEDLoad(N0.Val) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003287 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003288 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3289 TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003290 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3291 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3292 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003293 LN0->getSrcValueOffset(), EVT,
3294 LN0->isVolatile(),
3295 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003296 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003297 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003298 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003299 }
3300 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00003301 if (ISD::isZEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
3302 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003303 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003304 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3305 TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003306 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3307 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3308 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003309 LN0->getSrcValueOffset(), EVT,
3310 LN0->isVolatile(),
3311 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003312 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003313 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003314 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003315 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003316 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003317}
3318
Nate Begeman83e75ec2005-09-06 04:43:02 +00003319SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003320 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003321 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003322
3323 // noop truncate
3324 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003325 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003326 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003327 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003328 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003329 // fold (truncate (truncate x)) -> (truncate x)
3330 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003331 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003332 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003333 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3334 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00003335 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003336 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003337 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00003338 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003339 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003340 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003341 else
3342 // if the source and dest are the same type, we can drop both the extend
3343 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003344 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003345 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003346
Chris Lattner2b4c2792007-10-13 06:35:54 +00003347 // See if we can simplify the input to this truncate through knowledge that
3348 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3349 // -> trunc y
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003350 SDOperand Shorter =
3351 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00003352 VT.getSizeInBits()));
Chris Lattner2b4c2792007-10-13 06:35:54 +00003353 if (Shorter.Val)
3354 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3355
Nate Begeman3df4d522005-10-12 20:40:40 +00003356 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003357 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003358 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003359}
3360
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003361static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
3362 SDOperand Elt = N->getOperand(i);
3363 if (Elt.getOpcode() != ISD::MERGE_VALUES)
3364 return Elt.Val;
3365 return Elt.getOperand(Elt.ResNo).Val;
3366}
3367
3368/// CombineConsecutiveLoads - build_pair (load, load) -> load
3369/// if load locations are consecutive.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003370SDOperand DAGCombiner::CombineConsecutiveLoads(SDNode *N, MVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003371 assert(N->getOpcode() == ISD::BUILD_PAIR);
3372
3373 SDNode *LD1 = getBuildPairElt(N, 0);
3374 if (!ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse())
3375 return SDOperand();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003376 MVT LD1VT = LD1->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003377 SDNode *LD2 = getBuildPairElt(N, 1);
3378 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3379 if (ISD::isNON_EXTLoad(LD2) &&
3380 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003381 // If both are volatile this would reduce the number of volatile loads.
3382 // If one is volatile it might be ok, but play conservative and bail out.
3383 !cast<LoadSDNode>(LD1)->isVolatile() &&
3384 !cast<LoadSDNode>(LD2)->isVolatile() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003385 TLI.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1, MFI)) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003386 LoadSDNode *LD = cast<LoadSDNode>(LD1);
3387 unsigned Align = LD->getAlignment();
3388 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003389 getABITypeAlignment(VT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003390 if (NewAlign <= Align &&
3391 (!AfterLegalize || TLI.isOperationLegal(ISD::LOAD, VT)))
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003392 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(),
3393 LD->getSrcValue(), LD->getSrcValueOffset(),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003394 false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003395 }
3396 return SDOperand();
3397}
3398
Chris Lattner94683772005-12-23 05:30:37 +00003399SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3400 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003401 MVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00003402
Dan Gohman7f321562007-06-25 16:23:39 +00003403 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3404 // Only do this before legalize, since afterward the target may be depending
3405 // on the bitconvert.
3406 // First check to see if this is all constant.
3407 if (!AfterLegalize &&
3408 N0.getOpcode() == ISD::BUILD_VECTOR && N0.Val->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003409 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003410 bool isSimple = true;
3411 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3412 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3413 N0.getOperand(i).getOpcode() != ISD::Constant &&
3414 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3415 isSimple = false;
3416 break;
3417 }
3418
Duncan Sands83ec4b62008-06-06 12:08:01 +00003419 MVT DestEltVT = N->getValueType(0).getVectorElementType();
3420 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00003421 "Element type of vector ValueType must not be vector!");
3422 if (isSimple) {
3423 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.Val, DestEltVT);
3424 }
3425 }
3426
Chris Lattner94683772005-12-23 05:30:37 +00003427 // If the input is a constant, let getNode() fold it.
3428 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
3429 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3430 if (Res.Val != N) return Res;
3431 }
3432
Chris Lattnerc8547d82005-12-23 05:37:50 +00003433 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3434 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003435
Chris Lattner57104102005-12-23 05:44:41 +00003436 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003437 // If the resultant load doesn't need a higher alignment than the original!
3438 if (ISD::isNormalLoad(N0.Val) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003439 // Do not change the width of a volatile load.
3440 !cast<LoadSDNode>(N0)->isVolatile() &&
3441 (!AfterLegalize || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003442 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng59d5b682007-05-07 21:27:48 +00003443 unsigned Align = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003444 getABITypeAlignment(VT.getTypeForMVT());
Evan Cheng59d5b682007-05-07 21:27:48 +00003445 unsigned OrigAlign = LN0->getAlignment();
3446 if (Align <= OrigAlign) {
3447 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
3448 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Dan Gohman77455612008-06-28 00:45:22 +00003449 LN0->isVolatile(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00003450 AddToWorkList(N);
3451 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
3452 Load.getValue(1));
3453 return Load;
3454 }
Chris Lattner57104102005-12-23 05:44:41 +00003455 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003456
Chris Lattner3bd39d42008-01-27 17:42:27 +00003457 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3458 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3459 // This often reduces constant pool loads.
3460 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003461 N0.Val->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003462 SDOperand NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3463 AddToWorkList(NewConv.Val);
3464
Duncan Sands83ec4b62008-06-06 12:08:01 +00003465 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003466 if (N0.getOpcode() == ISD::FNEG)
3467 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3468 assert(N0.getOpcode() == ISD::FABS);
3469 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3470 }
3471
3472 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3473 // Note that we don't handle copysign(x,cst) because this can always be folded
3474 // to an fneg or fabs.
3475 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00003476 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003477 VT.isInteger() && !VT.isVector()) {
3478 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
3479 SDOperand X = DAG.getNode(ISD::BIT_CONVERT,
3480 MVT::getIntegerVT(OrigXWidth),
Chris Lattner3bd39d42008-01-27 17:42:27 +00003481 N0.getOperand(1));
3482 AddToWorkList(X.Val);
3483
3484 // If X has a different width than the result/lhs, sext it or truncate it.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003485 unsigned VTWidth = VT.getSizeInBits();
Chris Lattner3bd39d42008-01-27 17:42:27 +00003486 if (OrigXWidth < VTWidth) {
3487 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
3488 AddToWorkList(X.Val);
3489 } else if (OrigXWidth > VTWidth) {
3490 // To get the sign bit in the right place, we have to shift it right
3491 // before truncating.
3492 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3493 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
3494 AddToWorkList(X.Val);
3495 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3496 AddToWorkList(X.Val);
3497 }
3498
Duncan Sands83ec4b62008-06-06 12:08:01 +00003499 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003500 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
3501 AddToWorkList(X.Val);
3502
3503 SDOperand Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3504 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
3505 AddToWorkList(Cst.Val);
3506
3507 return DAG.getNode(ISD::OR, VT, X, Cst);
3508 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003509
3510 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
3511 if (N0.getOpcode() == ISD::BUILD_PAIR) {
3512 SDOperand CombineLD = CombineConsecutiveLoads(N0.Val, VT);
3513 if (CombineLD.Val)
3514 return CombineLD;
3515 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003516
Chris Lattner94683772005-12-23 05:30:37 +00003517 return SDOperand();
3518}
3519
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003520SDOperand DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003521 MVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003522 return CombineConsecutiveLoads(N, VT);
3523}
3524
Dan Gohman7f321562007-06-25 16:23:39 +00003525/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003526/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3527/// destination element value type.
3528SDOperand DAGCombiner::
Duncan Sands83ec4b62008-06-06 12:08:01 +00003529ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT DstEltVT) {
3530 MVT SrcEltVT = BV->getOperand(0).getValueType();
Chris Lattner6258fb22006-04-02 02:53:43 +00003531
3532 // If this is already the right type, we're done.
3533 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
3534
Duncan Sands83ec4b62008-06-06 12:08:01 +00003535 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
3536 unsigned DstBitSize = DstEltVT.getSizeInBits();
Chris Lattner6258fb22006-04-02 02:53:43 +00003537
3538 // If this is a conversion of N elements of one type to N elements of another
3539 // type, convert each element. This handles FP<->INT cases.
3540 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003541 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003542 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003543 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00003544 AddToWorkList(Ops.back().Val);
3545 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003546 MVT VT = MVT::getVectorVT(DstEltVT,
3547 BV->getValueType(0).getVectorNumElements());
Dan Gohman7f321562007-06-25 16:23:39 +00003548 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003549 }
3550
3551 // Otherwise, we're growing or shrinking the elements. To avoid having to
3552 // handle annoying details of growing/shrinking FP values, we convert them to
3553 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003554 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003555 // Convert the input float vector to a int vector where the elements are the
3556 // same sizes.
3557 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003558 MVT IntVT = MVT::getIntegerVT(SrcEltVT.getSizeInBits());
Dan Gohman7f321562007-06-25 16:23:39 +00003559 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003560 SrcEltVT = IntVT;
3561 }
3562
3563 // Now we know the input is an integer vector. If the output is a FP type,
3564 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003565 if (DstEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003566 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003567 MVT TmpVT = MVT::getIntegerVT(DstEltVT.getSizeInBits());
Dan Gohman7f321562007-06-25 16:23:39 +00003568 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003569
3570 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003571 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003572 }
3573
3574 // Okay, we know the src/dst types are both integers of differing types.
3575 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003576 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00003577 if (SrcBitSize < DstBitSize) {
3578 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3579
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003580 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003581 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003582 i += NumInputsPerOutput) {
3583 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00003584 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003585 bool EltIsUndef = true;
3586 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3587 // Shift the previously computed bits over.
3588 NewBits <<= SrcBitSize;
3589 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
3590 if (Op.getOpcode() == ISD::UNDEF) continue;
3591 EltIsUndef = false;
3592
Dan Gohman220a8232008-03-03 23:51:38 +00003593 NewBits |=
3594 APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003595 }
3596
3597 if (EltIsUndef)
3598 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3599 else
3600 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3601 }
3602
Duncan Sands83ec4b62008-06-06 12:08:01 +00003603 MVT VT = MVT::getVectorVT(DstEltVT, Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00003604 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003605 }
3606
3607 // Finally, this must be the case where we are shrinking elements: each input
3608 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00003609 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00003610 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003611 MVT VT = MVT::getVectorVT(DstEltVT, NumOutputsPerInput*BV->getNumOperands());
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003612 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003613 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003614 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3615 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3616 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3617 continue;
3618 }
Dan Gohman220a8232008-03-03 23:51:38 +00003619 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getAPIntValue();
Chris Lattner6258fb22006-04-02 02:53:43 +00003620 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohman220a8232008-03-03 23:51:38 +00003621 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003622 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohman220a8232008-03-03 23:51:38 +00003623 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00003624 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
3625 return DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00003626 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003627 }
3628
3629 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00003630 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00003631 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3632 }
Dan Gohman7f321562007-06-25 16:23:39 +00003633 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003634}
3635
3636
3637
Chris Lattner01b3d732005-09-28 22:28:18 +00003638SDOperand DAGCombiner::visitFADD(SDNode *N) {
3639 SDOperand N0 = N->getOperand(0);
3640 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003641 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3642 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003643 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003644
Dan Gohman7f321562007-06-25 16:23:39 +00003645 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003646 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00003647 SDOperand FoldedVOp = SimplifyVBinOp(N);
3648 if (FoldedVOp.Val) return FoldedVOp;
3649 }
Dan Gohman7f321562007-06-25 16:23:39 +00003650
Nate Begemana0e221d2005-10-18 00:28:13 +00003651 // fold (fadd c1, c2) -> c1+c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003652 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003653 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003654 // canonicalize constant to RHS
3655 if (N0CFP && !N1CFP)
3656 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003657 // fold (A + (-B)) -> A-B
Chris Lattner0254e702008-02-26 07:04:54 +00003658 if (isNegatibleForFree(N1, AfterLegalize) == 2)
3659 return DAG.getNode(ISD::FSUB, VT, N0,
3660 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner01b3d732005-09-28 22:28:18 +00003661 // fold ((-A) + B) -> B-A
Chris Lattner0254e702008-02-26 07:04:54 +00003662 if (isNegatibleForFree(N0, AfterLegalize) == 2)
3663 return DAG.getNode(ISD::FSUB, VT, N1,
3664 GetNegatedExpression(N0, DAG, AfterLegalize));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003665
3666 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3667 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
3668 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3669 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3670 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3671
Chris Lattner01b3d732005-09-28 22:28:18 +00003672 return SDOperand();
3673}
3674
3675SDOperand DAGCombiner::visitFSUB(SDNode *N) {
3676 SDOperand N0 = N->getOperand(0);
3677 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003678 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3679 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003680 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003681
Dan Gohman7f321562007-06-25 16:23:39 +00003682 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003683 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00003684 SDOperand FoldedVOp = SimplifyVBinOp(N);
3685 if (FoldedVOp.Val) return FoldedVOp;
3686 }
Dan Gohman7f321562007-06-25 16:23:39 +00003687
Nate Begemana0e221d2005-10-18 00:28:13 +00003688 // fold (fsub c1, c2) -> c1-c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003689 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003690 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003691 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003692 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Chris Lattner0254e702008-02-26 07:04:54 +00003693 if (isNegatibleForFree(N1, AfterLegalize))
3694 return GetNegatedExpression(N1, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00003695 return DAG.getNode(ISD::FNEG, VT, N1);
3696 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003697 // fold (A-(-B)) -> A+B
Chris Lattner0254e702008-02-26 07:04:54 +00003698 if (isNegatibleForFree(N1, AfterLegalize))
3699 return DAG.getNode(ISD::FADD, VT, N0,
3700 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003701
Chris Lattner01b3d732005-09-28 22:28:18 +00003702 return SDOperand();
3703}
3704
3705SDOperand DAGCombiner::visitFMUL(SDNode *N) {
3706 SDOperand N0 = N->getOperand(0);
3707 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003708 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3709 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003710 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003711
Dan Gohman7f321562007-06-25 16:23:39 +00003712 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003713 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00003714 SDOperand FoldedVOp = SimplifyVBinOp(N);
3715 if (FoldedVOp.Val) return FoldedVOp;
3716 }
Dan Gohman7f321562007-06-25 16:23:39 +00003717
Nate Begeman11af4ea2005-10-17 20:40:11 +00003718 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003719 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003720 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003721 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003722 if (N0CFP && !N1CFP)
3723 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003724 // fold (fmul X, 2.0) -> (fadd X, X)
3725 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3726 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003727 // fold (fmul X, -1.0) -> (fneg X)
3728 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3729 return DAG.getNode(ISD::FNEG, VT, N0);
3730
3731 // -X * -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003732 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3733 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003734 // Both can be negated for free, check to see if at least one is cheaper
3735 // negated.
3736 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003737 return DAG.getNode(ISD::FMUL, VT,
3738 GetNegatedExpression(N0, DAG, AfterLegalize),
3739 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003740 }
3741 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003742
3743 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3744 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
3745 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3746 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3747 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3748
Chris Lattner01b3d732005-09-28 22:28:18 +00003749 return SDOperand();
3750}
3751
3752SDOperand DAGCombiner::visitFDIV(SDNode *N) {
3753 SDOperand N0 = N->getOperand(0);
3754 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003755 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3756 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003757 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003758
Dan Gohman7f321562007-06-25 16:23:39 +00003759 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003760 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00003761 SDOperand FoldedVOp = SimplifyVBinOp(N);
3762 if (FoldedVOp.Val) return FoldedVOp;
3763 }
Dan Gohman7f321562007-06-25 16:23:39 +00003764
Nate Begemana148d982006-01-18 22:35:16 +00003765 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003766 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003767 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003768
3769
3770 // -X / -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003771 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3772 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003773 // Both can be negated for free, check to see if at least one is cheaper
3774 // negated.
3775 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003776 return DAG.getNode(ISD::FDIV, VT,
3777 GetNegatedExpression(N0, DAG, AfterLegalize),
3778 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003779 }
3780 }
3781
Chris Lattner01b3d732005-09-28 22:28:18 +00003782 return SDOperand();
3783}
3784
3785SDOperand DAGCombiner::visitFREM(SDNode *N) {
3786 SDOperand N0 = N->getOperand(0);
3787 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003788 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3789 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003790 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003791
Nate Begemana148d982006-01-18 22:35:16 +00003792 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003793 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003794 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003795
Chris Lattner01b3d732005-09-28 22:28:18 +00003796 return SDOperand();
3797}
3798
Chris Lattner12d83032006-03-05 05:30:57 +00003799SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3800 SDOperand N0 = N->getOperand(0);
3801 SDOperand N1 = N->getOperand(1);
3802 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3803 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003804 MVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00003805
Dale Johannesendb44bf82007-10-16 23:38:29 +00003806 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00003807 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3808
3809 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003810 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003811 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3812 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dale Johannesen87503a62007-08-25 22:10:57 +00003813 if (!V.isNegative())
Chris Lattner12d83032006-03-05 05:30:57 +00003814 return DAG.getNode(ISD::FABS, VT, N0);
3815 else
3816 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3817 }
3818
3819 // copysign(fabs(x), y) -> copysign(x, y)
3820 // copysign(fneg(x), y) -> copysign(x, y)
3821 // copysign(copysign(x,z), y) -> copysign(x, y)
3822 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3823 N0.getOpcode() == ISD::FCOPYSIGN)
3824 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3825
3826 // copysign(x, abs(y)) -> abs(x)
3827 if (N1.getOpcode() == ISD::FABS)
3828 return DAG.getNode(ISD::FABS, VT, N0);
3829
3830 // copysign(x, copysign(y,z)) -> copysign(x, z)
3831 if (N1.getOpcode() == ISD::FCOPYSIGN)
3832 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3833
3834 // copysign(x, fp_extend(y)) -> copysign(x, y)
3835 // copysign(x, fp_round(y)) -> copysign(x, y)
3836 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3837 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3838
3839 return SDOperand();
3840}
3841
3842
Chris Lattner01b3d732005-09-28 22:28:18 +00003843
Nate Begeman83e75ec2005-09-06 04:43:02 +00003844SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003845 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003846 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003847 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00003848 MVT OpVT = N0.getValueType();
3849
Nate Begeman1d4d4142005-09-01 00:19:25 +00003850 // fold (sint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00003851 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003852 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00003853
3854 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
3855 // but UINT_TO_FP is legal on this target, try to convert.
Chris Lattnerf77e46b2008-06-26 17:16:00 +00003856 if (!TLI.isOperationLegal(ISD::SINT_TO_FP, OpVT) &&
Chris Lattnercda88752008-06-26 00:16:49 +00003857 TLI.isOperationLegal(ISD::UINT_TO_FP, OpVT)) {
3858 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
3859 if (DAG.SignBitIsZero(N0))
3860 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
3861 }
3862
3863
Nate Begeman83e75ec2005-09-06 04:43:02 +00003864 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003865}
3866
Nate Begeman83e75ec2005-09-06 04:43:02 +00003867SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003868 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003869 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003870 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00003871 MVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00003872
Nate Begeman1d4d4142005-09-01 00:19:25 +00003873 // fold (uint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00003874 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003875 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00003876
3877 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
3878 // but SINT_TO_FP is legal on this target, try to convert.
Chris Lattnerf77e46b2008-06-26 17:16:00 +00003879 if (!TLI.isOperationLegal(ISD::UINT_TO_FP, OpVT) &&
Chris Lattnercda88752008-06-26 00:16:49 +00003880 TLI.isOperationLegal(ISD::SINT_TO_FP, OpVT)) {
3881 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
3882 if (DAG.SignBitIsZero(N0))
3883 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
3884 }
3885
Nate Begeman83e75ec2005-09-06 04:43:02 +00003886 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003887}
3888
Nate Begeman83e75ec2005-09-06 04:43:02 +00003889SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003890 SDOperand N0 = N->getOperand(0);
3891 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003892 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003893
3894 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003895 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003896 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003897 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003898}
3899
Nate Begeman83e75ec2005-09-06 04:43:02 +00003900SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003901 SDOperand N0 = N->getOperand(0);
3902 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003903 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003904
3905 // fold (fp_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00003906 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003907 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003908 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003909}
3910
Nate Begeman83e75ec2005-09-06 04:43:02 +00003911SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003912 SDOperand N0 = N->getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003913 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003914 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003915 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003916
3917 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003918 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner0bd48932008-01-17 07:00:52 +00003919 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003920
3921 // fold (fp_round (fp_extend x)) -> x
3922 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3923 return N0.getOperand(0);
3924
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00003925 // fold (fp_round (fp_round x)) -> (fp_round x)
3926 if (N0.getOpcode() == ISD::FP_ROUND) {
3927 // This is a value preserving truncation if both round's are.
3928 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
3929 N0.Val->getConstantOperandVal(1) == 1;
3930 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
3931 DAG.getIntPtrConstant(IsTrunc));
3932 }
3933
Chris Lattner79dbea52006-03-13 06:26:26 +00003934 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
3935 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
Chris Lattner0bd48932008-01-17 07:00:52 +00003936 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003937 AddToWorkList(Tmp.Val);
3938 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
3939 }
3940
Nate Begeman83e75ec2005-09-06 04:43:02 +00003941 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003942}
3943
Nate Begeman83e75ec2005-09-06 04:43:02 +00003944SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003945 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003946 MVT VT = N->getValueType(0);
3947 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00003948 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003949
Nate Begeman1d4d4142005-09-01 00:19:25 +00003950 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003951 if (N0CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003952 SDOperand Round = DAG.getConstantFP(N0CFP->getValueAPF(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003953 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003954 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003955 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003956}
3957
Nate Begeman83e75ec2005-09-06 04:43:02 +00003958SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003959 SDOperand N0 = N->getOperand(0);
3960 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003961 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003962
Chris Lattner5938bef2007-12-29 06:55:23 +00003963 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003964 if (N->hasOneUse() &&
3965 N->use_begin()->getSDOperand().getOpcode() == ISD::FP_ROUND)
Chris Lattner5938bef2007-12-29 06:55:23 +00003966 return SDOperand();
Chris Lattner0bd48932008-01-17 07:00:52 +00003967
Nate Begeman1d4d4142005-09-01 00:19:25 +00003968 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003969 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003970 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003971
3972 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
3973 // value of X.
3974 if (N0.getOpcode() == ISD::FP_ROUND && N0.Val->getConstantOperandVal(1) == 1){
3975 SDOperand In = N0.getOperand(0);
3976 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00003977 if (VT.bitsLT(In.getValueType()))
Chris Lattner0bd48932008-01-17 07:00:52 +00003978 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
3979 return DAG.getNode(ISD::FP_EXTEND, VT, In);
3980 }
3981
3982 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Dale Johannesenb6210fc2007-10-19 20:29:00 +00003983 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003984 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3985 TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003986 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3987 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3988 LN0->getBasePtr(), LN0->getSrcValue(),
3989 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003990 N0.getValueType(),
3991 LN0->isVolatile(),
3992 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00003993 CombineTo(N, ExtLoad);
Chris Lattner0bd48932008-01-17 07:00:52 +00003994 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad,
3995 DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00003996 ExtLoad.getValue(1));
3997 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3998 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003999
Nate Begeman83e75ec2005-09-06 04:43:02 +00004000 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004001}
4002
Nate Begeman83e75ec2005-09-06 04:43:02 +00004003SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00004004 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004005
Chris Lattner0254e702008-02-26 07:04:54 +00004006 if (isNegatibleForFree(N0, AfterLegalize))
4007 return GetNegatedExpression(N0, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00004008
Chris Lattner3bd39d42008-01-27 17:42:27 +00004009 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
4010 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00004011 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004012 N0.getOperand(0).getValueType().isInteger() &&
4013 !N0.getOperand(0).getValueType().isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004014 SDOperand Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004015 MVT IntVT = Int.getValueType();
4016 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004017 Int = DAG.getNode(ISD::XOR, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004018 DAG.getConstant(IntVT.getIntegerVTSignBit(), IntVT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00004019 AddToWorkList(Int.Val);
4020 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4021 }
4022 }
4023
Nate Begeman83e75ec2005-09-06 04:43:02 +00004024 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004025}
4026
Nate Begeman83e75ec2005-09-06 04:43:02 +00004027SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00004028 SDOperand N0 = N->getOperand(0);
4029 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004030 MVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00004031
Nate Begeman1d4d4142005-09-01 00:19:25 +00004032 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00004033 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004034 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004035 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004036 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00004037 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004038 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004039 // fold (fabs (fcopysign x, y)) -> (fabs x)
4040 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
4041 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
4042
Chris Lattner3bd39d42008-01-27 17:42:27 +00004043 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
4044 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00004045 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004046 N0.getOperand(0).getValueType().isInteger() &&
4047 !N0.getOperand(0).getValueType().isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004048 SDOperand Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004049 MVT IntVT = Int.getValueType();
4050 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004051 Int = DAG.getNode(ISD::AND, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004052 DAG.getConstant(~IntVT.getIntegerVTSignBit(), IntVT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00004053 AddToWorkList(Int.Val);
4054 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4055 }
4056 }
4057
Nate Begeman83e75ec2005-09-06 04:43:02 +00004058 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004059}
4060
Nate Begeman44728a72005-09-19 22:34:01 +00004061SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
4062 SDOperand Chain = N->getOperand(0);
4063 SDOperand N1 = N->getOperand(1);
4064 SDOperand N2 = N->getOperand(2);
4065 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4066
4067 // never taken branch, fold to chain
4068 if (N1C && N1C->isNullValue())
4069 return Chain;
4070 // unconditional branch
Dan Gohman002e5d02008-03-13 22:13:53 +00004071 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00004072 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004073 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
4074 // on the target.
4075 if (N1.getOpcode() == ISD::SETCC &&
4076 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
4077 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
4078 N1.getOperand(0), N1.getOperand(1), N2);
4079 }
Nate Begeman44728a72005-09-19 22:34:01 +00004080 return SDOperand();
4081}
4082
Chris Lattner3ea0b472005-10-05 06:47:48 +00004083// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
4084//
Nate Begeman44728a72005-09-19 22:34:01 +00004085SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00004086 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
4087 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
4088
Duncan Sands8eab8a22008-06-09 11:32:28 +00004089 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00004090 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00004091 if (Simp.Val) AddToWorkList(Simp.Val);
4092
Nate Begemane17daeb2005-10-05 21:43:42 +00004093 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
4094
4095 // fold br_cc true, dest -> br dest (unconditional branch)
Dan Gohman002e5d02008-03-13 22:13:53 +00004096 if (SCCC && !SCCC->isNullValue())
Nate Begemane17daeb2005-10-05 21:43:42 +00004097 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
4098 N->getOperand(4));
4099 // fold br_cc false, dest -> unconditional fall through
4100 if (SCCC && SCCC->isNullValue())
4101 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00004102
Nate Begemane17daeb2005-10-05 21:43:42 +00004103 // fold to a simpler setcc
4104 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
4105 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
4106 Simp.getOperand(2), Simp.getOperand(0),
4107 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00004108 return SDOperand();
4109}
4110
Chris Lattner448f2192006-11-11 00:39:41 +00004111
Duncan Sandsec87aa82008-06-15 20:12:31 +00004112/// CombineToPreIndexedLoadStore - Try turning a load / store into a
4113/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00004114/// and it has other uses besides the load / store. After the
4115/// transformation, the new indexed load / store has effectively folded
4116/// the add / subtract in and all of its other uses are redirected to the
4117/// new load / store.
4118bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
4119 if (!AfterLegalize)
4120 return false;
4121
4122 bool isLoad = true;
4123 SDOperand Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004124 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004125 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004126 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004127 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004128 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00004129 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00004130 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
4131 return false;
4132 Ptr = LD->getBasePtr();
4133 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004134 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004135 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004136 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004137 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
4138 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
4139 return false;
4140 Ptr = ST->getBasePtr();
4141 isLoad = false;
4142 } else
4143 return false;
4144
Chris Lattner9f1794e2006-11-11 00:56:29 +00004145 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4146 // out. There is no reason to make this a preinc/predec.
4147 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
4148 Ptr.Val->hasOneUse())
4149 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004150
Chris Lattner9f1794e2006-11-11 00:56:29 +00004151 // Ask the target to do addressing mode selection.
4152 SDOperand BasePtr;
4153 SDOperand Offset;
4154 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4155 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4156 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00004157 // Don't create a indexed load / store with zero offset.
4158 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004159 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004160 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004161
Chris Lattner41e53fd2006-11-11 01:00:15 +00004162 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00004163 // 1) The new base ptr is a frame index.
4164 // 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 +00004165 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00004166 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00004167 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00004168 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00004169
Chris Lattner41e53fd2006-11-11 01:00:15 +00004170 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4171 // (plus the implicit offset) to a register to preinc anyway.
4172 if (isa<FrameIndexSDNode>(BasePtr))
4173 return false;
4174
4175 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004176 if (!isLoad) {
4177 SDOperand Val = cast<StoreSDNode>(N)->getValue();
Evan Cheng917be682008-03-04 00:41:45 +00004178 if (Val == BasePtr || BasePtr.Val->isPredecessorOf(Val.Val))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004179 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004180 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004181
Evan Chengc843abe2007-05-24 02:35:39 +00004182 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004183 bool RealUse = false;
4184 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
4185 E = Ptr.Val->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004186 SDNode *Use = I->getUser();
Chris Lattner9f1794e2006-11-11 00:56:29 +00004187 if (Use == N)
4188 continue;
Evan Cheng917be682008-03-04 00:41:45 +00004189 if (Use->isPredecessorOf(N))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004190 return false;
4191
4192 if (!((Use->getOpcode() == ISD::LOAD &&
4193 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004194 (Use->getOpcode() == ISD::STORE &&
4195 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004196 RealUse = true;
4197 }
4198 if (!RealUse)
4199 return false;
4200
4201 SDOperand Result;
4202 if (isLoad)
4203 Result = DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM);
4204 else
4205 Result = DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4206 ++PreIndexedNodes;
4207 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004208 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004209 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4210 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004211 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004212 if (isLoad) {
4213 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004214 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004215 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004216 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004217 } else {
4218 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004219 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004220 }
4221
Chris Lattner9f1794e2006-11-11 00:56:29 +00004222 // Finally, since the node is now dead, remove it from the graph.
4223 DAG.DeleteNode(N);
4224
4225 // Replace the uses of Ptr with uses of the updated base value.
4226 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004227 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004228 removeFromWorkList(Ptr.Val);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004229 DAG.DeleteNode(Ptr.Val);
4230
4231 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004232}
4233
Duncan Sandsec87aa82008-06-15 20:12:31 +00004234/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00004235/// add / sub of the base pointer node into a post-indexed load / store.
4236/// The transformation folded the add / subtract into the new indexed
4237/// load / store effectively and all of its uses are redirected to the
4238/// new load / store.
4239bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
4240 if (!AfterLegalize)
4241 return false;
4242
4243 bool isLoad = true;
4244 SDOperand Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004245 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004246 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004247 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004248 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004249 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004250 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4251 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4252 return false;
4253 Ptr = LD->getBasePtr();
4254 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004255 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004256 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004257 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004258 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4259 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4260 return false;
4261 Ptr = ST->getBasePtr();
4262 isLoad = false;
4263 } else
4264 return false;
4265
Evan Chengcc470212006-11-16 00:08:20 +00004266 if (Ptr.Val->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004267 return false;
4268
4269 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
4270 E = Ptr.Val->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004271 SDNode *Op = I->getUser();
Chris Lattner9f1794e2006-11-11 00:56:29 +00004272 if (Op == N ||
4273 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4274 continue;
4275
4276 SDOperand BasePtr;
4277 SDOperand Offset;
4278 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4279 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4280 if (Ptr == Offset)
4281 std::swap(BasePtr, Offset);
4282 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00004283 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00004284 // Don't create a indexed load / store with zero offset.
4285 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004286 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004287 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004288
Chris Lattner9f1794e2006-11-11 00:56:29 +00004289 // Try turning it into a post-indexed load / store except when
4290 // 1) All uses are load / store ops that use it as base ptr.
4291 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4292 // nor a successor of N. Otherwise, if Op is folded that would
4293 // create a cycle.
4294
4295 // Check for #1.
4296 bool TryNext = false;
4297 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
4298 EE = BasePtr.Val->use_end(); II != EE; ++II) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004299 SDNode *Use = II->getUser();
Chris Lattner9f1794e2006-11-11 00:56:29 +00004300 if (Use == Ptr.Val)
Chris Lattner448f2192006-11-11 00:39:41 +00004301 continue;
4302
Chris Lattner9f1794e2006-11-11 00:56:29 +00004303 // If all the uses are load / store addresses, then don't do the
4304 // transformation.
4305 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4306 bool RealUse = false;
4307 for (SDNode::use_iterator III = Use->use_begin(),
4308 EEE = Use->use_end(); III != EEE; ++III) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004309 SDNode *UseUse = III->getUser();
Chris Lattner9f1794e2006-11-11 00:56:29 +00004310 if (!((UseUse->getOpcode() == ISD::LOAD &&
4311 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004312 (UseUse->getOpcode() == ISD::STORE &&
4313 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004314 RealUse = true;
4315 }
Chris Lattner448f2192006-11-11 00:39:41 +00004316
Chris Lattner9f1794e2006-11-11 00:56:29 +00004317 if (!RealUse) {
4318 TryNext = true;
4319 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004320 }
4321 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004322 }
4323 if (TryNext)
4324 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004325
Chris Lattner9f1794e2006-11-11 00:56:29 +00004326 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00004327 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Chris Lattner9f1794e2006-11-11 00:56:29 +00004328 SDOperand Result = isLoad
4329 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
4330 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4331 ++PostIndexedNodes;
4332 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004333 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004334 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4335 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004336 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004337 if (isLoad) {
4338 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004339 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004340 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004341 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004342 } else {
4343 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004344 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00004345 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004346
Chris Lattner9f1794e2006-11-11 00:56:29 +00004347 // Finally, since the node is now dead, remove it from the graph.
4348 DAG.DeleteNode(N);
4349
4350 // Replace the uses of Use with uses of the updated base value.
4351 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
4352 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004353 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004354 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004355 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004356 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004357 }
4358 }
4359 }
4360 return false;
4361}
4362
Chris Lattner00161a62008-01-25 07:20:16 +00004363/// InferAlignment - If we can infer some alignment information from this
4364/// pointer, return it.
4365static unsigned InferAlignment(SDOperand Ptr, SelectionDAG &DAG) {
4366 // If this is a direct reference to a stack slot, use information about the
4367 // stack slot's alignment.
Chris Lattner1329cb82008-01-26 19:45:50 +00004368 int FrameIdx = 1 << 31;
4369 int64_t FrameOffset = 0;
Chris Lattner00161a62008-01-25 07:20:16 +00004370 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1329cb82008-01-26 19:45:50 +00004371 FrameIdx = FI->getIndex();
4372 } else if (Ptr.getOpcode() == ISD::ADD &&
4373 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4374 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4375 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4376 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner00161a62008-01-25 07:20:16 +00004377 }
Chris Lattner1329cb82008-01-26 19:45:50 +00004378
4379 if (FrameIdx != (1 << 31)) {
4380 // FIXME: Handle FI+CST.
4381 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4382 if (MFI.isFixedObjectIndex(FrameIdx)) {
4383 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx);
4384
4385 // The alignment of the frame index can be determined from its offset from
4386 // the incoming frame position. If the frame object is at offset 32 and
4387 // the stack is guaranteed to be 16-byte aligned, then we know that the
4388 // object is 16-byte aligned.
4389 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4390 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4391
4392 // Finally, the frame object itself may have a known alignment. Factor
4393 // the alignment + offset into a new alignment. For example, if we know
4394 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4395 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4396 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4397 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4398 FrameOffset);
4399 return std::max(Align, FIInfoAlign);
4400 }
4401 }
Chris Lattner00161a62008-01-25 07:20:16 +00004402
4403 return 0;
4404}
Chris Lattner448f2192006-11-11 00:39:41 +00004405
Chris Lattner01a22022005-10-10 22:04:48 +00004406SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004407 LoadSDNode *LD = cast<LoadSDNode>(N);
4408 SDOperand Chain = LD->getChain();
4409 SDOperand Ptr = LD->getBasePtr();
Chris Lattner00161a62008-01-25 07:20:16 +00004410
4411 // Try to infer better alignment information than the load already has.
4412 if (LD->isUnindexed()) {
4413 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4414 if (Align > LD->getAlignment())
4415 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4416 Chain, Ptr, LD->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004417 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004418 LD->isVolatile(), Align);
4419 }
4420 }
4421
Evan Cheng45a7ca92007-05-01 00:38:21 +00004422
4423 // If load is not volatile and there are no uses of the loaded value (and
4424 // the updated indexed value in case of indexed loads), change uses of the
4425 // chain value into uses of the chain input (i.e. delete the dead load).
4426 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00004427 if (N->getValueType(1) == MVT::Other) {
4428 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004429 if (N->hasNUsesOfValue(0, 0)) {
4430 // It's not safe to use the two value CombineTo variant here. e.g.
4431 // v1, chain2 = load chain1, loc
4432 // v2, chain3 = load chain2, loc
4433 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004434 // Now we replace use of chain2 with chain1. This makes the second load
4435 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004436 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004437 DOUT << "\nWith chain: "; DEBUG(Chain.Val->dump(&DAG));
4438 DOUT << "\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004439 WorkListRemover DeadNodes(*this);
4440 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Chain, &DeadNodes);
Chris Lattner125991a2008-01-24 07:57:06 +00004441 if (N->use_empty()) {
4442 removeFromWorkList(N);
4443 DAG.DeleteNode(N);
4444 }
Evan Cheng02c42852008-01-16 23:11:54 +00004445 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
4446 }
Evan Cheng498f5592007-05-01 08:53:39 +00004447 } else {
4448 // Indexed loads.
4449 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4450 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Evan Cheng02c42852008-01-16 23:11:54 +00004451 SDOperand Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
4452 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
4453 DOUT << "\nWith: "; DEBUG(Undef.Val->dump(&DAG));
4454 DOUT << " and 2 other values\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004455 WorkListRemover DeadNodes(*this);
4456 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Undef, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004457 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1),
Chris Lattner4626b252008-01-17 07:20:38 +00004458 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004459 &DeadNodes);
4460 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004461 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00004462 DAG.DeleteNode(N);
4463 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004464 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004465 }
4466 }
Chris Lattner01a22022005-10-10 22:04:48 +00004467
4468 // If this load is directly stored, replace the load value with the stored
4469 // value.
4470 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004471 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohmanb061c4b2008-03-31 20:32:52 +00004472 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
4473 !LD->isVolatile()) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004474 if (ISD::isNON_TRUNCStore(Chain.Val)) {
4475 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4476 if (PrevST->getBasePtr() == Ptr &&
4477 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004478 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004479 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004480 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004481
Jim Laskey7ca56af2006-10-11 13:47:09 +00004482 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004483 // Walk up chain skipping non-aliasing memory nodes.
4484 SDOperand BetterChain = FindBetterChain(N, Chain);
4485
Jim Laskey6ff23e52006-10-04 16:53:27 +00004486 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004487 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004488 SDOperand ReplLoad;
4489
Jim Laskey279f0532006-09-25 16:29:54 +00004490 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004491 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4492 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004493 LD->getSrcValue(), LD->getSrcValueOffset(),
4494 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004495 } else {
4496 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4497 LD->getValueType(0),
4498 BetterChain, Ptr, LD->getSrcValue(),
4499 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004500 LD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004501 LD->isVolatile(),
4502 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004503 }
Jim Laskey279f0532006-09-25 16:29:54 +00004504
Jim Laskey6ff23e52006-10-04 16:53:27 +00004505 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00004506 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
4507 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004508
Jim Laskey274062c2006-10-13 23:32:28 +00004509 // Replace uses with load result and token factor. Don't add users
4510 // to work list.
4511 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004512 }
4513 }
4514
Evan Cheng7fc033a2006-11-03 03:06:21 +00004515 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004516 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00004517 return SDOperand(N, 0);
4518
Chris Lattner01a22022005-10-10 22:04:48 +00004519 return SDOperand();
4520}
4521
Chris Lattner07649d92008-01-08 23:08:06 +00004522
Chris Lattner87514ca2005-10-10 22:31:19 +00004523SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004524 StoreSDNode *ST = cast<StoreSDNode>(N);
4525 SDOperand Chain = ST->getChain();
4526 SDOperand Value = ST->getValue();
4527 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004528
Chris Lattner00161a62008-01-25 07:20:16 +00004529 // Try to infer better alignment information than the store already has.
4530 if (ST->isUnindexed()) {
4531 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4532 if (Align > ST->getAlignment())
4533 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004534 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004535 ST->isVolatile(), Align);
4536 }
4537 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004538
Evan Cheng59d5b682007-05-07 21:27:48 +00004539 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004540 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004541 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004542 ST->isUnindexed()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004543 unsigned Align = ST->getAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004544 MVT SVT = Value.getOperand(0).getValueType();
Evan Cheng59d5b682007-05-07 21:27:48 +00004545 unsigned OrigAlign = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004546 getABITypeAlignment(SVT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004547 if (Align <= OrigAlign &&
4548 ((!AfterLegalize && !ST->isVolatile()) ||
4549 TLI.isOperationLegal(ISD::STORE, SVT)))
Evan Cheng59d5b682007-05-07 21:27:48 +00004550 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohman77455612008-06-28 00:45:22 +00004551 ST->getSrcValueOffset(), ST->isVolatile(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00004552 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004553
Nate Begeman2cbba892006-12-11 02:23:46 +00004554 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004555 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004556 // NOTE: If the original store is volatile, this transform must not increase
4557 // the number of stores. For example, on x86-32 an f64 can be stored in one
4558 // processor operation but an i64 (which is not legal) requires two. So the
4559 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00004560 if (Value.getOpcode() != ISD::TargetConstantFP) {
4561 SDOperand Tmp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004562 switch (CFP->getValueType(0).getSimpleVT()) {
Chris Lattner62be1a72006-12-12 04:16:14 +00004563 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004564 case MVT::f80: // We don't do this for these yet.
4565 case MVT::f128:
4566 case MVT::ppcf128:
4567 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004568 case MVT::f32:
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004569 if ((!AfterLegalize && !ST->isVolatile()) ||
4570 TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004571 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
4572 convertToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004573 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004574 ST->getSrcValueOffset(), ST->isVolatile(),
4575 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004576 }
4577 break;
4578 case MVT::f64:
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004579 if ((!AfterLegalize && !ST->isVolatile()) ||
4580 TLI.isOperationLegal(ISD::STORE, MVT::i64)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004581 Tmp = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
4582 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004583 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004584 ST->getSrcValueOffset(), ST->isVolatile(),
4585 ST->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004586 } else if (!ST->isVolatile() &&
4587 TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004588 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004589 // argument passing. Since this is so common, custom legalize the
4590 // 64-bit integer store into two 32-bit stores.
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004591 uint64_t Val = CFP->getValueAPF().convertToAPInt().getZExtValue();
Chris Lattner62be1a72006-12-12 04:16:14 +00004592 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4593 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00004594 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00004595
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004596 int SVOffset = ST->getSrcValueOffset();
4597 unsigned Alignment = ST->getAlignment();
4598 bool isVolatile = ST->isVolatile();
4599
Chris Lattner62be1a72006-12-12 04:16:14 +00004600 SDOperand St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004601 ST->getSrcValueOffset(),
4602 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004603 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4604 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004605 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004606 Alignment = MinAlign(Alignment, 4U);
Chris Lattner62be1a72006-12-12 04:16:14 +00004607 SDOperand St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004608 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004609 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4610 }
4611 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004612 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004613 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004614 }
4615
Jim Laskey279f0532006-09-25 16:29:54 +00004616 if (CombinerAA) {
4617 // Walk up chain skipping non-aliasing memory nodes.
4618 SDOperand BetterChain = FindBetterChain(N, Chain);
4619
Jim Laskey6ff23e52006-10-04 16:53:27 +00004620 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004621 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004622 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004623 SDOperand ReplStore;
4624 if (ST->isTruncatingStore()) {
4625 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004626 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004627 ST->getMemoryVT(),
Chris Lattner4626b252008-01-17 07:20:38 +00004628 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004629 } else {
4630 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004631 ST->getSrcValue(), ST->getSrcValueOffset(),
4632 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004633 }
4634
Jim Laskey279f0532006-09-25 16:29:54 +00004635 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00004636 SDOperand Token =
4637 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4638
4639 // Don't add users to work list.
4640 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004641 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004642 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004643
Evan Cheng33dbedc2006-11-05 09:31:14 +00004644 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004645 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00004646 return SDOperand(N, 0);
4647
Chris Lattner3c872852007-12-29 06:26:16 +00004648 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00004649 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004650 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00004651 // See if we can simplify the input to this truncstore with knowledge that
4652 // only the low bits are being used. For example:
4653 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
4654 SDOperand Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004655 GetDemandedBits(Value,
4656 APInt::getLowBitsSet(Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004657 ST->getMemoryVT().getSizeInBits()));
Chris Lattner2b4c2792007-10-13 06:35:54 +00004658 AddToWorkList(Value.Val);
4659 if (Shorter.Val)
4660 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004661 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner2b4c2792007-10-13 06:35:54 +00004662 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004663
4664 // Otherwise, see if we can simplify the operation with
4665 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00004666 if (SimplifyDemandedBits(Value,
4667 APInt::getLowBitsSet(
4668 Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004669 ST->getMemoryVT().getSizeInBits())))
Chris Lattnere33544c2007-10-13 06:58:48 +00004670 return SDOperand(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004671 }
4672
Chris Lattner3c872852007-12-29 06:26:16 +00004673 // If this is a load followed by a store to the same location, then the store
4674 // is dead/noop.
4675 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004676 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004677 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00004678 // There can't be any side effects between the load and store, such as
4679 // a call or store.
Chris Lattner572dee72008-01-16 05:49:24 +00004680 Chain.reachesChainWithoutSideEffects(SDOperand(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00004681 // The store is dead, remove it.
4682 return Chain;
4683 }
4684 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004685
Chris Lattnerddf89562008-01-17 19:59:44 +00004686 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4687 // truncating store. We can do this even if this is already a truncstore.
4688 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004689 && Value.Val->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004690 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004691 ST->getMemoryVT())) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004692 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004693 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnerddf89562008-01-17 19:59:44 +00004694 ST->isVolatile(), ST->getAlignment());
4695 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004696
Chris Lattner87514ca2005-10-10 22:31:19 +00004697 return SDOperand();
4698}
4699
Chris Lattnerca242442006-03-19 01:27:56 +00004700SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4701 SDOperand InVec = N->getOperand(0);
4702 SDOperand InVal = N->getOperand(1);
4703 SDOperand EltNo = N->getOperand(2);
4704
4705 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4706 // vector with the inserted element.
4707 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
4708 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004709 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004710 if (Elt < Ops.size())
4711 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004712 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4713 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004714 }
4715
4716 return SDOperand();
4717}
4718
Evan Cheng513da432007-10-06 08:19:55 +00004719SDOperand DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004720 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
4721 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
4722 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
4723
4724 // Perform only after legalization to ensure build_vector / vector_shuffle
4725 // optimizations have already been done.
4726 if (!AfterLegalize) return SDOperand();
4727
Evan Cheng513da432007-10-06 08:19:55 +00004728 SDOperand InVec = N->getOperand(0);
4729 SDOperand EltNo = N->getOperand(1);
4730
Evan Cheng513da432007-10-06 08:19:55 +00004731 if (isa<ConstantSDNode>(EltNo)) {
4732 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
4733 bool NewLoad = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004734 MVT VT = InVec.getValueType();
4735 MVT EVT = VT.getVectorElementType();
4736 MVT LVT = EVT;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004737 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004738 MVT BCVT = InVec.getOperand(0).getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00004739 if (!BCVT.isVector() || EVT.bitsGT(BCVT.getVectorElementType()))
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004740 return SDOperand();
4741 InVec = InVec.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004742 EVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004743 NewLoad = true;
4744 }
Evan Cheng513da432007-10-06 08:19:55 +00004745
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004746 LoadSDNode *LN0 = NULL;
4747 if (ISD::isNormalLoad(InVec.Val))
4748 LN0 = cast<LoadSDNode>(InVec);
4749 else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4750 InVec.getOperand(0).getValueType() == EVT &&
4751 ISD::isNormalLoad(InVec.getOperand(0).Val)) {
4752 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4753 } else if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE) {
4754 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
4755 // =>
4756 // (load $addr+1*size)
4757 unsigned Idx = cast<ConstantSDNode>(InVec.getOperand(2).
4758 getOperand(Elt))->getValue();
4759 unsigned NumElems = InVec.getOperand(2).getNumOperands();
4760 InVec = (Idx < NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
4761 if (InVec.getOpcode() == ISD::BIT_CONVERT)
4762 InVec = InVec.getOperand(0);
4763 if (ISD::isNormalLoad(InVec.Val)) {
4764 LN0 = cast<LoadSDNode>(InVec);
4765 Elt = (Idx < NumElems) ? Idx : Idx - NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00004766 }
4767 }
Duncan Sandsec87aa82008-06-15 20:12:31 +00004768 if (!LN0 || !LN0->hasOneUse() || LN0->isVolatile())
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004769 return SDOperand();
4770
4771 unsigned Align = LN0->getAlignment();
4772 if (NewLoad) {
4773 // Check the resultant load doesn't need a higher alignment than the
4774 // original load.
4775 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004776 getABITypeAlignment(LVT.getTypeForMVT());
Duncan Sands184a8762008-06-14 17:48:34 +00004777 if (NewAlign > Align || !TLI.isOperationLegal(ISD::LOAD, LVT))
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004778 return SDOperand();
4779 Align = NewAlign;
4780 }
4781
4782 SDOperand NewPtr = LN0->getBasePtr();
4783 if (Elt) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004784 unsigned PtrOff = LVT.getSizeInBits() * Elt / 8;
4785 MVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004786 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00004787 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004788 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
4789 DAG.getConstant(PtrOff, PtrType));
4790 }
4791 return DAG.getLoad(LVT, LN0->getChain(), NewPtr,
4792 LN0->getSrcValue(), LN0->getSrcValueOffset(),
4793 LN0->isVolatile(), Align);
Evan Cheng513da432007-10-06 08:19:55 +00004794 }
4795 return SDOperand();
4796}
4797
4798
Dan Gohman7f321562007-06-25 16:23:39 +00004799SDOperand DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
4800 unsigned NumInScalars = N->getNumOperands();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004801 MVT VT = N->getValueType(0);
4802 unsigned NumElts = VT.getVectorNumElements();
4803 MVT EltType = VT.getVectorElementType();
Chris Lattnerca242442006-03-19 01:27:56 +00004804
Dan Gohman7f321562007-06-25 16:23:39 +00004805 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
4806 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
4807 // at most two distinct vectors, turn this into a shuffle node.
Chris Lattnerd7648c82006-03-28 20:28:38 +00004808 SDOperand VecIn1, VecIn2;
4809 for (unsigned i = 0; i != NumInScalars; ++i) {
4810 // Ignore undef inputs.
4811 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4812
Dan Gohman7f321562007-06-25 16:23:39 +00004813 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00004814 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00004815 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00004816 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
4817 VecIn1 = VecIn2 = SDOperand(0, 0);
4818 break;
4819 }
4820
Dan Gohman7f321562007-06-25 16:23:39 +00004821 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00004822 // we can't make a shuffle.
4823 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004824 if (ExtractedFromVec.getValueType() != VT) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004825 VecIn1 = VecIn2 = SDOperand(0, 0);
4826 break;
4827 }
4828
4829 // Otherwise, remember this. We allow up to two distinct input vectors.
4830 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
4831 continue;
4832
4833 if (VecIn1.Val == 0) {
4834 VecIn1 = ExtractedFromVec;
4835 } else if (VecIn2.Val == 0) {
4836 VecIn2 = ExtractedFromVec;
4837 } else {
4838 // Too many inputs.
4839 VecIn1 = VecIn2 = SDOperand(0, 0);
4840 break;
4841 }
4842 }
4843
4844 // If everything is good, we can make a shuffle operation.
4845 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004846 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004847 for (unsigned i = 0; i != NumInScalars; ++i) {
4848 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00004849 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004850 continue;
4851 }
4852
4853 SDOperand Extract = N->getOperand(i);
4854
4855 // If extracting from the first vector, just use the index directly.
4856 if (Extract.getOperand(0) == VecIn1) {
4857 BuildVecIndices.push_back(Extract.getOperand(1));
4858 continue;
4859 }
4860
4861 // Otherwise, use InIdx + VecSize
4862 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004863 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004864 }
4865
4866 // Add count and size info.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004867 MVT BuildVecVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004868
Dan Gohman7f321562007-06-25 16:23:39 +00004869 // Return the new VECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004870 SDOperand Ops[5];
4871 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00004872 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004873 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00004874 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004875 // Use an undef build_vector as input for the second operand.
Chris Lattnercef896e2006-03-28 22:19:47 +00004876 std::vector<SDOperand> UnOps(NumInScalars,
4877 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00004878 EltType));
4879 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004880 &UnOps[0], UnOps.size());
4881 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00004882 }
Dan Gohman7f321562007-06-25 16:23:39 +00004883 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004884 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004885 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004886 }
4887
4888 return SDOperand();
4889}
4890
Dan Gohman7f321562007-06-25 16:23:39 +00004891SDOperand DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
4892 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
4893 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
4894 // inputs come from at most two distinct vectors, turn this into a shuffle
4895 // node.
4896
4897 // If we only have one input vector, we don't need to do any concatenation.
4898 if (N->getNumOperands() == 1) {
4899 return N->getOperand(0);
4900 }
4901
4902 return SDOperand();
4903}
4904
Chris Lattner66445d32006-03-28 22:11:53 +00004905SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004906 SDOperand ShufMask = N->getOperand(2);
4907 unsigned NumElts = ShufMask.getNumOperands();
4908
4909 // If the shuffle mask is an identity operation on the LHS, return the LHS.
4910 bool isIdentity = true;
4911 for (unsigned i = 0; i != NumElts; ++i) {
4912 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4913 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
4914 isIdentity = false;
4915 break;
4916 }
4917 }
4918 if (isIdentity) return N->getOperand(0);
4919
4920 // If the shuffle mask is an identity operation on the RHS, return the RHS.
4921 isIdentity = true;
4922 for (unsigned i = 0; i != NumElts; ++i) {
4923 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4924 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
4925 isIdentity = false;
4926 break;
4927 }
4928 }
4929 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00004930
4931 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
4932 // needed at all.
4933 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00004934 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004935 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00004936 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004937 for (unsigned i = 0; i != NumElts; ++i)
4938 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
4939 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
4940 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00004941 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00004942 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00004943 BaseIdx = Idx;
4944 } else {
4945 if (BaseIdx != Idx)
4946 isSplat = false;
4947 if (VecNum != V) {
4948 isUnary = false;
4949 break;
4950 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00004951 }
4952 }
4953
4954 SDOperand N0 = N->getOperand(0);
4955 SDOperand N1 = N->getOperand(1);
4956 // Normalize unary shuffle so the RHS is undef.
4957 if (isUnary && VecNum == 1)
4958 std::swap(N0, N1);
4959
Evan Cheng917ec982006-07-21 08:25:53 +00004960 // If it is a splat, check if the argument vector is a build_vector with
4961 // all scalar elements the same.
4962 if (isSplat) {
4963 SDNode *V = N0.Val;
Evan Cheng917ec982006-07-21 08:25:53 +00004964
Dan Gohman7f321562007-06-25 16:23:39 +00004965 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00004966 // not the number of vector elements, look through it. Be careful not to
4967 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00004968 if (V->getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng59569222006-10-16 22:49:37 +00004969 SDOperand ConvInput = V->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004970 if (ConvInput.getValueType().getVectorNumElements() == NumElts)
Evan Cheng59569222006-10-16 22:49:37 +00004971 V = ConvInput.Val;
4972 }
4973
Dan Gohman7f321562007-06-25 16:23:39 +00004974 if (V->getOpcode() == ISD::BUILD_VECTOR) {
4975 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00004976 if (NumElems > BaseIdx) {
4977 SDOperand Base;
4978 bool AllSame = true;
4979 for (unsigned i = 0; i != NumElems; ++i) {
4980 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
4981 Base = V->getOperand(i);
4982 break;
4983 }
4984 }
4985 // Splat of <u, u, u, u>, return <u, u, u, u>
4986 if (!Base.Val)
4987 return N0;
4988 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00004989 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00004990 AllSame = false;
4991 break;
4992 }
4993 }
4994 // Splat of <x, x, x, x>, return <x, x, x, x>
4995 if (AllSame)
4996 return N0;
4997 }
4998 }
4999 }
5000
Evan Chenge7bec0d2006-07-20 22:44:41 +00005001 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
5002 // into an undef.
5003 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00005004 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
5005 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005006 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00005007 for (unsigned i = 0; i != NumElts; ++i) {
5008 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
5009 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
5010 MappedOps.push_back(ShufMask.getOperand(i));
5011 } else {
5012 unsigned NewIdx =
5013 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
5014 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
5015 }
5016 }
Dan Gohman7f321562007-06-25 16:23:39 +00005017 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005018 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00005019 AddToWorkList(ShufMask.Val);
Dan Gohman7f321562007-06-25 16:23:39 +00005020 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
5021 N0,
5022 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
5023 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00005024 }
Dan Gohman7f321562007-06-25 16:23:39 +00005025
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005026 return SDOperand();
5027}
5028
Evan Cheng44f1f092006-04-20 08:56:16 +00005029/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00005030/// an AND to a vector_shuffle with the destination vector and a zero vector.
5031/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00005032/// vector_shuffle V, Zero, <0, 4, 2, 4>
5033SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
5034 SDOperand LHS = N->getOperand(0);
5035 SDOperand RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00005036 if (N->getOpcode() == ISD::AND) {
5037 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00005038 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005039 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Evan Cheng44f1f092006-04-20 08:56:16 +00005040 std::vector<SDOperand> IdxOps;
5041 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00005042 unsigned NumElts = NumOps;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005043 MVT EVT = RHS.getValueType().getVectorElementType();
Evan Cheng44f1f092006-04-20 08:56:16 +00005044 for (unsigned i = 0; i != NumElts; ++i) {
5045 SDOperand Elt = RHS.getOperand(i);
5046 if (!isa<ConstantSDNode>(Elt))
5047 return SDOperand();
5048 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
5049 IdxOps.push_back(DAG.getConstant(i, EVT));
5050 else if (cast<ConstantSDNode>(Elt)->isNullValue())
5051 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
5052 else
5053 return SDOperand();
5054 }
5055
5056 // Let's see if the target supports this vector_shuffle.
5057 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
5058 return SDOperand();
5059
Dan Gohman7f321562007-06-25 16:23:39 +00005060 // Return the new VECTOR_SHUFFLE node.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005061 MVT VT = MVT::getVectorVT(EVT, NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00005062 std::vector<SDOperand> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005063 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00005064 Ops.push_back(LHS);
5065 AddToWorkList(LHS.Val);
5066 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00005067 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005068 &ZeroOps[0], ZeroOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00005069 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005070 &IdxOps[0], IdxOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00005071 SDOperand Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005072 &Ops[0], Ops.size());
Dan Gohman7a9a5af2008-07-16 16:13:58 +00005073 if (VT != N->getValueType(0))
5074 Result = DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00005075 return Result;
5076 }
5077 }
5078 return SDOperand();
5079}
5080
Dan Gohman7f321562007-06-25 16:23:39 +00005081/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
5082SDOperand DAGCombiner::SimplifyVBinOp(SDNode *N) {
5083 // After legalize, the target may be depending on adds and other
5084 // binary ops to provide legal ways to construct constants or other
5085 // things. Simplifying them may result in a loss of legality.
5086 if (AfterLegalize) return SDOperand();
5087
Duncan Sands83ec4b62008-06-06 12:08:01 +00005088 MVT VT = N->getValueType(0);
5089 assert(VT.isVector() && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00005090
Duncan Sands83ec4b62008-06-06 12:08:01 +00005091 MVT EltType = VT.getVectorElementType();
Chris Lattneredab1b92006-04-02 03:25:57 +00005092 SDOperand LHS = N->getOperand(0);
5093 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00005094 SDOperand Shuffle = XformToShuffleWithZero(N);
5095 if (Shuffle.Val) return Shuffle;
5096
Dan Gohman7f321562007-06-25 16:23:39 +00005097 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00005098 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00005099 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
5100 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005101 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005102 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Chris Lattneredab1b92006-04-02 03:25:57 +00005103 SDOperand LHSOp = LHS.getOperand(i);
5104 SDOperand RHSOp = RHS.getOperand(i);
5105 // If these two elements can't be folded, bail out.
5106 if ((LHSOp.getOpcode() != ISD::UNDEF &&
5107 LHSOp.getOpcode() != ISD::Constant &&
5108 LHSOp.getOpcode() != ISD::ConstantFP) ||
5109 (RHSOp.getOpcode() != ISD::UNDEF &&
5110 RHSOp.getOpcode() != ISD::Constant &&
5111 RHSOp.getOpcode() != ISD::ConstantFP))
5112 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00005113 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00005114 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
5115 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00005116 if ((RHSOp.getOpcode() == ISD::Constant &&
5117 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
5118 (RHSOp.getOpcode() == ISD::ConstantFP &&
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00005119 cast<ConstantFPSDNode>(RHSOp.Val)->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00005120 break;
5121 }
Dan Gohman7f321562007-06-25 16:23:39 +00005122 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00005123 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00005124 assert((Ops.back().getOpcode() == ISD::UNDEF ||
5125 Ops.back().getOpcode() == ISD::Constant ||
5126 Ops.back().getOpcode() == ISD::ConstantFP) &&
5127 "Scalar binop didn't fold!");
5128 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005129
Dan Gohman7f321562007-06-25 16:23:39 +00005130 if (Ops.size() == LHS.getNumOperands()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005131 MVT VT = LHS.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00005132 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005133 }
Chris Lattneredab1b92006-04-02 03:25:57 +00005134 }
5135
5136 return SDOperand();
5137}
5138
Nate Begeman44728a72005-09-19 22:34:01 +00005139SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00005140 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
5141
5142 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
5143 cast<CondCodeSDNode>(N0.getOperand(2))->get());
5144 // If we got a simplified select_cc node back from SimplifySelectCC, then
5145 // break it down into a new SETCC node, and a new SELECT node, and then return
5146 // the SELECT node, since we were called with a SELECT node.
5147 if (SCC.Val) {
5148 // Check to see if we got a select_cc back (to turn into setcc/select).
5149 // Otherwise, just return whatever node we got back, like fabs.
5150 if (SCC.getOpcode() == ISD::SELECT_CC) {
5151 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
5152 SCC.getOperand(0), SCC.getOperand(1),
5153 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00005154 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005155 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
5156 SCC.getOperand(3), SETCC);
5157 }
5158 return SCC;
5159 }
Nate Begeman44728a72005-09-19 22:34:01 +00005160 return SDOperand();
5161}
5162
Chris Lattner40c62d52005-10-18 06:04:22 +00005163/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
5164/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00005165/// select. Callers of this should assume that TheSelect is deleted if this
5166/// returns true. As such, they should return the appropriate thing (e.g. the
5167/// node) back to the top-level of the DAG combiner loop to avoid it being
5168/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00005169///
5170bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
5171 SDOperand RHS) {
5172
5173 // If this is a select from two identical things, try to pull the operation
5174 // through the select.
5175 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00005176 // If this is a load and the token chain is identical, replace the select
5177 // of two loads with a load through a select of the address to load from.
5178 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
5179 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00005180 if (LHS.getOpcode() == ISD::LOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005181 // Do not let this transformation reduce the number of volatile loads.
5182 !cast<LoadSDNode>(LHS)->isVolatile() &&
5183 !cast<LoadSDNode>(RHS)->isVolatile() &&
Chris Lattner40c62d52005-10-18 06:04:22 +00005184 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00005185 LHS.getOperand(0) == RHS.getOperand(0)) {
5186 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5187 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5188
5189 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005190 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00005191 // FIXME: this conflates two src values, discarding one. This is not
5192 // the right thing to do, but nothing uses srcvalues now. When they do,
5193 // turn SrcValue into a list of locations.
5194 SDOperand Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005195 if (TheSelect->getOpcode() == ISD::SELECT) {
5196 // Check that the condition doesn't reach either load. If so, folding
5197 // this will induce a cycle into the DAG.
Evan Cheng917be682008-03-04 00:41:45 +00005198 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5199 !RLD->isPredecessorOf(TheSelect->getOperand(0).Val)) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005200 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
5201 TheSelect->getOperand(0), LLD->getBasePtr(),
5202 RLD->getBasePtr());
5203 }
5204 } else {
5205 // Check that the condition doesn't reach either load. If so, folding
5206 // this will induce a cycle into the DAG.
Evan Cheng917be682008-03-04 00:41:45 +00005207 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5208 !RLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5209 !LLD->isPredecessorOf(TheSelect->getOperand(1).Val) &&
5210 !RLD->isPredecessorOf(TheSelect->getOperand(1).Val)) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005211 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00005212 TheSelect->getOperand(0),
5213 TheSelect->getOperand(1),
5214 LLD->getBasePtr(), RLD->getBasePtr(),
5215 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005216 }
Evan Cheng466685d2006-10-09 20:57:25 +00005217 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005218
5219 if (Addr.Val) {
5220 SDOperand Load;
5221 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
5222 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
5223 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005224 LLD->getSrcValueOffset(),
5225 LLD->isVolatile(),
5226 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005227 else {
5228 Load = DAG.getExtLoad(LLD->getExtensionType(),
5229 TheSelect->getValueType(0),
5230 LLD->getChain(), Addr, LLD->getSrcValue(),
5231 LLD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005232 LLD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005233 LLD->isVolatile(),
5234 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005235 }
5236 // Users of the select now use the result of the load.
5237 CombineTo(TheSelect, Load);
5238
5239 // Users of the old loads now use the new load's chain. We know the
5240 // old-load value is dead now.
5241 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
5242 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
5243 return true;
5244 }
Evan Chengc5484282006-10-04 00:56:09 +00005245 }
Chris Lattner40c62d52005-10-18 06:04:22 +00005246 }
5247 }
5248
5249 return false;
5250}
5251
Nate Begeman44728a72005-09-19 22:34:01 +00005252SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
5253 SDOperand N2, SDOperand N3,
Chris Lattner1eba01e2007-04-11 06:50:51 +00005254 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00005255
Duncan Sands83ec4b62008-06-06 12:08:01 +00005256 MVT VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00005257 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
5258 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
5259 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
5260
5261 // Determine if the condition we're dealing with is constant
Scott Michel5b8f82e2008-03-10 15:42:14 +00005262 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00005263 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005264 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
5265
5266 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00005267 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005268 return N2;
5269 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00005270 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005271 return N3;
5272
5273 // Check to see if we can simplify the select into an fabs node
5274 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5275 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00005276 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005277 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5278 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5279 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5280 N2 == N3.getOperand(0))
5281 return DAG.getNode(ISD::FABS, VT, N0);
5282
5283 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5284 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5285 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5286 N2.getOperand(0) == N3)
5287 return DAG.getNode(ISD::FABS, VT, N3);
5288 }
5289 }
5290
5291 // Check to see if we can perform the "gzip trick", transforming
5292 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00005293 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005294 N0.getValueType().isInteger() &&
5295 N2.getValueType().isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005296 (N1C->isNullValue() || // (a < 0) ? b : 0
5297 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Duncan Sands83ec4b62008-06-06 12:08:01 +00005298 MVT XType = N0.getValueType();
5299 MVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00005300 if (XType.bitsGE(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005301 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00005302 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00005303 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
5304 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005305 ShCtV = XType.getSizeInBits()-ShCtV-1;
Nate Begemanf845b452005-10-08 00:29:44 +00005306 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5307 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00005308 AddToWorkList(Shift.Val);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005309 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005310 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005311 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005312 }
5313 return DAG.getNode(ISD::AND, AType, Shift, N2);
5314 }
5315 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005316 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005317 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00005318 AddToWorkList(Shift.Val);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005319 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005320 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005321 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005322 }
5323 return DAG.getNode(ISD::AND, AType, Shift, N2);
5324 }
5325 }
Nate Begeman07ed4172005-10-10 21:26:48 +00005326
5327 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00005328 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Nate Begeman07ed4172005-10-10 21:26:48 +00005329 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00005330
5331 // If the caller doesn't want us to simplify this into a zext of a compare,
5332 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00005333 if (NotExtCompare && N2C->getAPIntValue() == 1)
Chris Lattner1eba01e2007-04-11 06:50:51 +00005334 return SDOperand();
5335
Nate Begeman07ed4172005-10-10 21:26:48 +00005336 // Get a SetCC of the condition
5337 // FIXME: Should probably make sure that setcc is legal if we ever have a
5338 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00005339 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00005340 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00005341 if (AfterLegalize) {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005342 SCC = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005343 if (N2.getValueType().bitsLT(SCC.getValueType()))
Chris Lattner555d8d62006-12-07 22:36:47 +00005344 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5345 else
5346 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005347 } else {
5348 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00005349 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005350 }
Chris Lattner5750df92006-03-01 04:03:14 +00005351 AddToWorkList(SCC.Val);
5352 AddToWorkList(Temp.Val);
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005353
Dan Gohman002e5d02008-03-13 22:13:53 +00005354 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005355 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00005356 // shl setcc result by log2 n2c
5357 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00005358 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Nate Begeman07ed4172005-10-10 21:26:48 +00005359 TLI.getShiftAmountTy()));
5360 }
5361
Nate Begemanf845b452005-10-08 00:29:44 +00005362 // Check to see if this is the equivalent of setcc
5363 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5364 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00005365 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005366 MVT XType = N0.getValueType();
Duncan Sands184a8762008-06-14 17:48:34 +00005367 if (!AfterLegalize ||
5368 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(N0))) {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005369 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00005370 if (Res.getValueType() != VT)
5371 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5372 return Res;
5373 }
5374
5375 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5376 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands184a8762008-06-14 17:48:34 +00005377 (!AfterLegalize ||
5378 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Nate Begemanf845b452005-10-08 00:29:44 +00005379 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
5380 return DAG.getNode(ISD::SRL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005381 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Nate Begemanf845b452005-10-08 00:29:44 +00005382 TLI.getShiftAmountTy()));
5383 }
5384 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5385 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
5386 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
5387 N0);
5388 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
5389 DAG.getConstant(~0ULL, XType));
5390 return DAG.getNode(ISD::SRL, XType,
5391 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00005392 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005393 TLI.getShiftAmountTy()));
5394 }
5395 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5396 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
5397 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005398 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005399 TLI.getShiftAmountTy()));
5400 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5401 }
5402 }
5403
5404 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5405 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5406 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005407 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005408 N2.getOperand(0) == N1 && N0.getValueType().isInteger()) {
5409 MVT XType = N0.getValueType();
Chris Lattner1982ef22007-04-11 05:11:38 +00005410 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005411 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005412 TLI.getShiftAmountTy()));
5413 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
5414 AddToWorkList(Shift.Val);
5415 AddToWorkList(Add.Val);
5416 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5417 }
5418 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5419 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5420 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5421 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5422 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005423 MVT XType = N0.getValueType();
5424 if (SubC->isNullValue() && XType.isInteger()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005425 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005426 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005427 TLI.getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00005428 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005429 AddToWorkList(Shift.Val);
5430 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005431 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5432 }
5433 }
5434 }
Chris Lattner1982ef22007-04-11 05:11:38 +00005435
Nate Begeman44728a72005-09-19 22:34:01 +00005436 return SDOperand();
5437}
5438
Evan Chengfa1eb272007-02-08 22:13:59 +00005439/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005440SDOperand DAGCombiner::SimplifySetCC(MVT VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00005441 SDOperand N1, ISD::CondCode Cond,
5442 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00005443 TargetLowering::DAGCombinerInfo
5444 DagCombineInfo(DAG, !AfterLegalize, false, this);
5445 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00005446}
5447
Nate Begeman69575232005-10-20 02:15:44 +00005448/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5449/// return a DAG expression to select that will generate the same value by
5450/// multiplying by a magic number. See:
5451/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5452SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005453 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005454 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
5455
Andrew Lenharth232c9102006-06-12 16:07:18 +00005456 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005457 ii != ee; ++ii)
5458 AddToWorkList(*ii);
5459 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005460}
5461
5462/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5463/// return a DAG expression to select that will generate the same value by
5464/// multiplying by a magic number. See:
5465/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5466SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005467 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005468 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00005469
Andrew Lenharth232c9102006-06-12 16:07:18 +00005470 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005471 ii != ee; ++ii)
5472 AddToWorkList(*ii);
5473 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005474}
5475
Jim Laskey71382342006-10-07 23:37:56 +00005476/// FindBaseOffset - Return true if base is known not to alias with anything
5477/// but itself. Provides base object and offset as results.
5478static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
5479 // Assume it is a primitive operation.
5480 Base = Ptr; Offset = 0;
5481
5482 // If it's an adding a simple constant then integrate the offset.
5483 if (Base.getOpcode() == ISD::ADD) {
5484 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5485 Base = Base.getOperand(0);
5486 Offset += C->getValue();
5487 }
5488 }
5489
5490 // If it's any of the following then it can't alias with anything but itself.
5491 return isa<FrameIndexSDNode>(Base) ||
5492 isa<ConstantPoolSDNode>(Base) ||
5493 isa<GlobalAddressSDNode>(Base);
5494}
5495
5496/// isAlias - Return true if there is any possibility that the two addresses
5497/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00005498bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
5499 const Value *SrcValue1, int SrcValueOffset1,
5500 SDOperand Ptr2, int64_t Size2,
5501 const Value *SrcValue2, int SrcValueOffset2)
5502{
Jim Laskey71382342006-10-07 23:37:56 +00005503 // If they are the same then they must be aliases.
5504 if (Ptr1 == Ptr2) return true;
5505
5506 // Gather base node and offset information.
5507 SDOperand Base1, Base2;
5508 int64_t Offset1, Offset2;
5509 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5510 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5511
5512 // If they have a same base address then...
5513 if (Base1 == Base2) {
5514 // Check to see if the addresses overlap.
5515 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5516 }
5517
Jim Laskey096c22e2006-10-18 12:29:57 +00005518 // If we know both bases then they can't alias.
5519 if (KnownBase1 && KnownBase2) return false;
5520
Jim Laskey07a27092006-10-18 19:08:31 +00005521 if (CombinerGlobalAA) {
5522 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00005523 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5524 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5525 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00005526 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00005527 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00005528 if (AAResult == AliasAnalysis::NoAlias)
5529 return false;
5530 }
Jim Laskey096c22e2006-10-18 12:29:57 +00005531
5532 // Otherwise we have to assume they alias.
5533 return true;
Jim Laskey71382342006-10-07 23:37:56 +00005534}
5535
5536/// FindAliasInfo - Extracts the relevant alias information from the memory
5537/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00005538bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00005539 SDOperand &Ptr, int64_t &Size,
5540 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005541 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5542 Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005543 Size = LD->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005544 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005545 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00005546 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005547 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005548 Ptr = ST->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005549 Size = ST->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005550 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005551 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00005552 } else {
Jim Laskey71382342006-10-07 23:37:56 +00005553 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005554 }
5555
5556 return false;
5557}
5558
Jim Laskey6ff23e52006-10-04 16:53:27 +00005559/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5560/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00005561void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005562 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005563 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005564 std::set<SDNode *> Visited; // Visited node set.
5565
Jim Laskey279f0532006-09-25 16:29:54 +00005566 // Get alias information for node.
5567 SDOperand Ptr;
5568 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005569 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005570 int SrcValueOffset;
5571 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005572
Jim Laskey6ff23e52006-10-04 16:53:27 +00005573 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005574 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005575
Jim Laskeybc588b82006-10-05 15:07:25 +00005576 // Look at each chain and determine if it is an alias. If so, add it to the
5577 // aliases list. If not, then continue up the chain looking for the next
5578 // candidate.
5579 while (!Chains.empty()) {
5580 SDOperand Chain = Chains.back();
5581 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005582
Jim Laskeybc588b82006-10-05 15:07:25 +00005583 // Don't bother if we've been before.
5584 if (Visited.find(Chain.Val) != Visited.end()) continue;
5585 Visited.insert(Chain.Val);
5586
5587 switch (Chain.getOpcode()) {
5588 case ISD::EntryToken:
5589 // Entry token is ideal chain operand, but handled in FindBetterChain.
5590 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005591
Jim Laskeybc588b82006-10-05 15:07:25 +00005592 case ISD::LOAD:
5593 case ISD::STORE: {
5594 // Get alias information for Chain.
5595 SDOperand OpPtr;
5596 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005597 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005598 int OpSrcValueOffset;
5599 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
5600 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005601
5602 // If chain is alias then stop here.
5603 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005604 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5605 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005606 Aliases.push_back(Chain);
5607 } else {
5608 // Look further up the chain.
5609 Chains.push_back(Chain.getOperand(0));
5610 // Clean up old chain.
5611 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00005612 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005613 break;
5614 }
5615
5616 case ISD::TokenFactor:
5617 // We have to check each of the operands of the token factor, so we queue
5618 // then up. Adding the operands to the queue (stack) in reverse order
5619 // maintains the original order and increases the likelihood that getNode
5620 // will find a matching token factor (CSE.)
5621 for (unsigned n = Chain.getNumOperands(); n;)
5622 Chains.push_back(Chain.getOperand(--n));
5623 // Eliminate the token factor if we can.
5624 AddToWorkList(Chain.Val);
5625 break;
5626
5627 default:
5628 // For all other instructions we will just have to take what we can get.
5629 Aliases.push_back(Chain);
5630 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005631 }
5632 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005633}
5634
5635/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5636/// for a better chain (aliasing node.)
5637SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
5638 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005639
Jim Laskey6ff23e52006-10-04 16:53:27 +00005640 // Accumulate all the aliases to this node.
5641 GatherAllAliases(N, OldChain, Aliases);
5642
5643 if (Aliases.size() == 0) {
5644 // If no operands then chain to entry token.
5645 return DAG.getEntryNode();
5646 } else if (Aliases.size() == 1) {
5647 // If a single operand then chain to it. We don't need to revisit it.
5648 return Aliases[0];
5649 }
5650
5651 // Construct a custom tailored token factor.
5652 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5653 &Aliases[0], Aliases.size());
5654
5655 // Make sure the old chain gets cleaned up.
5656 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
5657
5658 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005659}
5660
Nate Begeman1d4d4142005-09-01 00:19:25 +00005661// SelectionDAG::Combine - This is the entry point for the file.
5662//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005663void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Chris Lattner938ab022007-01-16 04:55:25 +00005664 if (!RunningAfterLegalize && ViewDAGCombine1)
5665 viewGraph();
5666 if (RunningAfterLegalize && ViewDAGCombine2)
5667 viewGraph();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005668 /// run - This is the main entry point to this class.
5669 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005670 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005671}