blob: a57d815c2d5d679f484d406c92fb25cde32ad4c1 [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Nate Begeman1d4d4142005-09-01 00:19:25 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
Nate Begeman1d4d4142005-09-01 00:19:25 +000012//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "dagcombine"
Nate Begeman1d4d4142005-09-01 00:19:25 +000016#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner00161a62008-01-25 07:20:16 +000017#include "llvm/CodeGen/MachineFunction.h"
18#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000019#include "llvm/Analysis/AliasAnalysis.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000020#include "llvm/Target/TargetData.h"
Chris Lattner1329cb82008-01-26 19:45:50 +000021#include "llvm/Target/TargetFrameInfo.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000022#include "llvm/Target/TargetLowering.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000023#include "llvm/Target/TargetMachine.h"
Chris Lattnerddae4bd2007-01-08 23:04:05 +000024#include "llvm/Target/TargetOptions.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000025#include "llvm/ADT/SmallPtrSet.h"
26#include "llvm/ADT/Statistic.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000028#include "llvm/Support/CommandLine.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/MathExtras.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000031#include <algorithm>
Dan Gohmanee335e32008-05-23 20:40:06 +000032#include <set>
Nate Begeman1d4d4142005-09-01 00:19:25 +000033using namespace llvm;
34
Chris Lattnercd3245a2006-12-19 22:41:21 +000035STATISTIC(NodesCombined , "Number of dag nodes combined");
36STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
37STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
38
Nate Begeman1d4d4142005-09-01 00:19:25 +000039namespace {
Jim Laskey71382342006-10-07 23:37:56 +000040 static cl::opt<bool>
41 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000042 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000043
Jim Laskey07a27092006-10-18 19:08:31 +000044 static cl::opt<bool>
45 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
46 cl::desc("Include global information in alias analysis"));
47
Jim Laskeybc588b82006-10-05 15:07:25 +000048//------------------------------ DAGCombiner ---------------------------------//
49
Jim Laskey71382342006-10-07 23:37:56 +000050 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000051 SelectionDAG &DAG;
52 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000053 bool AfterLegalize;
Dan Gohmana2676512008-08-20 16:30:28 +000054 bool Fast;
Nate Begeman1d4d4142005-09-01 00:19:25 +000055
Dan Gohmana3f8b7a2008-08-28 21:01:56 +000056 // Create a dummy node (which is not added to allnodes), that adds a reference
57 // to the root node, preventing it from being deleted, and tracking any
58 // changes of the root.
59 HandleSDNode Dummy;
60
Nate Begeman1d4d4142005-09-01 00:19:25 +000061 // Worklist of all of the nodes that need to be simplified.
Dan Gohmana3f8b7a2008-08-28 21:01:56 +000062 SmallVector<SDNode*, 8> WorkList;
63
64 // The current position of our iteration through the allnodes list.
65 SelectionDAG::allnodes_iterator CurNode;
Nate Begeman1d4d4142005-09-01 00:19:25 +000066
Jim Laskeyc7c3f112006-10-16 20:52:31 +000067 // AA - Used for DAG load/store alias analysis.
68 AliasAnalysis &AA;
69
Dan Gohmana3f8b7a2008-08-28 21:01:56 +000070 /// AdvanceCurNode - Update CurNode to point to the next node to process.
71 ///
72 void AdvanceCurNode() {
73 // We start at the end of the list and work towards the front. Setting
74 // CurNode to DAG.allnodes_end() indicates that we're done.
75 if (CurNode == DAG.allnodes_begin())
76 CurNode = DAG.allnodes_end();
77 else
78 --CurNode;
79 }
80
Nate Begeman1d4d4142005-09-01 00:19:25 +000081 /// AddUsersToWorkList - When an instruction is simplified, add all users of
82 /// the instruction to the work lists because they might get more simplified
83 /// now.
84 ///
85 void AddUsersToWorkList(SDNode *N) {
86 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000087 UI != UE; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +000088 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000089 }
90
Dan Gohman389079b2007-10-08 17:57:15 +000091 /// visit - call the node-specific routine that knows how to fold each
92 /// particular type of node.
Dan Gohman475871a2008-07-27 21:46:04 +000093 SDValue visit(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +000094
Chris Lattner24664722006-03-01 04:53:38 +000095 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +000096 /// AddToWorkList - Add to the work list making sure it's instance is at the
97 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +000098 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000099 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +0000100 WorkList.push_back(N);
101 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000102
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000103 /// removeFromWorkList - remove all instances of N from the worklist.
104 ///
105 void removeFromWorkList(SDNode *N) {
106 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
107 WorkList.end());
Dan Gohmana3f8b7a2008-08-28 21:01:56 +0000108 // If the next node we were planning to process is deleted,
109 // skip past it.
110 if (N == CurNode)
111 AdvanceCurNode();
Chris Lattner01a22022005-10-10 22:04:48 +0000112 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000113
Dan Gohman475871a2008-07-27 21:46:04 +0000114 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000115 bool AddTo = true);
116
Dan Gohman475871a2008-07-27 21:46:04 +0000117 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Jim Laskey274062c2006-10-13 23:32:28 +0000118 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000119 }
120
Dan Gohman475871a2008-07-27 21:46:04 +0000121 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Jim Laskey274062c2006-10-13 23:32:28 +0000122 bool AddTo = true) {
Dan Gohman475871a2008-07-27 21:46:04 +0000123 SDValue To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000124 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000125 }
Chris Lattner0bd48932008-01-17 07:00:52 +0000126
Chris Lattner24664722006-03-01 04:53:38 +0000127 private:
128
Chris Lattner012f2412006-02-17 21:58:01 +0000129 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000130 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000131 /// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000132 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000133 APInt Demanded = APInt::getAllOnesValue(Op.getValueSizeInBits());
134 return SimplifyDemandedBits(Op, Demanded);
135 }
136
Dan Gohman475871a2008-07-27 21:46:04 +0000137 bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded);
Chris Lattner87514ca2005-10-10 22:31:19 +0000138
Chris Lattner448f2192006-11-11 00:39:41 +0000139 bool CombineToPreIndexedLoadStore(SDNode *N);
140 bool CombineToPostIndexedLoadStore(SDNode *N);
141
142
Dan Gohman389079b2007-10-08 17:57:15 +0000143 /// combine - call the node-specific routine that knows how to fold each
144 /// particular type of node. If that doesn't do anything, try the
145 /// target-specific DAG combines.
Dan Gohman475871a2008-07-27 21:46:04 +0000146 SDValue combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000147
148 // Visitation implementation - Implement dag node combining for different
149 // node types. The semantics are as follows:
150 // Return Value:
Gabor Greifba36cb52008-08-28 21:40:38 +0000151 // SDValue.getNode() == 0 - No change was made
152 // SDValue.getNode() == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000153 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000154 //
Dan Gohman475871a2008-07-27 21:46:04 +0000155 SDValue visitTokenFactor(SDNode *N);
156 SDValue visitMERGE_VALUES(SDNode *N);
157 SDValue visitADD(SDNode *N);
158 SDValue visitSUB(SDNode *N);
159 SDValue visitADDC(SDNode *N);
160 SDValue visitADDE(SDNode *N);
161 SDValue visitMUL(SDNode *N);
162 SDValue visitSDIV(SDNode *N);
163 SDValue visitUDIV(SDNode *N);
164 SDValue visitSREM(SDNode *N);
165 SDValue visitUREM(SDNode *N);
166 SDValue visitMULHU(SDNode *N);
167 SDValue visitMULHS(SDNode *N);
168 SDValue visitSMUL_LOHI(SDNode *N);
169 SDValue visitUMUL_LOHI(SDNode *N);
170 SDValue visitSDIVREM(SDNode *N);
171 SDValue visitUDIVREM(SDNode *N);
172 SDValue visitAND(SDNode *N);
173 SDValue visitOR(SDNode *N);
174 SDValue visitXOR(SDNode *N);
175 SDValue SimplifyVBinOp(SDNode *N);
176 SDValue visitSHL(SDNode *N);
177 SDValue visitSRA(SDNode *N);
178 SDValue visitSRL(SDNode *N);
179 SDValue visitCTLZ(SDNode *N);
180 SDValue visitCTTZ(SDNode *N);
181 SDValue visitCTPOP(SDNode *N);
182 SDValue visitSELECT(SDNode *N);
183 SDValue visitSELECT_CC(SDNode *N);
184 SDValue visitSETCC(SDNode *N);
185 SDValue visitSIGN_EXTEND(SDNode *N);
186 SDValue visitZERO_EXTEND(SDNode *N);
187 SDValue visitANY_EXTEND(SDNode *N);
188 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
189 SDValue visitTRUNCATE(SDNode *N);
190 SDValue visitBIT_CONVERT(SDNode *N);
191 SDValue visitBUILD_PAIR(SDNode *N);
192 SDValue visitFADD(SDNode *N);
193 SDValue visitFSUB(SDNode *N);
194 SDValue visitFMUL(SDNode *N);
195 SDValue visitFDIV(SDNode *N);
196 SDValue visitFREM(SDNode *N);
197 SDValue visitFCOPYSIGN(SDNode *N);
198 SDValue visitSINT_TO_FP(SDNode *N);
199 SDValue visitUINT_TO_FP(SDNode *N);
200 SDValue visitFP_TO_SINT(SDNode *N);
201 SDValue visitFP_TO_UINT(SDNode *N);
202 SDValue visitFP_ROUND(SDNode *N);
203 SDValue visitFP_ROUND_INREG(SDNode *N);
204 SDValue visitFP_EXTEND(SDNode *N);
205 SDValue visitFNEG(SDNode *N);
206 SDValue visitFABS(SDNode *N);
207 SDValue visitBRCOND(SDNode *N);
208 SDValue visitBR_CC(SDNode *N);
209 SDValue visitLOAD(SDNode *N);
210 SDValue visitSTORE(SDNode *N);
211 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
212 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
213 SDValue visitBUILD_VECTOR(SDNode *N);
214 SDValue visitCONCAT_VECTORS(SDNode *N);
215 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000216
Dan Gohman475871a2008-07-27 21:46:04 +0000217 SDValue XformToShuffleWithZero(SDNode *N);
218 SDValue ReassociateOps(unsigned Opc, SDValue LHS, SDValue RHS);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000219
Dan Gohman475871a2008-07-27 21:46:04 +0000220 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattnere70da202007-12-06 07:33:36 +0000221
Dan Gohman475871a2008-07-27 21:46:04 +0000222 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
223 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
224 SDValue SimplifySelect(SDValue N0, SDValue N1, SDValue N2);
225 SDValue SimplifySelectCC(SDValue N0, SDValue N1, SDValue N2,
226 SDValue N3, ISD::CondCode CC,
Chris Lattner1eba01e2007-04-11 06:50:51 +0000227 bool NotExtCompare = false);
Dan Gohman475871a2008-07-27 21:46:04 +0000228 SDValue SimplifySetCC(MVT VT, SDValue N0, SDValue N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000229 ISD::CondCode Cond, bool foldBooleans = true);
Dan Gohman475871a2008-07-27 21:46:04 +0000230 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner5eee4272008-01-26 01:09:19 +0000231 unsigned HiOp);
Dan Gohman475871a2008-07-27 21:46:04 +0000232 SDValue CombineConsecutiveLoads(SDNode *N, MVT VT);
233 SDValue ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, MVT);
234 SDValue BuildSDIV(SDNode *N);
235 SDValue BuildUDIV(SDNode *N);
236 SDNode *MatchRotate(SDValue LHS, SDValue RHS);
237 SDValue ReduceLoadWidth(SDNode *N);
Jim Laskey279f0532006-09-25 16:29:54 +0000238
Dan Gohman475871a2008-07-27 21:46:04 +0000239 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Chris Lattner2b4c2792007-10-13 06:35:54 +0000240
Jim Laskey6ff23e52006-10-04 16:53:27 +0000241 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
242 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000243 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
244 SmallVector<SDValue, 8> &Aliases);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000245
Jim Laskey096c22e2006-10-18 12:29:57 +0000246 /// isAlias - Return true if there is any possibility that the two addresses
247 /// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +0000248 bool isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +0000249 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman475871a2008-07-27 21:46:04 +0000250 SDValue Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000251 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000252
Jim Laskey7ca56af2006-10-11 13:47:09 +0000253 /// FindAliasInfo - Extracts the relevant alias information from the memory
254 /// node. Returns true if the operand was a load.
255 bool FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000256 SDValue &Ptr, int64_t &Size,
Jim Laskey096c22e2006-10-18 12:29:57 +0000257 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000258
Jim Laskey279f0532006-09-25 16:29:54 +0000259 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000260 /// looking for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +0000261 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Jim Laskey279f0532006-09-25 16:29:54 +0000262
Nate Begeman1d4d4142005-09-01 00:19:25 +0000263public:
Dan Gohmana2676512008-08-20 16:30:28 +0000264 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, bool fast)
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000265 : DAG(D),
266 TLI(D.getTargetLoweringInfo()),
267 AfterLegalize(false),
Dan Gohmana2676512008-08-20 16:30:28 +0000268 Fast(fast),
Dan Gohmana3f8b7a2008-08-28 21:01:56 +0000269 Dummy(D.getRoot()),
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000270 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000271
272 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000273 void Run(bool RunningAfterLegalize);
Dan Gohmana3f8b7a2008-08-28 21:01:56 +0000274
275 /// ProcessNode - runs the dag combiner on a node
276 void ProcessNode(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000277 };
278}
279
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000280
281namespace {
282/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
283/// nodes from the worklist.
284class VISIBILITY_HIDDEN WorkListRemover :
285 public SelectionDAG::DAGUpdateListener {
286 DAGCombiner &DC;
287public:
Dan Gohmanb5660dc2008-02-20 16:44:09 +0000288 explicit WorkListRemover(DAGCombiner &dc) : DC(dc) {}
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000289
Duncan Sandsedfcf592008-06-11 11:42:12 +0000290 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000291 DC.removeFromWorkList(N);
292 }
293
294 virtual void NodeUpdated(SDNode *N) {
295 // Ignore updates.
296 }
297};
298}
299
Chris Lattner24664722006-03-01 04:53:38 +0000300//===----------------------------------------------------------------------===//
301// TargetLowering::DAGCombinerInfo implementation
302//===----------------------------------------------------------------------===//
303
304void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
305 ((DAGCombiner*)DC)->AddToWorkList(N);
306}
307
Dan Gohman475871a2008-07-27 21:46:04 +0000308SDValue TargetLowering::DAGCombinerInfo::
309CombineTo(SDNode *N, const std::vector<SDValue> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000310 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000311}
312
Dan Gohman475871a2008-07-27 21:46:04 +0000313SDValue TargetLowering::DAGCombinerInfo::
314CombineTo(SDNode *N, SDValue Res) {
Chris Lattner24664722006-03-01 04:53:38 +0000315 return ((DAGCombiner*)DC)->CombineTo(N, Res);
316}
317
318
Dan Gohman475871a2008-07-27 21:46:04 +0000319SDValue TargetLowering::DAGCombinerInfo::
320CombineTo(SDNode *N, SDValue Res0, SDValue Res1) {
Chris Lattner24664722006-03-01 04:53:38 +0000321 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
322}
323
324
Chris Lattner24664722006-03-01 04:53:38 +0000325//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000326// Helper Functions
327//===----------------------------------------------------------------------===//
328
329/// isNegatibleForFree - Return 1 if we can compute the negated form of the
330/// specified expression for the same cost as the expression itself, or 2 if we
331/// can compute the negated form more cheaply than the expression itself.
Dan Gohman475871a2008-07-27 21:46:04 +0000332static char isNegatibleForFree(SDValue Op, bool AfterLegalize,
Chris Lattner0254e702008-02-26 07:04:54 +0000333 unsigned Depth = 0) {
Dale Johannesendb44bf82007-10-16 23:38:29 +0000334 // No compile time optimizations on this type.
335 if (Op.getValueType() == MVT::ppcf128)
336 return 0;
337
Chris Lattner29446522007-05-14 22:04:50 +0000338 // fneg is removable even if it has multiple uses.
339 if (Op.getOpcode() == ISD::FNEG) return 2;
340
341 // Don't allow anything with multiple uses.
342 if (!Op.hasOneUse()) return 0;
343
Chris Lattner3adf9512007-05-25 02:19:06 +0000344 // Don't recurse exponentially.
345 if (Depth > 6) return 0;
346
Chris Lattner29446522007-05-14 22:04:50 +0000347 switch (Op.getOpcode()) {
348 default: return false;
349 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000350 // Don't invert constant FP values after legalize. The negated constant
351 // isn't necessarily legal.
352 return AfterLegalize ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000353 case ISD::FADD:
354 // FIXME: determine better conditions for this xform.
355 if (!UnsafeFPMath) return 0;
356
357 // -(A+B) -> -A - B
Chris Lattner0254e702008-02-26 07:04:54 +0000358 if (char V = isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000359 return V;
360 // -(A+B) -> -B - A
Chris Lattner0254e702008-02-26 07:04:54 +0000361 return isNegatibleForFree(Op.getOperand(1), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000362 case ISD::FSUB:
363 // We can't turn -(A-B) into B-A when we honor signed zeros.
364 if (!UnsafeFPMath) return 0;
365
366 // -(A-B) -> B-A
367 return 1;
368
369 case ISD::FMUL:
370 case ISD::FDIV:
371 if (HonorSignDependentRoundingFPMath()) return 0;
372
373 // -(X*Y) -> (-X * Y) or (X*-Y)
Chris Lattner0254e702008-02-26 07:04:54 +0000374 if (char V = isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000375 return V;
376
Chris Lattner0254e702008-02-26 07:04:54 +0000377 return isNegatibleForFree(Op.getOperand(1), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000378
379 case ISD::FP_EXTEND:
380 case ISD::FP_ROUND:
381 case ISD::FSIN:
Chris Lattner0254e702008-02-26 07:04:54 +0000382 return isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000383 }
384}
385
386/// GetNegatedExpression - If isNegatibleForFree returns true, this function
387/// returns the newly negated expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000388static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Chris Lattner0254e702008-02-26 07:04:54 +0000389 bool AfterLegalize, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000390 // fneg is removable even if it has multiple uses.
391 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
392
393 // Don't allow anything with multiple uses.
394 assert(Op.hasOneUse() && "Unknown reuse!");
395
Chris Lattner3adf9512007-05-25 02:19:06 +0000396 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000397 switch (Op.getOpcode()) {
398 default: assert(0 && "Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000399 case ISD::ConstantFP: {
400 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
401 V.changeSign();
402 return DAG.getConstantFP(V, Op.getValueType());
403 }
Chris Lattner29446522007-05-14 22:04:50 +0000404 case ISD::FADD:
405 // FIXME: determine better conditions for this xform.
406 assert(UnsafeFPMath);
407
408 // -(A+B) -> -A - B
Chris Lattner0254e702008-02-26 07:04:54 +0000409 if (isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000410 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000411 GetNegatedExpression(Op.getOperand(0), DAG,
412 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000413 Op.getOperand(1));
414 // -(A+B) -> -B - A
415 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000416 GetNegatedExpression(Op.getOperand(1), DAG,
417 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000418 Op.getOperand(0));
419 case ISD::FSUB:
420 // We can't turn -(A-B) into B-A when we honor signed zeros.
421 assert(UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000422
423 // -(0-B) -> B
424 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000425 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000426 return Op.getOperand(1);
Chris Lattner29446522007-05-14 22:04:50 +0000427
428 // -(A-B) -> B-A
429 return DAG.getNode(ISD::FSUB, Op.getValueType(), Op.getOperand(1),
430 Op.getOperand(0));
431
432 case ISD::FMUL:
433 case ISD::FDIV:
434 assert(!HonorSignDependentRoundingFPMath());
435
436 // -(X*Y) -> -X * Y
Chris Lattneraeecb6c2008-02-26 17:09:59 +0000437 if (isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000438 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000439 GetNegatedExpression(Op.getOperand(0), DAG,
440 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000441 Op.getOperand(1));
442
443 // -(X*Y) -> X * -Y
444 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
445 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000446 GetNegatedExpression(Op.getOperand(1), DAG,
447 AfterLegalize, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000448
449 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000450 case ISD::FSIN:
451 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000452 GetNegatedExpression(Op.getOperand(0), DAG,
453 AfterLegalize, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000454 case ISD::FP_ROUND:
455 return DAG.getNode(ISD::FP_ROUND, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000456 GetNegatedExpression(Op.getOperand(0), DAG,
457 AfterLegalize, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000458 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000459 }
460}
Chris Lattner24664722006-03-01 04:53:38 +0000461
462
Nate Begeman4ebd8052005-09-01 23:24:04 +0000463// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
464// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000465// Also, set the incoming LHS, RHS, and CC references to the appropriate
466// nodes based on the type of node we are checking. This simplifies life a
467// bit for the callers.
Dan Gohman475871a2008-07-27 21:46:04 +0000468static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
469 SDValue &CC) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000470 if (N.getOpcode() == ISD::SETCC) {
471 LHS = N.getOperand(0);
472 RHS = N.getOperand(1);
473 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000474 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000475 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000476 if (N.getOpcode() == ISD::SELECT_CC &&
477 N.getOperand(2).getOpcode() == ISD::Constant &&
478 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000479 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000480 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
481 LHS = N.getOperand(0);
482 RHS = N.getOperand(1);
483 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000484 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000485 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000486 return false;
487}
488
Nate Begeman99801192005-09-07 23:25:52 +0000489// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
490// one use. If this is true, it allows the users to invert the operation for
491// free when it is profitable to do so.
Dan Gohman475871a2008-07-27 21:46:04 +0000492static bool isOneUseSetCC(SDValue N) {
493 SDValue N0, N1, N2;
Gabor Greifba36cb52008-08-28 21:40:38 +0000494 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000495 return true;
496 return false;
497}
498
Dan Gohman475871a2008-07-27 21:46:04 +0000499SDValue DAGCombiner::ReassociateOps(unsigned Opc, SDValue N0, SDValue N1){
Duncan Sands83ec4b62008-06-06 12:08:01 +0000500 MVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000501 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
502 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
503 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
504 if (isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000505 SDValue OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000506 AddToWorkList(OpNode.getNode());
Nate Begemancd4d58c2006-02-03 06:46:56 +0000507 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
508 } else if (N0.hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000509 SDValue OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000510 AddToWorkList(OpNode.getNode());
Nate Begemancd4d58c2006-02-03 06:46:56 +0000511 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
512 }
513 }
514 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
515 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
516 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
517 if (isa<ConstantSDNode>(N0)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000518 SDValue OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000519 AddToWorkList(OpNode.getNode());
Nate Begemancd4d58c2006-02-03 06:46:56 +0000520 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
521 } else if (N1.hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000522 SDValue OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000523 AddToWorkList(OpNode.getNode());
Nate Begemancd4d58c2006-02-03 06:46:56 +0000524 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
525 }
526 }
Dan Gohman475871a2008-07-27 21:46:04 +0000527 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000528}
529
Dan Gohman475871a2008-07-27 21:46:04 +0000530SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
531 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000532 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
533 ++NodesCombined;
534 DOUT << "\nReplacing.1 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +0000535 DOUT << "\nWith: "; DEBUG(To[0].getNode()->dump(&DAG));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000536 DOUT << " and " << NumTo-1 << " other values\n";
537 WorkListRemover DeadNodes(*this);
538 DAG.ReplaceAllUsesWith(N, To, &DeadNodes);
539
540 if (AddTo) {
541 // Push the new nodes and any users onto the worklist
542 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000543 AddToWorkList(To[i].getNode());
544 AddUsersToWorkList(To[i].getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000545 }
546 }
547
548 // Nodes can be reintroduced into the worklist. Make sure we do not
549 // process a node that has been replaced.
550 removeFromWorkList(N);
551
552 // Finally, since the node is now dead, remove it from the graph.
553 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000554 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000555}
556
557/// SimplifyDemandedBits - Check the specified integer node value to see if
558/// it can be simplified or if things it uses can be simplified by bit
559/// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000560bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000561 TargetLowering::TargetLoweringOpt TLO(DAG, AfterLegalize);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000562 APInt KnownZero, KnownOne;
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000563 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
564 return false;
565
566 // Revisit the node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000567 AddToWorkList(Op.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000568
569 // Replace the old value with the new one.
570 ++NodesCombined;
Gabor Greifba36cb52008-08-28 21:40:38 +0000571 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.getNode()->dump(&DAG));
572 DOUT << "\nWith: "; DEBUG(TLO.New.getNode()->dump(&DAG));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000573 DOUT << '\n';
574
575 // Replace all uses. If any nodes become isomorphic to other nodes and
576 // are deleted, make sure to remove them from our worklist.
577 WorkListRemover DeadNodes(*this);
578 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
579
580 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000581 AddToWorkList(TLO.New.getNode());
582 AddUsersToWorkList(TLO.New.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000583
584 // Finally, if the node is now dead, remove it from the graph. The node
585 // may not be dead if the replacement process recursively simplified to
586 // something else needing this node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000587 if (TLO.Old.getNode()->use_empty()) {
588 removeFromWorkList(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000589
590 // If the operands of this node are only used by the node, they will now
591 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greifba36cb52008-08-28 21:40:38 +0000592 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
593 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
594 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000595
Gabor Greifba36cb52008-08-28 21:40:38 +0000596 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000597 }
598 return true;
599}
600
Chris Lattner29446522007-05-14 22:04:50 +0000601//===----------------------------------------------------------------------===//
602// Main DAG Combiner implementation
603//===----------------------------------------------------------------------===//
604
Dan Gohmana3f8b7a2008-08-28 21:01:56 +0000605void DAGCombiner::ProcessNode(SDNode *N) {
606 // If N has no uses, it is dead. Make sure to revisit all N's operands once
607 // N is deleted from the DAG, since they too may now be dead or may have a
608 // reduced number of uses, allowing other xforms.
609 if (N->use_empty() && N != &Dummy) {
610 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +0000611 AddToWorkList(N->getOperand(i).getNode());
Dan Gohmana3f8b7a2008-08-28 21:01:56 +0000612
613 DAG.DeleteNode(N);
614 return;
615 }
616
617 SDValue RV = combine(N);
618
Gabor Greifba36cb52008-08-28 21:40:38 +0000619 if (RV.getNode() == 0)
Dan Gohmana3f8b7a2008-08-28 21:01:56 +0000620 return;
621
622 ++NodesCombined;
623
624 // If we get back the same node we passed in, rather than a new node or
625 // zero, we know that the node must have defined multiple values and
626 // CombineTo was used. Since CombineTo takes care of the worklist
627 // mechanics for us, we have no work to do in this case.
Gabor Greifba36cb52008-08-28 21:40:38 +0000628 if (RV.getNode() == N)
Dan Gohmana3f8b7a2008-08-28 21:01:56 +0000629 return;
630
631 assert(N->getOpcode() != ISD::DELETED_NODE &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000632 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
Dan Gohmana3f8b7a2008-08-28 21:01:56 +0000633 "Node was deleted but visit returned new node!");
634
635 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +0000636 DOUT << "\nWith: "; DEBUG(RV.getNode()->dump(&DAG));
Dan Gohmana3f8b7a2008-08-28 21:01:56 +0000637 DOUT << '\n';
638
Gabor Greifba36cb52008-08-28 21:40:38 +0000639 if (N->getNumValues() == RV.getNode()->getNumValues())
640 DAG.ReplaceAllUsesWith(N, RV.getNode());
Dan Gohmana3f8b7a2008-08-28 21:01:56 +0000641 else {
642 assert(N->getValueType(0) == RV.getValueType() &&
643 N->getNumValues() == 1 && "Type mismatch");
644 SDValue OpV = RV;
645 DAG.ReplaceAllUsesWith(N, &OpV);
646 }
647
648 // Delete the old node.
649 removeFromWorkList(N);
650 DAG.DeleteNode(N);
651
652 // Push the new node and any users onto the worklist
Gabor Greifba36cb52008-08-28 21:40:38 +0000653 AddToWorkList(RV.getNode());
654 AddUsersToWorkList(RV.getNode());
Dan Gohmana3f8b7a2008-08-28 21:01:56 +0000655}
656
Nate Begeman4ebd8052005-09-01 23:24:04 +0000657void DAGCombiner::Run(bool RunningAfterLegalize) {
658 // set the instance variable, so that the various visit routines may use it.
659 AfterLegalize = RunningAfterLegalize;
660
Jim Laskey26f7fa72006-10-17 19:33:52 +0000661 // The root of the dag may dangle to deleted nodes until the dag combiner is
662 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +0000663 DAG.setRoot(SDValue());
Chris Lattner24664722006-03-01 04:53:38 +0000664
Dan Gohmana3f8b7a2008-08-28 21:01:56 +0000665 // Process all the original dag nodes. We process starting from the
666 // end of the list and working forward, which is in roughly topological
667 // order. Starting at the end and working forward means we won't
668 // accidentally revisit nodes created during the dagcombine process.
669 CurNode = prior(DAG.allnodes_end());
670 do {
671 SDNode *N = &*CurNode;
672 AdvanceCurNode();
673 ProcessNode(N);
674 // Processing the node may have resulted in nodes being added to the
675 // worklist, because the were newly created or because one of their
676 // operands changed or some other reason they should be revisited.
677 // While the worklist isn't empty, inspect the node on the end of it
678 // and try and combine it.
679 while (!WorkList.empty()) {
680 SDNode *N = WorkList.back();
681 WorkList.pop_back();
682 if (N == CurNode)
683 AdvanceCurNode();
684 ProcessNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000685 }
Dan Gohmana3f8b7a2008-08-28 21:01:56 +0000686 } while (CurNode != DAG.allnodes_end());
Chris Lattner729c6d12006-05-27 00:43:02 +0000687
Chris Lattner95038592005-10-05 06:35:28 +0000688 // If the root changed (e.g. it was a dead load, update the root).
689 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000690}
691
Dan Gohman475871a2008-07-27 21:46:04 +0000692SDValue DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000693 switch(N->getOpcode()) {
694 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000695 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000696 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000697 case ISD::ADD: return visitADD(N);
698 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000699 case ISD::ADDC: return visitADDC(N);
700 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000701 case ISD::MUL: return visitMUL(N);
702 case ISD::SDIV: return visitSDIV(N);
703 case ISD::UDIV: return visitUDIV(N);
704 case ISD::SREM: return visitSREM(N);
705 case ISD::UREM: return visitUREM(N);
706 case ISD::MULHU: return visitMULHU(N);
707 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000708 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
709 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
710 case ISD::SDIVREM: return visitSDIVREM(N);
711 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000712 case ISD::AND: return visitAND(N);
713 case ISD::OR: return visitOR(N);
714 case ISD::XOR: return visitXOR(N);
715 case ISD::SHL: return visitSHL(N);
716 case ISD::SRA: return visitSRA(N);
717 case ISD::SRL: return visitSRL(N);
718 case ISD::CTLZ: return visitCTLZ(N);
719 case ISD::CTTZ: return visitCTTZ(N);
720 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000721 case ISD::SELECT: return visitSELECT(N);
722 case ISD::SELECT_CC: return visitSELECT_CC(N);
723 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000724 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
725 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000726 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000727 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
728 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000729 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +0000730 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000731 case ISD::FADD: return visitFADD(N);
732 case ISD::FSUB: return visitFSUB(N);
733 case ISD::FMUL: return visitFMUL(N);
734 case ISD::FDIV: return visitFDIV(N);
735 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000736 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000737 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
738 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
739 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
740 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
741 case ISD::FP_ROUND: return visitFP_ROUND(N);
742 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
743 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
744 case ISD::FNEG: return visitFNEG(N);
745 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000746 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000747 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000748 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000749 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000750 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +0000751 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000752 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
753 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000754 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000755 }
Dan Gohman475871a2008-07-27 21:46:04 +0000756 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000757}
758
Dan Gohman475871a2008-07-27 21:46:04 +0000759SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman389079b2007-10-08 17:57:15 +0000760
Dan Gohman475871a2008-07-27 21:46:04 +0000761 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000762
763 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +0000764 if (RV.getNode() == 0) {
Dan Gohman389079b2007-10-08 17:57:15 +0000765 assert(N->getOpcode() != ISD::DELETED_NODE &&
766 "Node was deleted but visit returned NULL!");
767
768 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
769 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
770
771 // Expose the DAG combiner to the target combiner impls.
772 TargetLowering::DAGCombinerInfo
773 DagCombineInfo(DAG, !AfterLegalize, false, this);
774
775 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
776 }
777 }
778
Evan Cheng08b11732008-03-22 01:55:50 +0000779 // If N is a commutative binary node, try commuting it to enable more
780 // sdisel CSE.
Gabor Greifba36cb52008-08-28 21:40:38 +0000781 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +0000782 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
783 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +0000784 SDValue N0 = N->getOperand(0);
785 SDValue N1 = N->getOperand(1);
Evan Cheng08b11732008-03-22 01:55:50 +0000786 // Constant operands are canonicalized to RHS.
787 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000788 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +0000789 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
790 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +0000791 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +0000792 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +0000793 }
794 }
795
Dan Gohman389079b2007-10-08 17:57:15 +0000796 return RV;
797}
798
Chris Lattner6270f682006-10-08 22:57:01 +0000799/// getInputChainForNode - Given a node, return its input chain if it has one,
800/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000801static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000802 if (unsigned NumOps = N->getNumOperands()) {
803 if (N->getOperand(0).getValueType() == MVT::Other)
804 return N->getOperand(0);
805 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
806 return N->getOperand(NumOps-1);
807 for (unsigned i = 1; i < NumOps-1; ++i)
808 if (N->getOperand(i).getValueType() == MVT::Other)
809 return N->getOperand(i);
810 }
Dan Gohman475871a2008-07-27 21:46:04 +0000811 return SDValue(0, 0);
Chris Lattner6270f682006-10-08 22:57:01 +0000812}
813
Dan Gohman475871a2008-07-27 21:46:04 +0000814SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000815 // If N has two operands, where one has an input chain equal to the other,
816 // the 'other' chain is redundant.
817 if (N->getNumOperands() == 2) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000818 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +0000819 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000820 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +0000821 return N->getOperand(1);
822 }
823
Chris Lattnerc76d4412007-05-16 06:37:59 +0000824 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +0000825 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Chris Lattnerc76d4412007-05-16 06:37:59 +0000826 SmallPtrSet<SDNode*, 16> SeenOps;
827 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000828
829 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000830 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000831
Jim Laskey71382342006-10-07 23:37:56 +0000832 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000833 // encountered.
834 for (unsigned i = 0; i < TFs.size(); ++i) {
835 SDNode *TF = TFs[i];
836
Jim Laskey6ff23e52006-10-04 16:53:27 +0000837 // Check each of the operands.
838 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000839 SDValue Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000840
Jim Laskey6ff23e52006-10-04 16:53:27 +0000841 switch (Op.getOpcode()) {
842 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000843 // Entry tokens don't need to be added to the list. They are
844 // rededundant.
845 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000846 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000847
Jim Laskey6ff23e52006-10-04 16:53:27 +0000848 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000849 if ((CombinerAA || Op.hasOneUse()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000850 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000851 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +0000852 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +0000853 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +0000854 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +0000855 Changed = true;
856 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000857 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000858 // Fall thru
859
860 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000861 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +0000862 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +0000863 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000864 else
865 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000866 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000867 }
868 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000869 }
870
Dan Gohman475871a2008-07-27 21:46:04 +0000871 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000872
873 // If we've change things around then replace token factor.
874 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +0000875 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000876 // The entry token is the only possible outcome.
877 Result = DAG.getEntryNode();
878 } else {
879 // New and improved token factor.
880 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000881 }
Jim Laskey274062c2006-10-13 23:32:28 +0000882
883 // Don't add users to work list.
884 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000885 }
Jim Laskey279f0532006-09-25 16:29:54 +0000886
Jim Laskey6ff23e52006-10-04 16:53:27 +0000887 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000888}
889
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000890/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +0000891SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000892 WorkListRemover DeadNodes(*this);
893 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Dan Gohman475871a2008-07-27 21:46:04 +0000894 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i),
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000895 &DeadNodes);
896 removeFromWorkList(N);
897 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000898 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000899}
900
901
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000902static
Dan Gohman475871a2008-07-27 21:46:04 +0000903SDValue combineShlAddConstant(SDValue N0, SDValue N1, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000904 MVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000905 SDValue N00 = N0.getOperand(0);
906 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000907 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Gabor Greifba36cb52008-08-28 21:40:38 +0000908 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000909 isa<ConstantSDNode>(N00.getOperand(1))) {
910 N0 = DAG.getNode(ISD::ADD, VT,
911 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
912 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
913 return DAG.getNode(ISD::ADD, VT, N0, N1);
914 }
Dan Gohman475871a2008-07-27 21:46:04 +0000915 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000916}
917
Evan Chengb13cdbd2007-06-21 07:39:16 +0000918static
Dan Gohman475871a2008-07-27 21:46:04 +0000919SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
920 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000921 MVT VT = N->getValueType(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000922 unsigned Opc = N->getOpcode();
923 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
Dan Gohman475871a2008-07-27 21:46:04 +0000924 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
925 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000926 ISD::CondCode CC = ISD::SETCC_INVALID;
927 if (isSlctCC)
928 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
929 else {
Dan Gohman475871a2008-07-27 21:46:04 +0000930 SDValue CCOp = Slct.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000931 if (CCOp.getOpcode() == ISD::SETCC)
932 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
933 }
934
935 bool DoXform = false;
936 bool InvCC = false;
937 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
938 "Bad input!");
939 if (LHS.getOpcode() == ISD::Constant &&
940 cast<ConstantSDNode>(LHS)->isNullValue())
941 DoXform = true;
942 else if (CC != ISD::SETCC_INVALID &&
943 RHS.getOpcode() == ISD::Constant &&
944 cast<ConstantSDNode>(RHS)->isNullValue()) {
945 std::swap(LHS, RHS);
Dan Gohman475871a2008-07-27 21:46:04 +0000946 SDValue Op0 = Slct.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000947 bool isInt = (isSlctCC ? Op0.getValueType() :
948 Op0.getOperand(0).getValueType()).isInteger();
Evan Chengb13cdbd2007-06-21 07:39:16 +0000949 CC = ISD::getSetCCInverse(CC, isInt);
950 DoXform = true;
951 InvCC = true;
952 }
953
954 if (DoXform) {
Dan Gohman475871a2008-07-27 21:46:04 +0000955 SDValue Result = DAG.getNode(Opc, VT, OtherOp, RHS);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000956 if (isSlctCC)
957 return DAG.getSelectCC(OtherOp, Result,
958 Slct.getOperand(0), Slct.getOperand(1), CC);
Dan Gohman475871a2008-07-27 21:46:04 +0000959 SDValue CCOp = Slct.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000960 if (InvCC)
961 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
962 CCOp.getOperand(1), CC);
963 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
964 }
Dan Gohman475871a2008-07-27 21:46:04 +0000965 return SDValue();
Evan Chengb13cdbd2007-06-21 07:39:16 +0000966}
967
Dan Gohman475871a2008-07-27 21:46:04 +0000968SDValue DAGCombiner::visitADD(SDNode *N) {
969 SDValue N0 = N->getOperand(0);
970 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000971 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
972 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000973 MVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000974
975 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +0000976 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000977 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +0000978 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +0000979 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000980
Dan Gohman613e0d82007-07-03 14:03:57 +0000981 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +0000982 if (N0.getOpcode() == ISD::UNDEF)
983 return N0;
984 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +0000985 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000986 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000987 if (N0C && N1C)
Dan Gohman002e5d02008-03-13 22:13:53 +0000988 return DAG.getConstant(N0C->getAPIntValue() + N1C->getAPIntValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000989 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000990 if (N0C && !N1C)
991 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000992 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000993 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000994 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000995 // fold ((c1-A)+c2) -> (c1+c2)-A
996 if (N1C && N0.getOpcode() == ISD::SUB)
997 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
998 return DAG.getNode(ISD::SUB, VT,
Dan Gohman002e5d02008-03-13 22:13:53 +0000999 DAG.getConstant(N1C->getAPIntValue()+
1000 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001001 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +00001002 // reassociate add
Dan Gohman475871a2008-07-27 21:46:04 +00001003 SDValue RADD = ReassociateOps(ISD::ADD, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001004 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001005 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001006 // fold ((0-A) + B) -> B-A
1007 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1008 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +00001009 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001010 // fold (A + (0-B)) -> A-B
1011 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1012 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +00001013 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001014 // fold (A+(B-A)) -> B
1015 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001016 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +00001017
Dan Gohman475871a2008-07-27 21:46:04 +00001018 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1019 return SDValue(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +00001020
1021 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001022 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001023 APInt LHSZero, LHSOne;
1024 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001025 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001026 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001027 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001028 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +00001029
1030 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1031 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1032 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1033 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1034 return DAG.getNode(ISD::OR, VT, N0, N1);
1035 }
1036 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001037
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001038 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001039 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001040 SDValue Result = combineShlAddConstant(N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001041 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001042 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001043 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001044 SDValue Result = combineShlAddConstant(N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001045 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001046 }
1047
Evan Chengb13cdbd2007-06-21 07:39:16 +00001048 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Gabor Greifba36cb52008-08-28 21:40:38 +00001049 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001050 SDValue Result = combineSelectAndUse(N, N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001051 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001052 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001053 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001054 SDValue Result = combineSelectAndUse(N, N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001055 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001056 }
1057
Dan Gohman475871a2008-07-27 21:46:04 +00001058 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001059}
1060
Dan Gohman475871a2008-07-27 21:46:04 +00001061SDValue DAGCombiner::visitADDC(SDNode *N) {
1062 SDValue N0 = N->getOperand(0);
1063 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001064 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1065 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001066 MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001067
1068 // If the flag result is dead, turn this into an ADD.
1069 if (N->hasNUsesOfValue(0, 1))
1070 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +00001071 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +00001072
1073 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001074 if (N0C && !N1C)
Dan Gohman56867522008-06-21 22:06:07 +00001075 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Chris Lattner91153682007-03-04 20:03:15 +00001076
Chris Lattnerb6541762007-03-04 20:40:38 +00001077 // fold (addc x, 0) -> x + no carry out
1078 if (N1C && N1C->isNullValue())
1079 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1080
1081 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001082 APInt LHSZero, LHSOne;
1083 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001084 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001085 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001086 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001087 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001088
1089 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1090 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1091 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1092 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1093 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
1094 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1095 }
Chris Lattner91153682007-03-04 20:03:15 +00001096
Dan Gohman475871a2008-07-27 21:46:04 +00001097 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001098}
1099
Dan Gohman475871a2008-07-27 21:46:04 +00001100SDValue DAGCombiner::visitADDE(SDNode *N) {
1101 SDValue N0 = N->getOperand(0);
1102 SDValue N1 = N->getOperand(1);
1103 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001104 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1105 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001106 //MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001107
1108 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001109 if (N0C && !N1C)
Dan Gohman56867522008-06-21 22:06:07 +00001110 return DAG.getNode(ISD::ADDE, N->getVTList(), N1, N0, CarryIn);
Chris Lattner91153682007-03-04 20:03:15 +00001111
Chris Lattnerb6541762007-03-04 20:40:38 +00001112 // fold (adde x, y, false) -> (addc x, y)
Dan Gohman0a4627d2008-06-23 15:29:14 +00001113 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Dan Gohman56867522008-06-21 22:06:07 +00001114 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Chris Lattner91153682007-03-04 20:03:15 +00001115
Dan Gohman475871a2008-07-27 21:46:04 +00001116 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001117}
1118
1119
1120
Dan Gohman475871a2008-07-27 21:46:04 +00001121SDValue DAGCombiner::visitSUB(SDNode *N) {
1122 SDValue N0 = N->getOperand(0);
1123 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001124 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1125 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001126 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001127
Dan Gohman7f321562007-06-25 16:23:39 +00001128 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001129 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001130 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001131 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001132 }
Dan Gohman7f321562007-06-25 16:23:39 +00001133
Chris Lattner854077d2005-10-17 01:07:11 +00001134 // fold (sub x, x) -> 0
Evan Chengc8e3b142008-03-12 07:02:50 +00001135 if (N0 == N1)
Chris Lattner854077d2005-10-17 01:07:11 +00001136 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001137 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001138 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001139 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +00001140 // fold (sub x, c) -> (add x, -c)
1141 if (N1C)
Dan Gohman002e5d02008-03-13 22:13:53 +00001142 return DAG.getNode(ISD::ADD, VT, N0,
1143 DAG.getConstant(-N1C->getAPIntValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001144 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001145 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001146 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001147 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001148 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001149 return N0.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +00001150 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Gabor Greifba36cb52008-08-28 21:40:38 +00001151 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001152 SDValue Result = combineSelectAndUse(N, N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001153 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001154 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001155 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001156 if (N0.getOpcode() == ISD::UNDEF)
1157 return N0;
1158 if (N1.getOpcode() == ISD::UNDEF)
1159 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001160
Dan Gohman475871a2008-07-27 21:46:04 +00001161 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001162}
1163
Dan Gohman475871a2008-07-27 21:46:04 +00001164SDValue DAGCombiner::visitMUL(SDNode *N) {
1165 SDValue N0 = N->getOperand(0);
1166 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001167 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1168 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001169 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001170
Dan Gohman7f321562007-06-25 16:23:39 +00001171 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001172 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001173 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001174 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001175 }
Dan Gohman7f321562007-06-25 16:23:39 +00001176
Dan Gohman613e0d82007-07-03 14:03:57 +00001177 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001178 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001179 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001180 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001181 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001182 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001183 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001184 if (N0C && !N1C)
1185 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001186 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001187 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001188 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001189 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001190 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001191 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001192 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001193 if (N1C && N1C->getAPIntValue().isPowerOf2())
Chris Lattner3e6099b2005-10-30 06:41:49 +00001194 return DAG.getNode(ISD::SHL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001195 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001196 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001197 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
1198 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
1199 // FIXME: If the input is something that is easily negated (e.g. a
1200 // single-use add), we should put the negate there.
1201 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1202 DAG.getNode(ISD::SHL, VT, N0,
1203 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
1204 TLI.getShiftAmountTy())));
1205 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001206
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001207 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1208 if (N1C && N0.getOpcode() == ISD::SHL &&
1209 isa<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001210 SDValue C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001211 AddToWorkList(C3.getNode());
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001212 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1213 }
1214
1215 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1216 // use.
1217 {
Dan Gohman475871a2008-07-27 21:46:04 +00001218 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001219 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1220 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001221 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001222 Sh = N0; Y = N1;
1223 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001224 isa<ConstantSDNode>(N1.getOperand(1)) && N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001225 Sh = N1; Y = N0;
1226 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001227 if (Sh.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001228 SDValue Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001229 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1230 }
1231 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001232 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Gabor Greifba36cb52008-08-28 21:40:38 +00001233 if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
Chris Lattnera1deca32006-03-04 23:33:26 +00001234 isa<ConstantSDNode>(N0.getOperand(1))) {
1235 return DAG.getNode(ISD::ADD, VT,
1236 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1237 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1238 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001239
Nate Begemancd4d58c2006-02-03 06:46:56 +00001240 // reassociate mul
Dan Gohman475871a2008-07-27 21:46:04 +00001241 SDValue RMUL = ReassociateOps(ISD::MUL, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001242 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001243 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001244
Dan Gohman475871a2008-07-27 21:46:04 +00001245 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001246}
1247
Dan Gohman475871a2008-07-27 21:46:04 +00001248SDValue DAGCombiner::visitSDIV(SDNode *N) {
1249 SDValue N0 = N->getOperand(0);
1250 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001251 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1252 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001253 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001254
Dan Gohman7f321562007-06-25 16:23:39 +00001255 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001256 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001257 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001258 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001259 }
Dan Gohman7f321562007-06-25 16:23:39 +00001260
Nate Begeman1d4d4142005-09-01 00:19:25 +00001261 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001262 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001263 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001264 // fold (sdiv X, 1) -> X
1265 if (N1C && N1C->getSignExtended() == 1LL)
1266 return N0;
1267 // fold (sdiv X, -1) -> 0-X
1268 if (N1C && N1C->isAllOnesValue())
1269 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001270 // If we know the sign bits of both operands are zero, strength reduce to a
1271 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001272 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001273 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattnerf32aac32008-01-27 23:32:17 +00001274 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
1275 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001276 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman002e5d02008-03-13 22:13:53 +00001277 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001278 (isPowerOf2_64(N1C->getSignExtended()) ||
1279 isPowerOf2_64(-N1C->getSignExtended()))) {
1280 // If dividing by powers of two is cheap, then don't perform the following
1281 // fold.
1282 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001283 return SDValue();
Nate Begeman405e3ec2005-10-21 00:02:42 +00001284 int64_t pow2 = N1C->getSignExtended();
1285 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001286 unsigned lg2 = Log2_64(abs2);
1287 // Splat the sign bit into the register
Dan Gohman475871a2008-07-27 21:46:04 +00001288 SDValue SGN = DAG.getNode(ISD::SRA, VT, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001289 DAG.getConstant(VT.getSizeInBits()-1,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001290 TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00001291 AddToWorkList(SGN.getNode());
Chris Lattner8f4880b2006-02-16 08:02:36 +00001292 // Add (N0 < 0) ? abs2 - 1 : 0;
Dan Gohman475871a2008-07-27 21:46:04 +00001293 SDValue SRL = DAG.getNode(ISD::SRL, VT, SGN,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001294 DAG.getConstant(VT.getSizeInBits()-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001295 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00001296 SDValue ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001297 AddToWorkList(SRL.getNode());
1298 AddToWorkList(ADD.getNode()); // Divide by pow2
Dan Gohman475871a2008-07-27 21:46:04 +00001299 SDValue SRA = DAG.getNode(ISD::SRA, VT, ADD,
Chris Lattner8f4880b2006-02-16 08:02:36 +00001300 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001301 // If we're dividing by a positive value, we're done. Otherwise, we must
1302 // negate the result.
1303 if (pow2 > 0)
1304 return SRA;
Gabor Greifba36cb52008-08-28 21:40:38 +00001305 AddToWorkList(SRA.getNode());
Nate Begeman405e3ec2005-10-21 00:02:42 +00001306 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1307 }
Nate Begeman69575232005-10-20 02:15:44 +00001308 // if integer divide is expensive and we satisfy the requirements, emit an
1309 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001310 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001311 !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001312 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001313 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001314 }
Dan Gohman7f321562007-06-25 16:23:39 +00001315
Dan Gohman613e0d82007-07-03 14:03:57 +00001316 // undef / X -> 0
1317 if (N0.getOpcode() == ISD::UNDEF)
1318 return DAG.getConstant(0, VT);
1319 // X / undef -> undef
1320 if (N1.getOpcode() == ISD::UNDEF)
1321 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001322
Dan Gohman475871a2008-07-27 21:46:04 +00001323 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001324}
1325
Dan Gohman475871a2008-07-27 21:46:04 +00001326SDValue DAGCombiner::visitUDIV(SDNode *N) {
1327 SDValue N0 = N->getOperand(0);
1328 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001329 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1330 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001331 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001332
Dan Gohman7f321562007-06-25 16:23:39 +00001333 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001334 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001335 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001336 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001337 }
Dan Gohman7f321562007-06-25 16:23:39 +00001338
Nate Begeman1d4d4142005-09-01 00:19:25 +00001339 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001340 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001341 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001342 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001343 if (N1C && N1C->getAPIntValue().isPowerOf2())
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001344 return DAG.getNode(ISD::SRL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001345 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001346 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001347 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1348 if (N1.getOpcode() == ISD::SHL) {
1349 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001350 if (SHC->getAPIntValue().isPowerOf2()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001351 MVT ADDVT = N1.getOperand(1).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001352 SDValue Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001353 DAG.getConstant(SHC->getAPIntValue()
1354 .logBase2(),
Nate Begemanc031e332006-02-05 07:36:48 +00001355 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001356 AddToWorkList(Add.getNode());
Nate Begemanc031e332006-02-05 07:36:48 +00001357 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001358 }
1359 }
1360 }
Nate Begeman69575232005-10-20 02:15:44 +00001361 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001362 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001363 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001364 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00001365 }
Dan Gohman7f321562007-06-25 16:23:39 +00001366
Dan Gohman613e0d82007-07-03 14:03:57 +00001367 // undef / X -> 0
1368 if (N0.getOpcode() == ISD::UNDEF)
1369 return DAG.getConstant(0, VT);
1370 // X / undef -> undef
1371 if (N1.getOpcode() == ISD::UNDEF)
1372 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001373
Dan Gohman475871a2008-07-27 21:46:04 +00001374 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001375}
1376
Dan Gohman475871a2008-07-27 21:46:04 +00001377SDValue DAGCombiner::visitSREM(SDNode *N) {
1378 SDValue N0 = N->getOperand(0);
1379 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001380 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1381 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001382 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001383
1384 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001385 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001386 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001387 // If we know the sign bits of both operands are zero, strength reduce to a
1388 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00001389 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001390 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattneree339f42008-01-27 23:21:58 +00001391 return DAG.getNode(ISD::UREM, VT, N0, N1);
1392 }
Chris Lattner26d29902006-10-12 20:58:32 +00001393
Dan Gohman77003042007-11-26 23:46:11 +00001394 // If X/C can be simplified by the division-by-constant logic, lower
1395 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001396 if (N1C && !N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001397 SDValue Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001398 AddToWorkList(Div.getNode());
1399 SDValue OptimizedDiv = combine(Div.getNode());
1400 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001401 SDValue Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1402 SDValue Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001403 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001404 return Sub;
1405 }
Chris Lattner26d29902006-10-12 20:58:32 +00001406 }
1407
Dan Gohman613e0d82007-07-03 14:03:57 +00001408 // undef % X -> 0
1409 if (N0.getOpcode() == ISD::UNDEF)
1410 return DAG.getConstant(0, VT);
1411 // X % undef -> undef
1412 if (N1.getOpcode() == ISD::UNDEF)
1413 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001414
Dan Gohman475871a2008-07-27 21:46:04 +00001415 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001416}
1417
Dan Gohman475871a2008-07-27 21:46:04 +00001418SDValue DAGCombiner::visitUREM(SDNode *N) {
1419 SDValue N0 = N->getOperand(0);
1420 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001421 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1422 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001423 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001424
1425 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001426 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001427 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001428 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001429 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
1430 return DAG.getNode(ISD::AND, VT, N0,
1431 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001432 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1433 if (N1.getOpcode() == ISD::SHL) {
1434 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001435 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001436 SDValue Add =
Dan Gohman002e5d02008-03-13 22:13:53 +00001437 DAG.getNode(ISD::ADD, VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001438 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00001439 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001440 AddToWorkList(Add.getNode());
Nate Begemanc031e332006-02-05 07:36:48 +00001441 return DAG.getNode(ISD::AND, VT, N0, Add);
1442 }
1443 }
1444 }
Chris Lattner26d29902006-10-12 20:58:32 +00001445
Dan Gohman77003042007-11-26 23:46:11 +00001446 // If X/C can be simplified by the division-by-constant logic, lower
1447 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001448 if (N1C && !N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001449 SDValue Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001450 SDValue OptimizedDiv = combine(Div.getNode());
1451 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001452 SDValue Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1453 SDValue Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001454 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001455 return Sub;
1456 }
Chris Lattner26d29902006-10-12 20:58:32 +00001457 }
1458
Dan Gohman613e0d82007-07-03 14:03:57 +00001459 // undef % X -> 0
1460 if (N0.getOpcode() == ISD::UNDEF)
1461 return DAG.getConstant(0, VT);
1462 // X % undef -> undef
1463 if (N1.getOpcode() == ISD::UNDEF)
1464 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001465
Dan Gohman475871a2008-07-27 21:46:04 +00001466 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001467}
1468
Dan Gohman475871a2008-07-27 21:46:04 +00001469SDValue DAGCombiner::visitMULHS(SDNode *N) {
1470 SDValue N0 = N->getOperand(0);
1471 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001472 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001473 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001474
1475 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001476 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001477 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001478 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001479 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001480 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001481 DAG.getConstant(N0.getValueType().getSizeInBits()-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001482 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001483 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001484 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001485 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001486
Dan Gohman475871a2008-07-27 21:46:04 +00001487 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001488}
1489
Dan Gohman475871a2008-07-27 21:46:04 +00001490SDValue DAGCombiner::visitMULHU(SDNode *N) {
1491 SDValue N0 = N->getOperand(0);
1492 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001493 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001494 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001495
1496 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001497 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001498 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001499 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00001500 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001501 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001502 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001503 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001504 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001505
Dan Gohman475871a2008-07-27 21:46:04 +00001506 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001507}
1508
Dan Gohman389079b2007-10-08 17:57:15 +00001509/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1510/// compute two values. LoOp and HiOp give the opcodes for the two computations
1511/// that are being performed. Return true if a simplification was made.
1512///
Dan Gohman475871a2008-07-27 21:46:04 +00001513SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1514 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001515 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001516 bool HiExists = N->hasAnyUseOfValue(1);
1517 if (!HiExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001518 (!AfterLegalize ||
1519 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001520 SDValue Res = DAG.getNode(LoOp, N->getValueType(0), N->op_begin(),
Chris Lattner5eee4272008-01-26 01:09:19 +00001521 N->getNumOperands());
1522 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001523 }
1524
1525 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001526 bool LoExists = N->hasAnyUseOfValue(0);
1527 if (!LoExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001528 (!AfterLegalize ||
1529 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001530 SDValue Res = DAG.getNode(HiOp, N->getValueType(1), N->op_begin(),
Chris Lattner5eee4272008-01-26 01:09:19 +00001531 N->getNumOperands());
1532 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001533 }
1534
Evan Cheng44711942007-11-08 09:25:29 +00001535 // If both halves are used, return as it is.
1536 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00001537 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00001538
1539 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001540 if (LoExists) {
Dan Gohman475871a2008-07-27 21:46:04 +00001541 SDValue Lo = DAG.getNode(LoOp, N->getValueType(0),
Evan Cheng44711942007-11-08 09:25:29 +00001542 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001543 AddToWorkList(Lo.getNode());
1544 SDValue LoOpt = combine(Lo.getNode());
1545 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001546 (!AfterLegalize ||
1547 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001548 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001549 }
1550
Evan Cheng44711942007-11-08 09:25:29 +00001551 if (HiExists) {
Dan Gohman475871a2008-07-27 21:46:04 +00001552 SDValue Hi = DAG.getNode(HiOp, N->getValueType(1),
Evan Cheng44711942007-11-08 09:25:29 +00001553 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001554 AddToWorkList(Hi.getNode());
1555 SDValue HiOpt = combine(Hi.getNode());
1556 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001557 (!AfterLegalize ||
1558 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001559 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001560 }
Dan Gohman475871a2008-07-27 21:46:04 +00001561 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001562}
1563
Dan Gohman475871a2008-07-27 21:46:04 +00001564SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
1565 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00001566 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001567
Dan Gohman475871a2008-07-27 21:46:04 +00001568 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001569}
1570
Dan Gohman475871a2008-07-27 21:46:04 +00001571SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
1572 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00001573 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001574
Dan Gohman475871a2008-07-27 21:46:04 +00001575 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001576}
1577
Dan Gohman475871a2008-07-27 21:46:04 +00001578SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
1579 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001580 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001581
Dan Gohman475871a2008-07-27 21:46:04 +00001582 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001583}
1584
Dan Gohman475871a2008-07-27 21:46:04 +00001585SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
1586 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001587 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001588
Dan Gohman475871a2008-07-27 21:46:04 +00001589 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001590}
1591
Chris Lattner35e5c142006-05-05 05:51:50 +00001592/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1593/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00001594SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1595 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001596 MVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00001597 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1598
Chris Lattner540121f2006-05-05 06:31:05 +00001599 // For each of OP in AND/OR/XOR:
1600 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1601 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1602 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001603 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001604 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001605 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001606 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001607 SDValue ORNode = DAG.getNode(N->getOpcode(),
Chris Lattner35e5c142006-05-05 05:51:50 +00001608 N0.getOperand(0).getValueType(),
1609 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001610 AddToWorkList(ORNode.getNode());
Chris Lattner540121f2006-05-05 06:31:05 +00001611 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001612 }
1613
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001614 // For each of OP in SHL/SRL/SRA/AND...
1615 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1616 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1617 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001618 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001619 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001620 N0.getOperand(1) == N1.getOperand(1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001621 SDValue ORNode = DAG.getNode(N->getOpcode(),
Chris Lattner35e5c142006-05-05 05:51:50 +00001622 N0.getOperand(0).getValueType(),
1623 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001624 AddToWorkList(ORNode.getNode());
Chris Lattner35e5c142006-05-05 05:51:50 +00001625 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1626 }
1627
Dan Gohman475871a2008-07-27 21:46:04 +00001628 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00001629}
1630
Dan Gohman475871a2008-07-27 21:46:04 +00001631SDValue DAGCombiner::visitAND(SDNode *N) {
1632 SDValue N0 = N->getOperand(0);
1633 SDValue N1 = N->getOperand(1);
1634 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001635 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1636 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001637 MVT VT = N1.getValueType();
1638 unsigned BitWidth = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001639
Dan Gohman7f321562007-06-25 16:23:39 +00001640 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001641 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001642 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001643 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001644 }
Dan Gohman7f321562007-06-25 16:23:39 +00001645
Dan Gohman613e0d82007-07-03 14:03:57 +00001646 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001647 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001648 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001649 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001650 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001651 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001652 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001653 if (N0C && !N1C)
1654 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001655 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001656 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001657 return N0;
1658 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00001659 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001660 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001661 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001662 // reassociate and
Dan Gohman475871a2008-07-27 21:46:04 +00001663 SDValue RAND = ReassociateOps(ISD::AND, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001664 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001665 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001666 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001667 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001668 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001669 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001670 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001671 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1672 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00001673 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001674 APInt Mask = ~N1C->getAPIntValue();
1675 Mask.trunc(N0Op0.getValueSizeInBits());
1676 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001677 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001678 N0Op0);
Chris Lattner1ec05d12006-03-01 21:47:21 +00001679
1680 // Replace uses of the AND with uses of the Zero extend node.
1681 CombineTo(N, Zext);
1682
Chris Lattner3603cd62006-02-02 07:17:31 +00001683 // We actually want to replace all uses of the any_extend with the
1684 // zero_extend, to avoid duplicating things. This will later cause this
1685 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00001686 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00001687 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001688 }
1689 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001690 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1691 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1692 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1693 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1694
1695 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001696 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001697 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001698 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00001699 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001700 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001701 return DAG.getSetCC(VT, ORNode, LR, Op1);
1702 }
1703 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1704 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00001705 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001706 AddToWorkList(ANDNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001707 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1708 }
1709 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1710 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00001711 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001712 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001713 return DAG.getSetCC(VT, ORNode, LR, Op1);
1714 }
1715 }
1716 // canonicalize equivalent to ll == rl
1717 if (LL == RR && LR == RL) {
1718 Op1 = ISD::getSetCCSwappedOperands(Op1);
1719 std::swap(RL, RR);
1720 }
1721 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001722 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001723 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1724 if (Result != ISD::SETCC_INVALID)
1725 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1726 }
1727 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001728
1729 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1730 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001731 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001732 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001733 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001734
Nate Begemande996292006-02-03 22:24:05 +00001735 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1736 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001737 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00001738 SimplifyDemandedBits(SDValue(N, 0)))
1739 return SDValue(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001740 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00001741 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00001742 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001743 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001744 // If we zero all the possible extended bits, then we can turn this into
1745 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001746 unsigned BitWidth = N1.getValueSizeInBits();
1747 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001748 BitWidth - EVT.getSizeInBits())) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001749 ((!AfterLegalize && !LN0->isVolatile()) ||
1750 TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001751 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00001752 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001753 LN0->getSrcValueOffset(), EVT,
1754 LN0->isVolatile(),
1755 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001756 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001757 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001758 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001759 }
1760 }
1761 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00001762 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00001763 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001764 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001765 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001766 // If we zero all the possible extended bits, then we can turn this into
1767 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001768 unsigned BitWidth = N1.getValueSizeInBits();
1769 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001770 BitWidth - EVT.getSizeInBits())) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001771 ((!AfterLegalize && !LN0->isVolatile()) ||
1772 TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001773 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00001774 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001775 LN0->getSrcValueOffset(), EVT,
1776 LN0->isVolatile(),
1777 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001778 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001779 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001780 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001781 }
1782 }
Chris Lattner15045b62006-02-28 06:35:35 +00001783
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001784 // fold (and (load x), 255) -> (zextload x, i8)
1785 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001786 if (N1C && N0.getOpcode() == ISD::LOAD) {
1787 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1788 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001789 LN0->isUnindexed() && N0.hasOneUse() &&
1790 // Do not change the width of a volatile load.
1791 !LN0->isVolatile()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00001792 MVT EVT = MVT::Other;
1793 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
1794 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue()))
1795 EVT = MVT::getIntegerVT(ActiveBits);
1796
1797 MVT LoadedVT = LN0->getMemoryVT();
Duncan Sandsad205a72008-06-16 08:14:38 +00001798 // Do not generate loads of non-round integer types since these can
1799 // be expensive (and would be wrong if the type is not byte sized).
1800 if (EVT != MVT::Other && LoadedVT.bitsGT(EVT) && EVT.isRound() &&
Evan Cheng466685d2006-10-09 20:57:25 +00001801 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001802 MVT PtrType = N0.getOperand(1).getValueType();
Evan Cheng466685d2006-10-09 20:57:25 +00001803 // For big endian targets, we need to add an offset to the pointer to
1804 // load the correct bytes. For little endian systems, we merely need to
1805 // read fewer bytes from the same pointer.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001806 unsigned LVTStoreBytes = LoadedVT.getStoreSizeInBits()/8;
1807 unsigned EVTStoreBytes = EVT.getStoreSizeInBits()/8;
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001808 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001809 unsigned Alignment = LN0->getAlignment();
Dan Gohman475871a2008-07-27 21:46:04 +00001810 SDValue NewPtr = LN0->getBasePtr();
Duncan Sands0753fc12008-02-11 10:37:04 +00001811 if (TLI.isBigEndian()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001812 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1813 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001814 Alignment = MinAlign(Alignment, PtrOff);
1815 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001816 AddToWorkList(NewPtr.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00001817 SDValue Load =
Evan Cheng466685d2006-10-09 20:57:25 +00001818 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001819 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00001820 LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001821 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001822 CombineTo(N0.getNode(), Load, Load.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001823 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng466685d2006-10-09 20:57:25 +00001824 }
Chris Lattner15045b62006-02-28 06:35:35 +00001825 }
1826 }
1827
Dan Gohman475871a2008-07-27 21:46:04 +00001828 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001829}
1830
Dan Gohman475871a2008-07-27 21:46:04 +00001831SDValue DAGCombiner::visitOR(SDNode *N) {
1832 SDValue N0 = N->getOperand(0);
1833 SDValue N1 = N->getOperand(1);
1834 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001835 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1836 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001837 MVT VT = N1.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001838
Dan Gohman7f321562007-06-25 16:23:39 +00001839 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001840 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001841 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001842 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001843 }
Dan Gohman7f321562007-06-25 16:23:39 +00001844
Dan Gohman613e0d82007-07-03 14:03:57 +00001845 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001846 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001847 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001848 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001849 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001850 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001851 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001852 if (N0C && !N1C)
1853 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001854 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001855 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001856 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001857 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001858 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001859 return N1;
1860 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001861 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001862 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001863 // reassociate or
Dan Gohman475871a2008-07-27 21:46:04 +00001864 SDValue ROR = ReassociateOps(ISD::OR, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001865 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001866 return ROR;
1867 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Gabor Greifba36cb52008-08-28 21:40:38 +00001868 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001869 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001870 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1871 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1872 N1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001873 DAG.getConstant(N1C->getAPIntValue() |
1874 C1->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001875 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001876 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1877 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1878 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1879 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1880
1881 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001882 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001883 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1884 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001885 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001886 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001887 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001888 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001889 return DAG.getSetCC(VT, ORNode, LR, Op1);
1890 }
1891 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1892 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1893 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1894 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001895 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001896 AddToWorkList(ANDNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001897 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1898 }
1899 }
1900 // canonicalize equivalent to ll == rl
1901 if (LL == RR && LR == RL) {
1902 Op1 = ISD::getSetCCSwappedOperands(Op1);
1903 std::swap(RL, RR);
1904 }
1905 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001906 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001907 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1908 if (Result != ISD::SETCC_INVALID)
1909 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1910 }
1911 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001912
1913 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1914 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001915 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001916 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001917 }
Chris Lattner516b9622006-09-14 20:50:57 +00001918
Chris Lattner1ec72732006-09-14 21:11:37 +00001919 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1920 if (N0.getOpcode() == ISD::AND &&
1921 N1.getOpcode() == ISD::AND &&
1922 N0.getOperand(1).getOpcode() == ISD::Constant &&
1923 N1.getOperand(1).getOpcode() == ISD::Constant &&
1924 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00001925 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00001926 // We can only do this xform if we know that bits from X that are set in C2
1927 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001928 const APInt &LHSMask =
1929 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1930 const APInt &RHSMask =
1931 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Chris Lattner1ec72732006-09-14 21:11:37 +00001932
Dan Gohmanea859be2007-06-22 14:59:07 +00001933 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1934 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001935 SDValue X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
Chris Lattner1ec72732006-09-14 21:11:37 +00001936 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1937 }
1938 }
1939
1940
Chris Lattner516b9622006-09-14 20:50:57 +00001941 // See if this is some rotate idiom.
1942 if (SDNode *Rot = MatchRotate(N0, N1))
Dan Gohman475871a2008-07-27 21:46:04 +00001943 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001944
Dan Gohman475871a2008-07-27 21:46:04 +00001945 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001946}
1947
Chris Lattner516b9622006-09-14 20:50:57 +00001948
1949/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00001950static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00001951 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001952 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001953 Mask = Op.getOperand(1);
1954 Op = Op.getOperand(0);
1955 } else {
1956 return false;
1957 }
1958 }
1959
1960 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1961 Shift = Op;
1962 return true;
1963 }
1964 return false;
1965}
1966
1967
1968// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1969// idioms for rotate, and if the target supports rotation instructions, generate
1970// a rot[lr].
Dan Gohman475871a2008-07-27 21:46:04 +00001971SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001972 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001973 MVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00001974 if (!TLI.isTypeLegal(VT)) return 0;
1975
1976 // The target must have at least one rotate flavor.
1977 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1978 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1979 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001980
Chris Lattner516b9622006-09-14 20:50:57 +00001981 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00001982 SDValue LHSShift; // The shift.
1983 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00001984 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1985 return 0; // Not part of a rotate.
1986
Dan Gohman475871a2008-07-27 21:46:04 +00001987 SDValue RHSShift; // The shift.
1988 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00001989 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1990 return 0; // Not part of a rotate.
1991
1992 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1993 return 0; // Not shifting the same value.
1994
1995 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1996 return 0; // Shifts must disagree.
1997
1998 // Canonicalize shl to left side in a shl/srl pair.
1999 if (RHSShift.getOpcode() == ISD::SHL) {
2000 std::swap(LHS, RHS);
2001 std::swap(LHSShift, RHSShift);
2002 std::swap(LHSMask , RHSMask );
2003 }
2004
Duncan Sands83ec4b62008-06-06 12:08:01 +00002005 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00002006 SDValue LHSShiftArg = LHSShift.getOperand(0);
2007 SDValue LHSShiftAmt = LHSShift.getOperand(1);
2008 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00002009
2010 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
2011 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00002012 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
2013 RHSShiftAmt.getOpcode() == ISD::Constant) {
2014 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getValue();
2015 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getValue();
Chris Lattner516b9622006-09-14 20:50:57 +00002016 if ((LShVal + RShVal) != OpSizeInBits)
2017 return 0;
2018
Dan Gohman475871a2008-07-27 21:46:04 +00002019 SDValue Rot;
Chris Lattner516b9622006-09-14 20:50:57 +00002020 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002021 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002022 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002023 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002024
2025 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00002026 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002027 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Chris Lattner516b9622006-09-14 20:50:57 +00002028
Gabor Greifba36cb52008-08-28 21:40:38 +00002029 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002030 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2031 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002032 }
Gabor Greifba36cb52008-08-28 21:40:38 +00002033 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002034 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2035 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002036 }
2037
2038 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
2039 }
2040
Gabor Greifba36cb52008-08-28 21:40:38 +00002041 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002042 }
2043
2044 // If there is a mask here, and we have a variable shift, we can't be sure
2045 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00002046 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00002047 return 0;
2048
2049 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2050 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002051 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2052 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002053 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002054 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002055 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002056 if (HasROTL)
Gabor Greifba36cb52008-08-28 21:40:38 +00002057 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002058 else
Gabor Greifba36cb52008-08-28 21:40:38 +00002059 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002060 }
Chris Lattner516b9622006-09-14 20:50:57 +00002061 }
2062 }
2063
2064 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2065 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002066 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2067 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002068 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002069 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002070 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002071 if (HasROTL)
Gabor Greifba36cb52008-08-28 21:40:38 +00002072 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002073 else
Gabor Greifba36cb52008-08-28 21:40:38 +00002074 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002075 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002076 }
2077 }
2078
2079 // Look for sign/zext/any-extended cases:
2080 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2081 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2082 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
2083 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2084 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2085 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002086 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
2087 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +00002088 if (RExtOp0.getOpcode() == ISD::SUB &&
2089 RExtOp0.getOperand(1) == LExtOp0) {
2090 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2091 // (rotr x, y)
2092 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2093 // (rotl x, (sub 32, y))
2094 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002095 if (SUBC->getAPIntValue() == OpSizeInBits) {
Scott Michelc9dc1142007-04-02 21:36:32 +00002096 if (HasROTL)
Gabor Greifba36cb52008-08-28 21:40:38 +00002097 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002098 else
Gabor Greifba36cb52008-08-28 21:40:38 +00002099 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002100 }
2101 }
2102 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2103 RExtOp0 == LExtOp0.getOperand(1)) {
2104 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2105 // (rotl x, y)
2106 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2107 // (rotr x, (sub 32, y))
2108 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002109 if (SUBC->getAPIntValue() == OpSizeInBits) {
Scott Michelc9dc1142007-04-02 21:36:32 +00002110 if (HasROTL)
Gabor Greifba36cb52008-08-28 21:40:38 +00002111 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002112 else
Gabor Greifba36cb52008-08-28 21:40:38 +00002113 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002114 }
2115 }
Chris Lattner516b9622006-09-14 20:50:57 +00002116 }
2117 }
2118
2119 return 0;
2120}
2121
2122
Dan Gohman475871a2008-07-27 21:46:04 +00002123SDValue DAGCombiner::visitXOR(SDNode *N) {
2124 SDValue N0 = N->getOperand(0);
2125 SDValue N1 = N->getOperand(1);
2126 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00002127 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2128 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002129 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002130
Dan Gohman7f321562007-06-25 16:23:39 +00002131 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002132 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002133 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002134 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002135 }
Dan Gohman7f321562007-06-25 16:23:39 +00002136
Evan Cheng26471c42008-03-25 20:08:07 +00002137 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
2138 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
2139 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00002140 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002141 if (N0.getOpcode() == ISD::UNDEF)
2142 return N0;
2143 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002144 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002145 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002146 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002147 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00002148 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002149 if (N0C && !N1C)
2150 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002151 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002152 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002153 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002154 // reassociate xor
Dan Gohman475871a2008-07-27 21:46:04 +00002155 SDValue RXOR = ReassociateOps(ISD::XOR, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002156 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002157 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002158 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00002159 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002160 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00002161 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2162 isInt);
2163 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002164 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002165 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002166 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002167 assert(0 && "Unhandled SetCC Equivalent!");
2168 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002169 }
Chris Lattner61c5ff42007-09-10 21:39:07 +00002170 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002171 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greifba36cb52008-08-28 21:40:38 +00002172 N0.getNode()->hasOneUse() && isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00002173 SDValue V = N0.getOperand(0);
Chris Lattner61c5ff42007-09-10 21:39:07 +00002174 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002175 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002176 AddToWorkList(V.getNode());
Chris Lattner61c5ff42007-09-10 21:39:07 +00002177 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2178 }
2179
Nate Begeman99801192005-09-07 23:25:52 +00002180 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Dan Gohman002e5d02008-03-13 22:13:53 +00002181 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002182 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002183 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002184 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2185 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002186 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2187 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002188 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Nate Begeman99801192005-09-07 23:25:52 +00002189 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002190 }
2191 }
Nate Begeman99801192005-09-07 23:25:52 +00002192 // fold !(x or y) -> (!x and !y) iff x or y are constants
2193 if (N1C && N1C->isAllOnesValue() &&
2194 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002195 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002196 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2197 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002198 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2199 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002200 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Nate Begeman99801192005-09-07 23:25:52 +00002201 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002202 }
2203 }
Nate Begeman223df222005-09-08 20:18:10 +00002204 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2205 if (N1C && N0.getOpcode() == ISD::XOR) {
2206 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2207 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2208 if (N00C)
2209 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00002210 DAG.getConstant(N1C->getAPIntValue()^
2211 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002212 if (N01C)
2213 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
Dan Gohman002e5d02008-03-13 22:13:53 +00002214 DAG.getConstant(N1C->getAPIntValue()^
2215 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002216 }
2217 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002218 if (N0 == N1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002219 if (!VT.isVector()) {
Chris Lattner4fbdd592006-03-28 19:11:05 +00002220 return DAG.getConstant(0, VT);
2221 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
2222 // Produce a vector of zeros.
Dan Gohman475871a2008-07-27 21:46:04 +00002223 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
2224 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002225 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002226 }
2227 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002228
2229 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2230 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002231 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002232 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002233 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002234
Chris Lattner3e104b12006-04-08 04:15:24 +00002235 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002236 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002237 SimplifyDemandedBits(SDValue(N, 0)))
2238 return SDValue(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002239
Dan Gohman475871a2008-07-27 21:46:04 +00002240 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002241}
2242
Chris Lattnere70da202007-12-06 07:33:36 +00002243/// visitShiftByConstant - Handle transforms common to the three shifts, when
2244/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00002245SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002246 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00002247 if (!LHS->hasOneUse()) return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002248
2249 // We want to pull some binops through shifts, so that we have (and (shift))
2250 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2251 // thing happens with address calculations, so it's important to canonicalize
2252 // it.
2253 bool HighBitSet = false; // Can we transform this if the high bit is set?
2254
2255 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002256 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002257 case ISD::OR:
2258 case ISD::XOR:
2259 HighBitSet = false; // We can only transform sra if the high bit is clear.
2260 break;
2261 case ISD::AND:
2262 HighBitSet = true; // We can only transform sra if the high bit is set.
2263 break;
2264 case ISD::ADD:
2265 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00002266 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00002267 HighBitSet = false; // We can only transform sra if the high bit is clear.
2268 break;
2269 }
2270
2271 // We require the RHS of the binop to be a constant as well.
2272 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002273 if (!BinOpCst) return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002274
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002275
2276 // FIXME: disable this for unless the input to the binop is a shift by a
2277 // constant. If it is not a shift, it pessimizes some common cases like:
2278 //
2279 //void foo(int *X, int i) { X[i & 1235] = 1; }
2280 //int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00002281 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002282 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2283 BinOpLHSVal->getOpcode() != ISD::SRA &&
2284 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2285 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00002286 return SDValue();
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002287
Duncan Sands83ec4b62008-06-06 12:08:01 +00002288 MVT VT = N->getValueType(0);
Chris Lattnere70da202007-12-06 07:33:36 +00002289
2290 // If this is a signed shift right, and the high bit is modified
2291 // by the logical operation, do not perform the transformation.
2292 // The highBitSet boolean indicates the value of the high bit of
2293 // the constant which would cause it to be modified for this
2294 // operation.
2295 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00002296 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2297 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00002298 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002299 }
2300
2301 // Fold the constants, shifting the binop RHS by the shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00002302 SDValue NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
Chris Lattnere70da202007-12-06 07:33:36 +00002303 LHS->getOperand(1), N->getOperand(1));
2304
2305 // Create the new shift.
Dan Gohman475871a2008-07-27 21:46:04 +00002306 SDValue NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
Chris Lattnere70da202007-12-06 07:33:36 +00002307 N->getOperand(1));
2308
2309 // Create the new binop.
2310 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2311}
2312
2313
Dan Gohman475871a2008-07-27 21:46:04 +00002314SDValue DAGCombiner::visitSHL(SDNode *N) {
2315 SDValue N0 = N->getOperand(0);
2316 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002317 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2318 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002319 MVT VT = N0.getValueType();
2320 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002321
2322 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002323 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002324 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002325 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002326 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002327 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002328 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002329 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002330 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002331 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002332 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002333 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002334 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002335 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002336 APInt::getAllOnesValue(VT.getSizeInBits())))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002337 return DAG.getConstant(0, VT);
Dan Gohman475871a2008-07-27 21:46:04 +00002338 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2339 return SDValue(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002340 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002341 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002342 N0.getOperand(1).getOpcode() == ISD::Constant) {
2343 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002344 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002345 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002346 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002347 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002348 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002349 }
2350 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2351 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002352 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002353 N0.getOperand(1).getOpcode() == ISD::Constant) {
2354 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002355 uint64_t c2 = N1C->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00002356 SDValue Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman1d4d4142005-09-01 00:19:25 +00002357 DAG.getConstant(~0ULL << c1, VT));
2358 if (c2 > c1)
2359 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002360 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002361 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002362 return DAG.getNode(ISD::SRL, VT, Mask,
2363 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002364 }
2365 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002366 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002367 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002368 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnere70da202007-12-06 07:33:36 +00002369
Dan Gohman475871a2008-07-27 21:46:04 +00002370 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002371}
2372
Dan Gohman475871a2008-07-27 21:46:04 +00002373SDValue DAGCombiner::visitSRA(SDNode *N) {
2374 SDValue N0 = N->getOperand(0);
2375 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002376 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2377 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002378 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002379
2380 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002381 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002382 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002383 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002384 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002385 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002386 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002387 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002388 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002389 // fold (sra x, c >= size(x)) -> undef
Duncan Sands83ec4b62008-06-06 12:08:01 +00002390 if (N1C && N1C->getValue() >= VT.getSizeInBits())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002391 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002392 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002393 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002394 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002395 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2396 // sext_inreg.
2397 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002398 unsigned LowBits = VT.getSizeInBits() - (unsigned)N1C->getValue();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002399 MVT EVT = MVT::getIntegerVT(LowBits);
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002400 if (EVT.isSimple() && // TODO: remove when apint codegen support lands.
2401 (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT)))
Nate Begemanfb7217b2006-02-17 19:54:08 +00002402 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2403 DAG.getValueType(EVT));
2404 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002405
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002406 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2407 if (N1C && N0.getOpcode() == ISD::SRA) {
2408 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2409 unsigned Sum = N1C->getValue() + C1->getValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002410 if (Sum >= VT.getSizeInBits()) Sum = VT.getSizeInBits()-1;
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002411 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2412 DAG.getConstant(Sum, N1C->getValueType(0)));
2413 }
2414 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00002415
2416 // fold sra (shl X, m), result_size - n
2417 // -> (sign_extend (trunc (shl X, result_size - n - m))) for
Christopher Lambb9b04282008-03-20 04:31:39 +00002418 // result_size - n != m.
2419 // If truncate is free for the target sext(shl) is likely to result in better
2420 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002421 if (N0.getOpcode() == ISD::SHL) {
2422 // Get the two constanst of the shifts, CN0 = m, CN = n.
2423 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2424 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002425 // Determine what the truncate's result bitsize and type would be.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002426 unsigned VTValSize = VT.getSizeInBits();
2427 MVT TruncVT =
2428 MVT::getIntegerVT(VTValSize - N1C->getValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00002429 // Determine the residual right-shift amount.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002430 unsigned ShiftAmt = N1C->getValue() - N01C->getValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002431
Christopher Lambb9b04282008-03-20 04:31:39 +00002432 // If the shift is not a no-op (in which case this should be just a sign
2433 // extend already), the truncated to type is legal, sign_extend is legal
2434 // on that type, and the the truncate to that type is both legal and free,
2435 // perform the transform.
2436 if (ShiftAmt &&
Christopher Lambb9b04282008-03-20 04:31:39 +00002437 TLI.isOperationLegal(ISD::SIGN_EXTEND, TruncVT) &&
2438 TLI.isOperationLegal(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00002439 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002440
Dan Gohman475871a2008-07-27 21:46:04 +00002441 SDValue Amt = DAG.getConstant(ShiftAmt, TLI.getShiftAmountTy());
2442 SDValue Shift = DAG.getNode(ISD::SRL, VT, N0.getOperand(0), Amt);
2443 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, TruncVT, Shift);
Christopher Lambb9b04282008-03-20 04:31:39 +00002444 return DAG.getNode(ISD::SIGN_EXTEND, N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00002445 }
2446 }
2447 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002448
Chris Lattnera8504462006-05-08 20:51:54 +00002449 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00002450 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2451 return SDValue(N, 0);
Chris Lattnera8504462006-05-08 20:51:54 +00002452
2453
Nate Begeman1d4d4142005-09-01 00:19:25 +00002454 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002455 if (DAG.SignBitIsZero(N0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002456 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002457
Dan Gohman475871a2008-07-27 21:46:04 +00002458 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002459}
2460
Dan Gohman475871a2008-07-27 21:46:04 +00002461SDValue DAGCombiner::visitSRL(SDNode *N) {
2462 SDValue N0 = N->getOperand(0);
2463 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002464 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2465 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002466 MVT VT = N0.getValueType();
2467 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002468
2469 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002470 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002471 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002472 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002473 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002474 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002475 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002476 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002477 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002478 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002479 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002480 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002481 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002482 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002483 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002484 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002485
Nate Begeman1d4d4142005-09-01 00:19:25 +00002486 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002487 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002488 N0.getOperand(1).getOpcode() == ISD::Constant) {
2489 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002490 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002491 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002492 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002493 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002494 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002495 }
Chris Lattner350bec02006-04-02 06:11:11 +00002496
Chris Lattner06afe072006-05-05 22:53:17 +00002497 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2498 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2499 // Shifting in all undef bits?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002500 MVT SmallVT = N0.getOperand(0).getValueType();
2501 if (N1C->getValue() >= SmallVT.getSizeInBits())
Chris Lattner06afe072006-05-05 22:53:17 +00002502 return DAG.getNode(ISD::UNDEF, VT);
2503
Dan Gohman475871a2008-07-27 21:46:04 +00002504 SDValue SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002505 AddToWorkList(SmallShift.getNode());
Chris Lattner06afe072006-05-05 22:53:17 +00002506 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2507 }
2508
Chris Lattner3657ffe2006-10-12 20:23:19 +00002509 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2510 // bit, which is unmodified by sra.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002511 if (N1C && N1C->getValue()+1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00002512 if (N0.getOpcode() == ISD::SRA)
2513 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2514 }
2515
Chris Lattner350bec02006-04-02 06:11:11 +00002516 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2517 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002518 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00002519 APInt KnownZero, KnownOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002520 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00002521 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002522
2523 // If any of the input bits are KnownOne, then the input couldn't be all
2524 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002525 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Chris Lattner350bec02006-04-02 06:11:11 +00002526
2527 // If all of the bits input the to ctlz node are known to be zero, then
2528 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002529 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00002530 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2531
2532 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2533 if ((UnknownBits & (UnknownBits-1)) == 0) {
2534 // Okay, we know that only that the single bit specified by UnknownBits
2535 // could be set on input to the CTLZ node. If this bit is set, the SRL
2536 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2537 // to an SRL,XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002538 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00002539 SDValue Op = N0.getOperand(0);
Chris Lattner350bec02006-04-02 06:11:11 +00002540 if (ShAmt) {
2541 Op = DAG.getNode(ISD::SRL, VT, Op,
2542 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002543 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00002544 }
2545 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2546 }
2547 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002548
2549 // fold operands of srl based on knowledge that the low bits are not
2550 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00002551 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2552 return SDValue(N, 0);
Chris Lattner61a4c072007-04-18 03:06:49 +00002553
Dan Gohman475871a2008-07-27 21:46:04 +00002554 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002555}
2556
Dan Gohman475871a2008-07-27 21:46:04 +00002557SDValue DAGCombiner::visitCTLZ(SDNode *N) {
2558 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002559 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002560
2561 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002562 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002563 return DAG.getNode(ISD::CTLZ, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002564 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002565}
2566
Dan Gohman475871a2008-07-27 21:46:04 +00002567SDValue DAGCombiner::visitCTTZ(SDNode *N) {
2568 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002569 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002570
2571 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002572 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002573 return DAG.getNode(ISD::CTTZ, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002574 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002575}
2576
Dan Gohman475871a2008-07-27 21:46:04 +00002577SDValue DAGCombiner::visitCTPOP(SDNode *N) {
2578 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002579 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002580
2581 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002582 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002583 return DAG.getNode(ISD::CTPOP, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002584 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002585}
2586
Dan Gohman475871a2008-07-27 21:46:04 +00002587SDValue DAGCombiner::visitSELECT(SDNode *N) {
2588 SDValue N0 = N->getOperand(0);
2589 SDValue N1 = N->getOperand(1);
2590 SDValue N2 = N->getOperand(2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002591 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2592 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2593 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002594 MVT VT = N->getValueType(0);
2595 MVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002596
Nate Begeman452d7be2005-09-16 00:54:12 +00002597 // fold select C, X, X -> X
2598 if (N1 == N2)
2599 return N1;
2600 // fold select true, X, Y -> X
2601 if (N0C && !N0C->isNullValue())
2602 return N1;
2603 // fold select false, X, Y -> Y
2604 if (N0C && N0C->isNullValue())
2605 return N2;
2606 // fold select C, 1, X -> C | X
Duncan Sands83ec4b62008-06-06 12:08:01 +00002607 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002608 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Cheng571c4782007-08-18 05:57:05 +00002609 // fold select C, 0, 1 -> ~C
Duncan Sands83ec4b62008-06-06 12:08:01 +00002610 if (VT.isInteger() && VT0.isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00002611 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00002612 SDValue XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
Evan Cheng571c4782007-08-18 05:57:05 +00002613 if (VT == VT0)
2614 return XORNode;
Gabor Greifba36cb52008-08-28 21:40:38 +00002615 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00002616 if (VT.bitsGT(VT0))
Evan Cheng571c4782007-08-18 05:57:05 +00002617 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2618 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2619 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002620 // fold select C, 0, X -> ~C & X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002621 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002622 SDValue XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002623 AddToWorkList(XORNode.getNode());
Nate Begeman452d7be2005-09-16 00:54:12 +00002624 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2625 }
2626 // fold select C, X, 1 -> ~C | X
Dan Gohman002e5d02008-03-13 22:13:53 +00002627 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00002628 SDValue XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002629 AddToWorkList(XORNode.getNode());
Nate Begeman452d7be2005-09-16 00:54:12 +00002630 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2631 }
2632 // fold select C, X, 0 -> C & X
2633 // FIXME: this should check for C type == X type, not i1?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002634 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Nate Begeman452d7be2005-09-16 00:54:12 +00002635 return DAG.getNode(ISD::AND, VT, N0, N1);
2636 // fold X ? X : Y --> X ? 1 : Y --> X | Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002637 if (VT == MVT::i1 && N0 == N1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002638 return DAG.getNode(ISD::OR, VT, N0, N2);
2639 // fold X ? Y : X --> X ? Y : 0 --> X & Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002640 if (VT == MVT::i1 && N0 == N2)
Nate Begeman452d7be2005-09-16 00:54:12 +00002641 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002642
Chris Lattner40c62d52005-10-18 06:04:22 +00002643 // If we can fold this based on the true/false value, do so.
2644 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00002645 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002646
Nate Begeman44728a72005-09-19 22:34:01 +00002647 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002648 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002649 // FIXME:
2650 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2651 // having to say they don't support SELECT_CC on every type the DAG knows
2652 // about, since there is no way to mark an opcode illegal at all value types
2653 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2654 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2655 N1, N2, N0.getOperand(2));
2656 else
2657 return SimplifySelect(N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002658 }
Dan Gohman475871a2008-07-27 21:46:04 +00002659 return SDValue();
Nate Begeman452d7be2005-09-16 00:54:12 +00002660}
2661
Dan Gohman475871a2008-07-27 21:46:04 +00002662SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
2663 SDValue N0 = N->getOperand(0);
2664 SDValue N1 = N->getOperand(1);
2665 SDValue N2 = N->getOperand(2);
2666 SDValue N3 = N->getOperand(3);
2667 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002668 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2669
Nate Begeman44728a72005-09-19 22:34:01 +00002670 // fold select_cc lhs, rhs, x, x, cc -> x
2671 if (N2 == N3)
2672 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002673
Chris Lattner5f42a242006-09-20 06:19:26 +00002674 // Determine if the condition we're dealing with is constant
Dan Gohman475871a2008-07-27 21:46:04 +00002675 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00002676 if (SCC.getNode()) AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00002677
Gabor Greifba36cb52008-08-28 21:40:38 +00002678 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002679 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00002680 return N2; // cond always true -> true val
2681 else
2682 return N3; // cond always false -> false val
2683 }
2684
2685 // Fold to a simpler select_cc
Gabor Greifba36cb52008-08-28 21:40:38 +00002686 if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC)
Chris Lattner5f42a242006-09-20 06:19:26 +00002687 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2688 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2689 SCC.getOperand(2));
2690
Chris Lattner40c62d52005-10-18 06:04:22 +00002691 // If we can fold this based on the true/false value, do so.
2692 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00002693 return SDValue(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002694
Nate Begeman44728a72005-09-19 22:34:01 +00002695 // fold select_cc into other things, such as min/max/abs
2696 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002697}
2698
Dan Gohman475871a2008-07-27 21:46:04 +00002699SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002700 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2701 cast<CondCodeSDNode>(N->getOperand(2))->get());
2702}
2703
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002704// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2705// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2706// transformation. Returns true if extension are possible and the above
2707// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00002708static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002709 unsigned ExtOpc,
2710 SmallVector<SDNode*, 4> &ExtendNodes,
2711 TargetLowering &TLI) {
2712 bool HasCopyToRegUses = false;
2713 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greifba36cb52008-08-28 21:40:38 +00002714 for (SDNode::use_iterator UI = N0.getNode()->use_begin(), UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002715 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002716 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002717 if (User == N)
2718 continue;
2719 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2720 if (User->getOpcode() == ISD::SETCC) {
2721 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2722 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2723 // Sign bits will be lost after a zext.
2724 return false;
2725 bool Add = false;
2726 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002727 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002728 if (UseOp == N0)
2729 continue;
2730 if (!isa<ConstantSDNode>(UseOp))
2731 return false;
2732 Add = true;
2733 }
2734 if (Add)
2735 ExtendNodes.push_back(User);
2736 } else {
2737 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002738 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002739 if (UseOp == N0) {
2740 // If truncate from extended type to original load type is free
2741 // on this target, then it's ok to extend a CopyToReg.
2742 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2743 HasCopyToRegUses = true;
2744 else
2745 return false;
2746 }
2747 }
2748 }
2749 }
2750
2751 if (HasCopyToRegUses) {
2752 bool BothLiveOut = false;
2753 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2754 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002755 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002756 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002757 SDValue UseOp = User->getOperand(i);
Gabor Greifba36cb52008-08-28 21:40:38 +00002758 if (UseOp.getNode() == N && UseOp.getResNo() == 0) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002759 BothLiveOut = true;
2760 break;
2761 }
2762 }
2763 }
2764 if (BothLiveOut)
2765 // Both unextended and extended values are live out. There had better be
2766 // good a reason for the transformation.
2767 return ExtendNodes.size();
2768 }
2769 return true;
2770}
2771
Dan Gohman475871a2008-07-27 21:46:04 +00002772SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
2773 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002774 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002775
Nate Begeman1d4d4142005-09-01 00:19:25 +00002776 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002777 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002778 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002779
Nate Begeman1d4d4142005-09-01 00:19:25 +00002780 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002781 // fold (sext (aext x)) -> (sext x)
2782 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002783 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002784
Chris Lattner22558872007-02-26 03:13:59 +00002785 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002786 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2787 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002788 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
2789 if (NarrowLoad.getNode()) {
2790 if (NarrowLoad.getNode() != N0.getNode())
2791 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00002792 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2793 }
Evan Chengc88138f2007-03-22 01:54:19 +00002794
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002795 // See if the value being truncated is already sign extended. If so, just
2796 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00002797 SDValue Op = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002798 unsigned OpBits = Op.getValueType().getSizeInBits();
2799 unsigned MidBits = N0.getValueType().getSizeInBits();
2800 unsigned DestBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002801 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002802
2803 if (OpBits == DestBits) {
2804 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2805 // bits, it is already ready.
2806 if (NumSignBits > DestBits-MidBits)
2807 return Op;
2808 } else if (OpBits < DestBits) {
2809 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2810 // bits, just sext from i32.
2811 if (NumSignBits > OpBits-MidBits)
2812 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2813 } else {
2814 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2815 // bits, just truncate to i32.
2816 if (NumSignBits > OpBits-MidBits)
2817 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002818 }
Chris Lattner22558872007-02-26 03:13:59 +00002819
2820 // fold (sext (truncate x)) -> (sextinreg x).
2821 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2822 N0.getValueType())) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00002823 if (Op.getValueType().bitsLT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002824 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002825 else if (Op.getValueType().bitsGT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002826 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2827 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2828 DAG.getValueType(N0.getValueType()));
2829 }
Chris Lattner6007b842006-09-21 06:00:20 +00002830 }
Chris Lattner310b5782006-05-06 23:06:26 +00002831
Evan Cheng110dec22005-12-14 02:19:23 +00002832 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002833 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002834 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
2835 TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002836 bool DoXform = true;
2837 SmallVector<SDNode*, 4> SetCCs;
2838 if (!N0.hasOneUse())
2839 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
2840 if (DoXform) {
2841 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002842 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002843 LN0->getBasePtr(), LN0->getSrcValue(),
2844 LN0->getSrcValueOffset(),
2845 N0.getValueType(),
2846 LN0->isVolatile(),
2847 LN0->getAlignment());
2848 CombineTo(N, ExtLoad);
Dan Gohman475871a2008-07-27 21:46:04 +00002849 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00002850 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002851 // Extend SetCC uses if necessary.
2852 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2853 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00002854 SmallVector<SDValue, 4> Ops;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002855 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002856 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002857 if (SOp == Trunc)
2858 Ops.push_back(ExtLoad);
2859 else
2860 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
2861 }
2862 Ops.push_back(SetCC->getOperand(2));
2863 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2864 &Ops[0], Ops.size()));
2865 }
Dan Gohman475871a2008-07-27 21:46:04 +00002866 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002867 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002868 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002869
2870 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2871 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002872 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
2873 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002874 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002875 MVT EVT = LN0->getMemoryVT();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002876 if ((!AfterLegalize && !LN0->isVolatile()) ||
2877 TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002878 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002879 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002880 LN0->getSrcValueOffset(), EVT,
2881 LN0->isVolatile(),
2882 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002883 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00002884 CombineTo(N0.getNode(), DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002885 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002886 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002887 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002888 }
2889
Chris Lattner20a35c32007-04-11 05:32:27 +00002890 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2891 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00002892 SDValue SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002893 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2894 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2895 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00002896 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002897 }
2898
Dan Gohman8f0ad582008-04-28 16:58:24 +00002899 // fold (sext x) -> (zext x) if the sign bit is known zero.
Dan Gohman187db7b2008-04-28 18:47:17 +00002900 if ((!AfterLegalize || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
2901 DAG.SignBitIsZero(N0))
Dan Gohman8f0ad582008-04-28 16:58:24 +00002902 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2903
Dan Gohman475871a2008-07-27 21:46:04 +00002904 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002905}
2906
Dan Gohman475871a2008-07-27 21:46:04 +00002907SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
2908 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002909 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002910
Nate Begeman1d4d4142005-09-01 00:19:25 +00002911 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002912 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002913 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002914 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002915 // fold (zext (aext x)) -> (zext x)
2916 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002917 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002918
Evan Chengc88138f2007-03-22 01:54:19 +00002919 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2920 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002921 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002922 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
2923 if (NarrowLoad.getNode()) {
2924 if (NarrowLoad.getNode() != N0.getNode())
2925 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00002926 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2927 }
Evan Chengc88138f2007-03-22 01:54:19 +00002928 }
2929
Chris Lattner6007b842006-09-21 06:00:20 +00002930 // fold (zext (truncate x)) -> (and x, mask)
2931 if (N0.getOpcode() == ISD::TRUNCATE &&
2932 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00002933 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002934 if (Op.getValueType().bitsLT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00002935 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002936 } else if (Op.getValueType().bitsGT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00002937 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2938 }
2939 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2940 }
2941
Chris Lattner111c2282006-09-21 06:14:31 +00002942 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2943 if (N0.getOpcode() == ISD::AND &&
2944 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2945 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman475871a2008-07-27 21:46:04 +00002946 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002947 if (X.getValueType().bitsLT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00002948 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002949 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00002950 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2951 }
Dan Gohman220a8232008-03-03 23:51:38 +00002952 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002953 Mask.zext(VT.getSizeInBits());
Chris Lattner111c2282006-09-21 06:14:31 +00002954 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2955 }
2956
Evan Cheng110dec22005-12-14 02:19:23 +00002957 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002958 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002959 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
2960 TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002961 bool DoXform = true;
2962 SmallVector<SDNode*, 4> SetCCs;
2963 if (!N0.hasOneUse())
2964 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
2965 if (DoXform) {
2966 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002967 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002968 LN0->getBasePtr(), LN0->getSrcValue(),
2969 LN0->getSrcValueOffset(),
2970 N0.getValueType(),
2971 LN0->isVolatile(),
2972 LN0->getAlignment());
2973 CombineTo(N, ExtLoad);
Dan Gohman475871a2008-07-27 21:46:04 +00002974 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00002975 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002976 // Extend SetCC uses if necessary.
2977 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2978 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00002979 SmallVector<SDValue, 4> Ops;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002980 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002981 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002982 if (SOp == Trunc)
2983 Ops.push_back(ExtLoad);
2984 else
Evan Chengde1631b2007-10-30 20:11:21 +00002985 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002986 }
2987 Ops.push_back(SetCC->getOperand(2));
2988 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2989 &Ops[0], Ops.size()));
2990 }
Dan Gohman475871a2008-07-27 21:46:04 +00002991 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002992 }
Evan Cheng110dec22005-12-14 02:19:23 +00002993 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002994
2995 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2996 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002997 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
2998 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002999 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003000 MVT EVT = LN0->getMemoryVT();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003001 if ((!AfterLegalize && !LN0->isVolatile()) ||
3002 TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003003 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003004 LN0->getBasePtr(), LN0->getSrcValue(),
3005 LN0->getSrcValueOffset(), EVT,
3006 LN0->isVolatile(),
3007 LN0->getAlignment());
3008 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003009 CombineTo(N0.getNode(), DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003010 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003011 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003012 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003013 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003014
3015 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3016 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003017 SDValue SCC =
Chris Lattner20a35c32007-04-11 05:32:27 +00003018 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3019 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003020 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003021 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003022 }
3023
Dan Gohman475871a2008-07-27 21:46:04 +00003024 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003025}
3026
Dan Gohman475871a2008-07-27 21:46:04 +00003027SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
3028 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003029 MVT VT = N->getValueType(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00003030
3031 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003032 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00003033 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
3034 // fold (aext (aext x)) -> (aext x)
3035 // fold (aext (zext x)) -> (zext x)
3036 // fold (aext (sext x)) -> (sext x)
3037 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3038 N0.getOpcode() == ISD::ZERO_EXTEND ||
3039 N0.getOpcode() == ISD::SIGN_EXTEND)
3040 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
3041
Evan Chengc88138f2007-03-22 01:54:19 +00003042 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3043 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3044 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003045 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3046 if (NarrowLoad.getNode()) {
3047 if (NarrowLoad.getNode() != N0.getNode())
3048 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003049 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
3050 }
Evan Chengc88138f2007-03-22 01:54:19 +00003051 }
3052
Chris Lattner84750582006-09-20 06:29:17 +00003053 // fold (aext (truncate x))
3054 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00003055 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00003056 if (TruncOp.getValueType() == VT)
3057 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003058 if (TruncOp.getValueType().bitsGT(VT))
Chris Lattner84750582006-09-20 06:29:17 +00003059 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
3060 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
3061 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00003062
3063 // fold (aext (and (trunc x), cst)) -> (and x, cst).
3064 if (N0.getOpcode() == ISD::AND &&
3065 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3066 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman475871a2008-07-27 21:46:04 +00003067 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003068 if (X.getValueType().bitsLT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003069 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003070 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003071 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3072 }
Dan Gohman220a8232008-03-03 23:51:38 +00003073 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003074 Mask.zext(VT.getSizeInBits());
Chris Lattner0e4b9222006-09-21 06:40:43 +00003075 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3076 }
3077
Chris Lattner5ffc0662006-05-05 05:58:59 +00003078 // fold (aext (load x)) -> (aext (truncate (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003079 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003080 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3081 TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003082 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003083 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00003084 LN0->getBasePtr(), LN0->getSrcValue(),
3085 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003086 N0.getValueType(),
3087 LN0->isVolatile(),
3088 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003089 CombineTo(N, ExtLoad);
Dan Gohman75dcf082008-07-31 00:50:31 +00003090 // Redirect any chain users to the new load.
Gabor Greifba36cb52008-08-28 21:40:38 +00003091 DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1), SDValue(ExtLoad.getNode(), 1));
Dan Gohman75dcf082008-07-31 00:50:31 +00003092 // If any node needs the original loaded value, recompute it.
3093 if (!LN0->use_empty())
3094 CombineTo(LN0, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3095 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003096 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003097 }
3098
3099 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3100 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3101 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003102 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003103 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003104 N0.hasOneUse()) {
3105 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003106 MVT EVT = LN0->getMemoryVT();
Dan Gohman475871a2008-07-27 21:46:04 +00003107 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
Evan Cheng466685d2006-10-09 20:57:25 +00003108 LN0->getChain(), LN0->getBasePtr(),
3109 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003110 LN0->getSrcValueOffset(), EVT,
3111 LN0->isVolatile(),
3112 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003113 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003114 CombineTo(N0.getNode(), DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00003115 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003116 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003117 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003118
3119 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3120 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003121 SDValue SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003122 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3123 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003124 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003125 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003126 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003127 }
3128
Dan Gohman475871a2008-07-27 21:46:04 +00003129 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00003130}
3131
Chris Lattner2b4c2792007-10-13 06:35:54 +00003132/// GetDemandedBits - See if the specified operand can be simplified with the
3133/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00003134/// simpler operand, otherwise return a null SDValue.
3135SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003136 switch (V.getOpcode()) {
3137 default: break;
3138 case ISD::OR:
3139 case ISD::XOR:
3140 // If the LHS or RHS don't contribute bits to the or, drop them.
3141 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3142 return V.getOperand(1);
3143 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3144 return V.getOperand(0);
3145 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003146 case ISD::SRL:
3147 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003148 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00003149 break;
3150 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3151 // See if we can recursively simplify the LHS.
3152 unsigned Amt = RHSC->getValue();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003153 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00003154 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Gabor Greifba36cb52008-08-28 21:40:38 +00003155 if (SimplifyLHS.getNode()) {
Chris Lattnere33544c2007-10-13 06:58:48 +00003156 return DAG.getNode(ISD::SRL, V.getValueType(),
3157 SimplifyLHS, V.getOperand(1));
3158 }
3159 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003160 }
Dan Gohman475871a2008-07-27 21:46:04 +00003161 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00003162}
3163
Evan Chengc88138f2007-03-22 01:54:19 +00003164/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3165/// bits and then truncated to a narrower type and where N is a multiple
3166/// of number of bits of the narrower type, transform it to a narrower load
3167/// from address + N / num of bits of new type. If the result is to be
3168/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00003169SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00003170 unsigned Opc = N->getOpcode();
3171 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00003172 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003173 MVT VT = N->getValueType(0);
3174 MVT EVT = N->getValueType(0);
Evan Chengc88138f2007-03-22 01:54:19 +00003175
Dan Gohman7f8613e2008-08-14 20:04:46 +00003176 // This transformation isn't valid for vector loads.
3177 if (VT.isVector())
3178 return SDValue();
3179
Evan Chenge177e302007-03-23 22:13:36 +00003180 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3181 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003182 if (Opc == ISD::SIGN_EXTEND_INREG) {
3183 ExtType = ISD::SEXTLOAD;
3184 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00003185 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
Dan Gohman475871a2008-07-27 21:46:04 +00003186 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003187 }
3188
Duncan Sands83ec4b62008-06-06 12:08:01 +00003189 unsigned EVTBits = EVT.getSizeInBits();
Evan Chengc88138f2007-03-22 01:54:19 +00003190 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00003191 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00003192 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3193 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3194 ShAmt = N01->getValue();
3195 // Is the shift amount a multiple of size of VT?
3196 if ((ShAmt & (EVTBits-1)) == 0) {
3197 N0 = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003198 if (N0.getValueType().getSizeInBits() <= EVTBits)
Dan Gohman475871a2008-07-27 21:46:04 +00003199 return SDValue();
Evan Chengb37b80c2007-03-23 20:55:21 +00003200 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00003201 }
3202 }
3203 }
3204
Duncan Sandsad205a72008-06-16 08:14:38 +00003205 // Do not generate loads of non-round integer types since these can
3206 // be expensive (and would be wrong if the type is not byte sized).
Dan Gohman75dcf082008-07-31 00:50:31 +00003207 if (isa<LoadSDNode>(N0) && N0.hasOneUse() && VT.isRound() &&
3208 cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() > EVTBits &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003209 // Do not change the width of a volatile load.
3210 !cast<LoadSDNode>(N0)->isVolatile()) {
Evan Chengc88138f2007-03-22 01:54:19 +00003211 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003212 MVT PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00003213 // For big endian targets, we need to adjust the offset to the pointer to
3214 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003215 if (TLI.isBigEndian()) {
Dan Gohman75dcf082008-07-31 00:50:31 +00003216 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003217 unsigned EVTStoreBits = EVT.getStoreSizeInBits();
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003218 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3219 }
Evan Chengdae54ce2007-03-24 00:02:43 +00003220 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003221 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Dan Gohman475871a2008-07-27 21:46:04 +00003222 SDValue NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
Evan Chengc88138f2007-03-22 01:54:19 +00003223 DAG.getConstant(PtrOff, PtrType));
Gabor Greifba36cb52008-08-28 21:40:38 +00003224 AddToWorkList(NewPtr.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00003225 SDValue Load = (ExtType == ISD::NON_EXTLOAD)
Evan Chengc88138f2007-03-22 01:54:19 +00003226 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003227 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
Duncan Sandsdc846502007-10-28 12:59:45 +00003228 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003229 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003230 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
3231 EVT, LN0->isVolatile(), NewAlign);
Evan Chengc88138f2007-03-22 01:54:19 +00003232 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00003233 if (CombineSRL) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003234 WorkListRemover DeadNodes(*this);
3235 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3236 &DeadNodes);
Gabor Greifba36cb52008-08-28 21:40:38 +00003237 CombineTo(N->getOperand(0).getNode(), Load);
Evan Chengb37b80c2007-03-23 20:55:21 +00003238 } else
Gabor Greifba36cb52008-08-28 21:40:38 +00003239 CombineTo(N0.getNode(), Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00003240 if (ShAmt) {
3241 if (Opc == ISD::SIGN_EXTEND_INREG)
3242 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
3243 else
3244 return DAG.getNode(Opc, VT, Load);
3245 }
Dan Gohman475871a2008-07-27 21:46:04 +00003246 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chengc88138f2007-03-22 01:54:19 +00003247 }
3248
Dan Gohman475871a2008-07-27 21:46:04 +00003249 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003250}
3251
Chris Lattner5ffc0662006-05-05 05:58:59 +00003252
Dan Gohman475871a2008-07-27 21:46:04 +00003253SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
3254 SDValue N0 = N->getOperand(0);
3255 SDValue N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003256 MVT VT = N->getValueType(0);
3257 MVT EVT = cast<VTSDNode>(N1)->getVT();
3258 unsigned VTBits = VT.getSizeInBits();
3259 unsigned EVTBits = EVT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003260
Nate Begeman1d4d4142005-09-01 00:19:25 +00003261 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003262 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003263 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003264
Chris Lattner541a24f2006-05-06 22:43:44 +00003265 // If the input is already sign extended, just drop the extension.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003266 if (DAG.ComputeNumSignBits(N0) >= VT.getSizeInBits()-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003267 return N0;
3268
Nate Begeman646d7e22005-09-02 21:18:40 +00003269 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3270 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00003271 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003272 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003273 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003274
Dan Gohman75dcf082008-07-31 00:50:31 +00003275 // fold (sext_in_reg (sext x)) -> (sext x)
3276 // fold (sext_in_reg (aext x)) -> (sext x)
3277 // if x is small enough.
3278 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
3279 SDValue N00 = N0.getOperand(0);
3280 if (N00.getValueType().getSizeInBits() < EVTBits)
3281 return DAG.getNode(ISD::SIGN_EXTEND, VT, N00, N1);
3282 }
3283
Chris Lattner95a5e052007-04-17 19:03:21 +00003284 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003285 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Nate Begemande996292006-02-03 22:24:05 +00003286 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003287
Chris Lattner95a5e052007-04-17 19:03:21 +00003288 // fold operands of sext_in_reg based on knowledge that the top bits are not
3289 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00003290 if (SimplifyDemandedBits(SDValue(N, 0)))
3291 return SDValue(N, 0);
Chris Lattner95a5e052007-04-17 19:03:21 +00003292
Evan Chengc88138f2007-03-22 01:54:19 +00003293 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3294 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00003295 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003296 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00003297 return NarrowLoad;
3298
Chris Lattner4b37e872006-05-08 21:18:59 +00003299 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3300 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3301 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3302 if (N0.getOpcode() == ISD::SRL) {
3303 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00003304 if (ShAmt->getValue()+EVTBits <= VT.getSizeInBits()) {
Chris Lattner4b37e872006-05-08 21:18:59 +00003305 // We can turn this into an SRA iff the input to the SRL is already sign
3306 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003307 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00003308 if (VT.getSizeInBits()-(ShAmt->getValue()+EVTBits) < InSignBits)
Chris Lattner4b37e872006-05-08 21:18:59 +00003309 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3310 }
3311 }
Evan Chengc88138f2007-03-22 01:54:19 +00003312
Nate Begemanded49632005-10-13 03:11:28 +00003313 // fold (sext_inreg (extload x)) -> (sextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00003314 if (ISD::isEXTLoad(N0.getNode()) &&
3315 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003316 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003317 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3318 TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003319 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003320 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00003321 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003322 LN0->getSrcValueOffset(), EVT,
3323 LN0->isVolatile(),
3324 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003325 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003326 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003327 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003328 }
3329 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00003330 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003331 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003332 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003333 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3334 TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003335 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003336 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00003337 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003338 LN0->getSrcValueOffset(), EVT,
3339 LN0->isVolatile(),
3340 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003341 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003342 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003343 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003344 }
Dan Gohman475871a2008-07-27 21:46:04 +00003345 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003346}
3347
Dan Gohman475871a2008-07-27 21:46:04 +00003348SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
3349 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003350 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003351
3352 // noop truncate
3353 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003354 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003355 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003356 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003357 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003358 // fold (truncate (truncate x)) -> (truncate x)
3359 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003360 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003361 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003362 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3363 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00003364 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003365 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003366 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00003367 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003368 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003369 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003370 else
3371 // if the source and dest are the same type, we can drop both the extend
3372 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003373 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003374 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003375
Chris Lattner2b4c2792007-10-13 06:35:54 +00003376 // See if we can simplify the input to this truncate through knowledge that
3377 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3378 // -> trunc y
Dan Gohman475871a2008-07-27 21:46:04 +00003379 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003380 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00003381 VT.getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003382 if (Shorter.getNode())
Chris Lattner2b4c2792007-10-13 06:35:54 +00003383 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3384
Nate Begeman3df4d522005-10-12 20:40:40 +00003385 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003386 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003387 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003388}
3389
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003390static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003391 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003392 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00003393 return Elt.getNode();
3394 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003395}
3396
3397/// CombineConsecutiveLoads - build_pair (load, load) -> load
3398/// if load locations are consecutive.
Dan Gohman475871a2008-07-27 21:46:04 +00003399SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, MVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003400 assert(N->getOpcode() == ISD::BUILD_PAIR);
3401
3402 SDNode *LD1 = getBuildPairElt(N, 0);
3403 if (!ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00003404 return SDValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003405 MVT LD1VT = LD1->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003406 SDNode *LD2 = getBuildPairElt(N, 1);
3407 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3408 if (ISD::isNON_EXTLoad(LD2) &&
3409 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003410 // If both are volatile this would reduce the number of volatile loads.
3411 // If one is volatile it might be ok, but play conservative and bail out.
3412 !cast<LoadSDNode>(LD1)->isVolatile() &&
3413 !cast<LoadSDNode>(LD2)->isVolatile() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003414 TLI.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1, MFI)) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003415 LoadSDNode *LD = cast<LoadSDNode>(LD1);
3416 unsigned Align = LD->getAlignment();
3417 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003418 getABITypeAlignment(VT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003419 if (NewAlign <= Align &&
3420 (!AfterLegalize || TLI.isOperationLegal(ISD::LOAD, VT)))
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003421 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(),
3422 LD->getSrcValue(), LD->getSrcValueOffset(),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003423 false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003424 }
Dan Gohman475871a2008-07-27 21:46:04 +00003425 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003426}
3427
Dan Gohman475871a2008-07-27 21:46:04 +00003428SDValue DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3429 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003430 MVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00003431
Dan Gohman7f321562007-06-25 16:23:39 +00003432 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3433 // Only do this before legalize, since afterward the target may be depending
3434 // on the bitconvert.
3435 // First check to see if this is all constant.
3436 if (!AfterLegalize &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003437 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003438 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003439 bool isSimple = true;
3440 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3441 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3442 N0.getOperand(i).getOpcode() != ISD::Constant &&
3443 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3444 isSimple = false;
3445 break;
3446 }
3447
Duncan Sands83ec4b62008-06-06 12:08:01 +00003448 MVT DestEltVT = N->getValueType(0).getVectorElementType();
3449 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00003450 "Element type of vector ValueType must not be vector!");
3451 if (isSimple) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003452 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00003453 }
3454 }
3455
Chris Lattner94683772005-12-23 05:30:37 +00003456 // If the input is a constant, let getNode() fold it.
3457 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003458 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
Gabor Greifba36cb52008-08-28 21:40:38 +00003459 if (Res.getNode() != N) return Res;
Chris Lattner94683772005-12-23 05:30:37 +00003460 }
3461
Chris Lattnerc8547d82005-12-23 05:37:50 +00003462 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3463 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003464
Chris Lattner57104102005-12-23 05:44:41 +00003465 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003466 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00003467 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003468 // Do not change the width of a volatile load.
3469 !cast<LoadSDNode>(N0)->isVolatile() &&
3470 (!AfterLegalize || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003471 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng59d5b682007-05-07 21:27:48 +00003472 unsigned Align = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003473 getABITypeAlignment(VT.getTypeForMVT());
Evan Cheng59d5b682007-05-07 21:27:48 +00003474 unsigned OrigAlign = LN0->getAlignment();
3475 if (Align <= OrigAlign) {
Dan Gohman475871a2008-07-27 21:46:04 +00003476 SDValue Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
Evan Cheng59d5b682007-05-07 21:27:48 +00003477 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Dan Gohman77455612008-06-28 00:45:22 +00003478 LN0->isVolatile(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00003479 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003480 CombineTo(N0.getNode(), DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00003481 Load.getValue(1));
3482 return Load;
3483 }
Chris Lattner57104102005-12-23 05:44:41 +00003484 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003485
Chris Lattner3bd39d42008-01-27 17:42:27 +00003486 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3487 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3488 // This often reduces constant pool loads.
3489 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003490 N0.getNode()->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003491 SDValue NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00003492 AddToWorkList(NewConv.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003493
Duncan Sands83ec4b62008-06-06 12:08:01 +00003494 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003495 if (N0.getOpcode() == ISD::FNEG)
3496 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3497 assert(N0.getOpcode() == ISD::FABS);
3498 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3499 }
3500
3501 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3502 // Note that we don't handle copysign(x,cst) because this can always be folded
3503 // to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003504 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00003505 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003506 VT.isInteger() && !VT.isVector()) {
3507 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00003508 SDValue X = DAG.getNode(ISD::BIT_CONVERT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003509 MVT::getIntegerVT(OrigXWidth),
Chris Lattner3bd39d42008-01-27 17:42:27 +00003510 N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00003511 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003512
3513 // If X has a different width than the result/lhs, sext it or truncate it.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003514 unsigned VTWidth = VT.getSizeInBits();
Chris Lattner3bd39d42008-01-27 17:42:27 +00003515 if (OrigXWidth < VTWidth) {
3516 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
Gabor Greifba36cb52008-08-28 21:40:38 +00003517 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003518 } else if (OrigXWidth > VTWidth) {
3519 // To get the sign bit in the right place, we have to shift it right
3520 // before truncating.
3521 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3522 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003523 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003524 X = DAG.getNode(ISD::TRUNCATE, VT, X);
Gabor Greifba36cb52008-08-28 21:40:38 +00003525 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003526 }
3527
Duncan Sands83ec4b62008-06-06 12:08:01 +00003528 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003529 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00003530 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003531
Dan Gohman475871a2008-07-27 21:46:04 +00003532 SDValue Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner3bd39d42008-01-27 17:42:27 +00003533 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00003534 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003535
3536 return DAG.getNode(ISD::OR, VT, X, Cst);
3537 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003538
3539 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
3540 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003541 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
3542 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003543 return CombineLD;
3544 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003545
Dan Gohman475871a2008-07-27 21:46:04 +00003546 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00003547}
3548
Dan Gohman475871a2008-07-27 21:46:04 +00003549SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003550 MVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003551 return CombineConsecutiveLoads(N, VT);
3552}
3553
Dan Gohman7f321562007-06-25 16:23:39 +00003554/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003555/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3556/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00003557SDValue DAGCombiner::
Duncan Sands83ec4b62008-06-06 12:08:01 +00003558ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT DstEltVT) {
3559 MVT SrcEltVT = BV->getOperand(0).getValueType();
Chris Lattner6258fb22006-04-02 02:53:43 +00003560
3561 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00003562 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003563
Duncan Sands83ec4b62008-06-06 12:08:01 +00003564 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
3565 unsigned DstBitSize = DstEltVT.getSizeInBits();
Chris Lattner6258fb22006-04-02 02:53:43 +00003566
3567 // If this is a conversion of N elements of one type to N elements of another
3568 // type, convert each element. This handles FP<->INT cases.
3569 if (SrcBitSize == DstBitSize) {
Dan Gohman475871a2008-07-27 21:46:04 +00003570 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003571 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003572 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Gabor Greifba36cb52008-08-28 21:40:38 +00003573 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00003574 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003575 MVT VT = MVT::getVectorVT(DstEltVT,
3576 BV->getValueType(0).getVectorNumElements());
Dan Gohman7f321562007-06-25 16:23:39 +00003577 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003578 }
3579
3580 // Otherwise, we're growing or shrinking the elements. To avoid having to
3581 // handle annoying details of growing/shrinking FP values, we convert them to
3582 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003583 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003584 // Convert the input float vector to a int vector where the elements are the
3585 // same sizes.
3586 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003587 MVT IntVT = MVT::getIntegerVT(SrcEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00003588 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00003589 SrcEltVT = IntVT;
3590 }
3591
3592 // Now we know the input is an integer vector. If the output is a FP type,
3593 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003594 if (DstEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003595 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003596 MVT TmpVT = MVT::getIntegerVT(DstEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00003597 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00003598
3599 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003600 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003601 }
3602
3603 // Okay, we know the src/dst types are both integers of differing types.
3604 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003605 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00003606 if (SrcBitSize < DstBitSize) {
3607 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3608
Dan Gohman475871a2008-07-27 21:46:04 +00003609 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003610 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003611 i += NumInputsPerOutput) {
3612 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00003613 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003614 bool EltIsUndef = true;
3615 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3616 // Shift the previously computed bits over.
3617 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00003618 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00003619 if (Op.getOpcode() == ISD::UNDEF) continue;
3620 EltIsUndef = false;
3621
Dan Gohman220a8232008-03-03 23:51:38 +00003622 NewBits |=
3623 APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003624 }
3625
3626 if (EltIsUndef)
3627 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3628 else
3629 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3630 }
3631
Duncan Sands83ec4b62008-06-06 12:08:01 +00003632 MVT VT = MVT::getVectorVT(DstEltVT, Ops.size());
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 // Finally, this must be the case where we are shrinking elements: each input
3637 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00003638 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00003639 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003640 MVT VT = MVT::getVectorVT(DstEltVT, NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00003641 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003642 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003643 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3644 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3645 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3646 continue;
3647 }
Dan Gohman220a8232008-03-03 23:51:38 +00003648 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getAPIntValue();
Chris Lattner6258fb22006-04-02 02:53:43 +00003649 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohman220a8232008-03-03 23:51:38 +00003650 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003651 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohman220a8232008-03-03 23:51:38 +00003652 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00003653 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
3654 return DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00003655 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003656 }
3657
3658 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00003659 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00003660 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3661 }
Dan Gohman7f321562007-06-25 16:23:39 +00003662 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003663}
3664
3665
3666
Dan Gohman475871a2008-07-27 21:46:04 +00003667SDValue DAGCombiner::visitFADD(SDNode *N) {
3668 SDValue N0 = N->getOperand(0);
3669 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003670 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3671 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003672 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003673
Dan Gohman7f321562007-06-25 16:23:39 +00003674 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003675 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003676 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003677 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003678 }
Dan Gohman7f321562007-06-25 16:23:39 +00003679
Nate Begemana0e221d2005-10-18 00:28:13 +00003680 // fold (fadd c1, c2) -> c1+c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003681 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003682 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003683 // canonicalize constant to RHS
3684 if (N0CFP && !N1CFP)
3685 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003686 // fold (A + (-B)) -> A-B
Chris Lattner0254e702008-02-26 07:04:54 +00003687 if (isNegatibleForFree(N1, AfterLegalize) == 2)
3688 return DAG.getNode(ISD::FSUB, VT, N0,
3689 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner01b3d732005-09-28 22:28:18 +00003690 // fold ((-A) + B) -> B-A
Chris Lattner0254e702008-02-26 07:04:54 +00003691 if (isNegatibleForFree(N0, AfterLegalize) == 2)
3692 return DAG.getNode(ISD::FSUB, VT, N1,
3693 GetNegatedExpression(N0, DAG, AfterLegalize));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003694
3695 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3696 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003697 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003698 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3699 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3700
Dan Gohman475871a2008-07-27 21:46:04 +00003701 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003702}
3703
Dan Gohman475871a2008-07-27 21:46:04 +00003704SDValue DAGCombiner::visitFSUB(SDNode *N) {
3705 SDValue N0 = N->getOperand(0);
3706 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003707 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3708 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003709 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003710
Dan Gohman7f321562007-06-25 16:23:39 +00003711 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003712 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003713 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003714 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003715 }
Dan Gohman7f321562007-06-25 16:23:39 +00003716
Nate Begemana0e221d2005-10-18 00:28:13 +00003717 // fold (fsub c1, c2) -> c1-c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003718 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003719 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003720 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003721 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Chris Lattner0254e702008-02-26 07:04:54 +00003722 if (isNegatibleForFree(N1, AfterLegalize))
3723 return GetNegatedExpression(N1, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00003724 return DAG.getNode(ISD::FNEG, VT, N1);
3725 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003726 // fold (A-(-B)) -> A+B
Chris Lattner0254e702008-02-26 07:04:54 +00003727 if (isNegatibleForFree(N1, AfterLegalize))
3728 return DAG.getNode(ISD::FADD, VT, N0,
3729 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003730
Dan Gohman475871a2008-07-27 21:46:04 +00003731 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003732}
3733
Dan Gohman475871a2008-07-27 21:46:04 +00003734SDValue DAGCombiner::visitFMUL(SDNode *N) {
3735 SDValue N0 = N->getOperand(0);
3736 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003737 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3738 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003739 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003740
Dan Gohman7f321562007-06-25 16:23:39 +00003741 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003742 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003743 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003744 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003745 }
Dan Gohman7f321562007-06-25 16:23:39 +00003746
Nate Begeman11af4ea2005-10-17 20:40:11 +00003747 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003748 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003749 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003750 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003751 if (N0CFP && !N1CFP)
3752 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003753 // fold (fmul X, 2.0) -> (fadd X, X)
3754 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3755 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003756 // fold (fmul X, -1.0) -> (fneg X)
3757 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3758 return DAG.getNode(ISD::FNEG, VT, N0);
3759
3760 // -X * -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003761 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3762 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003763 // Both can be negated for free, check to see if at least one is cheaper
3764 // negated.
3765 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003766 return DAG.getNode(ISD::FMUL, VT,
3767 GetNegatedExpression(N0, DAG, AfterLegalize),
3768 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003769 }
3770 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003771
3772 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3773 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003774 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003775 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3776 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3777
Dan Gohman475871a2008-07-27 21:46:04 +00003778 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003779}
3780
Dan Gohman475871a2008-07-27 21:46:04 +00003781SDValue DAGCombiner::visitFDIV(SDNode *N) {
3782 SDValue N0 = N->getOperand(0);
3783 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003784 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3785 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003786 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003787
Dan Gohman7f321562007-06-25 16:23:39 +00003788 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003789 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003790 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003791 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003792 }
Dan Gohman7f321562007-06-25 16:23:39 +00003793
Nate Begemana148d982006-01-18 22:35:16 +00003794 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003795 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003796 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003797
3798
3799 // -X / -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003800 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3801 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003802 // Both can be negated for free, check to see if at least one is cheaper
3803 // negated.
3804 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003805 return DAG.getNode(ISD::FDIV, VT,
3806 GetNegatedExpression(N0, DAG, AfterLegalize),
3807 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003808 }
3809 }
3810
Dan Gohman475871a2008-07-27 21:46:04 +00003811 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003812}
3813
Dan Gohman475871a2008-07-27 21:46:04 +00003814SDValue DAGCombiner::visitFREM(SDNode *N) {
3815 SDValue N0 = N->getOperand(0);
3816 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003817 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3818 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003819 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003820
Nate Begemana148d982006-01-18 22:35:16 +00003821 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003822 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003823 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003824
Dan Gohman475871a2008-07-27 21:46:04 +00003825 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003826}
3827
Dan Gohman475871a2008-07-27 21:46:04 +00003828SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3829 SDValue N0 = N->getOperand(0);
3830 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00003831 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3832 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003833 MVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00003834
Dale Johannesendb44bf82007-10-16 23:38:29 +00003835 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00003836 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3837
3838 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003839 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003840 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3841 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dale Johannesen87503a62007-08-25 22:10:57 +00003842 if (!V.isNegative())
Chris Lattner12d83032006-03-05 05:30:57 +00003843 return DAG.getNode(ISD::FABS, VT, N0);
3844 else
3845 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3846 }
3847
3848 // copysign(fabs(x), y) -> copysign(x, y)
3849 // copysign(fneg(x), y) -> copysign(x, y)
3850 // copysign(copysign(x,z), y) -> copysign(x, y)
3851 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3852 N0.getOpcode() == ISD::FCOPYSIGN)
3853 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3854
3855 // copysign(x, abs(y)) -> abs(x)
3856 if (N1.getOpcode() == ISD::FABS)
3857 return DAG.getNode(ISD::FABS, VT, N0);
3858
3859 // copysign(x, copysign(y,z)) -> copysign(x, z)
3860 if (N1.getOpcode() == ISD::FCOPYSIGN)
3861 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3862
3863 // copysign(x, fp_extend(y)) -> copysign(x, y)
3864 // copysign(x, fp_round(y)) -> copysign(x, y)
3865 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3866 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3867
Dan Gohman475871a2008-07-27 21:46:04 +00003868 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00003869}
3870
3871
Chris Lattner01b3d732005-09-28 22:28:18 +00003872
Dan Gohman475871a2008-07-27 21:46:04 +00003873SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
3874 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003875 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003876 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00003877 MVT OpVT = N0.getValueType();
3878
Nate Begeman1d4d4142005-09-01 00:19:25 +00003879 // fold (sint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00003880 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003881 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00003882
3883 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
3884 // but UINT_TO_FP is legal on this target, try to convert.
Chris Lattnerf77e46b2008-06-26 17:16:00 +00003885 if (!TLI.isOperationLegal(ISD::SINT_TO_FP, OpVT) &&
Chris Lattnercda88752008-06-26 00:16:49 +00003886 TLI.isOperationLegal(ISD::UINT_TO_FP, OpVT)) {
3887 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
3888 if (DAG.SignBitIsZero(N0))
3889 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
3890 }
3891
3892
Dan Gohman475871a2008-07-27 21:46:04 +00003893 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003894}
3895
Dan Gohman475871a2008-07-27 21:46:04 +00003896SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
3897 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003898 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003899 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00003900 MVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00003901
Nate Begeman1d4d4142005-09-01 00:19:25 +00003902 // fold (uint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00003903 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003904 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00003905
3906 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
3907 // but SINT_TO_FP is legal on this target, try to convert.
Chris Lattnerf77e46b2008-06-26 17:16:00 +00003908 if (!TLI.isOperationLegal(ISD::UINT_TO_FP, OpVT) &&
Chris Lattnercda88752008-06-26 00:16:49 +00003909 TLI.isOperationLegal(ISD::SINT_TO_FP, OpVT)) {
3910 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
3911 if (DAG.SignBitIsZero(N0))
3912 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
3913 }
3914
Dan Gohman475871a2008-07-27 21:46:04 +00003915 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003916}
3917
Dan Gohman475871a2008-07-27 21:46:04 +00003918SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
3919 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003920 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003921 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003922
3923 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003924 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003925 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003926 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003927}
3928
Dan Gohman475871a2008-07-27 21:46:04 +00003929SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
3930 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003931 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003932 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003933
3934 // fold (fp_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00003935 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003936 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003937 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003938}
3939
Dan Gohman475871a2008-07-27 21:46:04 +00003940SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
3941 SDValue N0 = N->getOperand(0);
3942 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003943 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003944 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003945
3946 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003947 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner0bd48932008-01-17 07:00:52 +00003948 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003949
3950 // fold (fp_round (fp_extend x)) -> x
3951 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3952 return N0.getOperand(0);
3953
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00003954 // fold (fp_round (fp_round x)) -> (fp_round x)
3955 if (N0.getOpcode() == ISD::FP_ROUND) {
3956 // This is a value preserving truncation if both round's are.
3957 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003958 N0.getNode()->getConstantOperandVal(1) == 1;
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00003959 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
3960 DAG.getIntPtrConstant(IsTrunc));
3961 }
3962
Chris Lattner79dbea52006-03-13 06:26:26 +00003963 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00003964 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003965 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003966 AddToWorkList(Tmp.getNode());
Chris Lattner79dbea52006-03-13 06:26:26 +00003967 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
3968 }
3969
Dan Gohman475871a2008-07-27 21:46:04 +00003970 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003971}
3972
Dan Gohman475871a2008-07-27 21:46:04 +00003973SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
3974 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003975 MVT VT = N->getValueType(0);
3976 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00003977 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003978
Nate Begeman1d4d4142005-09-01 00:19:25 +00003979 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003980 if (N0CFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00003981 SDValue Round = DAG.getConstantFP(N0CFP->getValueAPF(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003982 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003983 }
Dan Gohman475871a2008-07-27 21:46:04 +00003984 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003985}
3986
Dan Gohman475871a2008-07-27 21:46:04 +00003987SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
3988 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003989 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003990 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003991
Chris Lattner5938bef2007-12-29 06:55:23 +00003992 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003993 if (N->hasOneUse() &&
Dan Gohman475871a2008-07-27 21:46:04 +00003994 N->use_begin().getUse().getSDValue().getOpcode() == ISD::FP_ROUND)
3995 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00003996
Nate Begeman1d4d4142005-09-01 00:19:25 +00003997 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003998 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003999 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004000
4001 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
4002 // value of X.
Gabor Greifba36cb52008-08-28 21:40:38 +00004003 if (N0.getOpcode() == ISD::FP_ROUND && N0.getNode()->getConstantOperandVal(1) == 1){
Dan Gohman475871a2008-07-27 21:46:04 +00004004 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004005 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00004006 if (VT.bitsLT(In.getValueType()))
Chris Lattner0bd48932008-01-17 07:00:52 +00004007 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
4008 return DAG.getNode(ISD::FP_EXTEND, VT, In);
4009 }
4010
4011 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004012 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004013 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
4014 TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00004015 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004016 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00004017 LN0->getBasePtr(), LN0->getSrcValue(),
4018 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004019 N0.getValueType(),
4020 LN0->isVolatile(),
4021 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00004022 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004023 CombineTo(N0.getNode(), DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad,
Chris Lattner0bd48932008-01-17 07:00:52 +00004024 DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00004025 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004026 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00004027 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004028
Dan Gohman475871a2008-07-27 21:46:04 +00004029 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004030}
4031
Dan Gohman475871a2008-07-27 21:46:04 +00004032SDValue DAGCombiner::visitFNEG(SDNode *N) {
4033 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004034
Chris Lattner0254e702008-02-26 07:04:54 +00004035 if (isNegatibleForFree(N0, AfterLegalize))
4036 return GetNegatedExpression(N0, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00004037
Chris Lattner3bd39d42008-01-27 17:42:27 +00004038 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
4039 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004040 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004041 N0.getOperand(0).getValueType().isInteger() &&
4042 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004043 SDValue Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004044 MVT IntVT = Int.getValueType();
4045 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004046 Int = DAG.getNode(ISD::XOR, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004047 DAG.getConstant(IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004048 AddToWorkList(Int.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004049 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4050 }
4051 }
4052
Dan Gohman475871a2008-07-27 21:46:04 +00004053 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004054}
4055
Dan Gohman475871a2008-07-27 21:46:04 +00004056SDValue DAGCombiner::visitFABS(SDNode *N) {
4057 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004058 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004059 MVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00004060
Nate Begeman1d4d4142005-09-01 00:19:25 +00004061 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00004062 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004063 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004064 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004065 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00004066 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004067 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004068 // fold (fabs (fcopysign x, y)) -> (fabs x)
4069 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
4070 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
4071
Chris Lattner3bd39d42008-01-27 17:42:27 +00004072 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
4073 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004074 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004075 N0.getOperand(0).getValueType().isInteger() &&
4076 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004077 SDValue Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004078 MVT IntVT = Int.getValueType();
4079 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004080 Int = DAG.getNode(ISD::AND, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004081 DAG.getConstant(~IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004082 AddToWorkList(Int.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004083 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4084 }
4085 }
4086
Dan Gohman475871a2008-07-27 21:46:04 +00004087 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004088}
4089
Dan Gohman475871a2008-07-27 21:46:04 +00004090SDValue DAGCombiner::visitBRCOND(SDNode *N) {
4091 SDValue Chain = N->getOperand(0);
4092 SDValue N1 = N->getOperand(1);
4093 SDValue N2 = N->getOperand(2);
Nate Begeman44728a72005-09-19 22:34:01 +00004094 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4095
4096 // never taken branch, fold to chain
4097 if (N1C && N1C->isNullValue())
4098 return Chain;
4099 // unconditional branch
Dan Gohman002e5d02008-03-13 22:13:53 +00004100 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00004101 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004102 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
4103 // on the target.
4104 if (N1.getOpcode() == ISD::SETCC &&
4105 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
4106 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
4107 N1.getOperand(0), N1.getOperand(1), N2);
4108 }
Dan Gohman475871a2008-07-27 21:46:04 +00004109 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004110}
4111
Chris Lattner3ea0b472005-10-05 06:47:48 +00004112// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
4113//
Dan Gohman475871a2008-07-27 21:46:04 +00004114SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00004115 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004116 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Chris Lattner3ea0b472005-10-05 06:47:48 +00004117
Duncan Sands8eab8a22008-06-09 11:32:28 +00004118 // Use SimplifySetCC to simplify SETCC's.
Dan Gohman475871a2008-07-27 21:46:04 +00004119 SDValue Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Gabor Greifba36cb52008-08-28 21:40:38 +00004120 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00004121
Gabor Greifba36cb52008-08-28 21:40:38 +00004122 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.getNode());
Nate Begemane17daeb2005-10-05 21:43:42 +00004123
4124 // fold br_cc true, dest -> br dest (unconditional branch)
Dan Gohman002e5d02008-03-13 22:13:53 +00004125 if (SCCC && !SCCC->isNullValue())
Nate Begemane17daeb2005-10-05 21:43:42 +00004126 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
4127 N->getOperand(4));
4128 // fold br_cc false, dest -> unconditional fall through
4129 if (SCCC && SCCC->isNullValue())
4130 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00004131
Nate Begemane17daeb2005-10-05 21:43:42 +00004132 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00004133 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Nate Begemane17daeb2005-10-05 21:43:42 +00004134 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
4135 Simp.getOperand(2), Simp.getOperand(0),
4136 Simp.getOperand(1), N->getOperand(4));
Dan Gohman475871a2008-07-27 21:46:04 +00004137 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004138}
4139
Chris Lattner448f2192006-11-11 00:39:41 +00004140
Duncan Sandsec87aa82008-06-15 20:12:31 +00004141/// CombineToPreIndexedLoadStore - Try turning a load / store into a
4142/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00004143/// and it has other uses besides the load / store. After the
4144/// transformation, the new indexed load / store has effectively folded
4145/// the add / subtract in and all of its other uses are redirected to the
4146/// new load / store.
4147bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
4148 if (!AfterLegalize)
4149 return false;
4150
4151 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004152 SDValue Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004153 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004154 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004155 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004156 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004157 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00004158 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00004159 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
4160 return false;
4161 Ptr = LD->getBasePtr();
4162 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004163 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004164 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004165 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004166 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
4167 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
4168 return false;
4169 Ptr = ST->getBasePtr();
4170 isLoad = false;
4171 } else
4172 return false;
4173
Chris Lattner9f1794e2006-11-11 00:56:29 +00004174 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4175 // out. There is no reason to make this a preinc/predec.
4176 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00004177 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004178 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004179
Chris Lattner9f1794e2006-11-11 00:56:29 +00004180 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00004181 SDValue BasePtr;
4182 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004183 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4184 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4185 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00004186 // Don't create a indexed load / store with zero offset.
4187 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004188 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004189 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004190
Chris Lattner41e53fd2006-11-11 01:00:15 +00004191 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00004192 // 1) The new base ptr is a frame index.
4193 // 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 +00004194 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00004195 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00004196 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00004197 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00004198
Chris Lattner41e53fd2006-11-11 01:00:15 +00004199 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4200 // (plus the implicit offset) to a register to preinc anyway.
4201 if (isa<FrameIndexSDNode>(BasePtr))
4202 return false;
4203
4204 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004205 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004206 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004207 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004208 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004209 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004210
Evan Chengc843abe2007-05-24 02:35:39 +00004211 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004212 bool RealUse = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00004213 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4214 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004215 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004216 if (Use == N)
4217 continue;
Evan Cheng917be682008-03-04 00:41:45 +00004218 if (Use->isPredecessorOf(N))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004219 return false;
4220
4221 if (!((Use->getOpcode() == ISD::LOAD &&
4222 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004223 (Use->getOpcode() == ISD::STORE &&
4224 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004225 RealUse = true;
4226 }
4227 if (!RealUse)
4228 return false;
4229
Dan Gohman475871a2008-07-27 21:46:04 +00004230 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004231 if (isLoad)
Dan Gohman475871a2008-07-27 21:46:04 +00004232 Result = DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004233 else
Dan Gohman475871a2008-07-27 21:46:04 +00004234 Result = DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004235 ++PreIndexedNodes;
4236 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004237 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004238 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004239 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004240 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004241 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004242 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004243 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004244 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004245 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004246 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004247 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004248 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004249 }
4250
Chris Lattner9f1794e2006-11-11 00:56:29 +00004251 // Finally, since the node is now dead, remove it from the graph.
4252 DAG.DeleteNode(N);
4253
4254 // Replace the uses of Ptr with uses of the updated base value.
4255 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004256 &DeadNodes);
Gabor Greifba36cb52008-08-28 21:40:38 +00004257 removeFromWorkList(Ptr.getNode());
4258 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00004259
4260 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004261}
4262
Duncan Sandsec87aa82008-06-15 20:12:31 +00004263/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00004264/// add / sub of the base pointer node into a post-indexed load / store.
4265/// The transformation folded the add / subtract into the new indexed
4266/// load / store effectively and all of its uses are redirected to the
4267/// new load / store.
4268bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
4269 if (!AfterLegalize)
4270 return false;
4271
4272 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004273 SDValue Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004274 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004275 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004276 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004277 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004278 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004279 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4280 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4281 return false;
4282 Ptr = LD->getBasePtr();
4283 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004284 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004285 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004286 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004287 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4288 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4289 return false;
4290 Ptr = ST->getBasePtr();
4291 isLoad = false;
4292 } else
4293 return false;
4294
Gabor Greifba36cb52008-08-28 21:40:38 +00004295 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004296 return false;
4297
Gabor Greifba36cb52008-08-28 21:40:38 +00004298 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4299 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004300 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004301 if (Op == N ||
4302 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4303 continue;
4304
Dan Gohman475871a2008-07-27 21:46:04 +00004305 SDValue BasePtr;
4306 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004307 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4308 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4309 if (Ptr == Offset)
4310 std::swap(BasePtr, Offset);
4311 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00004312 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00004313 // Don't create a indexed load / store with zero offset.
4314 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004315 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004316 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004317
Chris Lattner9f1794e2006-11-11 00:56:29 +00004318 // Try turning it into a post-indexed load / store except when
4319 // 1) All uses are load / store ops that use it as base ptr.
4320 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4321 // nor a successor of N. Otherwise, if Op is folded that would
4322 // create a cycle.
4323
4324 // Check for #1.
4325 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00004326 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
4327 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00004328 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00004329 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00004330 continue;
4331
Chris Lattner9f1794e2006-11-11 00:56:29 +00004332 // If all the uses are load / store addresses, then don't do the
4333 // transformation.
4334 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4335 bool RealUse = false;
4336 for (SDNode::use_iterator III = Use->use_begin(),
4337 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00004338 SDNode *UseUse = *III;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004339 if (!((UseUse->getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004340 cast<LoadSDNode>(UseUse)->getBasePtr().getNode() == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004341 (UseUse->getOpcode() == ISD::STORE &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004342 cast<StoreSDNode>(UseUse)->getBasePtr().getNode() == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004343 RealUse = true;
4344 }
Chris Lattner448f2192006-11-11 00:39:41 +00004345
Chris Lattner9f1794e2006-11-11 00:56:29 +00004346 if (!RealUse) {
4347 TryNext = true;
4348 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004349 }
4350 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004351 }
4352 if (TryNext)
4353 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004354
Chris Lattner9f1794e2006-11-11 00:56:29 +00004355 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00004356 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004357 SDValue Result = isLoad
4358 ? DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM)
4359 : DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004360 ++PostIndexedNodes;
4361 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004362 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004363 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004364 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004365 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004366 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004367 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004368 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004369 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004370 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004371 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004372 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004373 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00004374 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004375
Chris Lattner9f1794e2006-11-11 00:56:29 +00004376 // Finally, since the node is now dead, remove it from the graph.
4377 DAG.DeleteNode(N);
4378
4379 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00004380 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Chris Lattner9f1794e2006-11-11 00:56:29 +00004381 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004382 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004383 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004384 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004385 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004386 }
4387 }
4388 }
4389 return false;
4390}
4391
Chris Lattner00161a62008-01-25 07:20:16 +00004392/// InferAlignment - If we can infer some alignment information from this
4393/// pointer, return it.
Dan Gohman475871a2008-07-27 21:46:04 +00004394static unsigned InferAlignment(SDValue Ptr, SelectionDAG &DAG) {
Chris Lattner00161a62008-01-25 07:20:16 +00004395 // If this is a direct reference to a stack slot, use information about the
4396 // stack slot's alignment.
Chris Lattner1329cb82008-01-26 19:45:50 +00004397 int FrameIdx = 1 << 31;
4398 int64_t FrameOffset = 0;
Chris Lattner00161a62008-01-25 07:20:16 +00004399 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1329cb82008-01-26 19:45:50 +00004400 FrameIdx = FI->getIndex();
4401 } else if (Ptr.getOpcode() == ISD::ADD &&
4402 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4403 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4404 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4405 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner00161a62008-01-25 07:20:16 +00004406 }
Chris Lattner1329cb82008-01-26 19:45:50 +00004407
4408 if (FrameIdx != (1 << 31)) {
4409 // FIXME: Handle FI+CST.
4410 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4411 if (MFI.isFixedObjectIndex(FrameIdx)) {
Dan Gohman8cea8ff2008-08-11 18:27:03 +00004412 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx) + FrameOffset;
Chris Lattner1329cb82008-01-26 19:45:50 +00004413
4414 // The alignment of the frame index can be determined from its offset from
4415 // the incoming frame position. If the frame object is at offset 32 and
4416 // the stack is guaranteed to be 16-byte aligned, then we know that the
4417 // object is 16-byte aligned.
4418 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4419 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4420
4421 // Finally, the frame object itself may have a known alignment. Factor
4422 // the alignment + offset into a new alignment. For example, if we know
4423 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4424 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4425 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4426 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4427 FrameOffset);
4428 return std::max(Align, FIInfoAlign);
4429 }
4430 }
Chris Lattner00161a62008-01-25 07:20:16 +00004431
4432 return 0;
4433}
Chris Lattner448f2192006-11-11 00:39:41 +00004434
Dan Gohman475871a2008-07-27 21:46:04 +00004435SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004436 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004437 SDValue Chain = LD->getChain();
4438 SDValue Ptr = LD->getBasePtr();
Chris Lattner00161a62008-01-25 07:20:16 +00004439
4440 // Try to infer better alignment information than the load already has.
Dan Gohmana2676512008-08-20 16:30:28 +00004441 if (!Fast && LD->isUnindexed()) {
Chris Lattner00161a62008-01-25 07:20:16 +00004442 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4443 if (Align > LD->getAlignment())
4444 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4445 Chain, Ptr, LD->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004446 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004447 LD->isVolatile(), Align);
4448 }
4449 }
4450
Evan Cheng45a7ca92007-05-01 00:38:21 +00004451
4452 // If load is not volatile and there are no uses of the loaded value (and
4453 // the updated indexed value in case of indexed loads), change uses of the
4454 // chain value into uses of the chain input (i.e. delete the dead load).
4455 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00004456 if (N->getValueType(1) == MVT::Other) {
4457 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004458 if (N->hasNUsesOfValue(0, 0)) {
4459 // It's not safe to use the two value CombineTo variant here. e.g.
4460 // v1, chain2 = load chain1, loc
4461 // v2, chain3 = load chain2, loc
4462 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004463 // Now we replace use of chain2 with chain1. This makes the second load
4464 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004465 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004466 DOUT << "\nWith chain: "; DEBUG(Chain.getNode()->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004467 DOUT << "\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004468 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004469 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain, &DeadNodes);
Chris Lattner125991a2008-01-24 07:57:06 +00004470 if (N->use_empty()) {
4471 removeFromWorkList(N);
4472 DAG.DeleteNode(N);
4473 }
Dan Gohman475871a2008-07-27 21:46:04 +00004474 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00004475 }
Evan Cheng498f5592007-05-01 08:53:39 +00004476 } else {
4477 // Indexed loads.
4478 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4479 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004480 SDValue Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
Evan Cheng02c42852008-01-16 23:11:54 +00004481 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004482 DOUT << "\nWith: "; DEBUG(Undef.getNode()->dump(&DAG));
Evan Cheng02c42852008-01-16 23:11:54 +00004483 DOUT << " and 2 other values\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004484 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004485 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef, &DeadNodes);
4486 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Chris Lattner4626b252008-01-17 07:20:38 +00004487 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004488 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004489 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004490 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00004491 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004492 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004493 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004494 }
4495 }
Chris Lattner01a22022005-10-10 22:04:48 +00004496
4497 // If this load is directly stored, replace the load value with the stored
4498 // value.
4499 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004500 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohmanb061c4b2008-03-31 20:32:52 +00004501 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
4502 !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004503 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004504 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4505 if (PrevST->getBasePtr() == Ptr &&
4506 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004507 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004508 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004509 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004510
Jim Laskey7ca56af2006-10-11 13:47:09 +00004511 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004512 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00004513 SDValue BetterChain = FindBetterChain(N, Chain);
Jim Laskey279f0532006-09-25 16:29:54 +00004514
Jim Laskey6ff23e52006-10-04 16:53:27 +00004515 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004516 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00004517 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004518
Jim Laskey279f0532006-09-25 16:29:54 +00004519 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004520 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4521 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004522 LD->getSrcValue(), LD->getSrcValueOffset(),
4523 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004524 } else {
4525 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4526 LD->getValueType(0),
4527 BetterChain, Ptr, LD->getSrcValue(),
4528 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004529 LD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004530 LD->isVolatile(),
4531 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004532 }
Jim Laskey279f0532006-09-25 16:29:54 +00004533
Jim Laskey6ff23e52006-10-04 16:53:27 +00004534 // Create token factor to keep old chain connected.
Dan Gohman475871a2008-07-27 21:46:04 +00004535 SDValue Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
Jim Laskey288af5e2006-09-25 19:32:58 +00004536 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004537
Jim Laskey274062c2006-10-13 23:32:28 +00004538 // Replace uses with load result and token factor. Don't add users
4539 // to work list.
4540 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004541 }
4542 }
4543
Evan Cheng7fc033a2006-11-03 03:06:21 +00004544 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004545 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00004546 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00004547
Dan Gohman475871a2008-07-27 21:46:04 +00004548 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00004549}
4550
Chris Lattner07649d92008-01-08 23:08:06 +00004551
Dan Gohman475871a2008-07-27 21:46:04 +00004552SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004553 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004554 SDValue Chain = ST->getChain();
4555 SDValue Value = ST->getValue();
4556 SDValue Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004557
Chris Lattner00161a62008-01-25 07:20:16 +00004558 // Try to infer better alignment information than the store already has.
Dan Gohmana2676512008-08-20 16:30:28 +00004559 if (!Fast && ST->isUnindexed()) {
Chris Lattner00161a62008-01-25 07:20:16 +00004560 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4561 if (Align > ST->getAlignment())
4562 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004563 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004564 ST->isVolatile(), Align);
4565 }
4566 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004567
Evan Cheng59d5b682007-05-07 21:27:48 +00004568 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004569 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004570 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004571 ST->isUnindexed()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004572 unsigned Align = ST->getAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004573 MVT SVT = Value.getOperand(0).getValueType();
Evan Cheng59d5b682007-05-07 21:27:48 +00004574 unsigned OrigAlign = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004575 getABITypeAlignment(SVT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004576 if (Align <= OrigAlign &&
4577 ((!AfterLegalize && !ST->isVolatile()) ||
4578 TLI.isOperationLegal(ISD::STORE, SVT)))
Evan Cheng59d5b682007-05-07 21:27:48 +00004579 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohman77455612008-06-28 00:45:22 +00004580 ST->getSrcValueOffset(), ST->isVolatile(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00004581 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004582
Nate Begeman2cbba892006-12-11 02:23:46 +00004583 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004584 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004585 // NOTE: If the original store is volatile, this transform must not increase
4586 // the number of stores. For example, on x86-32 an f64 can be stored in one
4587 // processor operation but an i64 (which is not legal) requires two. So the
4588 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00004589 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00004590 SDValue Tmp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004591 switch (CFP->getValueType(0).getSimpleVT()) {
Chris Lattner62be1a72006-12-12 04:16:14 +00004592 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004593 case MVT::f80: // We don't do this for these yet.
4594 case MVT::f128:
4595 case MVT::ppcf128:
4596 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004597 case MVT::f32:
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004598 if ((!AfterLegalize && !ST->isVolatile()) ||
4599 TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004600 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
4601 convertToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004602 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004603 ST->getSrcValueOffset(), ST->isVolatile(),
4604 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004605 }
4606 break;
4607 case MVT::f64:
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004608 if ((!AfterLegalize && !ST->isVolatile()) ||
4609 TLI.isOperationLegal(ISD::STORE, MVT::i64)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004610 Tmp = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
4611 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004612 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004613 ST->getSrcValueOffset(), ST->isVolatile(),
4614 ST->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004615 } else if (!ST->isVolatile() &&
4616 TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004617 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004618 // argument passing. Since this is so common, custom legalize the
4619 // 64-bit integer store into two 32-bit stores.
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004620 uint64_t Val = CFP->getValueAPF().convertToAPInt().getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00004621 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4622 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00004623 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00004624
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004625 int SVOffset = ST->getSrcValueOffset();
4626 unsigned Alignment = ST->getAlignment();
4627 bool isVolatile = ST->isVolatile();
4628
Dan Gohman475871a2008-07-27 21:46:04 +00004629 SDValue St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004630 ST->getSrcValueOffset(),
4631 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004632 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4633 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004634 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004635 Alignment = MinAlign(Alignment, 4U);
Dan Gohman475871a2008-07-27 21:46:04 +00004636 SDValue St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004637 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004638 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4639 }
4640 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004641 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004642 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004643 }
4644
Jim Laskey279f0532006-09-25 16:29:54 +00004645 if (CombinerAA) {
4646 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00004647 SDValue BetterChain = FindBetterChain(N, Chain);
Jim Laskey279f0532006-09-25 16:29:54 +00004648
Jim Laskey6ff23e52006-10-04 16:53:27 +00004649 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004650 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004651 // Replace the chain to avoid dependency.
Dan Gohman475871a2008-07-27 21:46:04 +00004652 SDValue ReplStore;
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004653 if (ST->isTruncatingStore()) {
4654 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004655 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004656 ST->getMemoryVT(),
Chris Lattner4626b252008-01-17 07:20:38 +00004657 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004658 } else {
4659 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004660 ST->getSrcValue(), ST->getSrcValueOffset(),
4661 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004662 }
4663
Jim Laskey279f0532006-09-25 16:29:54 +00004664 // Create token to keep both nodes around.
Dan Gohman475871a2008-07-27 21:46:04 +00004665 SDValue Token =
Jim Laskey274062c2006-10-13 23:32:28 +00004666 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4667
4668 // Don't add users to work list.
4669 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004670 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004671 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004672
Evan Cheng33dbedc2006-11-05 09:31:14 +00004673 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004674 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00004675 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00004676
Chris Lattner3c872852007-12-29 06:26:16 +00004677 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00004678 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004679 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00004680 // See if we can simplify the input to this truncstore with knowledge that
4681 // only the low bits are being used. For example:
4682 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Dan Gohman475871a2008-07-27 21:46:04 +00004683 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004684 GetDemandedBits(Value,
4685 APInt::getLowBitsSet(Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004686 ST->getMemoryVT().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00004687 AddToWorkList(Value.getNode());
4688 if (Shorter.getNode())
Chris Lattner2b4c2792007-10-13 06:35:54 +00004689 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004690 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner2b4c2792007-10-13 06:35:54 +00004691 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004692
4693 // Otherwise, see if we can simplify the operation with
4694 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00004695 if (SimplifyDemandedBits(Value,
4696 APInt::getLowBitsSet(
4697 Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004698 ST->getMemoryVT().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00004699 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004700 }
4701
Chris Lattner3c872852007-12-29 06:26:16 +00004702 // If this is a load followed by a store to the same location, then the store
4703 // is dead/noop.
4704 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004705 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004706 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00004707 // There can't be any side effects between the load and store, such as
4708 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00004709 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00004710 // The store is dead, remove it.
4711 return Chain;
4712 }
4713 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004714
Chris Lattnerddf89562008-01-17 19:59:44 +00004715 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4716 // truncating store. We can do this even if this is already a truncstore.
4717 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00004718 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004719 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004720 ST->getMemoryVT())) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004721 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004722 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnerddf89562008-01-17 19:59:44 +00004723 ST->isVolatile(), ST->getAlignment());
4724 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004725
Dan Gohman475871a2008-07-27 21:46:04 +00004726 return SDValue();
Chris Lattner87514ca2005-10-10 22:31:19 +00004727}
4728
Dan Gohman475871a2008-07-27 21:46:04 +00004729SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4730 SDValue InVec = N->getOperand(0);
4731 SDValue InVal = N->getOperand(1);
4732 SDValue EltNo = N->getOperand(2);
Chris Lattnerca242442006-03-19 01:27:56 +00004733
4734 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4735 // vector with the inserted element.
4736 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
4737 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004738 SmallVector<SDValue, 8> Ops(InVec.getNode()->op_begin(), InVec.getNode()->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004739 if (Elt < Ops.size())
4740 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004741 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4742 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004743 }
4744
Dan Gohman475871a2008-07-27 21:46:04 +00004745 return SDValue();
Chris Lattnerca242442006-03-19 01:27:56 +00004746}
4747
Dan Gohman475871a2008-07-27 21:46:04 +00004748SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004749 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
4750 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
4751 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
4752
4753 // Perform only after legalization to ensure build_vector / vector_shuffle
4754 // optimizations have already been done.
Dan Gohman475871a2008-07-27 21:46:04 +00004755 if (!AfterLegalize) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004756
Dan Gohman475871a2008-07-27 21:46:04 +00004757 SDValue InVec = N->getOperand(0);
4758 SDValue EltNo = N->getOperand(1);
Evan Cheng513da432007-10-06 08:19:55 +00004759
Evan Cheng513da432007-10-06 08:19:55 +00004760 if (isa<ConstantSDNode>(EltNo)) {
4761 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
4762 bool NewLoad = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004763 MVT VT = InVec.getValueType();
4764 MVT EVT = VT.getVectorElementType();
4765 MVT LVT = EVT;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004766 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004767 MVT BCVT = InVec.getOperand(0).getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00004768 if (!BCVT.isVector() || EVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004769 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004770 InVec = InVec.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004771 EVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004772 NewLoad = true;
4773 }
Evan Cheng513da432007-10-06 08:19:55 +00004774
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004775 LoadSDNode *LN0 = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004776 if (ISD::isNormalLoad(InVec.getNode()))
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004777 LN0 = cast<LoadSDNode>(InVec);
4778 else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4779 InVec.getOperand(0).getValueType() == EVT &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004780 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004781 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4782 } else if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE) {
4783 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
4784 // =>
4785 // (load $addr+1*size)
4786 unsigned Idx = cast<ConstantSDNode>(InVec.getOperand(2).
4787 getOperand(Elt))->getValue();
4788 unsigned NumElems = InVec.getOperand(2).getNumOperands();
4789 InVec = (Idx < NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
4790 if (InVec.getOpcode() == ISD::BIT_CONVERT)
4791 InVec = InVec.getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00004792 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004793 LN0 = cast<LoadSDNode>(InVec);
4794 Elt = (Idx < NumElems) ? Idx : Idx - NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00004795 }
4796 }
Duncan Sandsec87aa82008-06-15 20:12:31 +00004797 if (!LN0 || !LN0->hasOneUse() || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00004798 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004799
4800 unsigned Align = LN0->getAlignment();
4801 if (NewLoad) {
4802 // Check the resultant load doesn't need a higher alignment than the
4803 // original load.
4804 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004805 getABITypeAlignment(LVT.getTypeForMVT());
Duncan Sands184a8762008-06-14 17:48:34 +00004806 if (NewAlign > Align || !TLI.isOperationLegal(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00004807 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004808 Align = NewAlign;
4809 }
4810
Dan Gohman475871a2008-07-27 21:46:04 +00004811 SDValue NewPtr = LN0->getBasePtr();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004812 if (Elt) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004813 unsigned PtrOff = LVT.getSizeInBits() * Elt / 8;
4814 MVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004815 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00004816 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004817 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
4818 DAG.getConstant(PtrOff, PtrType));
4819 }
4820 return DAG.getLoad(LVT, LN0->getChain(), NewPtr,
4821 LN0->getSrcValue(), LN0->getSrcValueOffset(),
4822 LN0->isVolatile(), Align);
Evan Cheng513da432007-10-06 08:19:55 +00004823 }
Dan Gohman475871a2008-07-27 21:46:04 +00004824 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00004825}
4826
4827
Dan Gohman475871a2008-07-27 21:46:04 +00004828SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00004829 unsigned NumInScalars = N->getNumOperands();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004830 MVT VT = N->getValueType(0);
4831 unsigned NumElts = VT.getVectorNumElements();
4832 MVT EltType = VT.getVectorElementType();
Chris Lattnerca242442006-03-19 01:27:56 +00004833
Dan Gohman7f321562007-06-25 16:23:39 +00004834 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
4835 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
4836 // at most two distinct vectors, turn this into a shuffle node.
Dan Gohman475871a2008-07-27 21:46:04 +00004837 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004838 for (unsigned i = 0; i != NumInScalars; ++i) {
4839 // Ignore undef inputs.
4840 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4841
Dan Gohman7f321562007-06-25 16:23:39 +00004842 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00004843 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00004844 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00004845 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00004846 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004847 break;
4848 }
4849
Dan Gohman7f321562007-06-25 16:23:39 +00004850 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00004851 // we can't make a shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00004852 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004853 if (ExtractedFromVec.getValueType() != VT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004854 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004855 break;
4856 }
4857
4858 // Otherwise, remember this. We allow up to two distinct input vectors.
4859 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
4860 continue;
4861
Gabor Greifba36cb52008-08-28 21:40:38 +00004862 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004863 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00004864 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004865 VecIn2 = ExtractedFromVec;
4866 } else {
4867 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00004868 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004869 break;
4870 }
4871 }
4872
4873 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00004874 if (VecIn1.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004875 SmallVector<SDValue, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004876 for (unsigned i = 0; i != NumInScalars; ++i) {
4877 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00004878 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004879 continue;
4880 }
4881
Dan Gohman475871a2008-07-27 21:46:04 +00004882 SDValue Extract = N->getOperand(i);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004883
4884 // If extracting from the first vector, just use the index directly.
4885 if (Extract.getOperand(0) == VecIn1) {
4886 BuildVecIndices.push_back(Extract.getOperand(1));
4887 continue;
4888 }
4889
4890 // Otherwise, use InIdx + VecSize
4891 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004892 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004893 }
4894
4895 // Add count and size info.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004896 MVT BuildVecVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004897
Dan Gohman7f321562007-06-25 16:23:39 +00004898 // Return the new VECTOR_SHUFFLE node.
Dan Gohman475871a2008-07-27 21:46:04 +00004899 SDValue Ops[5];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004900 Ops[0] = VecIn1;
Gabor Greifba36cb52008-08-28 21:40:38 +00004901 if (VecIn2.getNode()) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004902 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00004903 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004904 // Use an undef build_vector as input for the second operand.
Dan Gohman475871a2008-07-27 21:46:04 +00004905 std::vector<SDValue> UnOps(NumInScalars,
Chris Lattnercef896e2006-03-28 22:19:47 +00004906 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00004907 EltType));
4908 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004909 &UnOps[0], UnOps.size());
Gabor Greifba36cb52008-08-28 21:40:38 +00004910 AddToWorkList(Ops[1].getNode());
Chris Lattnercef896e2006-03-28 22:19:47 +00004911 }
Dan Gohman7f321562007-06-25 16:23:39 +00004912 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004913 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004914 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004915 }
4916
Dan Gohman475871a2008-07-27 21:46:04 +00004917 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00004918}
4919
Dan Gohman475871a2008-07-27 21:46:04 +00004920SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00004921 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
4922 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
4923 // inputs come from at most two distinct vectors, turn this into a shuffle
4924 // node.
4925
4926 // If we only have one input vector, we don't need to do any concatenation.
4927 if (N->getNumOperands() == 1) {
4928 return N->getOperand(0);
4929 }
4930
Dan Gohman475871a2008-07-27 21:46:04 +00004931 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00004932}
4933
Dan Gohman475871a2008-07-27 21:46:04 +00004934SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
4935 SDValue ShufMask = N->getOperand(2);
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004936 unsigned NumElts = ShufMask.getNumOperands();
4937
4938 // If the shuffle mask is an identity operation on the LHS, return the LHS.
4939 bool isIdentity = true;
4940 for (unsigned i = 0; i != NumElts; ++i) {
4941 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4942 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
4943 isIdentity = false;
4944 break;
4945 }
4946 }
4947 if (isIdentity) return N->getOperand(0);
4948
4949 // If the shuffle mask is an identity operation on the RHS, return the RHS.
4950 isIdentity = true;
4951 for (unsigned i = 0; i != NumElts; ++i) {
4952 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4953 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
4954 isIdentity = false;
4955 break;
4956 }
4957 }
4958 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00004959
4960 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
4961 // needed at all.
4962 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00004963 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004964 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00004965 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004966 for (unsigned i = 0; i != NumElts; ++i)
4967 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
4968 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
4969 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00004970 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00004971 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00004972 BaseIdx = Idx;
4973 } else {
4974 if (BaseIdx != Idx)
4975 isSplat = false;
4976 if (VecNum != V) {
4977 isUnary = false;
4978 break;
4979 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00004980 }
4981 }
4982
Dan Gohman475871a2008-07-27 21:46:04 +00004983 SDValue N0 = N->getOperand(0);
4984 SDValue N1 = N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00004985 // Normalize unary shuffle so the RHS is undef.
4986 if (isUnary && VecNum == 1)
4987 std::swap(N0, N1);
4988
Evan Cheng917ec982006-07-21 08:25:53 +00004989 // If it is a splat, check if the argument vector is a build_vector with
4990 // all scalar elements the same.
4991 if (isSplat) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004992 SDNode *V = N0.getNode();
Evan Cheng917ec982006-07-21 08:25:53 +00004993
Dan Gohman7f321562007-06-25 16:23:39 +00004994 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00004995 // not the number of vector elements, look through it. Be careful not to
4996 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00004997 if (V->getOpcode() == ISD::BIT_CONVERT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004998 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00004999 if (ConvInput.getValueType().isVector() &&
5000 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +00005001 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +00005002 }
5003
Dan Gohman7f321562007-06-25 16:23:39 +00005004 if (V->getOpcode() == ISD::BUILD_VECTOR) {
5005 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00005006 if (NumElems > BaseIdx) {
Dan Gohman475871a2008-07-27 21:46:04 +00005007 SDValue Base;
Evan Cheng917ec982006-07-21 08:25:53 +00005008 bool AllSame = true;
5009 for (unsigned i = 0; i != NumElems; ++i) {
5010 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
5011 Base = V->getOperand(i);
5012 break;
5013 }
5014 }
5015 // Splat of <u, u, u, u>, return <u, u, u, u>
Gabor Greifba36cb52008-08-28 21:40:38 +00005016 if (!Base.getNode())
Evan Cheng917ec982006-07-21 08:25:53 +00005017 return N0;
5018 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00005019 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00005020 AllSame = false;
5021 break;
5022 }
5023 }
5024 // Splat of <x, x, x, x>, return <x, x, x, x>
5025 if (AllSame)
5026 return N0;
5027 }
5028 }
5029 }
5030
Evan Chenge7bec0d2006-07-20 22:44:41 +00005031 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
5032 // into an undef.
5033 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00005034 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
5035 // first operand.
Dan Gohman475871a2008-07-27 21:46:04 +00005036 SmallVector<SDValue, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00005037 for (unsigned i = 0; i != NumElts; ++i) {
5038 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
5039 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
5040 MappedOps.push_back(ShufMask.getOperand(i));
5041 } else {
5042 unsigned NewIdx =
5043 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
Duncan Sandsd038e042008-07-21 10:20:31 +00005044 MappedOps.push_back(DAG.getConstant(NewIdx,
5045 ShufMask.getOperand(i).getValueType()));
Chris Lattner17614ea2006-04-08 05:34:25 +00005046 }
5047 }
Dan Gohman7f321562007-06-25 16:23:39 +00005048 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005049 &MappedOps[0], MappedOps.size());
Gabor Greifba36cb52008-08-28 21:40:38 +00005050 AddToWorkList(ShufMask.getNode());
Dan Gohman7f321562007-06-25 16:23:39 +00005051 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
5052 N0,
5053 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
5054 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00005055 }
Dan Gohman7f321562007-06-25 16:23:39 +00005056
Dan Gohman475871a2008-07-27 21:46:04 +00005057 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005058}
5059
Evan Cheng44f1f092006-04-20 08:56:16 +00005060/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00005061/// an AND to a vector_shuffle with the destination vector and a zero vector.
5062/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00005063/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00005064SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
5065 SDValue LHS = N->getOperand(0);
5066 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00005067 if (N->getOpcode() == ISD::AND) {
5068 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00005069 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005070 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00005071 std::vector<SDValue> IdxOps;
Evan Cheng44f1f092006-04-20 08:56:16 +00005072 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00005073 unsigned NumElts = NumOps;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005074 MVT EVT = RHS.getValueType().getVectorElementType();
Evan Cheng44f1f092006-04-20 08:56:16 +00005075 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005076 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00005077 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00005078 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005079 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Duncan Sands77926da2008-07-18 20:12:05 +00005080 IdxOps.push_back(DAG.getConstant(i, EVT));
Evan Cheng44f1f092006-04-20 08:56:16 +00005081 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Duncan Sands77926da2008-07-18 20:12:05 +00005082 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
Evan Cheng44f1f092006-04-20 08:56:16 +00005083 else
Dan Gohman475871a2008-07-27 21:46:04 +00005084 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005085 }
5086
5087 // Let's see if the target supports this vector_shuffle.
5088 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
Dan Gohman475871a2008-07-27 21:46:04 +00005089 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005090
Dan Gohman7f321562007-06-25 16:23:39 +00005091 // Return the new VECTOR_SHUFFLE node.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005092 MVT VT = MVT::getVectorVT(EVT, NumElts);
Dan Gohman475871a2008-07-27 21:46:04 +00005093 std::vector<SDValue> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005094 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00005095 Ops.push_back(LHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00005096 AddToWorkList(LHS.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00005097 std::vector<SDValue> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00005098 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005099 &ZeroOps[0], ZeroOps.size()));
Duncan Sands77926da2008-07-18 20:12:05 +00005100 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005101 &IdxOps[0], IdxOps.size()));
Dan Gohman475871a2008-07-27 21:46:04 +00005102 SDValue Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005103 &Ops[0], Ops.size());
Dan Gohman7a9a5af2008-07-16 16:13:58 +00005104 if (VT != N->getValueType(0))
5105 Result = DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00005106 return Result;
5107 }
5108 }
Dan Gohman475871a2008-07-27 21:46:04 +00005109 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005110}
5111
Dan Gohman7f321562007-06-25 16:23:39 +00005112/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00005113SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005114 // After legalize, the target may be depending on adds and other
5115 // binary ops to provide legal ways to construct constants or other
5116 // things. Simplifying them may result in a loss of legality.
Dan Gohman475871a2008-07-27 21:46:04 +00005117 if (AfterLegalize) return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00005118
Duncan Sands83ec4b62008-06-06 12:08:01 +00005119 MVT VT = N->getValueType(0);
5120 assert(VT.isVector() && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00005121
Duncan Sands83ec4b62008-06-06 12:08:01 +00005122 MVT EltType = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00005123 SDValue LHS = N->getOperand(0);
5124 SDValue RHS = N->getOperand(1);
5125 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005126 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +00005127
Dan Gohman7f321562007-06-25 16:23:39 +00005128 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00005129 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00005130 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
5131 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00005132 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005133 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005134 SDValue LHSOp = LHS.getOperand(i);
5135 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00005136 // If these two elements can't be folded, bail out.
5137 if ((LHSOp.getOpcode() != ISD::UNDEF &&
5138 LHSOp.getOpcode() != ISD::Constant &&
5139 LHSOp.getOpcode() != ISD::ConstantFP) ||
5140 (RHSOp.getOpcode() != ISD::UNDEF &&
5141 RHSOp.getOpcode() != ISD::Constant &&
5142 RHSOp.getOpcode() != ISD::ConstantFP))
5143 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00005144 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00005145 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
5146 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00005147 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005148 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +00005149 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005150 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00005151 break;
5152 }
Dan Gohman7f321562007-06-25 16:23:39 +00005153 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Gabor Greifba36cb52008-08-28 21:40:38 +00005154 AddToWorkList(Ops.back().getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +00005155 assert((Ops.back().getOpcode() == ISD::UNDEF ||
5156 Ops.back().getOpcode() == ISD::Constant ||
5157 Ops.back().getOpcode() == ISD::ConstantFP) &&
5158 "Scalar binop didn't fold!");
5159 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005160
Dan Gohman7f321562007-06-25 16:23:39 +00005161 if (Ops.size() == LHS.getNumOperands()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005162 MVT VT = LHS.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00005163 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005164 }
Chris Lattneredab1b92006-04-02 03:25:57 +00005165 }
5166
Dan Gohman475871a2008-07-27 21:46:04 +00005167 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00005168}
5169
Dan Gohman475871a2008-07-27 21:46:04 +00005170SDValue DAGCombiner::SimplifySelect(SDValue N0, SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00005171 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
5172
Dan Gohman475871a2008-07-27 21:46:04 +00005173 SDValue SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00005174 cast<CondCodeSDNode>(N0.getOperand(2))->get());
5175 // If we got a simplified select_cc node back from SimplifySelectCC, then
5176 // break it down into a new SETCC node, and a new SELECT node, and then return
5177 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +00005178 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005179 // Check to see if we got a select_cc back (to turn into setcc/select).
5180 // Otherwise, just return whatever node we got back, like fabs.
5181 if (SCC.getOpcode() == ISD::SELECT_CC) {
Dan Gohman475871a2008-07-27 21:46:04 +00005182 SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
Nate Begemanf845b452005-10-08 00:29:44 +00005183 SCC.getOperand(0), SCC.getOperand(1),
5184 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +00005185 AddToWorkList(SETCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005186 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
5187 SCC.getOperand(3), SETCC);
5188 }
5189 return SCC;
5190 }
Dan Gohman475871a2008-07-27 21:46:04 +00005191 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005192}
5193
Chris Lattner40c62d52005-10-18 06:04:22 +00005194/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
5195/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00005196/// select. Callers of this should assume that TheSelect is deleted if this
5197/// returns true. As such, they should return the appropriate thing (e.g. the
5198/// node) back to the top-level of the DAG combiner loop to avoid it being
5199/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00005200///
Dan Gohman475871a2008-07-27 21:46:04 +00005201bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
5202 SDValue RHS) {
Chris Lattner40c62d52005-10-18 06:04:22 +00005203
5204 // If this is a select from two identical things, try to pull the operation
5205 // through the select.
5206 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00005207 // If this is a load and the token chain is identical, replace the select
5208 // of two loads with a load through a select of the address to load from.
5209 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
5210 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00005211 if (LHS.getOpcode() == ISD::LOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005212 // Do not let this transformation reduce the number of volatile loads.
5213 !cast<LoadSDNode>(LHS)->isVolatile() &&
5214 !cast<LoadSDNode>(RHS)->isVolatile() &&
Chris Lattner40c62d52005-10-18 06:04:22 +00005215 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00005216 LHS.getOperand(0) == RHS.getOperand(0)) {
5217 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5218 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5219
5220 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005221 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00005222 // FIXME: this conflates two src values, discarding one. This is not
5223 // the right thing to do, but nothing uses srcvalues now. When they do,
5224 // turn SrcValue into a list of locations.
Dan Gohman475871a2008-07-27 21:46:04 +00005225 SDValue Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005226 if (TheSelect->getOpcode() == ISD::SELECT) {
5227 // Check that the condition doesn't reach either load. If so, folding
5228 // this will induce a cycle into the DAG.
Gabor Greifba36cb52008-08-28 21:40:38 +00005229 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5230 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode())) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005231 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
5232 TheSelect->getOperand(0), LLD->getBasePtr(),
5233 RLD->getBasePtr());
5234 }
5235 } else {
5236 // Check that the condition doesn't reach either load. If so, folding
5237 // this will induce a cycle into the DAG.
Gabor Greifba36cb52008-08-28 21:40:38 +00005238 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5239 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5240 !LLD->isPredecessorOf(TheSelect->getOperand(1).getNode()) &&
5241 !RLD->isPredecessorOf(TheSelect->getOperand(1).getNode())) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005242 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00005243 TheSelect->getOperand(0),
5244 TheSelect->getOperand(1),
5245 LLD->getBasePtr(), RLD->getBasePtr(),
5246 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005247 }
Evan Cheng466685d2006-10-09 20:57:25 +00005248 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005249
Gabor Greifba36cb52008-08-28 21:40:38 +00005250 if (Addr.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005251 SDValue Load;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005252 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
5253 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
5254 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005255 LLD->getSrcValueOffset(),
5256 LLD->isVolatile(),
5257 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005258 else {
5259 Load = DAG.getExtLoad(LLD->getExtensionType(),
5260 TheSelect->getValueType(0),
5261 LLD->getChain(), Addr, LLD->getSrcValue(),
5262 LLD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005263 LLD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005264 LLD->isVolatile(),
5265 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005266 }
5267 // Users of the select now use the result of the load.
5268 CombineTo(TheSelect, Load);
5269
5270 // Users of the old loads now use the new load's chain. We know the
5271 // old-load value is dead now.
Gabor Greifba36cb52008-08-28 21:40:38 +00005272 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
5273 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005274 return true;
5275 }
Evan Chengc5484282006-10-04 00:56:09 +00005276 }
Chris Lattner40c62d52005-10-18 06:04:22 +00005277 }
5278 }
5279
5280 return false;
5281}
5282
Dan Gohman475871a2008-07-27 21:46:04 +00005283SDValue DAGCombiner::SimplifySelectCC(SDValue N0, SDValue N1,
5284 SDValue N2, SDValue N3,
5285 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00005286
Duncan Sands83ec4b62008-06-06 12:08:01 +00005287 MVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +00005288 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
5289 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
5290 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005291
5292 // Determine if the condition we're dealing with is constant
Dan Gohman475871a2008-07-27 21:46:04 +00005293 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00005294 if (SCC.getNode()) AddToWorkList(SCC.getNode());
5295 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005296
5297 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00005298 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005299 return N2;
5300 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00005301 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005302 return N3;
5303
5304 // Check to see if we can simplify the select into an fabs node
5305 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5306 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00005307 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005308 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5309 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5310 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5311 N2 == N3.getOperand(0))
5312 return DAG.getNode(ISD::FABS, VT, N0);
5313
5314 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5315 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5316 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5317 N2.getOperand(0) == N3)
5318 return DAG.getNode(ISD::FABS, VT, N3);
5319 }
5320 }
5321
5322 // Check to see if we can perform the "gzip trick", transforming
5323 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00005324 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005325 N0.getValueType().isInteger() &&
5326 N2.getValueType().isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005327 (N1C->isNullValue() || // (a < 0) ? b : 0
5328 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Duncan Sands83ec4b62008-06-06 12:08:01 +00005329 MVT XType = N0.getValueType();
5330 MVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00005331 if (XType.bitsGE(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005332 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00005333 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00005334 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
5335 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005336 ShCtV = XType.getSizeInBits()-ShCtV-1;
Dan Gohman475871a2008-07-27 21:46:04 +00005337 SDValue ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5338 SDValue Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +00005339 AddToWorkList(Shift.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00005340 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005341 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005342 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005343 }
5344 return DAG.getNode(ISD::AND, AType, Shift, N2);
5345 }
Dan Gohman475871a2008-07-27 21:46:04 +00005346 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005347 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005348 TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00005349 AddToWorkList(Shift.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00005350 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005351 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005352 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005353 }
5354 return DAG.getNode(ISD::AND, AType, Shift, N2);
5355 }
5356 }
Nate Begeman07ed4172005-10-10 21:26:48 +00005357
5358 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00005359 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Nate Begeman07ed4172005-10-10 21:26:48 +00005360 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00005361
5362 // If the caller doesn't want us to simplify this into a zext of a compare,
5363 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00005364 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00005365 return SDValue();
Chris Lattner1eba01e2007-04-11 06:50:51 +00005366
Nate Begeman07ed4172005-10-10 21:26:48 +00005367 // Get a SetCC of the condition
5368 // FIXME: Should probably make sure that setcc is legal if we ever have a
5369 // target where it isn't.
Dan Gohman475871a2008-07-27 21:46:04 +00005370 SDValue Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00005371 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00005372 if (AfterLegalize) {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005373 SCC = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005374 if (N2.getValueType().bitsLT(SCC.getValueType()))
Chris Lattner555d8d62006-12-07 22:36:47 +00005375 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5376 else
5377 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005378 } else {
5379 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00005380 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005381 }
Gabor Greifba36cb52008-08-28 21:40:38 +00005382 AddToWorkList(SCC.getNode());
5383 AddToWorkList(Temp.getNode());
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005384
Dan Gohman002e5d02008-03-13 22:13:53 +00005385 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005386 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00005387 // shl setcc result by log2 n2c
5388 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00005389 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Nate Begeman07ed4172005-10-10 21:26:48 +00005390 TLI.getShiftAmountTy()));
5391 }
5392
Nate Begemanf845b452005-10-08 00:29:44 +00005393 // Check to see if this is the equivalent of setcc
5394 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5395 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00005396 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005397 MVT XType = N0.getValueType();
Duncan Sands184a8762008-06-14 17:48:34 +00005398 if (!AfterLegalize ||
5399 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(N0))) {
Dan Gohman475871a2008-07-27 21:46:04 +00005400 SDValue Res = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00005401 if (Res.getValueType() != VT)
5402 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5403 return Res;
5404 }
5405
5406 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5407 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands184a8762008-06-14 17:48:34 +00005408 (!AfterLegalize ||
5409 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Dan Gohman475871a2008-07-27 21:46:04 +00005410 SDValue Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
Nate Begemanf845b452005-10-08 00:29:44 +00005411 return DAG.getNode(ISD::SRL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005412 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Nate Begemanf845b452005-10-08 00:29:44 +00005413 TLI.getShiftAmountTy()));
5414 }
5415 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5416 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005417 SDValue NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
Nate Begemanf845b452005-10-08 00:29:44 +00005418 N0);
Dan Gohman475871a2008-07-27 21:46:04 +00005419 SDValue NotN0 = DAG.getNode(ISD::XOR, XType, N0,
Nate Begemanf845b452005-10-08 00:29:44 +00005420 DAG.getConstant(~0ULL, XType));
5421 return DAG.getNode(ISD::SRL, XType,
5422 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00005423 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005424 TLI.getShiftAmountTy()));
5425 }
5426 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5427 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005428 SDValue Sign = DAG.getNode(ISD::SRL, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005429 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005430 TLI.getShiftAmountTy()));
5431 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5432 }
5433 }
5434
5435 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5436 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5437 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005438 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005439 N2.getOperand(0) == N1 && N0.getValueType().isInteger()) {
5440 MVT XType = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00005441 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005442 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005443 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00005444 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005445 AddToWorkList(Shift.getNode());
5446 AddToWorkList(Add.getNode());
Chris Lattner1982ef22007-04-11 05:11:38 +00005447 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5448 }
5449 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5450 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5451 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5452 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5453 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005454 MVT XType = N0.getValueType();
5455 if (SubC->isNullValue() && XType.isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005456 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005457 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005458 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00005459 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005460 AddToWorkList(Shift.getNode());
5461 AddToWorkList(Add.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005462 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5463 }
5464 }
5465 }
Chris Lattner1982ef22007-04-11 05:11:38 +00005466
Dan Gohman475871a2008-07-27 21:46:04 +00005467 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005468}
5469
Evan Chengfa1eb272007-02-08 22:13:59 +00005470/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Dan Gohman475871a2008-07-27 21:46:04 +00005471SDValue DAGCombiner::SimplifySetCC(MVT VT, SDValue N0,
5472 SDValue N1, ISD::CondCode Cond,
5473 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00005474 TargetLowering::DAGCombinerInfo
5475 DagCombineInfo(DAG, !AfterLegalize, false, this);
5476 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00005477}
5478
Nate Begeman69575232005-10-20 02:15:44 +00005479/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5480/// return a DAG expression to select that will generate the same value by
5481/// multiplying by a magic number. See:
5482/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00005483SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005484 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00005485 SDValue S = TLI.BuildSDIV(N, DAG, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005486
Andrew Lenharth232c9102006-06-12 16:07:18 +00005487 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005488 ii != ee; ++ii)
5489 AddToWorkList(*ii);
5490 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005491}
5492
5493/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5494/// return a DAG expression to select that will generate the same value by
5495/// multiplying by a magic number. See:
5496/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00005497SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005498 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00005499 SDValue S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00005500
Andrew Lenharth232c9102006-06-12 16:07:18 +00005501 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005502 ii != ee; ++ii)
5503 AddToWorkList(*ii);
5504 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005505}
5506
Jim Laskey71382342006-10-07 23:37:56 +00005507/// FindBaseOffset - Return true if base is known not to alias with anything
5508/// but itself. Provides base object and offset as results.
Dan Gohman475871a2008-07-27 21:46:04 +00005509static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset) {
Jim Laskey71382342006-10-07 23:37:56 +00005510 // Assume it is a primitive operation.
5511 Base = Ptr; Offset = 0;
5512
5513 // If it's an adding a simple constant then integrate the offset.
5514 if (Base.getOpcode() == ISD::ADD) {
5515 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5516 Base = Base.getOperand(0);
5517 Offset += C->getValue();
5518 }
5519 }
5520
5521 // If it's any of the following then it can't alias with anything but itself.
5522 return isa<FrameIndexSDNode>(Base) ||
5523 isa<ConstantPoolSDNode>(Base) ||
5524 isa<GlobalAddressSDNode>(Base);
5525}
5526
5527/// isAlias - Return true if there is any possibility that the two addresses
5528/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +00005529bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +00005530 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman475871a2008-07-27 21:46:04 +00005531 SDValue Ptr2, int64_t Size2,
Jim Laskey096c22e2006-10-18 12:29:57 +00005532 const Value *SrcValue2, int SrcValueOffset2)
5533{
Jim Laskey71382342006-10-07 23:37:56 +00005534 // If they are the same then they must be aliases.
5535 if (Ptr1 == Ptr2) return true;
5536
5537 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +00005538 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +00005539 int64_t Offset1, Offset2;
5540 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5541 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5542
5543 // If they have a same base address then...
5544 if (Base1 == Base2) {
5545 // Check to see if the addresses overlap.
5546 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5547 }
5548
Jim Laskey096c22e2006-10-18 12:29:57 +00005549 // If we know both bases then they can't alias.
5550 if (KnownBase1 && KnownBase2) return false;
5551
Jim Laskey07a27092006-10-18 19:08:31 +00005552 if (CombinerGlobalAA) {
5553 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00005554 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5555 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5556 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00005557 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00005558 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00005559 if (AAResult == AliasAnalysis::NoAlias)
5560 return false;
5561 }
Jim Laskey096c22e2006-10-18 12:29:57 +00005562
5563 // Otherwise we have to assume they alias.
5564 return true;
Jim Laskey71382342006-10-07 23:37:56 +00005565}
5566
5567/// FindAliasInfo - Extracts the relevant alias information from the memory
5568/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00005569bool DAGCombiner::FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +00005570 SDValue &Ptr, int64_t &Size,
Jim Laskey096c22e2006-10-18 12:29:57 +00005571 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005572 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5573 Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005574 Size = LD->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005575 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005576 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00005577 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005578 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005579 Ptr = ST->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005580 Size = ST->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005581 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005582 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00005583 } else {
Jim Laskey71382342006-10-07 23:37:56 +00005584 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005585 }
5586
5587 return false;
5588}
5589
Jim Laskey6ff23e52006-10-04 16:53:27 +00005590/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5591/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +00005592void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
5593 SmallVector<SDValue, 8> &Aliases) {
5594 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005595 std::set<SDNode *> Visited; // Visited node set.
5596
Jim Laskey279f0532006-09-25 16:29:54 +00005597 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +00005598 SDValue Ptr;
Jim Laskey279f0532006-09-25 16:29:54 +00005599 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005600 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005601 int SrcValueOffset;
5602 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005603
Jim Laskey6ff23e52006-10-04 16:53:27 +00005604 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005605 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005606
Jim Laskeybc588b82006-10-05 15:07:25 +00005607 // Look at each chain and determine if it is an alias. If so, add it to the
5608 // aliases list. If not, then continue up the chain looking for the next
5609 // candidate.
5610 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005611 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +00005612 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005613
Jim Laskeybc588b82006-10-05 15:07:25 +00005614 // Don't bother if we've been before.
Gabor Greifba36cb52008-08-28 21:40:38 +00005615 if (Visited.find(Chain.getNode()) != Visited.end()) continue;
5616 Visited.insert(Chain.getNode());
Jim Laskeybc588b82006-10-05 15:07:25 +00005617
5618 switch (Chain.getOpcode()) {
5619 case ISD::EntryToken:
5620 // Entry token is ideal chain operand, but handled in FindBetterChain.
5621 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005622
Jim Laskeybc588b82006-10-05 15:07:25 +00005623 case ISD::LOAD:
5624 case ISD::STORE: {
5625 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +00005626 SDValue OpPtr;
Jim Laskeybc588b82006-10-05 15:07:25 +00005627 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005628 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005629 int OpSrcValueOffset;
Gabor Greifba36cb52008-08-28 21:40:38 +00005630 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Jim Laskey096c22e2006-10-18 12:29:57 +00005631 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005632
5633 // If chain is alias then stop here.
5634 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005635 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5636 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005637 Aliases.push_back(Chain);
5638 } else {
5639 // Look further up the chain.
5640 Chains.push_back(Chain.getOperand(0));
5641 // Clean up old chain.
Gabor Greifba36cb52008-08-28 21:40:38 +00005642 AddToWorkList(Chain.getNode());
Jim Laskey279f0532006-09-25 16:29:54 +00005643 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005644 break;
5645 }
5646
5647 case ISD::TokenFactor:
5648 // We have to check each of the operands of the token factor, so we queue
5649 // then up. Adding the operands to the queue (stack) in reverse order
5650 // maintains the original order and increases the likelihood that getNode
5651 // will find a matching token factor (CSE.)
5652 for (unsigned n = Chain.getNumOperands(); n;)
5653 Chains.push_back(Chain.getOperand(--n));
5654 // Eliminate the token factor if we can.
Gabor Greifba36cb52008-08-28 21:40:38 +00005655 AddToWorkList(Chain.getNode());
Jim Laskeybc588b82006-10-05 15:07:25 +00005656 break;
5657
5658 default:
5659 // For all other instructions we will just have to take what we can get.
5660 Aliases.push_back(Chain);
5661 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005662 }
5663 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005664}
5665
5666/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5667/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +00005668SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
5669 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005670
Jim Laskey6ff23e52006-10-04 16:53:27 +00005671 // Accumulate all the aliases to this node.
5672 GatherAllAliases(N, OldChain, Aliases);
5673
5674 if (Aliases.size() == 0) {
5675 // If no operands then chain to entry token.
5676 return DAG.getEntryNode();
5677 } else if (Aliases.size() == 1) {
5678 // If a single operand then chain to it. We don't need to revisit it.
5679 return Aliases[0];
5680 }
5681
5682 // Construct a custom tailored token factor.
Dan Gohman475871a2008-07-27 21:46:04 +00005683 SDValue NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005684 &Aliases[0], Aliases.size());
5685
5686 // Make sure the old chain gets cleaned up.
Gabor Greifba36cb52008-08-28 21:40:38 +00005687 if (NewChain != OldChain) AddToWorkList(OldChain.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00005688
5689 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005690}
5691
Nate Begeman1d4d4142005-09-01 00:19:25 +00005692// SelectionDAG::Combine - This is the entry point for the file.
5693//
Dan Gohmana2676512008-08-20 16:30:28 +00005694void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA,
5695 bool Fast) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00005696 /// run - This is the main entry point to this class.
5697 ///
Dan Gohmana2676512008-08-20 16:30:28 +00005698 DAGCombiner(*this, AA, Fast).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005699}