blob: 2d7147d24944a32ac82a112dda6cc3ad6fa41589 [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.
Scott Michelfdc40a02009-02-17 22:15:04 +000012//
Dan Gohman41287002009-04-25 17:09:45 +000013// This pass is not a substitute for the LLVM IR instcombine pass. This pass is
14// primarily intended to handle simplification opportunities that are implicit
15// in the LLVM IR and exposed by the various codegen lowering phases.
16//
Nate Begeman1d4d4142005-09-01 00:19:25 +000017//===----------------------------------------------------------------------===//
18
19#define DEBUG_TYPE "dagcombine"
Nate Begeman1d4d4142005-09-01 00:19:25 +000020#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner600fec32009-03-11 05:08:08 +000021#include "llvm/DerivedTypes.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000022#include "llvm/LLVMContext.h"
Chris Lattner00161a62008-01-25 07:20:16 +000023#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner600fec32009-03-11 05:08:08 +000025#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000026#include "llvm/Analysis/AliasAnalysis.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000027#include "llvm/Target/TargetData.h"
Chris Lattner1329cb82008-01-26 19:45:50 +000028#include "llvm/Target/TargetFrameInfo.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000029#include "llvm/Target/TargetLowering.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000030#include "llvm/Target/TargetMachine.h"
Chris Lattnerddae4bd2007-01-08 23:04:05 +000031#include "llvm/Target/TargetOptions.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000032#include "llvm/ADT/SmallPtrSet.h"
33#include "llvm/ADT/Statistic.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000034#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000035#include "llvm/Support/CommandLine.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000036#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000037#include "llvm/Support/ErrorHandling.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000038#include "llvm/Support/MathExtras.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000039#include <algorithm>
Dan Gohmanee335e32008-05-23 20:40:06 +000040#include <set>
Nate Begeman1d4d4142005-09-01 00:19:25 +000041using namespace llvm;
42
Chris Lattnercd3245a2006-12-19 22:41:21 +000043STATISTIC(NodesCombined , "Number of dag nodes combined");
44STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
45STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
Evan Cheng8b944d32009-05-28 00:35:15 +000046STATISTIC(OpsNarrowed , "Number of load/op/store narrowed");
Chris Lattnercd3245a2006-12-19 22:41:21 +000047
Nate Begeman1d4d4142005-09-01 00:19:25 +000048namespace {
Jim Laskey71382342006-10-07 23:37:56 +000049 static cl::opt<bool>
50 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000051 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000052
Jim Laskey07a27092006-10-18 19:08:31 +000053 static cl::opt<bool>
54 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
55 cl::desc("Include global information in alias analysis"));
56
Jim Laskeybc588b82006-10-05 15:07:25 +000057//------------------------------ DAGCombiner ---------------------------------//
58
Jim Laskey71382342006-10-07 23:37:56 +000059 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000060 SelectionDAG &DAG;
Dan Gohman79ce2762009-01-15 19:20:50 +000061 const TargetLowering &TLI;
Duncan Sands25cf2272008-11-24 14:53:14 +000062 CombineLevel Level;
Bill Wendling98a366d2009-04-29 23:29:43 +000063 CodeGenOpt::Level OptLevel;
Duncan Sands25cf2272008-11-24 14:53:14 +000064 bool LegalOperations;
65 bool LegalTypes;
Nate Begeman1d4d4142005-09-01 00:19:25 +000066
67 // Worklist of all of the nodes that need to be simplified.
Evan Cheng17a568b2008-08-29 22:21:44 +000068 std::vector<SDNode*> WorkList;
Nate Begeman1d4d4142005-09-01 00:19:25 +000069
Jim Laskeyc7c3f112006-10-16 20:52:31 +000070 // AA - Used for DAG load/store alias analysis.
71 AliasAnalysis &AA;
72
Nate Begeman1d4d4142005-09-01 00:19:25 +000073 /// AddUsersToWorkList - When an instruction is simplified, add all users of
74 /// the instruction to the work lists because they might get more simplified
75 /// now.
76 ///
77 void AddUsersToWorkList(SDNode *N) {
78 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000079 UI != UE; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +000080 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000081 }
82
Dan Gohman389079b2007-10-08 17:57:15 +000083 /// visit - call the node-specific routine that knows how to fold each
84 /// particular type of node.
Dan Gohman475871a2008-07-27 21:46:04 +000085 SDValue visit(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +000086
Chris Lattner24664722006-03-01 04:53:38 +000087 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +000088 /// AddToWorkList - Add to the work list making sure it's instance is at the
89 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +000090 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000091 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +000092 WorkList.push_back(N);
93 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000094
Chris Lattnerf8dc0612008-02-03 06:49:24 +000095 /// removeFromWorkList - remove all instances of N from the worklist.
96 ///
97 void removeFromWorkList(SDNode *N) {
98 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
99 WorkList.end());
Chris Lattner01a22022005-10-10 22:04:48 +0000100 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000101
Dan Gohman475871a2008-07-27 21:46:04 +0000102 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000103 bool AddTo = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000104
Dan Gohman475871a2008-07-27 21:46:04 +0000105 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Jim Laskey274062c2006-10-13 23:32:28 +0000106 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000107 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000108
Dan Gohman475871a2008-07-27 21:46:04 +0000109 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000110 bool AddTo = true) {
Dan Gohman475871a2008-07-27 21:46:04 +0000111 SDValue To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000112 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000113 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000114
115 void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO);
Scott Michelfdc40a02009-02-17 22:15:04 +0000116
117 private:
118
Chris Lattner012f2412006-02-17 21:58:01 +0000119 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000120 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000121 /// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000122 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000123 APInt Demanded = APInt::getAllOnesValue(Op.getValueSizeInBits());
124 return SimplifyDemandedBits(Op, Demanded);
125 }
126
Dan Gohman475871a2008-07-27 21:46:04 +0000127 bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded);
Chris Lattner87514ca2005-10-10 22:31:19 +0000128
Chris Lattner448f2192006-11-11 00:39:41 +0000129 bool CombineToPreIndexedLoadStore(SDNode *N);
130 bool CombineToPostIndexedLoadStore(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000131
132
Dan Gohman389079b2007-10-08 17:57:15 +0000133 /// combine - call the node-specific routine that knows how to fold each
134 /// particular type of node. If that doesn't do anything, try the
135 /// target-specific DAG combines.
Dan Gohman475871a2008-07-27 21:46:04 +0000136 SDValue combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000137
138 // Visitation implementation - Implement dag node combining for different
139 // node types. The semantics are as follows:
140 // Return Value:
Evan Cheng17a568b2008-08-29 22:21:44 +0000141 // SDValue.getNode() == 0 - No change was made
142 // SDValue.getNode() == N - N was replaced, is dead and has been handled.
143 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000144 //
Dan Gohman475871a2008-07-27 21:46:04 +0000145 SDValue visitTokenFactor(SDNode *N);
146 SDValue visitMERGE_VALUES(SDNode *N);
147 SDValue visitADD(SDNode *N);
148 SDValue visitSUB(SDNode *N);
149 SDValue visitADDC(SDNode *N);
150 SDValue visitADDE(SDNode *N);
151 SDValue visitMUL(SDNode *N);
152 SDValue visitSDIV(SDNode *N);
153 SDValue visitUDIV(SDNode *N);
154 SDValue visitSREM(SDNode *N);
155 SDValue visitUREM(SDNode *N);
156 SDValue visitMULHU(SDNode *N);
157 SDValue visitMULHS(SDNode *N);
158 SDValue visitSMUL_LOHI(SDNode *N);
159 SDValue visitUMUL_LOHI(SDNode *N);
160 SDValue visitSDIVREM(SDNode *N);
161 SDValue visitUDIVREM(SDNode *N);
162 SDValue visitAND(SDNode *N);
163 SDValue visitOR(SDNode *N);
164 SDValue visitXOR(SDNode *N);
165 SDValue SimplifyVBinOp(SDNode *N);
166 SDValue visitSHL(SDNode *N);
167 SDValue visitSRA(SDNode *N);
168 SDValue visitSRL(SDNode *N);
169 SDValue visitCTLZ(SDNode *N);
170 SDValue visitCTTZ(SDNode *N);
171 SDValue visitCTPOP(SDNode *N);
172 SDValue visitSELECT(SDNode *N);
173 SDValue visitSELECT_CC(SDNode *N);
174 SDValue visitSETCC(SDNode *N);
175 SDValue visitSIGN_EXTEND(SDNode *N);
176 SDValue visitZERO_EXTEND(SDNode *N);
177 SDValue visitANY_EXTEND(SDNode *N);
178 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
179 SDValue visitTRUNCATE(SDNode *N);
180 SDValue visitBIT_CONVERT(SDNode *N);
181 SDValue visitBUILD_PAIR(SDNode *N);
182 SDValue visitFADD(SDNode *N);
183 SDValue visitFSUB(SDNode *N);
184 SDValue visitFMUL(SDNode *N);
185 SDValue visitFDIV(SDNode *N);
186 SDValue visitFREM(SDNode *N);
187 SDValue visitFCOPYSIGN(SDNode *N);
188 SDValue visitSINT_TO_FP(SDNode *N);
189 SDValue visitUINT_TO_FP(SDNode *N);
190 SDValue visitFP_TO_SINT(SDNode *N);
191 SDValue visitFP_TO_UINT(SDNode *N);
192 SDValue visitFP_ROUND(SDNode *N);
193 SDValue visitFP_ROUND_INREG(SDNode *N);
194 SDValue visitFP_EXTEND(SDNode *N);
195 SDValue visitFNEG(SDNode *N);
196 SDValue visitFABS(SDNode *N);
197 SDValue visitBRCOND(SDNode *N);
198 SDValue visitBR_CC(SDNode *N);
199 SDValue visitLOAD(SDNode *N);
200 SDValue visitSTORE(SDNode *N);
201 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
202 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
203 SDValue visitBUILD_VECTOR(SDNode *N);
204 SDValue visitCONCAT_VECTORS(SDNode *N);
205 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000206
Dan Gohman475871a2008-07-27 21:46:04 +0000207 SDValue XformToShuffleWithZero(SDNode *N);
Bill Wendling35247c32009-01-30 00:45:56 +0000208 SDValue ReassociateOps(unsigned Opc, DebugLoc DL, SDValue LHS, SDValue RHS);
Scott Michelfdc40a02009-02-17 22:15:04 +0000209
Dan Gohman475871a2008-07-27 21:46:04 +0000210 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattnere70da202007-12-06 07:33:36 +0000211
Dan Gohman475871a2008-07-27 21:46:04 +0000212 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
213 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Bill Wendling836ca7d2009-01-30 23:59:18 +0000214 SDValue SimplifySelect(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2);
Scott Michelfdc40a02009-02-17 22:15:04 +0000215 SDValue SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2,
216 SDValue N3, ISD::CondCode CC,
Bill Wendling836ca7d2009-01-30 23:59:18 +0000217 bool NotExtCompare = false);
Owen Andersone50ed302009-08-10 22:56:29 +0000218 SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond,
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000219 DebugLoc DL, bool foldBooleans = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000220 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner5eee4272008-01-26 01:09:19 +0000221 unsigned HiOp);
Owen Andersone50ed302009-08-10 22:56:29 +0000222 SDValue CombineConsecutiveLoads(SDNode *N, EVT VT);
223 SDValue ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, EVT);
Dan Gohman475871a2008-07-27 21:46:04 +0000224 SDValue BuildSDIV(SDNode *N);
225 SDValue BuildUDIV(SDNode *N);
Bill Wendling317bd702009-01-30 21:14:50 +0000226 SDNode *MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL);
Dan Gohman475871a2008-07-27 21:46:04 +0000227 SDValue ReduceLoadWidth(SDNode *N);
Evan Cheng8b944d32009-05-28 00:35:15 +0000228 SDValue ReduceLoadOpStoreWidth(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000229
Dan Gohman475871a2008-07-27 21:46:04 +0000230 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Scott Michelfdc40a02009-02-17 22:15:04 +0000231
Jim Laskey6ff23e52006-10-04 16:53:27 +0000232 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
233 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000234 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
235 SmallVector<SDValue, 8> &Aliases);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000236
Jim Laskey096c22e2006-10-18 12:29:57 +0000237 /// isAlias - Return true if there is any possibility that the two addresses
238 /// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +0000239 bool isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +0000240 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman475871a2008-07-27 21:46:04 +0000241 SDValue Ptr2, int64_t Size2,
Bill Wendling836ca7d2009-01-30 23:59:18 +0000242 const Value *SrcValue2, int SrcValueOffset2) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000243
Jim Laskey7ca56af2006-10-11 13:47:09 +0000244 /// FindAliasInfo - Extracts the relevant alias information from the memory
245 /// node. Returns true if the operand was a load.
246 bool FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000247 SDValue &Ptr, int64_t &Size,
Bill Wendling836ca7d2009-01-30 23:59:18 +0000248 const Value *&SrcValue, int &SrcValueOffset) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000249
Jim Laskey279f0532006-09-25 16:29:54 +0000250 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000251 /// looking for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +0000252 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Duncan Sands92abc622009-01-31 15:50:11 +0000253
254 /// getShiftAmountTy - Returns a type large enough to hold any valid
255 /// shift amount - before type legalization these can be huge.
Owen Andersone50ed302009-08-10 22:56:29 +0000256 EVT getShiftAmountTy() {
Duncan Sands92abc622009-01-31 15:50:11 +0000257 return LegalTypes ? TLI.getShiftAmountTy() : TLI.getPointerTy();
258 }
259
Nate Begeman1d4d4142005-09-01 00:19:25 +0000260public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000261 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, CodeGenOpt::Level OL)
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000262 : DAG(D),
263 TLI(D.getTargetLoweringInfo()),
Duncan Sands25cf2272008-11-24 14:53:14 +0000264 Level(Unrestricted),
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000265 OptLevel(OL),
Duncan Sands25cf2272008-11-24 14:53:14 +0000266 LegalOperations(false),
267 LegalTypes(false),
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000268 AA(A) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000269
Nate Begeman1d4d4142005-09-01 00:19:25 +0000270 /// Run - runs the dag combiner on all nodes in the work list
Duncan Sands25cf2272008-11-24 14:53:14 +0000271 void Run(CombineLevel AtLevel);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000272 };
273}
274
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000275
276namespace {
277/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
278/// nodes from the worklist.
Scott Michelfdc40a02009-02-17 22:15:04 +0000279class VISIBILITY_HIDDEN WorkListRemover :
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000280 public SelectionDAG::DAGUpdateListener {
281 DAGCombiner &DC;
282public:
Dan Gohmanb5660dc2008-02-20 16:44:09 +0000283 explicit WorkListRemover(DAGCombiner &dc) : DC(dc) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000284
Duncan Sandsedfcf592008-06-11 11:42:12 +0000285 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000286 DC.removeFromWorkList(N);
287 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000288
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000289 virtual void NodeUpdated(SDNode *N) {
290 // Ignore updates.
291 }
292};
293}
294
Chris Lattner24664722006-03-01 04:53:38 +0000295//===----------------------------------------------------------------------===//
296// TargetLowering::DAGCombinerInfo implementation
297//===----------------------------------------------------------------------===//
298
299void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
300 ((DAGCombiner*)DC)->AddToWorkList(N);
301}
302
Dan Gohman475871a2008-07-27 21:46:04 +0000303SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000304CombineTo(SDNode *N, const std::vector<SDValue> &To, bool AddTo) {
305 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000306}
307
Dan Gohman475871a2008-07-27 21:46:04 +0000308SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000309CombineTo(SDNode *N, SDValue Res, bool AddTo) {
310 return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000311}
312
313
Dan Gohman475871a2008-07-27 21:46:04 +0000314SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000315CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) {
316 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000317}
318
Dan Gohmane5af2d32009-01-29 01:59:02 +0000319void TargetLowering::DAGCombinerInfo::
320CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
321 return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO);
322}
Chris Lattner24664722006-03-01 04:53:38 +0000323
Chris Lattner24664722006-03-01 04:53:38 +0000324//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000325// Helper Functions
326//===----------------------------------------------------------------------===//
327
328/// isNegatibleForFree - Return 1 if we can compute the negated form of the
329/// specified expression for the same cost as the expression itself, or 2 if we
330/// can compute the negated form more cheaply than the expression itself.
Duncan Sands25cf2272008-11-24 14:53:14 +0000331static char isNegatibleForFree(SDValue Op, bool LegalOperations,
Chris Lattner0254e702008-02-26 07:04:54 +0000332 unsigned Depth = 0) {
Dale Johannesendb44bf82007-10-16 23:38:29 +0000333 // No compile time optimizations on this type.
Owen Anderson825b72b2009-08-11 20:47:22 +0000334 if (Op.getValueType() == MVT::ppcf128)
Dale Johannesendb44bf82007-10-16 23:38:29 +0000335 return 0;
336
Chris Lattner29446522007-05-14 22:04:50 +0000337 // fneg is removable even if it has multiple uses.
338 if (Op.getOpcode() == ISD::FNEG) return 2;
Scott Michelfdc40a02009-02-17 22:15:04 +0000339
Chris Lattner29446522007-05-14 22:04:50 +0000340 // Don't allow anything with multiple uses.
341 if (!Op.hasOneUse()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000342
Chris Lattner3adf9512007-05-25 02:19:06 +0000343 // Don't recurse exponentially.
344 if (Depth > 6) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000345
Chris Lattner29446522007-05-14 22:04:50 +0000346 switch (Op.getOpcode()) {
347 default: return false;
348 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000349 // Don't invert constant FP values after legalize. The negated constant
350 // isn't necessarily legal.
Duncan Sands25cf2272008-11-24 14:53:14 +0000351 return LegalOperations ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000352 case ISD::FADD:
353 // FIXME: determine better conditions for this xform.
354 if (!UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000355
Bill Wendlingd34470c2009-01-30 23:10:18 +0000356 // fold (fsub (fadd A, B)) -> (fsub (fneg A), B)
Duncan Sands25cf2272008-11-24 14:53:14 +0000357 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000358 return V;
Bill Wendlingd34470c2009-01-30 23:10:18 +0000359 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Duncan Sands25cf2272008-11-24 14:53:14 +0000360 return isNegatibleForFree(Op.getOperand(1), LegalOperations, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000361 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000362 // We can't turn -(A-B) into B-A when we honor signed zeros.
Chris Lattner29446522007-05-14 22:04:50 +0000363 if (!UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000364
Bill Wendlingd34470c2009-01-30 23:10:18 +0000365 // fold (fneg (fsub A, B)) -> (fsub B, A)
Chris Lattner29446522007-05-14 22:04:50 +0000366 return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000367
Chris Lattner29446522007-05-14 22:04:50 +0000368 case ISD::FMUL:
369 case ISD::FDIV:
370 if (HonorSignDependentRoundingFPMath()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000371
Bill Wendlingd34470c2009-01-30 23:10:18 +0000372 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y))
Duncan Sands25cf2272008-11-24 14:53:14 +0000373 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000374 return V;
Scott Michelfdc40a02009-02-17 22:15:04 +0000375
Duncan Sands25cf2272008-11-24 14:53:14 +0000376 return isNegatibleForFree(Op.getOperand(1), LegalOperations, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000377
Chris Lattner29446522007-05-14 22:04:50 +0000378 case ISD::FP_EXTEND:
379 case ISD::FP_ROUND:
380 case ISD::FSIN:
Duncan Sands25cf2272008-11-24 14:53:14 +0000381 return isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000382 }
383}
384
385/// GetNegatedExpression - If isNegatibleForFree returns true, this function
386/// returns the newly negated expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000387static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000388 bool LegalOperations, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000389 // fneg is removable even if it has multiple uses.
390 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000391
Chris Lattner29446522007-05-14 22:04:50 +0000392 // Don't allow anything with multiple uses.
393 assert(Op.hasOneUse() && "Unknown reuse!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000394
Chris Lattner3adf9512007-05-25 02:19:06 +0000395 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000396 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000397 default: llvm_unreachable("Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000398 case ISD::ConstantFP: {
399 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
400 V.changeSign();
401 return DAG.getConstantFP(V, Op.getValueType());
402 }
Chris Lattner29446522007-05-14 22:04:50 +0000403 case ISD::FADD:
404 // FIXME: determine better conditions for this xform.
405 assert(UnsafeFPMath);
Scott Michelfdc40a02009-02-17 22:15:04 +0000406
Bill Wendlingd34470c2009-01-30 23:10:18 +0000407 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Duncan Sands25cf2272008-11-24 14:53:14 +0000408 if (isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Bill Wendling35247c32009-01-30 00:45:56 +0000409 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000410 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000411 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000412 Op.getOperand(1));
Bill Wendlingd34470c2009-01-30 23:10:18 +0000413 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Bill Wendling35247c32009-01-30 00:45:56 +0000414 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000415 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000416 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000417 Op.getOperand(0));
418 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000419 // We can't turn -(A-B) into B-A when we honor signed zeros.
Chris Lattner29446522007-05-14 22:04:50 +0000420 assert(UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000421
Bill Wendlingd34470c2009-01-30 23:10:18 +0000422 // fold (fneg (fsub 0, B)) -> B
Dan Gohman23ff1822007-07-02 15:48:56 +0000423 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000424 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000425 return Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000426
Bill Wendlingd34470c2009-01-30 23:10:18 +0000427 // fold (fneg (fsub A, B)) -> (fsub B, A)
Bill Wendling35247c32009-01-30 00:45:56 +0000428 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
429 Op.getOperand(1), Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +0000430
Chris Lattner29446522007-05-14 22:04:50 +0000431 case ISD::FMUL:
432 case ISD::FDIV:
433 assert(!HonorSignDependentRoundingFPMath());
Scott Michelfdc40a02009-02-17 22:15:04 +0000434
Bill Wendlingd34470c2009-01-30 23:10:18 +0000435 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y)
Duncan Sands25cf2272008-11-24 14:53:14 +0000436 if (isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Bill Wendling35247c32009-01-30 00:45:56 +0000437 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000438 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000439 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000440 Op.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000441
Bill Wendlingd34470c2009-01-30 23:10:18 +0000442 // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y))
Bill Wendling35247c32009-01-30 00:45:56 +0000443 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Chris Lattner29446522007-05-14 22:04:50 +0000444 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000445 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000446 LegalOperations, Depth+1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000447
Chris Lattner29446522007-05-14 22:04:50 +0000448 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000449 case ISD::FSIN:
Bill Wendling35247c32009-01-30 00:45:56 +0000450 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000451 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000452 LegalOperations, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000453 case ISD::FP_ROUND:
Bill Wendling35247c32009-01-30 00:45:56 +0000454 return DAG.getNode(ISD::FP_ROUND, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000455 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000456 LegalOperations, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000457 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000458 }
459}
Chris Lattner24664722006-03-01 04:53:38 +0000460
461
Nate Begeman4ebd8052005-09-01 23:24:04 +0000462// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
463// that selects between the values 1 and 0, making it equivalent to a setcc.
Scott Michelfdc40a02009-02-17 22:15:04 +0000464// Also, set the incoming LHS, RHS, and CC references to the appropriate
Nate Begeman646d7e22005-09-02 21:18:40 +0000465// nodes based on the type of node we are checking. This simplifies life a
466// bit for the callers.
Dan Gohman475871a2008-07-27 21:46:04 +0000467static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
468 SDValue &CC) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000469 if (N.getOpcode() == ISD::SETCC) {
470 LHS = N.getOperand(0);
471 RHS = N.getOperand(1);
472 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000473 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000474 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000475 if (N.getOpcode() == ISD::SELECT_CC &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000476 N.getOperand(2).getOpcode() == ISD::Constant &&
477 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000478 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000479 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
480 LHS = N.getOperand(0);
481 RHS = N.getOperand(1);
482 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000483 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000484 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000485 return false;
486}
487
Nate Begeman99801192005-09-07 23:25:52 +0000488// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
489// one use. If this is true, it allows the users to invert the operation for
490// free when it is profitable to do so.
Dan Gohman475871a2008-07-27 21:46:04 +0000491static bool isOneUseSetCC(SDValue N) {
492 SDValue N0, N1, N2;
Gabor Greifba36cb52008-08-28 21:40:38 +0000493 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000494 return true;
495 return false;
496}
497
Bill Wendling35247c32009-01-30 00:45:56 +0000498SDValue DAGCombiner::ReassociateOps(unsigned Opc, DebugLoc DL,
499 SDValue N0, SDValue N1) {
Owen Andersone50ed302009-08-10 22:56:29 +0000500 EVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000501 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
502 if (isa<ConstantSDNode>(N1)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000503 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000504 SDValue OpNode =
505 DAG.FoldConstantArithmetic(Opc, VT,
506 cast<ConstantSDNode>(N0.getOperand(1)),
507 cast<ConstantSDNode>(N1));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000508 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000509 } else if (N0.hasOneUse()) {
Bill Wendling35247c32009-01-30 00:45:56 +0000510 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
511 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
512 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000513 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000514 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000515 }
516 }
Bill Wendling35247c32009-01-30 00:45:56 +0000517
Nate Begemancd4d58c2006-02-03 06:46:56 +0000518 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
519 if (isa<ConstantSDNode>(N0)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000520 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000521 SDValue OpNode =
522 DAG.FoldConstantArithmetic(Opc, VT,
523 cast<ConstantSDNode>(N1.getOperand(1)),
524 cast<ConstantSDNode>(N0));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000525 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000526 } else if (N1.hasOneUse()) {
Bill Wendling35247c32009-01-30 00:45:56 +0000527 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
Bill Wendlingd69c3142009-01-30 02:23:43 +0000528 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000529 N1.getOperand(0), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000530 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000531 return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000532 }
533 }
Bill Wendling35247c32009-01-30 00:45:56 +0000534
Dan Gohman475871a2008-07-27 21:46:04 +0000535 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000536}
537
Dan Gohman475871a2008-07-27 21:46:04 +0000538SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
539 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000540 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
541 ++NodesCombined;
542 DOUT << "\nReplacing.1 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +0000543 DOUT << "\nWith: "; DEBUG(To[0].getNode()->dump(&DAG));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000544 DOUT << " and " << NumTo-1 << " other values\n";
Dan Gohman764fd0c2009-01-21 15:17:51 +0000545 DEBUG(for (unsigned i = 0, e = NumTo; i != e; ++i)
546 assert(N->getValueType(i) == To[i].getValueType() &&
547 "Cannot combine value to value of different type!"));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000548 WorkListRemover DeadNodes(*this);
549 DAG.ReplaceAllUsesWith(N, To, &DeadNodes);
Scott Michelfdc40a02009-02-17 22:15:04 +0000550
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000551 if (AddTo) {
552 // Push the new nodes and any users onto the worklist
553 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattnerd1980a52009-03-12 06:52:53 +0000554 if (To[i].getNode()) {
555 AddToWorkList(To[i].getNode());
556 AddUsersToWorkList(To[i].getNode());
557 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000558 }
559 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000560
Dan Gohmandbe664a2009-01-19 21:44:21 +0000561 // Finally, if the node is now dead, remove it from the graph. The node
562 // may not be dead if the replacement process recursively simplified to
563 // something else needing this node.
564 if (N->use_empty()) {
565 // Nodes can be reintroduced into the worklist. Make sure we do not
566 // process a node that has been replaced.
567 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000568
Dan Gohmandbe664a2009-01-19 21:44:21 +0000569 // Finally, since the node is now dead, remove it from the graph.
570 DAG.DeleteNode(N);
571 }
Dan Gohman475871a2008-07-27 21:46:04 +0000572 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000573}
574
Dan Gohmane5af2d32009-01-29 01:59:02 +0000575void
576DAGCombiner::CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &
577 TLO) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000578 // Replace all uses. If any nodes become isomorphic to other nodes and
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000579 // are deleted, make sure to remove them from our worklist.
580 WorkListRemover DeadNodes(*this);
581 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000582
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000583 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000584 AddToWorkList(TLO.New.getNode());
585 AddUsersToWorkList(TLO.New.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000586
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000587 // Finally, if the node is now dead, remove it from the graph. The node
588 // may not be dead if the replacement process recursively simplified to
589 // something else needing this node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000590 if (TLO.Old.getNode()->use_empty()) {
591 removeFromWorkList(TLO.Old.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000592
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000593 // If the operands of this node are only used by the node, they will now
594 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greifba36cb52008-08-28 21:40:38 +0000595 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
596 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
597 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000598
Gabor Greifba36cb52008-08-28 21:40:38 +0000599 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000600 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000601}
602
603/// SimplifyDemandedBits - Check the specified integer node value to see if
604/// it can be simplified or if things it uses can be simplified by bit
605/// propagation. If so, return true.
606bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
607 TargetLowering::TargetLoweringOpt TLO(DAG);
608 APInt KnownZero, KnownOne;
609 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
610 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000611
Dan Gohmane5af2d32009-01-29 01:59:02 +0000612 // Revisit the node.
613 AddToWorkList(Op.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000614
Dan Gohmane5af2d32009-01-29 01:59:02 +0000615 // Replace the old value with the new one.
616 ++NodesCombined;
617 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.getNode()->dump(&DAG));
618 DOUT << "\nWith: "; DEBUG(TLO.New.getNode()->dump(&DAG));
619 DOUT << '\n';
Scott Michelfdc40a02009-02-17 22:15:04 +0000620
Dan Gohmane5af2d32009-01-29 01:59:02 +0000621 CommitTargetLoweringOpt(TLO);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000622 return true;
623}
624
Chris Lattner29446522007-05-14 22:04:50 +0000625//===----------------------------------------------------------------------===//
626// Main DAG Combiner implementation
627//===----------------------------------------------------------------------===//
628
Duncan Sands25cf2272008-11-24 14:53:14 +0000629void DAGCombiner::Run(CombineLevel AtLevel) {
630 // set the instance variables, so that the various visit routines may use it.
631 Level = AtLevel;
632 LegalOperations = Level >= NoIllegalOperations;
633 LegalTypes = Level >= NoIllegalTypes;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000634
Evan Cheng17a568b2008-08-29 22:21:44 +0000635 // Add all the dag nodes to the worklist.
636 WorkList.reserve(DAG.allnodes_size());
637 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
638 E = DAG.allnodes_end(); I != E; ++I)
639 WorkList.push_back(I);
Duncan Sands25cf2272008-11-24 14:53:14 +0000640
Evan Cheng17a568b2008-08-29 22:21:44 +0000641 // Create a dummy node (which is not added to allnodes), that adds a reference
642 // to the root node, preventing it from being deleted, and tracking any
643 // changes of the root.
644 HandleSDNode Dummy(DAG.getRoot());
Scott Michelfdc40a02009-02-17 22:15:04 +0000645
Jim Laskey26f7fa72006-10-17 19:33:52 +0000646 // The root of the dag may dangle to deleted nodes until the dag combiner is
647 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +0000648 DAG.setRoot(SDValue());
Scott Michelfdc40a02009-02-17 22:15:04 +0000649
Evan Cheng17a568b2008-08-29 22:21:44 +0000650 // while the worklist isn't empty, inspect the node on the end of it and
651 // try and combine it.
652 while (!WorkList.empty()) {
653 SDNode *N = WorkList.back();
654 WorkList.pop_back();
Scott Michelfdc40a02009-02-17 22:15:04 +0000655
Evan Cheng17a568b2008-08-29 22:21:44 +0000656 // If N has no uses, it is dead. Make sure to revisit all N's operands once
657 // N is deleted from the DAG, since they too may now be dead or may have a
658 // reduced number of uses, allowing other xforms.
659 if (N->use_empty() && N != &Dummy) {
660 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
661 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000662
Evan Cheng17a568b2008-08-29 22:21:44 +0000663 DAG.DeleteNode(N);
664 continue;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000665 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000666
Evan Cheng17a568b2008-08-29 22:21:44 +0000667 SDValue RV = combine(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000668
Evan Cheng17a568b2008-08-29 22:21:44 +0000669 if (RV.getNode() == 0)
670 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +0000671
Evan Cheng17a568b2008-08-29 22:21:44 +0000672 ++NodesCombined;
Scott Michelfdc40a02009-02-17 22:15:04 +0000673
Evan Cheng17a568b2008-08-29 22:21:44 +0000674 // If we get back the same node we passed in, rather than a new node or
675 // zero, we know that the node must have defined multiple values and
Scott Michelfdc40a02009-02-17 22:15:04 +0000676 // CombineTo was used. Since CombineTo takes care of the worklist
Evan Cheng17a568b2008-08-29 22:21:44 +0000677 // mechanics for us, we have no work to do in this case.
678 if (RV.getNode() == N)
679 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +0000680
Evan Cheng17a568b2008-08-29 22:21:44 +0000681 assert(N->getOpcode() != ISD::DELETED_NODE &&
682 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
683 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +0000684
Evan Cheng17a568b2008-08-29 22:21:44 +0000685 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
686 DOUT << "\nWith: "; DEBUG(RV.getNode()->dump(&DAG));
687 DOUT << '\n';
688 WorkListRemover DeadNodes(*this);
689 if (N->getNumValues() == RV.getNode()->getNumValues())
690 DAG.ReplaceAllUsesWith(N, RV.getNode(), &DeadNodes);
691 else {
692 assert(N->getValueType(0) == RV.getValueType() &&
693 N->getNumValues() == 1 && "Type mismatch");
694 SDValue OpV = RV;
695 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
696 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000697
Evan Cheng17a568b2008-08-29 22:21:44 +0000698 // Push the new node and any users onto the worklist
699 AddToWorkList(RV.getNode());
700 AddUsersToWorkList(RV.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000701
Evan Cheng17a568b2008-08-29 22:21:44 +0000702 // Add any uses of the old node to the worklist in case this node is the
703 // last one that uses them. They may become dead after this node is
704 // deleted.
705 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
706 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000707
Dan Gohmandbe664a2009-01-19 21:44:21 +0000708 // Finally, if the node is now dead, remove it from the graph. The node
709 // may not be dead if the replacement process recursively simplified to
710 // something else needing this node.
711 if (N->use_empty()) {
712 // Nodes can be reintroduced into the worklist. Make sure we do not
713 // process a node that has been replaced.
714 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000715
Dan Gohmandbe664a2009-01-19 21:44:21 +0000716 // Finally, since the node is now dead, remove it from the graph.
717 DAG.DeleteNode(N);
718 }
Evan Cheng17a568b2008-08-29 22:21:44 +0000719 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000720
Chris Lattner95038592005-10-05 06:35:28 +0000721 // If the root changed (e.g. it was a dead load, update the root).
722 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000723}
724
Dan Gohman475871a2008-07-27 21:46:04 +0000725SDValue DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000726 switch(N->getOpcode()) {
727 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000728 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000729 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000730 case ISD::ADD: return visitADD(N);
731 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000732 case ISD::ADDC: return visitADDC(N);
733 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000734 case ISD::MUL: return visitMUL(N);
735 case ISD::SDIV: return visitSDIV(N);
736 case ISD::UDIV: return visitUDIV(N);
737 case ISD::SREM: return visitSREM(N);
738 case ISD::UREM: return visitUREM(N);
739 case ISD::MULHU: return visitMULHU(N);
740 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000741 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
742 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
743 case ISD::SDIVREM: return visitSDIVREM(N);
744 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000745 case ISD::AND: return visitAND(N);
746 case ISD::OR: return visitOR(N);
747 case ISD::XOR: return visitXOR(N);
748 case ISD::SHL: return visitSHL(N);
749 case ISD::SRA: return visitSRA(N);
750 case ISD::SRL: return visitSRL(N);
751 case ISD::CTLZ: return visitCTLZ(N);
752 case ISD::CTTZ: return visitCTTZ(N);
753 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000754 case ISD::SELECT: return visitSELECT(N);
755 case ISD::SELECT_CC: return visitSELECT_CC(N);
756 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000757 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
758 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000759 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000760 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
761 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000762 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +0000763 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000764 case ISD::FADD: return visitFADD(N);
765 case ISD::FSUB: return visitFSUB(N);
766 case ISD::FMUL: return visitFMUL(N);
767 case ISD::FDIV: return visitFDIV(N);
768 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000769 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000770 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
771 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
772 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
773 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
774 case ISD::FP_ROUND: return visitFP_ROUND(N);
775 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
776 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
777 case ISD::FNEG: return visitFNEG(N);
778 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000779 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000780 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000781 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000782 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000783 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +0000784 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000785 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
786 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000787 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000788 }
Dan Gohman475871a2008-07-27 21:46:04 +0000789 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000790}
791
Dan Gohman475871a2008-07-27 21:46:04 +0000792SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +0000793 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000794
795 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +0000796 if (RV.getNode() == 0) {
Dan Gohman389079b2007-10-08 17:57:15 +0000797 assert(N->getOpcode() != ISD::DELETED_NODE &&
798 "Node was deleted but visit returned NULL!");
799
800 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
801 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
802
803 // Expose the DAG combiner to the target combiner impls.
Scott Michelfdc40a02009-02-17 22:15:04 +0000804 TargetLowering::DAGCombinerInfo
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +0000805 DagCombineInfo(DAG, !LegalTypes, !LegalOperations, false, this);
Dan Gohman389079b2007-10-08 17:57:15 +0000806
807 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
808 }
809 }
810
Scott Michelfdc40a02009-02-17 22:15:04 +0000811 // If N is a commutative binary node, try commuting it to enable more
Evan Cheng08b11732008-03-22 01:55:50 +0000812 // sdisel CSE.
Scott Michelfdc40a02009-02-17 22:15:04 +0000813 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +0000814 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
815 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +0000816 SDValue N0 = N->getOperand(0);
817 SDValue N1 = N->getOperand(1);
Bill Wendling5c71acf2009-01-30 01:13:16 +0000818
Evan Cheng08b11732008-03-22 01:55:50 +0000819 // Constant operands are canonicalized to RHS.
820 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000821 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +0000822 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
823 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +0000824 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +0000825 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +0000826 }
827 }
828
Dan Gohman389079b2007-10-08 17:57:15 +0000829 return RV;
Scott Michelfdc40a02009-02-17 22:15:04 +0000830}
Dan Gohman389079b2007-10-08 17:57:15 +0000831
Chris Lattner6270f682006-10-08 22:57:01 +0000832/// getInputChainForNode - Given a node, return its input chain if it has one,
833/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000834static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000835 if (unsigned NumOps = N->getNumOperands()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000836 if (N->getOperand(0).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +0000837 return N->getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000838 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +0000839 return N->getOperand(NumOps-1);
840 for (unsigned i = 1; i < NumOps-1; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +0000841 if (N->getOperand(i).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +0000842 return N->getOperand(i);
843 }
Bill Wendling5c71acf2009-01-30 01:13:16 +0000844 return SDValue();
Chris Lattner6270f682006-10-08 22:57:01 +0000845}
846
Dan Gohman475871a2008-07-27 21:46:04 +0000847SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000848 // If N has two operands, where one has an input chain equal to the other,
849 // the 'other' chain is redundant.
850 if (N->getNumOperands() == 2) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000851 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +0000852 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000853 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +0000854 return N->getOperand(1);
855 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000856
Chris Lattnerc76d4412007-05-16 06:37:59 +0000857 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +0000858 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +0000859 SmallPtrSet<SDNode*, 16> SeenOps;
Chris Lattnerc76d4412007-05-16 06:37:59 +0000860 bool Changed = false; // If we should replace this token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +0000861
Jim Laskey6ff23e52006-10-04 16:53:27 +0000862 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000863 TFs.push_back(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000864
Jim Laskey71382342006-10-07 23:37:56 +0000865 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000866 // encountered.
867 for (unsigned i = 0; i < TFs.size(); ++i) {
868 SDNode *TF = TFs[i];
Scott Michelfdc40a02009-02-17 22:15:04 +0000869
Jim Laskey6ff23e52006-10-04 16:53:27 +0000870 // Check each of the operands.
871 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000872 SDValue Op = TF->getOperand(i);
Scott Michelfdc40a02009-02-17 22:15:04 +0000873
Jim Laskey6ff23e52006-10-04 16:53:27 +0000874 switch (Op.getOpcode()) {
875 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000876 // Entry tokens don't need to be added to the list. They are
877 // rededundant.
878 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000879 break;
Scott Michelfdc40a02009-02-17 22:15:04 +0000880
Jim Laskey6ff23e52006-10-04 16:53:27 +0000881 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000882 if ((CombinerAA || Op.hasOneUse()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000883 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000884 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +0000885 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +0000886 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +0000887 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +0000888 Changed = true;
889 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000890 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000891 // Fall thru
Scott Michelfdc40a02009-02-17 22:15:04 +0000892
Jim Laskey6ff23e52006-10-04 16:53:27 +0000893 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000894 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +0000895 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +0000896 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000897 else
898 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000899 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000900 }
901 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000902 }
903
Dan Gohman475871a2008-07-27 21:46:04 +0000904 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000905
906 // If we've change things around then replace token factor.
907 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +0000908 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000909 // The entry token is the only possible outcome.
910 Result = DAG.getEntryNode();
911 } else {
912 // New and improved token factor.
Bill Wendling5c71acf2009-01-30 01:13:16 +0000913 Result = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000914 MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000915 }
Bill Wendling5c71acf2009-01-30 01:13:16 +0000916
Jim Laskey274062c2006-10-13 23:32:28 +0000917 // Don't add users to work list.
918 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000919 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000920
Jim Laskey6ff23e52006-10-04 16:53:27 +0000921 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000922}
923
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000924/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +0000925SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000926 WorkListRemover DeadNodes(*this);
Dan Gohman00edf392009-08-10 23:43:19 +0000927 // Replacing results may cause a different MERGE_VALUES to suddenly
928 // be CSE'd with N, and carry its uses with it. Iterate until no
929 // uses remain, to ensure that the node can be safely deleted.
930 do {
931 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
932 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i),
933 &DeadNodes);
934 } while (!N->use_empty());
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000935 removeFromWorkList(N);
936 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000937 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000938}
939
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000940static
Bill Wendlingd69c3142009-01-30 02:23:43 +0000941SDValue combineShlAddConstant(DebugLoc DL, SDValue N0, SDValue N1,
942 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +0000943 EVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000944 SDValue N00 = N0.getOperand(0);
945 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000946 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Bill Wendlingd69c3142009-01-30 02:23:43 +0000947
Gabor Greifba36cb52008-08-28 21:40:38 +0000948 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000949 isa<ConstantSDNode>(N00.getOperand(1))) {
Bill Wendlingd69c3142009-01-30 02:23:43 +0000950 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
951 N0 = DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT,
952 DAG.getNode(ISD::SHL, N00.getDebugLoc(), VT,
953 N00.getOperand(0), N01),
954 DAG.getNode(ISD::SHL, N01.getDebugLoc(), VT,
955 N00.getOperand(1), N01));
956 return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000957 }
Bill Wendlingd69c3142009-01-30 02:23:43 +0000958
Dan Gohman475871a2008-07-27 21:46:04 +0000959 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000960}
961
Dan Gohman475871a2008-07-27 21:46:04 +0000962SDValue DAGCombiner::visitADD(SDNode *N) {
963 SDValue N0 = N->getOperand(0);
964 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000965 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
966 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +0000967 EVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000968
969 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +0000970 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000971 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +0000972 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +0000973 }
Bill Wendling2476e5d2008-12-10 22:36:00 +0000974
Dan Gohman613e0d82007-07-03 14:03:57 +0000975 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +0000976 if (N0.getOpcode() == ISD::UNDEF)
977 return N0;
978 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +0000979 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000980 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000981 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +0000982 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +0000983 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000984 if (N0C && !N1C)
Bill Wendlingf4eb2262009-01-30 02:31:17 +0000985 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000986 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000987 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000988 return N0;
Dan Gohman6520e202008-10-18 02:06:02 +0000989 // fold (add Sym, c) -> Sym+c
990 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +0000991 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman6520e202008-10-18 02:06:02 +0000992 GA->getOpcode() == ISD::GlobalAddress)
993 return DAG.getGlobalAddress(GA->getGlobal(), VT,
994 GA->getOffset() +
995 (uint64_t)N1C->getSExtValue());
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000996 // fold ((c1-A)+c2) -> (c1+c2)-A
997 if (N1C && N0.getOpcode() == ISD::SUB)
998 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
Bill Wendlingf4eb2262009-01-30 02:31:17 +0000999 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
Dan Gohman002e5d02008-03-13 22:13:53 +00001000 DAG.getConstant(N1C->getAPIntValue()+
1001 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001002 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +00001003 // reassociate add
Bill Wendling35247c32009-01-30 00:45:56 +00001004 SDValue RADD = ReassociateOps(ISD::ADD, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001005 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001006 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001007 // fold ((0-A) + B) -> B-A
1008 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1009 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001010 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001011 // fold (A + (0-B)) -> A-B
1012 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1013 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001014 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001015 // fold (A+(B-A)) -> B
1016 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001017 return N1.getOperand(0);
Dale Johannesen56eca912008-11-27 00:43:21 +00001018 // fold ((B-A)+A) -> B
1019 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1020 return N0.getOperand(0);
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001021 // fold (A+(B-(A+C))) to (B-C)
1022 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001023 N0 == N1.getOperand(1).getOperand(0))
1024 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001025 N1.getOperand(1).getOperand(1));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001026 // fold (A+(B-(C+A))) to (B-C)
1027 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001028 N0 == N1.getOperand(1).getOperand(1))
1029 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001030 N1.getOperand(1).getOperand(0));
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001031 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesen34d79852008-12-02 18:40:40 +00001032 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1033 N1.getOperand(0).getOpcode() == ISD::SUB &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001034 N0 == N1.getOperand(0).getOperand(1))
1035 return DAG.getNode(N1.getOpcode(), N->getDebugLoc(), VT,
1036 N1.getOperand(0).getOperand(0), N1.getOperand(1));
Dale Johannesen34d79852008-12-02 18:40:40 +00001037
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001038 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1039 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1040 SDValue N00 = N0.getOperand(0);
1041 SDValue N01 = N0.getOperand(1);
1042 SDValue N10 = N1.getOperand(0);
1043 SDValue N11 = N1.getOperand(1);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001044
1045 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10))
1046 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1047 DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT, N00, N10),
1048 DAG.getNode(ISD::ADD, N1.getDebugLoc(), VT, N01, N11));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001049 }
Chris Lattner947c2892006-03-13 06:51:27 +00001050
Dan Gohman475871a2008-07-27 21:46:04 +00001051 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1052 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001053
Chris Lattner947c2892006-03-13 06:51:27 +00001054 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001055 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001056 APInt LHSZero, LHSOne;
1057 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001058 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001059 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001060
Dan Gohman948d8ea2008-02-20 16:33:30 +00001061 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001062 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001063
Chris Lattner947c2892006-03-13 06:51:27 +00001064 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1065 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1066 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1067 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001068 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1);
Chris Lattner947c2892006-03-13 06:51:27 +00001069 }
1070 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001071
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001072 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001073 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001074 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001075 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001076 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001077 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001078 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001079 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001080 }
1081
Dan Gohman475871a2008-07-27 21:46:04 +00001082 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001083}
1084
Dan Gohman475871a2008-07-27 21:46:04 +00001085SDValue DAGCombiner::visitADDC(SDNode *N) {
1086 SDValue N0 = N->getOperand(0);
1087 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001088 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1089 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001090 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001091
Chris Lattner91153682007-03-04 20:03:15 +00001092 // If the flag result is dead, turn this into an ADD.
1093 if (N->hasNUsesOfValue(0, 1))
Bill Wendling14036c02009-01-30 02:38:00 +00001094 return CombineTo(N, DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0),
Dale Johannesen874ae252009-06-02 03:12:52 +00001095 DAG.getNode(ISD::CARRY_FALSE,
Owen Anderson825b72b2009-08-11 20:47:22 +00001096 N->getDebugLoc(), MVT::Flag));
Scott Michelfdc40a02009-02-17 22:15:04 +00001097
Chris Lattner91153682007-03-04 20:03:15 +00001098 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001099 if (N0C && !N1C)
Bill Wendling14036c02009-01-30 02:38:00 +00001100 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001101
Chris Lattnerb6541762007-03-04 20:40:38 +00001102 // fold (addc x, 0) -> x + no carry out
1103 if (N1C && N1C->isNullValue())
Dale Johannesen874ae252009-06-02 03:12:52 +00001104 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE,
Owen Anderson825b72b2009-08-11 20:47:22 +00001105 N->getDebugLoc(), MVT::Flag));
Scott Michelfdc40a02009-02-17 22:15:04 +00001106
Dale Johannesen874ae252009-06-02 03:12:52 +00001107 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001108 APInt LHSZero, LHSOne;
1109 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001110 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001111 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Bill Wendling14036c02009-01-30 02:38:00 +00001112
Dan Gohman948d8ea2008-02-20 16:33:30 +00001113 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001114 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001115
Chris Lattnerb6541762007-03-04 20:40:38 +00001116 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1117 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1118 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1119 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
Bill Wendling14036c02009-01-30 02:38:00 +00001120 return CombineTo(N, DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001121 DAG.getNode(ISD::CARRY_FALSE,
Owen Anderson825b72b2009-08-11 20:47:22 +00001122 N->getDebugLoc(), MVT::Flag));
Chris Lattnerb6541762007-03-04 20:40:38 +00001123 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001124
Dan Gohman475871a2008-07-27 21:46:04 +00001125 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001126}
1127
Dan Gohman475871a2008-07-27 21:46:04 +00001128SDValue DAGCombiner::visitADDE(SDNode *N) {
1129 SDValue N0 = N->getOperand(0);
1130 SDValue N1 = N->getOperand(1);
1131 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001132 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1133 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001134
Chris Lattner91153682007-03-04 20:03:15 +00001135 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001136 if (N0C && !N1C)
Bill Wendling14036c02009-01-30 02:38:00 +00001137 return DAG.getNode(ISD::ADDE, N->getDebugLoc(), N->getVTList(),
1138 N1, N0, CarryIn);
Scott Michelfdc40a02009-02-17 22:15:04 +00001139
Chris Lattnerb6541762007-03-04 20:40:38 +00001140 // fold (adde x, y, false) -> (addc x, y)
Dale Johannesen874ae252009-06-02 03:12:52 +00001141 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
1142 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001143
Dan Gohman475871a2008-07-27 21:46:04 +00001144 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001145}
1146
Dan Gohman475871a2008-07-27 21:46:04 +00001147SDValue DAGCombiner::visitSUB(SDNode *N) {
1148 SDValue N0 = N->getOperand(0);
1149 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001150 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1151 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001152 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001153
Dan Gohman7f321562007-06-25 16:23:39 +00001154 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001155 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001156 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001157 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001158 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001159
Chris Lattner854077d2005-10-17 01:07:11 +00001160 // fold (sub x, x) -> 0
Evan Chengc8e3b142008-03-12 07:02:50 +00001161 if (N0 == N1)
Chris Lattner854077d2005-10-17 01:07:11 +00001162 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001163 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001164 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001165 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattner05b57432005-10-11 06:07:15 +00001166 // fold (sub x, c) -> (add x, -c)
1167 if (N1C)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001168 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001169 DAG.getConstant(-N1C->getAPIntValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001170 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001171 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001172 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001173 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001174 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Scott Michelfdc40a02009-02-17 22:15:04 +00001175 return N0.getOperand(0);
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001176 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001177 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001178 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1179 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001180 N0.getOperand(1).getOperand(0) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001181 return DAG.getNode(N0.getOperand(1).getOpcode(), N->getDebugLoc(), VT,
1182 N0.getOperand(0), N0.getOperand(1).getOperand(1));
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001183 // fold ((A+(C+B))-B) -> A+C
1184 if (N0.getOpcode() == ISD::ADD &&
1185 N0.getOperand(1).getOpcode() == ISD::ADD &&
1186 N0.getOperand(1).getOperand(1) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001187 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1188 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dale Johannesen58e39b02008-12-23 01:59:54 +00001189 // fold ((A-(B-C))-C) -> A-B
1190 if (N0.getOpcode() == ISD::SUB &&
1191 N0.getOperand(1).getOpcode() == ISD::SUB &&
1192 N0.getOperand(1).getOperand(1) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001193 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1194 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Bill Wendlingb0702e02009-01-30 02:42:10 +00001195
Dan Gohman613e0d82007-07-03 14:03:57 +00001196 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001197 if (N0.getOpcode() == ISD::UNDEF)
1198 return N0;
1199 if (N1.getOpcode() == ISD::UNDEF)
1200 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001201
Dan Gohman6520e202008-10-18 02:06:02 +00001202 // If the relocation model supports it, consider symbol offsets.
1203 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001204 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman6520e202008-10-18 02:06:02 +00001205 // fold (sub Sym, c) -> Sym-c
1206 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
1207 return DAG.getGlobalAddress(GA->getGlobal(), VT,
1208 GA->getOffset() -
1209 (uint64_t)N1C->getSExtValue());
1210 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1211 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1212 if (GA->getGlobal() == GB->getGlobal())
1213 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1214 VT);
1215 }
1216
Dan Gohman475871a2008-07-27 21:46:04 +00001217 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001218}
1219
Dan Gohman475871a2008-07-27 21:46:04 +00001220SDValue DAGCombiner::visitMUL(SDNode *N) {
1221 SDValue N0 = N->getOperand(0);
1222 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001223 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1224 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001225 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001226
Dan Gohman7f321562007-06-25 16:23:39 +00001227 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001228 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001229 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001230 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001231 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001232
Dan Gohman613e0d82007-07-03 14:03:57 +00001233 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001234 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001235 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001236 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001237 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001238 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001239 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001240 if (N0C && !N1C)
Bill Wendling9c8148a2009-01-30 02:45:56 +00001241 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001242 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001243 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001244 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001245 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001246 if (N1C && N1C->isAllOnesValue())
Bill Wendling9c8148a2009-01-30 02:45:56 +00001247 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1248 DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001249 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001250 if (N1C && N1C->getAPIntValue().isPowerOf2())
Bill Wendling9c8148a2009-01-30 02:45:56 +00001251 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001252 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Duncan Sands92abc622009-01-31 15:50:11 +00001253 getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001254 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Chris Lattner66b8bc32009-03-09 20:22:18 +00001255 if (N1C && (-N1C->getAPIntValue()).isPowerOf2()) {
1256 unsigned Log2Val = (-N1C->getAPIntValue()).logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00001257 // FIXME: If the input is something that is easily negated (e.g. a
Chris Lattner3e6099b2005-10-30 06:41:49 +00001258 // single-use add), we should put the negate there.
Bill Wendling9c8148a2009-01-30 02:45:56 +00001259 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1260 DAG.getConstant(0, VT),
Bill Wendling73e16b22009-01-30 02:49:26 +00001261 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Chris Lattner66b8bc32009-03-09 20:22:18 +00001262 DAG.getConstant(Log2Val, getShiftAmountTy())));
1263 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001264 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
Bill Wendling73e16b22009-01-30 02:49:26 +00001265 if (N1C && N0.getOpcode() == ISD::SHL &&
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001266 isa<ConstantSDNode>(N0.getOperand(1))) {
Bill Wendling9c8148a2009-01-30 02:45:56 +00001267 SDValue C3 = DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1268 N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001269 AddToWorkList(C3.getNode());
Bill Wendling9c8148a2009-01-30 02:45:56 +00001270 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1271 N0.getOperand(0), C3);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001272 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001273
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001274 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1275 // use.
1276 {
Dan Gohman475871a2008-07-27 21:46:04 +00001277 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001278 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1279 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001280 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001281 Sh = N0; Y = N1;
Scott Michelfdc40a02009-02-17 22:15:04 +00001282 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001283 isa<ConstantSDNode>(N1.getOperand(1)) &&
1284 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001285 Sh = N1; Y = N0;
1286 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001287
Gabor Greifba36cb52008-08-28 21:40:38 +00001288 if (Sh.getNode()) {
Bill Wendling9c8148a2009-01-30 02:45:56 +00001289 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1290 Sh.getOperand(0), Y);
1291 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1292 Mul, Sh.getOperand(1));
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001293 }
1294 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001295
Chris Lattnera1deca32006-03-04 23:33:26 +00001296 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Scott Michelfdc40a02009-02-17 22:15:04 +00001297 if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
Bill Wendling9c8148a2009-01-30 02:45:56 +00001298 isa<ConstantSDNode>(N0.getOperand(1)))
1299 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1300 DAG.getNode(ISD::MUL, N0.getDebugLoc(), VT,
1301 N0.getOperand(0), N1),
1302 DAG.getNode(ISD::MUL, N1.getDebugLoc(), VT,
1303 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001304
Nate Begemancd4d58c2006-02-03 06:46:56 +00001305 // reassociate mul
Bill Wendling35247c32009-01-30 00:45:56 +00001306 SDValue RMUL = ReassociateOps(ISD::MUL, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001307 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001308 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001309
Dan Gohman475871a2008-07-27 21:46:04 +00001310 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001311}
1312
Dan Gohman475871a2008-07-27 21:46:04 +00001313SDValue DAGCombiner::visitSDIV(SDNode *N) {
1314 SDValue N0 = N->getOperand(0);
1315 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001316 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1317 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001318 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001319
Dan Gohman7f321562007-06-25 16:23:39 +00001320 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001321 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001322 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001323 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001324 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001325
Nate Begeman1d4d4142005-09-01 00:19:25 +00001326 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001327 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001328 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001329 // fold (sdiv X, 1) -> X
Dan Gohman7810bfe2008-09-26 21:54:37 +00001330 if (N1C && N1C->getSExtValue() == 1LL)
Nate Begeman405e3ec2005-10-21 00:02:42 +00001331 return N0;
1332 // fold (sdiv X, -1) -> 0-X
1333 if (N1C && N1C->isAllOnesValue())
Bill Wendling944d34b2009-01-30 02:52:17 +00001334 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1335 DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001336 // If we know the sign bits of both operands are zero, strength reduce to a
1337 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001338 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001339 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendling944d34b2009-01-30 02:52:17 +00001340 return DAG.getNode(ISD::UDIV, N->getDebugLoc(), N1.getValueType(),
1341 N0, N1);
Chris Lattnerf32aac32008-01-27 23:32:17 +00001342 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001343 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman002e5d02008-03-13 22:13:53 +00001344 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Scott Michelfdc40a02009-02-17 22:15:04 +00001345 (isPowerOf2_64(N1C->getSExtValue()) ||
Dan Gohman7810bfe2008-09-26 21:54:37 +00001346 isPowerOf2_64(-N1C->getSExtValue()))) {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001347 // If dividing by powers of two is cheap, then don't perform the following
1348 // fold.
1349 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001350 return SDValue();
Bill Wendling944d34b2009-01-30 02:52:17 +00001351
Dan Gohman7810bfe2008-09-26 21:54:37 +00001352 int64_t pow2 = N1C->getSExtValue();
Nate Begeman405e3ec2005-10-21 00:02:42 +00001353 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001354 unsigned lg2 = Log2_64(abs2);
Bill Wendling944d34b2009-01-30 02:52:17 +00001355
Chris Lattner8f4880b2006-02-16 08:02:36 +00001356 // Splat the sign bit into the register
Bill Wendling944d34b2009-01-30 02:52:17 +00001357 SDValue SGN = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
1358 DAG.getConstant(VT.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00001359 getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00001360 AddToWorkList(SGN.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001361
Chris Lattner8f4880b2006-02-16 08:02:36 +00001362 // Add (N0 < 0) ? abs2 - 1 : 0;
Bill Wendling944d34b2009-01-30 02:52:17 +00001363 SDValue SRL = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, SGN,
1364 DAG.getConstant(VT.getSizeInBits() - lg2,
Duncan Sands92abc622009-01-31 15:50:11 +00001365 getShiftAmountTy()));
Bill Wendling944d34b2009-01-30 02:52:17 +00001366 SDValue ADD = DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001367 AddToWorkList(SRL.getNode());
1368 AddToWorkList(ADD.getNode()); // Divide by pow2
Bill Wendling944d34b2009-01-30 02:52:17 +00001369 SDValue SRA = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, ADD,
Duncan Sands92abc622009-01-31 15:50:11 +00001370 DAG.getConstant(lg2, getShiftAmountTy()));
Bill Wendling944d34b2009-01-30 02:52:17 +00001371
Nate Begeman405e3ec2005-10-21 00:02:42 +00001372 // If we're dividing by a positive value, we're done. Otherwise, we must
1373 // negate the result.
1374 if (pow2 > 0)
1375 return SRA;
Bill Wendling944d34b2009-01-30 02:52:17 +00001376
Gabor Greifba36cb52008-08-28 21:40:38 +00001377 AddToWorkList(SRA.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001378 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1379 DAG.getConstant(0, VT), SRA);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001380 }
Bill Wendling944d34b2009-01-30 02:52:17 +00001381
Nate Begeman69575232005-10-20 02:15:44 +00001382 // if integer divide is expensive and we satisfy the requirements, emit an
1383 // alternate sequence.
Scott Michelfdc40a02009-02-17 22:15:04 +00001384 if (N1C && (N1C->getSExtValue() < -1 || N1C->getSExtValue() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001385 !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001386 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001387 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001388 }
Dan Gohman7f321562007-06-25 16:23:39 +00001389
Dan Gohman613e0d82007-07-03 14:03:57 +00001390 // undef / X -> 0
1391 if (N0.getOpcode() == ISD::UNDEF)
1392 return DAG.getConstant(0, VT);
1393 // X / undef -> undef
1394 if (N1.getOpcode() == ISD::UNDEF)
1395 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001396
Dan Gohman475871a2008-07-27 21:46:04 +00001397 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001398}
1399
Dan Gohman475871a2008-07-27 21:46:04 +00001400SDValue DAGCombiner::visitUDIV(SDNode *N) {
1401 SDValue N0 = N->getOperand(0);
1402 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001403 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1404 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001405 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001406
Dan Gohman7f321562007-06-25 16:23:39 +00001407 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001408 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001409 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001410 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001411 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001412
Nate Begeman1d4d4142005-09-01 00:19:25 +00001413 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001414 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001415 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001416 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001417 if (N1C && N1C->getAPIntValue().isPowerOf2())
Scott Michelfdc40a02009-02-17 22:15:04 +00001418 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001419 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Duncan Sands92abc622009-01-31 15:50:11 +00001420 getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001421 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1422 if (N1.getOpcode() == ISD::SHL) {
1423 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001424 if (SHC->getAPIntValue().isPowerOf2()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001425 EVT ADDVT = N1.getOperand(1).getValueType();
Bill Wendling07d85142009-01-30 02:55:25 +00001426 SDValue Add = DAG.getNode(ISD::ADD, N->getDebugLoc(), ADDVT,
1427 N1.getOperand(1),
1428 DAG.getConstant(SHC->getAPIntValue()
1429 .logBase2(),
1430 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001431 AddToWorkList(Add.getNode());
Bill Wendling07d85142009-01-30 02:55:25 +00001432 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001433 }
1434 }
1435 }
Nate Begeman69575232005-10-20 02:15:44 +00001436 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001437 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001438 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001439 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00001440 }
Dan Gohman7f321562007-06-25 16:23:39 +00001441
Dan Gohman613e0d82007-07-03 14:03:57 +00001442 // undef / X -> 0
1443 if (N0.getOpcode() == ISD::UNDEF)
1444 return DAG.getConstant(0, VT);
1445 // X / undef -> undef
1446 if (N1.getOpcode() == ISD::UNDEF)
1447 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001448
Dan Gohman475871a2008-07-27 21:46:04 +00001449 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001450}
1451
Dan Gohman475871a2008-07-27 21:46:04 +00001452SDValue DAGCombiner::visitSREM(SDNode *N) {
1453 SDValue N0 = N->getOperand(0);
1454 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001455 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1456 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001457 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001458
Nate Begeman1d4d4142005-09-01 00:19:25 +00001459 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001460 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001461 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001462 // If we know the sign bits of both operands are zero, strength reduce to a
1463 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00001464 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001465 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001466 return DAG.getNode(ISD::UREM, N->getDebugLoc(), VT, N0, N1);
Chris Lattneree339f42008-01-27 23:21:58 +00001467 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001468
Dan Gohman77003042007-11-26 23:46:11 +00001469 // If X/C can be simplified by the division-by-constant logic, lower
1470 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001471 if (N1C && !N1C->isNullValue()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001472 SDValue Div = DAG.getNode(ISD::SDIV, N->getDebugLoc(), VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001473 AddToWorkList(Div.getNode());
1474 SDValue OptimizedDiv = combine(Div.getNode());
1475 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001476 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1477 OptimizedDiv, N1);
1478 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001479 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001480 return Sub;
1481 }
Chris Lattner26d29902006-10-12 20:58:32 +00001482 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001483
Dan Gohman613e0d82007-07-03 14:03:57 +00001484 // undef % X -> 0
1485 if (N0.getOpcode() == ISD::UNDEF)
1486 return DAG.getConstant(0, VT);
1487 // X % undef -> undef
1488 if (N1.getOpcode() == ISD::UNDEF)
1489 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001490
Dan Gohman475871a2008-07-27 21:46:04 +00001491 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001492}
1493
Dan Gohman475871a2008-07-27 21:46:04 +00001494SDValue DAGCombiner::visitUREM(SDNode *N) {
1495 SDValue N0 = N->getOperand(0);
1496 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001497 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1498 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001499 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001500
Nate Begeman1d4d4142005-09-01 00:19:25 +00001501 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001502 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001503 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001504 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001505 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001506 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001507 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001508 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1509 if (N1.getOpcode() == ISD::SHL) {
1510 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001511 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001512 SDValue Add =
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001513 DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001514 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00001515 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001516 AddToWorkList(Add.getNode());
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001517 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, Add);
Nate Begemanc031e332006-02-05 07:36:48 +00001518 }
1519 }
1520 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001521
Dan Gohman77003042007-11-26 23:46:11 +00001522 // If X/C can be simplified by the division-by-constant logic, lower
1523 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001524 if (N1C && !N1C->isNullValue()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001525 SDValue Div = DAG.getNode(ISD::UDIV, N->getDebugLoc(), VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00001526 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00001527 SDValue OptimizedDiv = combine(Div.getNode());
1528 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001529 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1530 OptimizedDiv, N1);
1531 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001532 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001533 return Sub;
1534 }
Chris Lattner26d29902006-10-12 20:58:32 +00001535 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001536
Dan Gohman613e0d82007-07-03 14:03:57 +00001537 // undef % X -> 0
1538 if (N0.getOpcode() == ISD::UNDEF)
1539 return DAG.getConstant(0, VT);
1540 // X % undef -> undef
1541 if (N1.getOpcode() == ISD::UNDEF)
1542 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001543
Dan Gohman475871a2008-07-27 21:46:04 +00001544 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001545}
1546
Dan Gohman475871a2008-07-27 21:46:04 +00001547SDValue DAGCombiner::visitMULHS(SDNode *N) {
1548 SDValue N0 = N->getOperand(0);
1549 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001550 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001551 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001552
Nate Begeman1d4d4142005-09-01 00:19:25 +00001553 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001554 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001555 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001556 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001557 if (N1C && N1C->getAPIntValue() == 1)
Bill Wendling326411d2009-01-30 03:00:18 +00001558 return DAG.getNode(ISD::SRA, N->getDebugLoc(), N0.getValueType(), N0,
1559 DAG.getConstant(N0.getValueType().getSizeInBits() - 1,
Duncan Sands92abc622009-01-31 15:50:11 +00001560 getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001561 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001562 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001563 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001564
Dan Gohman475871a2008-07-27 21:46:04 +00001565 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001566}
1567
Dan Gohman475871a2008-07-27 21:46:04 +00001568SDValue DAGCombiner::visitMULHU(SDNode *N) {
1569 SDValue N0 = N->getOperand(0);
1570 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001571 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001572 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001573
Nate Begeman1d4d4142005-09-01 00:19:25 +00001574 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001575 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001576 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001577 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00001578 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001579 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001580 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001581 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001582 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001583
Dan Gohman475871a2008-07-27 21:46:04 +00001584 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001585}
1586
Dan Gohman389079b2007-10-08 17:57:15 +00001587/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1588/// compute two values. LoOp and HiOp give the opcodes for the two computations
1589/// that are being performed. Return true if a simplification was made.
1590///
Scott Michelfdc40a02009-02-17 22:15:04 +00001591SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Dan Gohman475871a2008-07-27 21:46:04 +00001592 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001593 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001594 bool HiExists = N->hasAnyUseOfValue(1);
1595 if (!HiExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001596 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00001597 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Bill Wendling826d1142009-01-30 03:08:40 +00001598 SDValue Res = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
1599 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001600 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001601 }
1602
1603 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001604 bool LoExists = N->hasAnyUseOfValue(0);
1605 if (!LoExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001606 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00001607 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Bill Wendling826d1142009-01-30 03:08:40 +00001608 SDValue Res = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
1609 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001610 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001611 }
1612
Evan Cheng44711942007-11-08 09:25:29 +00001613 // If both halves are used, return as it is.
1614 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00001615 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00001616
1617 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001618 if (LoExists) {
Bill Wendling826d1142009-01-30 03:08:40 +00001619 SDValue Lo = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
1620 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001621 AddToWorkList(Lo.getNode());
1622 SDValue LoOpt = combine(Lo.getNode());
1623 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001624 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001625 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001626 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001627 }
1628
Evan Cheng44711942007-11-08 09:25:29 +00001629 if (HiExists) {
Bill Wendling826d1142009-01-30 03:08:40 +00001630 SDValue Hi = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
Duncan Sands25cf2272008-11-24 14:53:14 +00001631 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001632 AddToWorkList(Hi.getNode());
1633 SDValue HiOpt = combine(Hi.getNode());
1634 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001635 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001636 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001637 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001638 }
Bill Wendling826d1142009-01-30 03:08:40 +00001639
Dan Gohman475871a2008-07-27 21:46:04 +00001640 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001641}
1642
Dan Gohman475871a2008-07-27 21:46:04 +00001643SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
1644 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00001645 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001646
Dan Gohman475871a2008-07-27 21:46:04 +00001647 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001648}
1649
Dan Gohman475871a2008-07-27 21:46:04 +00001650SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
1651 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00001652 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001653
Dan Gohman475871a2008-07-27 21:46:04 +00001654 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001655}
1656
Dan Gohman475871a2008-07-27 21:46:04 +00001657SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
1658 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001659 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00001660
Dan Gohman475871a2008-07-27 21:46:04 +00001661 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001662}
1663
Dan Gohman475871a2008-07-27 21:46:04 +00001664SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
1665 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001666 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00001667
Dan Gohman475871a2008-07-27 21:46:04 +00001668 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001669}
1670
Chris Lattner35e5c142006-05-05 05:51:50 +00001671/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1672/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00001673SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1674 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00001675 EVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00001676 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001677
Chris Lattner540121f2006-05-05 06:31:05 +00001678 // For each of OP in AND/OR/XOR:
1679 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1680 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1681 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Dan Gohman97121ba2009-04-08 00:15:30 +00001682 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y)) (if trunc isn't free)
Chris Lattner540121f2006-05-05 06:31:05 +00001683 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Dan Gohman97121ba2009-04-08 00:15:30 +00001684 N0.getOpcode() == ISD::SIGN_EXTEND ||
1685 (N0.getOpcode() == ISD::TRUNCATE &&
1686 !TLI.isTruncateFree(N0.getOperand(0).getValueType(), VT))) &&
Jakob Stoklund Olesen17421d82009-08-08 20:42:17 +00001687 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType() &&
1688 (!LegalOperations ||
1689 TLI.isOperationLegal(N->getOpcode(), N0.getOperand(0).getValueType()))) {
Bill Wendlingb74c8672009-01-30 19:25:47 +00001690 SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(),
1691 N0.getOperand(0).getValueType(),
1692 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001693 AddToWorkList(ORNode.getNode());
Bill Wendlingb74c8672009-01-30 19:25:47 +00001694 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001695 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001696
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001697 // For each of OP in SHL/SRL/SRA/AND...
1698 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1699 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1700 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001701 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001702 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001703 N0.getOperand(1) == N1.getOperand(1)) {
Bill Wendlingb74c8672009-01-30 19:25:47 +00001704 SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(),
1705 N0.getOperand(0).getValueType(),
1706 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001707 AddToWorkList(ORNode.getNode());
Bill Wendlingb74c8672009-01-30 19:25:47 +00001708 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
1709 ORNode, N0.getOperand(1));
Chris Lattner35e5c142006-05-05 05:51:50 +00001710 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001711
Dan Gohman475871a2008-07-27 21:46:04 +00001712 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00001713}
1714
Dan Gohman475871a2008-07-27 21:46:04 +00001715SDValue DAGCombiner::visitAND(SDNode *N) {
1716 SDValue N0 = N->getOperand(0);
1717 SDValue N1 = N->getOperand(1);
1718 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001719 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1720 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001721 EVT VT = N1.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001722 unsigned BitWidth = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00001723
Dan Gohman7f321562007-06-25 16:23:39 +00001724 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001725 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001726 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001727 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001728 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001729
Dan Gohman613e0d82007-07-03 14:03:57 +00001730 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001731 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001732 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001733 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001734 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001735 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001736 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001737 if (N0C && !N1C)
Bill Wendlingfc4b6772009-02-01 11:19:36 +00001738 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001739 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001740 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001741 return N0;
1742 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00001743 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001744 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001745 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001746 // reassociate and
Bill Wendling35247c32009-01-30 00:45:56 +00001747 SDValue RAND = ReassociateOps(ISD::AND, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001748 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001749 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001750 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001751 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001752 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001753 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001754 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001755 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1756 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00001757 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001758 APInt Mask = ~N1C->getAPIntValue();
1759 Mask.trunc(N0Op0.getValueSizeInBits());
1760 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Bill Wendling2627a882009-01-30 20:43:18 +00001761 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(),
1762 N0.getValueType(), N0Op0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001763
Chris Lattner1ec05d12006-03-01 21:47:21 +00001764 // Replace uses of the AND with uses of the Zero extend node.
1765 CombineTo(N, Zext);
Scott Michelfdc40a02009-02-17 22:15:04 +00001766
Chris Lattner3603cd62006-02-02 07:17:31 +00001767 // We actually want to replace all uses of the any_extend with the
1768 // zero_extend, to avoid duplicating things. This will later cause this
1769 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00001770 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00001771 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001772 }
1773 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001774 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1775 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1776 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1777 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00001778
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001779 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001780 LL.getValueType().isInteger()) {
Bill Wendling2627a882009-01-30 20:43:18 +00001781 // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001782 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Bill Wendling2627a882009-01-30 20:43:18 +00001783 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
1784 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001785 AddToWorkList(ORNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00001786 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001787 }
Bill Wendling2627a882009-01-30 20:43:18 +00001788 // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001789 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Bill Wendling2627a882009-01-30 20:43:18 +00001790 SDValue ANDNode = DAG.getNode(ISD::AND, N0.getDebugLoc(),
1791 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001792 AddToWorkList(ANDNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00001793 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001794 }
Bill Wendling2627a882009-01-30 20:43:18 +00001795 // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001796 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Bill Wendling2627a882009-01-30 20:43:18 +00001797 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
1798 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001799 AddToWorkList(ORNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00001800 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001801 }
1802 }
1803 // canonicalize equivalent to ll == rl
1804 if (LL == RR && LR == RL) {
1805 Op1 = ISD::getSetCCSwappedOperands(Op1);
1806 std::swap(RL, RR);
1807 }
1808 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001809 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001810 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00001811 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001812 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Bill Wendling2627a882009-01-30 20:43:18 +00001813 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
1814 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001815 }
1816 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001817
Bill Wendling2627a882009-01-30 20:43:18 +00001818 // Simplify: (and (op x...), (op y...)) -> (op (and x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00001819 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001820 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001821 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001822 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001823
Nate Begemande996292006-02-03 22:24:05 +00001824 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1825 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001826 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00001827 SimplifyDemandedBits(SDValue(N, 0)))
1828 return SDValue(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001829 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00001830 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00001831 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00001832 EVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001833 // If we zero all the possible extended bits, then we can turn this into
1834 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001835 unsigned BitWidth = N1.getValueSizeInBits();
1836 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001837 BitWidth - EVT.getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001838 ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00001839 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Bill Wendling2627a882009-01-30 20:43:18 +00001840 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
1841 LN0->getChain(), LN0->getBasePtr(),
1842 LN0->getSrcValue(),
Duncan Sands25cf2272008-11-24 14:53:14 +00001843 LN0->getSrcValueOffset(), EVT,
1844 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001845 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001846 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001847 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001848 }
1849 }
1850 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00001851 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00001852 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001853 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00001854 EVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001855 // If we zero all the possible extended bits, then we can turn this into
1856 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001857 unsigned BitWidth = N1.getValueSizeInBits();
1858 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001859 BitWidth - EVT.getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001860 ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00001861 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Bill Wendling2627a882009-01-30 20:43:18 +00001862 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
1863 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00001864 LN0->getBasePtr(), LN0->getSrcValue(),
1865 LN0->getSrcValueOffset(), EVT,
1866 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001867 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001868 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001869 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001870 }
1871 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001872
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001873 // fold (and (load x), 255) -> (zextload x, i8)
1874 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001875 if (N1C && N0.getOpcode() == ISD::LOAD) {
1876 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1877 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001878 LN0->isUnindexed() && N0.hasOneUse() &&
1879 // Do not change the width of a volatile load.
1880 !LN0->isVolatile()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001881 EVT ExtVT = MVT::Other;
Duncan Sands8eab8a22008-06-09 11:32:28 +00001882 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
1883 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue()))
Owen Andersone50ed302009-08-10 22:56:29 +00001884 ExtVT = EVT::getIntegerVT(ActiveBits);
Duncan Sands8eab8a22008-06-09 11:32:28 +00001885
Owen Andersone50ed302009-08-10 22:56:29 +00001886 EVT LoadedVT = LN0->getMemoryVT();
Bill Wendling2627a882009-01-30 20:43:18 +00001887
Duncan Sandsad205a72008-06-16 08:14:38 +00001888 // Do not generate loads of non-round integer types since these can
1889 // be expensive (and would be wrong if the type is not byte sized).
Owen Anderson825b72b2009-08-11 20:47:22 +00001890 if (ExtVT != MVT::Other && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() &&
Owen Andersone50ed302009-08-10 22:56:29 +00001891 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
1892 EVT PtrType = N0.getOperand(1).getValueType();
Bill Wendling2627a882009-01-30 20:43:18 +00001893
Evan Cheng466685d2006-10-09 20:57:25 +00001894 // For big endian targets, we need to add an offset to the pointer to
1895 // load the correct bytes. For little endian systems, we merely need to
1896 // read fewer bytes from the same pointer.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001897 unsigned LVTStoreBytes = LoadedVT.getStoreSizeInBits()/8;
Owen Andersone50ed302009-08-10 22:56:29 +00001898 unsigned EVTStoreBytes = ExtVT.getStoreSizeInBits()/8;
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001899 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001900 unsigned Alignment = LN0->getAlignment();
Dan Gohman475871a2008-07-27 21:46:04 +00001901 SDValue NewPtr = LN0->getBasePtr();
Bill Wendling2627a882009-01-30 20:43:18 +00001902
Duncan Sands0753fc12008-02-11 10:37:04 +00001903 if (TLI.isBigEndian()) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00001904 NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(), PtrType,
Bill Wendling2627a882009-01-30 20:43:18 +00001905 NewPtr, DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001906 Alignment = MinAlign(Alignment, PtrOff);
1907 }
Bill Wendling2627a882009-01-30 20:43:18 +00001908
Gabor Greifba36cb52008-08-28 21:40:38 +00001909 AddToWorkList(NewPtr.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00001910 SDValue Load =
Bill Wendling2627a882009-01-30 20:43:18 +00001911 DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), VT, LN0->getChain(),
1912 NewPtr, LN0->getSrcValue(), LN0->getSrcValueOffset(),
Owen Andersone50ed302009-08-10 22:56:29 +00001913 ExtVT, LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001914 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001915 CombineTo(N0.getNode(), Load, Load.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001916 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng466685d2006-10-09 20:57:25 +00001917 }
Chris Lattner15045b62006-02-28 06:35:35 +00001918 }
1919 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001920
Dan Gohman475871a2008-07-27 21:46:04 +00001921 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001922}
1923
Dan Gohman475871a2008-07-27 21:46:04 +00001924SDValue DAGCombiner::visitOR(SDNode *N) {
1925 SDValue N0 = N->getOperand(0);
1926 SDValue N1 = N->getOperand(1);
1927 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001928 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1929 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001930 EVT VT = N1.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001931
Dan Gohman7f321562007-06-25 16:23:39 +00001932 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001933 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001934 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001935 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001936 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001937
Dan Gohman613e0d82007-07-03 14:03:57 +00001938 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001939 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman5cbd37e2009-08-06 09:18:59 +00001940 return DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001941 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001942 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001943 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001944 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001945 if (N0C && !N1C)
Bill Wendling09025642009-01-30 20:59:34 +00001946 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001947 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001948 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001949 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001950 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001951 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001952 return N1;
1953 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001954 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001955 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001956 // reassociate or
Bill Wendling35247c32009-01-30 00:45:56 +00001957 SDValue ROR = ReassociateOps(ISD::OR, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001958 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001959 return ROR;
1960 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Gabor Greifba36cb52008-08-28 21:40:38 +00001961 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001962 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001963 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
Bill Wendling09025642009-01-30 20:59:34 +00001964 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
1965 DAG.getNode(ISD::OR, N0.getDebugLoc(), VT,
1966 N0.getOperand(0), N1),
1967 DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1));
Nate Begeman223df222005-09-08 20:18:10 +00001968 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001969 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1970 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1971 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1972 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00001973
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001974 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001975 LL.getValueType().isInteger()) {
Bill Wendling09025642009-01-30 20:59:34 +00001976 // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0)
1977 // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00001978 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001979 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Bill Wendling09025642009-01-30 20:59:34 +00001980 SDValue ORNode = DAG.getNode(ISD::OR, LR.getDebugLoc(),
1981 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001982 AddToWorkList(ORNode.getNode());
Bill Wendling09025642009-01-30 20:59:34 +00001983 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001984 }
Bill Wendling09025642009-01-30 20:59:34 +00001985 // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1)
1986 // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1)
Scott Michelfdc40a02009-02-17 22:15:04 +00001987 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001988 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Bill Wendling09025642009-01-30 20:59:34 +00001989 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getDebugLoc(),
1990 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001991 AddToWorkList(ANDNode.getNode());
Bill Wendling09025642009-01-30 20:59:34 +00001992 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001993 }
1994 }
1995 // canonicalize equivalent to ll == rl
1996 if (LL == RR && LR == RL) {
1997 Op1 = ISD::getSetCCSwappedOperands(Op1);
1998 std::swap(RL, RR);
1999 }
2000 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002001 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002002 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00002003 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002004 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Bill Wendling09025642009-01-30 20:59:34 +00002005 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
2006 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002007 }
2008 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002009
Bill Wendling09025642009-01-30 20:59:34 +00002010 // Simplify: (or (op x...), (op y...)) -> (op (or x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00002011 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002012 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002013 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002014 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002015
Bill Wendling09025642009-01-30 20:59:34 +00002016 // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible.
Chris Lattner1ec72732006-09-14 21:11:37 +00002017 if (N0.getOpcode() == ISD::AND &&
2018 N1.getOpcode() == ISD::AND &&
2019 N0.getOperand(1).getOpcode() == ISD::Constant &&
2020 N1.getOperand(1).getOpcode() == ISD::Constant &&
2021 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00002022 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00002023 // We can only do this xform if we know that bits from X that are set in C2
2024 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002025 const APInt &LHSMask =
2026 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
2027 const APInt &RHSMask =
2028 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00002029
Dan Gohmanea859be2007-06-22 14:59:07 +00002030 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
2031 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Bill Wendling09025642009-01-30 20:59:34 +00002032 SDValue X = DAG.getNode(ISD::OR, N0.getDebugLoc(), VT,
2033 N0.getOperand(0), N1.getOperand(0));
2034 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, X,
2035 DAG.getConstant(LHSMask | RHSMask, VT));
Chris Lattner1ec72732006-09-14 21:11:37 +00002036 }
2037 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002038
Chris Lattner516b9622006-09-14 20:50:57 +00002039 // See if this is some rotate idiom.
Bill Wendling317bd702009-01-30 21:14:50 +00002040 if (SDNode *Rot = MatchRotate(N0, N1, N->getDebugLoc()))
Dan Gohman475871a2008-07-27 21:46:04 +00002041 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00002042
Dan Gohman475871a2008-07-27 21:46:04 +00002043 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002044}
2045
Chris Lattner516b9622006-09-14 20:50:57 +00002046/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00002047static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00002048 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00002049 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00002050 Mask = Op.getOperand(1);
2051 Op = Op.getOperand(0);
2052 } else {
2053 return false;
2054 }
2055 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002056
Chris Lattner516b9622006-09-14 20:50:57 +00002057 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
2058 Shift = Op;
2059 return true;
2060 }
Bill Wendling09025642009-01-30 20:59:34 +00002061
Scott Michelfdc40a02009-02-17 22:15:04 +00002062 return false;
Chris Lattner516b9622006-09-14 20:50:57 +00002063}
2064
Chris Lattner516b9622006-09-14 20:50:57 +00002065// MatchRotate - Handle an 'or' of two operands. If this is one of the many
2066// idioms for rotate, and if the target supports rotation instructions, generate
2067// a rot[lr].
Bill Wendling317bd702009-01-30 21:14:50 +00002068SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002069 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Owen Andersone50ed302009-08-10 22:56:29 +00002070 EVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00002071 if (!TLI.isTypeLegal(VT)) return 0;
2072
2073 // The target must have at least one rotate flavor.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002074 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
2075 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Chris Lattner516b9622006-09-14 20:50:57 +00002076 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002077
Chris Lattner516b9622006-09-14 20:50:57 +00002078 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00002079 SDValue LHSShift; // The shift.
2080 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00002081 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
2082 return 0; // Not part of a rotate.
2083
Dan Gohman475871a2008-07-27 21:46:04 +00002084 SDValue RHSShift; // The shift.
2085 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00002086 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
2087 return 0; // Not part of a rotate.
Scott Michelfdc40a02009-02-17 22:15:04 +00002088
Chris Lattner516b9622006-09-14 20:50:57 +00002089 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
2090 return 0; // Not shifting the same value.
2091
2092 if (LHSShift.getOpcode() == RHSShift.getOpcode())
2093 return 0; // Shifts must disagree.
Scott Michelfdc40a02009-02-17 22:15:04 +00002094
Chris Lattner516b9622006-09-14 20:50:57 +00002095 // Canonicalize shl to left side in a shl/srl pair.
2096 if (RHSShift.getOpcode() == ISD::SHL) {
2097 std::swap(LHS, RHS);
2098 std::swap(LHSShift, RHSShift);
2099 std::swap(LHSMask , RHSMask );
2100 }
2101
Duncan Sands83ec4b62008-06-06 12:08:01 +00002102 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00002103 SDValue LHSShiftArg = LHSShift.getOperand(0);
2104 SDValue LHSShiftAmt = LHSShift.getOperand(1);
2105 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00002106
2107 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
2108 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00002109 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
2110 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002111 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
2112 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00002113 if ((LShVal + RShVal) != OpSizeInBits)
2114 return 0;
2115
Dan Gohman475871a2008-07-27 21:46:04 +00002116 SDValue Rot;
Chris Lattner516b9622006-09-14 20:50:57 +00002117 if (HasROTL)
Bill Wendling317bd702009-01-30 21:14:50 +00002118 Rot = DAG.getNode(ISD::ROTL, DL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002119 else
Bill Wendling317bd702009-01-30 21:14:50 +00002120 Rot = DAG.getNode(ISD::ROTR, DL, VT, LHSShiftArg, RHSShiftAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00002121
Chris Lattner516b9622006-09-14 20:50:57 +00002122 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00002123 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002124 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00002125
Gabor Greifba36cb52008-08-28 21:40:38 +00002126 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002127 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2128 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002129 }
Gabor Greifba36cb52008-08-28 21:40:38 +00002130 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002131 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2132 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002133 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002134
Bill Wendling317bd702009-01-30 21:14:50 +00002135 Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT));
Chris Lattner516b9622006-09-14 20:50:57 +00002136 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002137
Gabor Greifba36cb52008-08-28 21:40:38 +00002138 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002139 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002140
Chris Lattner516b9622006-09-14 20:50:57 +00002141 // If there is a mask here, and we have a variable shift, we can't be sure
2142 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00002143 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00002144 return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00002145
Chris Lattner516b9622006-09-14 20:50:57 +00002146 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2147 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002148 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2149 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002150 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002151 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002152 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002153 if (HasROTL)
Bill Wendling317bd702009-01-30 21:14:50 +00002154 return DAG.getNode(ISD::ROTL, DL, VT,
2155 LHSShiftArg, LHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002156 else
Bill Wendling317bd702009-01-30 21:14:50 +00002157 return DAG.getNode(ISD::ROTR, DL, VT,
2158 LHSShiftArg, RHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002159 }
Chris Lattner516b9622006-09-14 20:50:57 +00002160 }
2161 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002162
Chris Lattner516b9622006-09-14 20:50:57 +00002163 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2164 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002165 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2166 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002167 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002168 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002169 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling2692d592008-08-31 01:13:31 +00002170 if (HasROTR)
Bill Wendling317bd702009-01-30 21:14:50 +00002171 return DAG.getNode(ISD::ROTR, DL, VT,
2172 LHSShiftArg, RHSShiftAmt).getNode();
Bill Wendling2692d592008-08-31 01:13:31 +00002173 else
Bill Wendling317bd702009-01-30 21:14:50 +00002174 return DAG.getNode(ISD::ROTL, DL, VT,
2175 LHSShiftArg, LHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002176 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002177 }
2178 }
2179
Dan Gohman74feef22008-10-17 01:23:35 +00002180 // Look for sign/zext/any-extended or truncate cases:
Scott Michelc9dc1142007-04-02 21:36:32 +00002181 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2182 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00002183 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2184 || LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
Scott Michelc9dc1142007-04-02 21:36:32 +00002185 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2186 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00002187 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2188 || RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002189 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
2190 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +00002191 if (RExtOp0.getOpcode() == ISD::SUB &&
2192 RExtOp0.getOperand(1) == LExtOp0) {
2193 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002194 // (rotl x, y)
Scott Michelc9dc1142007-04-02 21:36:32 +00002195 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002196 // (rotr x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00002197 if (ConstantSDNode *SUBC =
2198 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002199 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00002200 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
2201 LHSShiftArg,
Gabor Greif12632d22008-08-30 19:29:20 +00002202 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002203 }
2204 }
2205 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2206 RExtOp0 == LExtOp0.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002207 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002208 // (rotr x, y)
Bill Wendling353dea22008-08-31 01:04:56 +00002209 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002210 // (rotl x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00002211 if (ConstantSDNode *SUBC =
2212 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002213 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00002214 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT,
2215 LHSShiftArg,
Bill Wendling353dea22008-08-31 01:04:56 +00002216 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002217 }
2218 }
Chris Lattner516b9622006-09-14 20:50:57 +00002219 }
2220 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002221
Chris Lattner516b9622006-09-14 20:50:57 +00002222 return 0;
2223}
2224
Dan Gohman475871a2008-07-27 21:46:04 +00002225SDValue DAGCombiner::visitXOR(SDNode *N) {
2226 SDValue N0 = N->getOperand(0);
2227 SDValue N1 = N->getOperand(1);
2228 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00002229 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2230 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002231 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00002232
Dan Gohman7f321562007-06-25 16:23:39 +00002233 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002234 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002235 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002236 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002237 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002238
Evan Cheng26471c42008-03-25 20:08:07 +00002239 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
2240 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
2241 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00002242 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002243 if (N0.getOpcode() == ISD::UNDEF)
2244 return N0;
2245 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002246 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002247 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002248 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002249 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002250 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002251 if (N0C && !N1C)
Bill Wendling317bd702009-01-30 21:14:50 +00002252 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002253 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002254 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002255 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002256 // reassociate xor
Bill Wendling35247c32009-01-30 00:45:56 +00002257 SDValue RXOR = ReassociateOps(ISD::XOR, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002258 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002259 return RXOR;
Bill Wendlingae89bb12008-11-11 08:25:46 +00002260
Nate Begeman1d4d4142005-09-01 00:19:25 +00002261 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00002262 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002263 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00002264 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2265 isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +00002266
Duncan Sands25cf2272008-11-24 14:53:14 +00002267 if (!LegalOperations || TLI.isCondCodeLegal(NotCC, LHS.getValueType())) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00002268 switch (N0.getOpcode()) {
2269 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002270 llvm_unreachable("Unhandled SetCC Equivalent!");
Bill Wendlingae89bb12008-11-11 08:25:46 +00002271 case ISD::SETCC:
Bill Wendling317bd702009-01-30 21:14:50 +00002272 return DAG.getSetCC(N->getDebugLoc(), VT, LHS, RHS, NotCC);
Bill Wendlingae89bb12008-11-11 08:25:46 +00002273 case ISD::SELECT_CC:
Bill Wendling317bd702009-01-30 21:14:50 +00002274 return DAG.getSelectCC(N->getDebugLoc(), LHS, RHS, N0.getOperand(2),
Bill Wendlingae89bb12008-11-11 08:25:46 +00002275 N0.getOperand(3), NotCC);
2276 }
2277 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00002278 }
Bill Wendlingae89bb12008-11-11 08:25:46 +00002279
Chris Lattner61c5ff42007-09-10 21:39:07 +00002280 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002281 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00002282 N0.getNode()->hasOneUse() &&
2283 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00002284 SDValue V = N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002285 V = DAG.getNode(ISD::XOR, N0.getDebugLoc(), V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002286 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002287 AddToWorkList(V.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00002288 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, V);
Chris Lattner61c5ff42007-09-10 21:39:07 +00002289 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002290
Bill Wendling317bd702009-01-30 21:14:50 +00002291 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc
Owen Anderson825b72b2009-08-11 20:47:22 +00002292 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002293 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002294 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002295 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2296 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling317bd702009-01-30 21:14:50 +00002297 LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS
2298 RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002299 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00002300 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002301 }
2302 }
Bill Wendling317bd702009-01-30 21:14:50 +00002303 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants
Scott Michelfdc40a02009-02-17 22:15:04 +00002304 if (N1C && N1C->isAllOnesValue() &&
Nate Begeman99801192005-09-07 23:25:52 +00002305 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002306 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002307 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2308 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling317bd702009-01-30 21:14:50 +00002309 LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS
2310 RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002311 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00002312 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002313 }
2314 }
Bill Wendling317bd702009-01-30 21:14:50 +00002315 // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2))
Nate Begeman223df222005-09-08 20:18:10 +00002316 if (N1C && N0.getOpcode() == ISD::XOR) {
2317 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2318 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2319 if (N00C)
Bill Wendling317bd702009-01-30 21:14:50 +00002320 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(1),
2321 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00002322 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002323 if (N01C)
Bill Wendling317bd702009-01-30 21:14:50 +00002324 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(0),
2325 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00002326 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002327 }
2328 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002329 if (N0 == N1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002330 if (!VT.isVector()) {
Chris Lattner4fbdd592006-03-28 19:11:05 +00002331 return DAG.getConstant(0, VT);
Duncan Sands25cf2272008-11-24 14:53:14 +00002332 } else if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)){
Chris Lattner4fbdd592006-03-28 19:11:05 +00002333 // Produce a vector of zeros.
Dan Gohman475871a2008-07-27 21:46:04 +00002334 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
2335 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
Evan Chenga87008d2009-02-25 22:49:59 +00002336 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT,
2337 &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002338 }
2339 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002340
Chris Lattner35e5c142006-05-05 05:51:50 +00002341 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2342 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002343 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002344 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002345 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002346
Chris Lattner3e104b12006-04-08 04:15:24 +00002347 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002348 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002349 SimplifyDemandedBits(SDValue(N, 0)))
2350 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002351
Dan Gohman475871a2008-07-27 21:46:04 +00002352 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002353}
2354
Chris Lattnere70da202007-12-06 07:33:36 +00002355/// visitShiftByConstant - Handle transforms common to the three shifts, when
2356/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00002357SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002358 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00002359 if (!LHS->hasOneUse()) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00002360
Chris Lattnere70da202007-12-06 07:33:36 +00002361 // We want to pull some binops through shifts, so that we have (and (shift))
2362 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2363 // thing happens with address calculations, so it's important to canonicalize
2364 // it.
2365 bool HighBitSet = false; // Can we transform this if the high bit is set?
Scott Michelfdc40a02009-02-17 22:15:04 +00002366
Chris Lattnere70da202007-12-06 07:33:36 +00002367 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002368 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002369 case ISD::OR:
2370 case ISD::XOR:
2371 HighBitSet = false; // We can only transform sra if the high bit is clear.
2372 break;
2373 case ISD::AND:
2374 HighBitSet = true; // We can only transform sra if the high bit is set.
2375 break;
2376 case ISD::ADD:
Scott Michelfdc40a02009-02-17 22:15:04 +00002377 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00002378 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00002379 HighBitSet = false; // We can only transform sra if the high bit is clear.
2380 break;
2381 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002382
Chris Lattnere70da202007-12-06 07:33:36 +00002383 // We require the RHS of the binop to be a constant as well.
2384 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002385 if (!BinOpCst) return SDValue();
Bill Wendling88103372009-01-30 21:37:17 +00002386
2387 // FIXME: disable this unless the input to the binop is a shift by a constant.
2388 // If it is not a shift, it pessimizes some common cases like:
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002389 //
Bill Wendling88103372009-01-30 21:37:17 +00002390 // void foo(int *X, int i) { X[i & 1235] = 1; }
2391 // int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00002392 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00002393 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002394 BinOpLHSVal->getOpcode() != ISD::SRA &&
2395 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2396 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00002397 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00002398
Owen Andersone50ed302009-08-10 22:56:29 +00002399 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002400
Bill Wendling88103372009-01-30 21:37:17 +00002401 // If this is a signed shift right, and the high bit is modified by the
2402 // logical operation, do not perform the transformation. The highBitSet
2403 // boolean indicates the value of the high bit of the constant which would
2404 // cause it to be modified for this operation.
Chris Lattnere70da202007-12-06 07:33:36 +00002405 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00002406 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2407 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00002408 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002409 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002410
Chris Lattnere70da202007-12-06 07:33:36 +00002411 // Fold the constants, shifting the binop RHS by the shift amount.
Bill Wendling88103372009-01-30 21:37:17 +00002412 SDValue NewRHS = DAG.getNode(N->getOpcode(), LHS->getOperand(1).getDebugLoc(),
2413 N->getValueType(0),
2414 LHS->getOperand(1), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00002415
2416 // Create the new shift.
Bill Wendling88103372009-01-30 21:37:17 +00002417 SDValue NewShift = DAG.getNode(N->getOpcode(), LHS->getOperand(0).getDebugLoc(),
2418 VT, LHS->getOperand(0), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00002419
2420 // Create the new binop.
Bill Wendling88103372009-01-30 21:37:17 +00002421 return DAG.getNode(LHS->getOpcode(), N->getDebugLoc(), VT, NewShift, NewRHS);
Chris Lattnere70da202007-12-06 07:33:36 +00002422}
2423
Dan Gohman475871a2008-07-27 21:46:04 +00002424SDValue DAGCombiner::visitSHL(SDNode *N) {
2425 SDValue N0 = N->getOperand(0);
2426 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002427 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2428 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002429 EVT VT = N0.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002430 unsigned OpSizeInBits = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002431
Nate Begeman1d4d4142005-09-01 00:19:25 +00002432 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002433 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002434 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002435 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002436 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002437 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002438 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002439 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00002440 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002441 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002442 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002443 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002444 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002445 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002446 APInt::getAllOnesValue(VT.getSizeInBits())))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002447 return DAG.getConstant(0, VT);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002448 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00002449 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002450 N1.getOperand(0).getOpcode() == ISD::AND &&
2451 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002452 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002453 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00002454 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002455 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002456 APInt TruncC = N101C->getAPIntValue();
2457 TruncC.trunc(TruncVT.getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00002458 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00002459 DAG.getNode(ISD::AND, N->getDebugLoc(), TruncVT,
2460 DAG.getNode(ISD::TRUNCATE,
2461 N->getDebugLoc(),
2462 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00002463 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002464 }
2465 }
2466
Dan Gohman475871a2008-07-27 21:46:04 +00002467 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2468 return SDValue(N, 0);
Bill Wendling88103372009-01-30 21:37:17 +00002469
2470 // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00002471 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002472 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002473 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2474 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002475 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002476 return DAG.getConstant(0, VT);
Bill Wendling88103372009-01-30 21:37:17 +00002477 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002478 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002479 }
Bill Wendling88103372009-01-30 21:37:17 +00002480 // fold (shl (srl x, c1), c2) -> (shl (and x, (shl -1, c1)), (sub c2, c1)) or
2481 // (srl (and x, (shl -1, c1)), (sub c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00002482 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002483 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002484 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
Evan Chengd101a722009-07-21 05:40:15 +00002485 if (c1 < VT.getSizeInBits()) {
2486 uint64_t c2 = N1C->getZExtValue();
Dan Gohman5cbd37e2009-08-06 09:18:59 +00002487 SDValue HiBitsMask =
2488 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
2489 VT.getSizeInBits() - c1),
2490 VT);
Evan Chengd101a722009-07-21 05:40:15 +00002491 SDValue Mask = DAG.getNode(ISD::AND, N0.getDebugLoc(), VT,
2492 N0.getOperand(0),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00002493 HiBitsMask);
Evan Chengd101a722009-07-21 05:40:15 +00002494 if (c2 > c1)
2495 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, Mask,
2496 DAG.getConstant(c2-c1, N1.getValueType()));
2497 else
2498 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, Mask,
2499 DAG.getConstant(c1-c2, N1.getValueType()));
2500 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00002501 }
Bill Wendling88103372009-01-30 21:37:17 +00002502 // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1))
Dan Gohman5cbd37e2009-08-06 09:18:59 +00002503 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) {
2504 SDValue HiBitsMask =
2505 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
2506 VT.getSizeInBits() -
2507 N1C->getZExtValue()),
2508 VT);
Bill Wendling88103372009-01-30 21:37:17 +00002509 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0.getOperand(0),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00002510 HiBitsMask);
2511 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002512
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002513 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002514}
2515
Dan Gohman475871a2008-07-27 21:46:04 +00002516SDValue DAGCombiner::visitSRA(SDNode *N) {
2517 SDValue N0 = N->getOperand(0);
2518 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002519 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2520 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002521 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00002522
Bill Wendling88103372009-01-30 21:37:17 +00002523 // fold (sra c1, c2) -> (sra c1, c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002524 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002525 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002526 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002527 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002528 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002529 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002530 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002531 return N0;
Bill Wendling88103372009-01-30 21:37:17 +00002532 // fold (sra x, (setge c, size(x))) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002533 if (N1C && N1C->getZExtValue() >= VT.getSizeInBits())
Dale Johannesene8d72302009-02-06 23:05:02 +00002534 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002535 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002536 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002537 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002538 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2539 // sext_inreg.
2540 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002541 unsigned LowBits = VT.getSizeInBits() - (unsigned)N1C->getZExtValue();
Owen Andersone50ed302009-08-10 22:56:29 +00002542 EVT EVT = EVT::getIntegerVT(LowBits);
Duncan Sands25cf2272008-11-24 14:53:14 +00002543 if ((!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT)))
Bill Wendling88103372009-01-30 21:37:17 +00002544 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
2545 N0.getOperand(0), DAG.getValueType(EVT));
Nate Begemanfb7217b2006-02-17 19:54:08 +00002546 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002547
Bill Wendling88103372009-01-30 21:37:17 +00002548 // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2))
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002549 if (N1C && N0.getOpcode() == ISD::SRA) {
2550 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002551 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002552 if (Sum >= VT.getSizeInBits()) Sum = VT.getSizeInBits()-1;
Bill Wendling88103372009-01-30 21:37:17 +00002553 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0.getOperand(0),
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002554 DAG.getConstant(Sum, N1C->getValueType(0)));
2555 }
2556 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00002557
Bill Wendling88103372009-01-30 21:37:17 +00002558 // fold (sra (shl X, m), (sub result_size, n))
2559 // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for
Scott Michelfdc40a02009-02-17 22:15:04 +00002560 // result_size - n != m.
2561 // If truncate is free for the target sext(shl) is likely to result in better
Christopher Lambb9b04282008-03-20 04:31:39 +00002562 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002563 if (N0.getOpcode() == ISD::SHL) {
2564 // Get the two constanst of the shifts, CN0 = m, CN = n.
2565 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2566 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002567 // Determine what the truncate's result bitsize and type would be.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002568 unsigned VTValSize = VT.getSizeInBits();
Owen Andersone50ed302009-08-10 22:56:29 +00002569 EVT TruncVT =
2570 EVT::getIntegerVT(VTValSize - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00002571 // Determine the residual right-shift amount.
Torok Edwin6bb49582009-05-23 17:29:48 +00002572 signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002573
Scott Michelfdc40a02009-02-17 22:15:04 +00002574 // If the shift is not a no-op (in which case this should be just a sign
2575 // extend already), the truncated to type is legal, sign_extend is legal
Gabor Greif12632d22008-08-30 19:29:20 +00002576 // on that type, and the the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00002577 // perform the transform.
Torok Edwin6bb49582009-05-23 17:29:48 +00002578 if ((ShiftAmt > 0) &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002579 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
2580 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00002581 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002582
Duncan Sands92abc622009-01-31 15:50:11 +00002583 SDValue Amt = DAG.getConstant(ShiftAmt, getShiftAmountTy());
Bill Wendling88103372009-01-30 21:37:17 +00002584 SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT,
2585 N0.getOperand(0), Amt);
2586 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), TruncVT,
2587 Shift);
2588 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(),
2589 N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00002590 }
2591 }
2592 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002593
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002594 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00002595 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002596 N1.getOperand(0).getOpcode() == ISD::AND &&
2597 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002598 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002599 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00002600 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002601 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002602 APInt TruncC = N101C->getAPIntValue();
2603 TruncC.trunc(TruncVT.getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00002604 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
Bill Wendling9729c5a2009-01-31 03:12:48 +00002605 DAG.getNode(ISD::AND, N->getDebugLoc(),
Bill Wendling88103372009-01-30 21:37:17 +00002606 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00002607 DAG.getNode(ISD::TRUNCATE,
2608 N->getDebugLoc(),
2609 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00002610 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002611 }
2612 }
2613
Scott Michelfdc40a02009-02-17 22:15:04 +00002614 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00002615 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2616 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002617
2618
Nate Begeman1d4d4142005-09-01 00:19:25 +00002619 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002620 if (DAG.SignBitIsZero(N0))
Bill Wendling88103372009-01-30 21:37:17 +00002621 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002622
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002623 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002624}
2625
Dan Gohman475871a2008-07-27 21:46:04 +00002626SDValue DAGCombiner::visitSRL(SDNode *N) {
2627 SDValue N0 = N->getOperand(0);
2628 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002629 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2630 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002631 EVT VT = N0.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002632 unsigned OpSizeInBits = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002633
Nate Begeman1d4d4142005-09-01 00:19:25 +00002634 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002635 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002636 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002637 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002638 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002639 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002640 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002641 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00002642 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002643 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002644 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002645 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002646 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002647 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002648 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002649 return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002650
Bill Wendling88103372009-01-30 21:37:17 +00002651 // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00002652 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002653 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002654 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2655 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002656 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002657 return DAG.getConstant(0, VT);
Bill Wendling88103372009-01-30 21:37:17 +00002658 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002659 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002660 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002661
Chris Lattner06afe072006-05-05 22:53:17 +00002662 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2663 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2664 // Shifting in all undef bits?
Owen Andersone50ed302009-08-10 22:56:29 +00002665 EVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002666 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Dale Johannesene8d72302009-02-06 23:05:02 +00002667 return DAG.getUNDEF(VT);
Chris Lattner06afe072006-05-05 22:53:17 +00002668
Bill Wendling88103372009-01-30 21:37:17 +00002669 SDValue SmallShift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), SmallVT,
2670 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002671 AddToWorkList(SmallShift.getNode());
Bill Wendling88103372009-01-30 21:37:17 +00002672 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, SmallShift);
Chris Lattner06afe072006-05-05 22:53:17 +00002673 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002674
Chris Lattner3657ffe2006-10-12 20:23:19 +00002675 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2676 // bit, which is unmodified by sra.
Bill Wendling88103372009-01-30 21:37:17 +00002677 if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00002678 if (N0.getOpcode() == ISD::SRA)
Bill Wendling88103372009-01-30 21:37:17 +00002679 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0), N1);
Chris Lattner3657ffe2006-10-12 20:23:19 +00002680 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002681
Chris Lattner350bec02006-04-02 06:11:11 +00002682 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
Scott Michelfdc40a02009-02-17 22:15:04 +00002683 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002684 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00002685 APInt KnownZero, KnownOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002686 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00002687 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00002688
Chris Lattner350bec02006-04-02 06:11:11 +00002689 // If any of the input bits are KnownOne, then the input couldn't be all
2690 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002691 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002692
Chris Lattner350bec02006-04-02 06:11:11 +00002693 // If all of the bits input the to ctlz node are known to be zero, then
2694 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002695 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00002696 if (UnknownBits == 0) return DAG.getConstant(1, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002697
Chris Lattner350bec02006-04-02 06:11:11 +00002698 // Otherwise, check to see if there is exactly one bit input to the ctlz.
Bill Wendling88103372009-01-30 21:37:17 +00002699 if ((UnknownBits & (UnknownBits - 1)) == 0) {
Chris Lattner350bec02006-04-02 06:11:11 +00002700 // Okay, we know that only that the single bit specified by UnknownBits
Bill Wendling88103372009-01-30 21:37:17 +00002701 // could be set on input to the CTLZ node. If this bit is set, the SRL
2702 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2703 // to an SRL/XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002704 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00002705 SDValue Op = N0.getOperand(0);
Bill Wendling88103372009-01-30 21:37:17 +00002706
Chris Lattner350bec02006-04-02 06:11:11 +00002707 if (ShAmt) {
Bill Wendling88103372009-01-30 21:37:17 +00002708 Op = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT, Op,
Duncan Sands92abc622009-01-31 15:50:11 +00002709 DAG.getConstant(ShAmt, getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002710 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00002711 }
Bill Wendling88103372009-01-30 21:37:17 +00002712
2713 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT,
2714 Op, DAG.getConstant(1, VT));
Chris Lattner350bec02006-04-02 06:11:11 +00002715 }
2716 }
Evan Chengeb9f8922008-08-30 02:03:58 +00002717
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002718 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00002719 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002720 N1.getOperand(0).getOpcode() == ISD::AND &&
2721 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002722 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002723 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00002724 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002725 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002726 APInt TruncC = N101C->getAPIntValue();
2727 TruncC.trunc(TruncVT.getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00002728 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
Bill Wendling9729c5a2009-01-31 03:12:48 +00002729 DAG.getNode(ISD::AND, N->getDebugLoc(),
Bill Wendling88103372009-01-30 21:37:17 +00002730 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00002731 DAG.getNode(ISD::TRUNCATE,
2732 N->getDebugLoc(),
2733 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00002734 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002735 }
2736 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002737
Chris Lattner61a4c072007-04-18 03:06:49 +00002738 // fold operands of srl based on knowledge that the low bits are not
2739 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00002740 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2741 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002742
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002743 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002744}
2745
Dan Gohman475871a2008-07-27 21:46:04 +00002746SDValue DAGCombiner::visitCTLZ(SDNode *N) {
2747 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00002748 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002749
2750 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002751 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00002752 return DAG.getNode(ISD::CTLZ, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002753 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002754}
2755
Dan Gohman475871a2008-07-27 21:46:04 +00002756SDValue DAGCombiner::visitCTTZ(SDNode *N) {
2757 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00002758 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002759
Nate Begeman1d4d4142005-09-01 00:19:25 +00002760 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002761 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00002762 return DAG.getNode(ISD::CTTZ, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002763 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002764}
2765
Dan Gohman475871a2008-07-27 21:46:04 +00002766SDValue DAGCombiner::visitCTPOP(SDNode *N) {
2767 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00002768 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002769
Nate Begeman1d4d4142005-09-01 00:19:25 +00002770 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002771 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00002772 return DAG.getNode(ISD::CTPOP, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002773 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002774}
2775
Dan Gohman475871a2008-07-27 21:46:04 +00002776SDValue DAGCombiner::visitSELECT(SDNode *N) {
2777 SDValue N0 = N->getOperand(0);
2778 SDValue N1 = N->getOperand(1);
2779 SDValue N2 = N->getOperand(2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002780 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2781 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2782 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Owen Andersone50ed302009-08-10 22:56:29 +00002783 EVT VT = N->getValueType(0);
2784 EVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002785
Bill Wendling34584e62009-01-30 22:02:18 +00002786 // fold (select C, X, X) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00002787 if (N1 == N2)
2788 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00002789 // fold (select true, X, Y) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00002790 if (N0C && !N0C->isNullValue())
2791 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00002792 // fold (select false, X, Y) -> Y
Nate Begeman452d7be2005-09-16 00:54:12 +00002793 if (N0C && N0C->isNullValue())
2794 return N2;
Bill Wendling34584e62009-01-30 22:02:18 +00002795 // fold (select C, 1, X) -> (or C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00002796 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Bill Wendling34584e62009-01-30 22:02:18 +00002797 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2);
2798 // fold (select C, 0, 1) -> (xor C, 1)
Bob Wilson67ba2232009-01-22 22:05:48 +00002799 if (VT.isInteger() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00002800 (VT0 == MVT::i1 ||
Bob Wilson67ba2232009-01-22 22:05:48 +00002801 (VT0.isInteger() &&
2802 TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00002803 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00002804 SDValue XORNode;
Evan Cheng571c4782007-08-18 05:57:05 +00002805 if (VT == VT0)
Bill Wendling34584e62009-01-30 22:02:18 +00002806 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT0,
2807 N0, DAG.getConstant(1, VT0));
2808 XORNode = DAG.getNode(ISD::XOR, N0.getDebugLoc(), VT0,
2809 N0, DAG.getConstant(1, VT0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002810 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00002811 if (VT.bitsGT(VT0))
Bill Wendling34584e62009-01-30 22:02:18 +00002812 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, XORNode);
2813 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, XORNode);
Evan Cheng571c4782007-08-18 05:57:05 +00002814 }
Bill Wendling34584e62009-01-30 22:02:18 +00002815 // fold (select C, 0, X) -> (and (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00002816 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Bill Wendling7581bfa2009-01-30 23:03:19 +00002817 SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00002818 AddToWorkList(NOTNode.getNode());
Bill Wendling7581bfa2009-01-30 23:03:19 +00002819 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, NOTNode, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002820 }
Bill Wendling34584e62009-01-30 22:02:18 +00002821 // fold (select C, X, 1) -> (or (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00002822 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00002823 SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00002824 AddToWorkList(NOTNode.getNode());
Bill Wendling7581bfa2009-01-30 23:03:19 +00002825 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, NOTNode, N1);
Nate Begeman452d7be2005-09-16 00:54:12 +00002826 }
Bill Wendling34584e62009-01-30 22:02:18 +00002827 // fold (select C, X, 0) -> (and C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00002828 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Bill Wendling34584e62009-01-30 22:02:18 +00002829 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1);
2830 // fold (select X, X, Y) -> (or X, Y)
2831 // fold (select X, 1, Y) -> (or X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00002832 if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1)))
Bill Wendling34584e62009-01-30 22:02:18 +00002833 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2);
2834 // fold (select X, Y, X) -> (and X, Y)
2835 // fold (select X, Y, 0) -> (and X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00002836 if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0)))
Bill Wendling34584e62009-01-30 22:02:18 +00002837 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002838
Chris Lattner40c62d52005-10-18 06:04:22 +00002839 // If we can fold this based on the true/false value, do so.
2840 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00002841 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002842
Nate Begeman44728a72005-09-19 22:34:01 +00002843 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002844 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002845 // FIXME:
Owen Anderson825b72b2009-08-11 20:47:22 +00002846 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
Nate Begeman750ac1b2006-02-01 07:19:44 +00002847 // having to say they don't support SELECT_CC on every type the DAG knows
2848 // about, since there is no way to mark an opcode illegal at all value types
Owen Anderson825b72b2009-08-11 20:47:22 +00002849 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) &&
Dan Gohman4ea48042009-08-02 16:19:38 +00002850 TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT))
Bill Wendling34584e62009-01-30 22:02:18 +00002851 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT,
2852 N0.getOperand(0), N0.getOperand(1),
Nate Begeman750ac1b2006-02-01 07:19:44 +00002853 N1, N2, N0.getOperand(2));
Chris Lattner600fec32009-03-11 05:08:08 +00002854 return SimplifySelect(N->getDebugLoc(), N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002855 }
Bill Wendling34584e62009-01-30 22:02:18 +00002856
Dan Gohman475871a2008-07-27 21:46:04 +00002857 return SDValue();
Nate Begeman452d7be2005-09-16 00:54:12 +00002858}
2859
Dan Gohman475871a2008-07-27 21:46:04 +00002860SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
2861 SDValue N0 = N->getOperand(0);
2862 SDValue N1 = N->getOperand(1);
2863 SDValue N2 = N->getOperand(2);
2864 SDValue N3 = N->getOperand(3);
2865 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002866 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00002867
Nate Begeman44728a72005-09-19 22:34:01 +00002868 // fold select_cc lhs, rhs, x, x, cc -> x
2869 if (N2 == N3)
2870 return N2;
Scott Michelfdc40a02009-02-17 22:15:04 +00002871
Chris Lattner5f42a242006-09-20 06:19:26 +00002872 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00002873 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002874 N0, N1, CC, N->getDebugLoc(), false);
Gabor Greifba36cb52008-08-28 21:40:38 +00002875 if (SCC.getNode()) AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00002876
Gabor Greifba36cb52008-08-28 21:40:38 +00002877 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002878 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00002879 return N2; // cond always true -> true val
2880 else
2881 return N3; // cond always false -> false val
2882 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002883
Chris Lattner5f42a242006-09-20 06:19:26 +00002884 // Fold to a simpler select_cc
Gabor Greifba36cb52008-08-28 21:40:38 +00002885 if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC)
Scott Michelfdc40a02009-02-17 22:15:04 +00002886 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), N2.getValueType(),
2887 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
Chris Lattner5f42a242006-09-20 06:19:26 +00002888 SCC.getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00002889
Chris Lattner40c62d52005-10-18 06:04:22 +00002890 // If we can fold this based on the true/false value, do so.
2891 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00002892 return SDValue(N, 0); // Don't revisit N.
Scott Michelfdc40a02009-02-17 22:15:04 +00002893
Nate Begeman44728a72005-09-19 22:34:01 +00002894 // fold select_cc into other things, such as min/max/abs
Bill Wendling836ca7d2009-01-30 23:59:18 +00002895 return SimplifySelectCC(N->getDebugLoc(), N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002896}
2897
Dan Gohman475871a2008-07-27 21:46:04 +00002898SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002899 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002900 cast<CondCodeSDNode>(N->getOperand(2))->get(),
2901 N->getDebugLoc());
Nate Begeman452d7be2005-09-16 00:54:12 +00002902}
2903
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002904// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
Dan Gohman57fc82d2009-04-09 03:51:29 +00002905// "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))"
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002906// transformation. Returns true if extension are possible and the above
Scott Michelfdc40a02009-02-17 22:15:04 +00002907// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00002908static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002909 unsigned ExtOpc,
2910 SmallVector<SDNode*, 4> &ExtendNodes,
Dan Gohman79ce2762009-01-15 19:20:50 +00002911 const TargetLowering &TLI) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002912 bool HasCopyToRegUses = false;
2913 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00002914 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
2915 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002916 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002917 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002918 if (User == N)
2919 continue;
Dan Gohman57fc82d2009-04-09 03:51:29 +00002920 if (UI.getUse().getResNo() != N0.getResNo())
2921 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002922 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
Dan Gohman57fc82d2009-04-09 03:51:29 +00002923 if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002924 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2925 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2926 // Sign bits will be lost after a zext.
2927 return false;
2928 bool Add = false;
2929 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002930 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002931 if (UseOp == N0)
2932 continue;
2933 if (!isa<ConstantSDNode>(UseOp))
2934 return false;
2935 Add = true;
2936 }
2937 if (Add)
2938 ExtendNodes.push_back(User);
Dan Gohman57fc82d2009-04-09 03:51:29 +00002939 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002940 }
Dan Gohman57fc82d2009-04-09 03:51:29 +00002941 // If truncates aren't free and there are users we can't
2942 // extend, it isn't worthwhile.
2943 if (!isTruncFree)
2944 return false;
2945 // Remember if this value is live-out.
2946 if (User->getOpcode() == ISD::CopyToReg)
2947 HasCopyToRegUses = true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002948 }
2949
2950 if (HasCopyToRegUses) {
2951 bool BothLiveOut = false;
2952 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2953 UI != UE; ++UI) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00002954 SDUse &Use = UI.getUse();
2955 if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) {
2956 BothLiveOut = true;
2957 break;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002958 }
2959 }
2960 if (BothLiveOut)
2961 // Both unextended and extended values are live out. There had better be
2962 // good a reason for the transformation.
2963 return ExtendNodes.size();
2964 }
2965 return true;
2966}
2967
Dan Gohman475871a2008-07-27 21:46:04 +00002968SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
2969 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00002970 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002971
Nate Begeman1d4d4142005-09-01 00:19:25 +00002972 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002973 if (isa<ConstantSDNode>(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00002974 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002975
Nate Begeman1d4d4142005-09-01 00:19:25 +00002976 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002977 // fold (sext (aext x)) -> (sext x)
2978 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Bill Wendling6ce610f2009-01-30 22:23:15 +00002979 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT,
2980 N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002981
Chris Lattner22558872007-02-26 03:13:59 +00002982 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002983 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2984 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002985 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
2986 if (NarrowLoad.getNode()) {
2987 if (NarrowLoad.getNode() != N0.getNode())
2988 CombineTo(N0.getNode(), NarrowLoad);
Dan Gohmanc7b34442009-04-27 02:00:55 +00002989 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00002990 }
Evan Chengc88138f2007-03-22 01:54:19 +00002991
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002992 // See if the value being truncated is already sign extended. If so, just
2993 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00002994 SDValue Op = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002995 unsigned OpBits = Op.getValueType().getSizeInBits();
2996 unsigned MidBits = N0.getValueType().getSizeInBits();
2997 unsigned DestBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002998 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00002999
Chris Lattner22558872007-02-26 03:13:59 +00003000 if (OpBits == DestBits) {
3001 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
3002 // bits, it is already ready.
3003 if (NumSignBits > DestBits-MidBits)
3004 return Op;
3005 } else if (OpBits < DestBits) {
3006 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
3007 // bits, just sext from i32.
3008 if (NumSignBits > OpBits-MidBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00003009 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, Op);
Chris Lattner22558872007-02-26 03:13:59 +00003010 } else {
3011 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
3012 // bits, just truncate to i32.
3013 if (NumSignBits > OpBits-MidBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00003014 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00003015 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003016
Chris Lattner22558872007-02-26 03:13:59 +00003017 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sands25cf2272008-11-24 14:53:14 +00003018 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
3019 N0.getValueType())) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00003020 if (Op.getValueType().bitsLT(VT))
Bill Wendling6ce610f2009-01-30 22:23:15 +00003021 Op = DAG.getNode(ISD::ANY_EXTEND, N0.getDebugLoc(), VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003022 else if (Op.getValueType().bitsGT(VT))
Bill Wendling6ce610f2009-01-30 22:23:15 +00003023 Op = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), VT, Op);
3024 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, Op,
Chris Lattner22558872007-02-26 03:13:59 +00003025 DAG.getValueType(N0.getValueType()));
3026 }
Chris Lattner6007b842006-09-21 06:00:20 +00003027 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003028
Evan Cheng110dec22005-12-14 02:19:23 +00003029 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003030 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003031 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003032 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003033 bool DoXform = true;
3034 SmallVector<SDNode*, 4> SetCCs;
3035 if (!N0.hasOneUse())
3036 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
3037 if (DoXform) {
3038 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman57fc82d2009-04-09 03:51:29 +00003039 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
3040 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003041 LN0->getBasePtr(), LN0->getSrcValue(),
3042 LN0->getSrcValueOffset(),
3043 N0.getValueType(),
3044 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003045 CombineTo(N, ExtLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00003046 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3047 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003048 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling6ce610f2009-01-30 22:23:15 +00003049
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003050 // Extend SetCC uses if necessary.
3051 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3052 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00003053 SmallVector<SDValue, 4> Ops;
Bill Wendling6ce610f2009-01-30 22:23:15 +00003054
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003055 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00003056 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003057 if (SOp == Trunc)
3058 Ops.push_back(ExtLoad);
3059 else
Dan Gohman57fc82d2009-04-09 03:51:29 +00003060 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND,
3061 N->getDebugLoc(), VT, SOp));
Bill Wendling6ce610f2009-01-30 22:23:15 +00003062 }
3063
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003064 Ops.push_back(SetCC->getOperand(2));
Bill Wendling9729c5a2009-01-31 03:12:48 +00003065 CombineTo(SetCC, DAG.getNode(ISD::SETCC, N->getDebugLoc(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00003066 SetCC->getValueType(0),
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003067 &Ops[0], Ops.size()));
3068 }
Bill Wendling6ce610f2009-01-30 22:23:15 +00003069
Dan Gohman475871a2008-07-27 21:46:04 +00003070 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003071 }
Nate Begeman3df4d522005-10-12 20:40:40 +00003072 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003073
3074 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
3075 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003076 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3077 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003078 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00003079 EVT EVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00003080 if ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003081 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT)) {
Bill Wendling6ce610f2009-01-30 22:23:15 +00003082 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
3083 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003084 LN0->getBasePtr(), LN0->getSrcValue(),
3085 LN0->getSrcValueOffset(), EVT,
3086 LN0->isVolatile(), LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003087 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00003088 CombineTo(N0.getNode(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00003089 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3090 N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003091 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003092 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003093 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003094 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003095
Chris Lattner20a35c32007-04-11 05:32:27 +00003096 if (N0.getOpcode() == ISD::SETCC) {
Chris Lattner2b7a2712009-07-08 00:31:33 +00003097 // sext(setcc) -> sext_in_reg(vsetcc) for vectors.
3098 if (VT.isVector() &&
3099 // We know that the # elements of the results is the same as the
3100 // # elements of the compare (and the # elements of the compare result
3101 // for that matter). Check to see that they are the same size. If so,
3102 // we know that the element size of the sext'd result matches the
3103 // element size of the compare operands.
3104 VT.getSizeInBits() == N0.getOperand(0).getValueType().getSizeInBits() &&
3105
3106 // Only do this before legalize for now.
3107 !LegalOperations) {
3108 return DAG.getVSetCC(N->getDebugLoc(), VT, N0.getOperand(0),
3109 N0.getOperand(1),
3110 cast<CondCodeSDNode>(N0.getOperand(2))->get());
3111 }
3112
3113 // sext(setcc x, y, cc) -> (select_cc x, y, -1, 0, cc)
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003114 SDValue NegOne =
3115 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003116 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00003117 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003118 NegOne, DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003119 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003120 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003121 }
Chris Lattner2b7a2712009-07-08 00:31:33 +00003122
3123
Scott Michelfdc40a02009-02-17 22:15:04 +00003124
Dan Gohman8f0ad582008-04-28 16:58:24 +00003125 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sands25cf2272008-11-24 14:53:14 +00003126 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman187db7b2008-04-28 18:47:17 +00003127 DAG.SignBitIsZero(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00003128 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003129
Dan Gohman475871a2008-07-27 21:46:04 +00003130 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003131}
3132
Dan Gohman475871a2008-07-27 21:46:04 +00003133SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
3134 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003135 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003136
Nate Begeman1d4d4142005-09-01 00:19:25 +00003137 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00003138 if (isa<ConstantSDNode>(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00003139 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003140 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00003141 // fold (zext (aext x)) -> (zext x)
3142 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Bill Wendling6ce610f2009-01-30 22:23:15 +00003143 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT,
3144 N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00003145
Evan Chengc88138f2007-03-22 01:54:19 +00003146 // fold (zext (truncate (load x))) -> (zext (smaller load x))
3147 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00003148 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003149 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3150 if (NarrowLoad.getNode()) {
3151 if (NarrowLoad.getNode() != N0.getNode())
3152 CombineTo(N0.getNode(), NarrowLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00003153 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003154 }
Evan Chengc88138f2007-03-22 01:54:19 +00003155 }
3156
Chris Lattner6007b842006-09-21 06:00:20 +00003157 // fold (zext (truncate x)) -> (and x, mask)
3158 if (N0.getOpcode() == ISD::TRUNCATE &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003159 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00003160 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003161 if (Op.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003162 Op = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003163 } else if (Op.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003164 Op = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00003165 }
Bill Wendling6ce610f2009-01-30 22:23:15 +00003166 return DAG.getZeroExtendInReg(Op, N->getDebugLoc(), N0.getValueType());
Chris Lattner6007b842006-09-21 06:00:20 +00003167 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003168
Dan Gohman97121ba2009-04-08 00:15:30 +00003169 // Fold (zext (and (trunc x), cst)) -> (and x, cst),
3170 // if either of the casts is not free.
Chris Lattner111c2282006-09-21 06:14:31 +00003171 if (N0.getOpcode() == ISD::AND &&
3172 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00003173 N0.getOperand(1).getOpcode() == ISD::Constant &&
3174 (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
3175 N0.getValueType()) ||
3176 !TLI.isZExtFree(N0.getValueType(), VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00003177 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003178 if (X.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003179 X = DAG.getNode(ISD::ANY_EXTEND, X.getDebugLoc(), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003180 } else if (X.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003181 X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X);
Chris Lattner111c2282006-09-21 06:14:31 +00003182 }
Dan Gohman220a8232008-03-03 23:51:38 +00003183 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003184 Mask.zext(VT.getSizeInBits());
Bill Wendling6ce610f2009-01-30 22:23:15 +00003185 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
3186 X, DAG.getConstant(Mask, VT));
Chris Lattner111c2282006-09-21 06:14:31 +00003187 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003188
Evan Cheng110dec22005-12-14 02:19:23 +00003189 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003190 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003191 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003192 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003193 bool DoXform = true;
3194 SmallVector<SDNode*, 4> SetCCs;
3195 if (!N0.hasOneUse())
3196 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
3197 if (DoXform) {
3198 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendling6ce610f2009-01-30 22:23:15 +00003199 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT,
3200 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003201 LN0->getBasePtr(), LN0->getSrcValue(),
3202 LN0->getSrcValueOffset(),
3203 N0.getValueType(),
3204 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003205 CombineTo(N, ExtLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00003206 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3207 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003208 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling6ce610f2009-01-30 22:23:15 +00003209
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003210 // Extend SetCC uses if necessary.
3211 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3212 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00003213 SmallVector<SDValue, 4> Ops;
Bill Wendling6ce610f2009-01-30 22:23:15 +00003214
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003215 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00003216 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003217 if (SOp == Trunc)
3218 Ops.push_back(ExtLoad);
3219 else
Bill Wendling9729c5a2009-01-31 03:12:48 +00003220 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND,
3221 N->getDebugLoc(), VT, SOp));
Bill Wendling6ce610f2009-01-30 22:23:15 +00003222 }
3223
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003224 Ops.push_back(SetCC->getOperand(2));
Bill Wendling9729c5a2009-01-31 03:12:48 +00003225 CombineTo(SetCC, DAG.getNode(ISD::SETCC, N->getDebugLoc(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00003226 SetCC->getValueType(0),
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003227 &Ops[0], Ops.size()));
3228 }
Bill Wendling6ce610f2009-01-30 22:23:15 +00003229
Dan Gohman475871a2008-07-27 21:46:04 +00003230 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003231 }
Evan Cheng110dec22005-12-14 02:19:23 +00003232 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003233
3234 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
3235 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003236 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3237 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003238 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00003239 EVT EVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00003240 if ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003241 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT)) {
Bill Wendling6ce610f2009-01-30 22:23:15 +00003242 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT,
3243 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003244 LN0->getBasePtr(), LN0->getSrcValue(),
3245 LN0->getSrcValueOffset(), EVT,
3246 LN0->isVolatile(), LN0->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003247 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00003248 CombineTo(N0.getNode(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00003249 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), N0.getValueType(),
3250 ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003251 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003252 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003253 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003254 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003255
Chris Lattner20a35c32007-04-11 05:32:27 +00003256 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3257 if (N0.getOpcode() == ISD::SETCC) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003258 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00003259 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Chris Lattner20a35c32007-04-11 05:32:27 +00003260 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003261 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003262 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003263 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003264
Dan Gohman475871a2008-07-27 21:46:04 +00003265 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003266}
3267
Dan Gohman475871a2008-07-27 21:46:04 +00003268SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
3269 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003270 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003271
Chris Lattner5ffc0662006-05-05 05:58:59 +00003272 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003273 if (isa<ConstantSDNode>(N0))
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003274 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, N0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00003275 // fold (aext (aext x)) -> (aext x)
3276 // fold (aext (zext x)) -> (zext x)
3277 // fold (aext (sext x)) -> (sext x)
3278 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3279 N0.getOpcode() == ISD::ZERO_EXTEND ||
3280 N0.getOpcode() == ISD::SIGN_EXTEND)
Bill Wendling683c9572009-01-30 22:27:33 +00003281 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00003282
Evan Chengc88138f2007-03-22 01:54:19 +00003283 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3284 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3285 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003286 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3287 if (NarrowLoad.getNode()) {
3288 if (NarrowLoad.getNode() != N0.getNode())
3289 CombineTo(N0.getNode(), NarrowLoad);
Bill Wendling683c9572009-01-30 22:27:33 +00003290 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003291 }
Evan Chengc88138f2007-03-22 01:54:19 +00003292 }
3293
Chris Lattner84750582006-09-20 06:29:17 +00003294 // fold (aext (truncate x))
3295 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00003296 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00003297 if (TruncOp.getValueType() == VT)
3298 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003299 if (TruncOp.getValueType().bitsGT(VT))
Bill Wendling683c9572009-01-30 22:27:33 +00003300 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, TruncOp);
3301 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, TruncOp);
Chris Lattner84750582006-09-20 06:29:17 +00003302 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003303
Dan Gohman97121ba2009-04-08 00:15:30 +00003304 // Fold (aext (and (trunc x), cst)) -> (and x, cst)
3305 // if the trunc is not free.
Chris Lattner0e4b9222006-09-21 06:40:43 +00003306 if (N0.getOpcode() == ISD::AND &&
3307 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00003308 N0.getOperand(1).getOpcode() == ISD::Constant &&
3309 !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
3310 N0.getValueType())) {
Dan Gohman475871a2008-07-27 21:46:04 +00003311 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003312 if (X.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003313 X = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003314 } else if (X.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003315 X = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, X);
Chris Lattner0e4b9222006-09-21 06:40:43 +00003316 }
Dan Gohman220a8232008-03-03 23:51:38 +00003317 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003318 Mask.zext(VT.getSizeInBits());
Bill Wendling683c9572009-01-30 22:27:33 +00003319 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
3320 X, DAG.getConstant(Mask, VT));
Chris Lattner0e4b9222006-09-21 06:40:43 +00003321 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003322
Chris Lattner5ffc0662006-05-05 05:58:59 +00003323 // fold (aext (load x)) -> (aext (truncate (extload x)))
Dan Gohman57fc82d2009-04-09 03:51:29 +00003324 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003325 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003326 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00003327 bool DoXform = true;
3328 SmallVector<SDNode*, 4> SetCCs;
3329 if (!N0.hasOneUse())
3330 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI);
3331 if (DoXform) {
3332 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3333 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT,
3334 LN0->getChain(),
3335 LN0->getBasePtr(), LN0->getSrcValue(),
3336 LN0->getSrcValueOffset(),
3337 N0.getValueType(),
3338 LN0->isVolatile(), LN0->getAlignment());
3339 CombineTo(N, ExtLoad);
3340 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3341 N0.getValueType(), ExtLoad);
3342 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
3343
3344 // Extend SetCC uses if necessary.
3345 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3346 SDNode *SetCC = SetCCs[i];
3347 SmallVector<SDValue, 4> Ops;
3348
3349 for (unsigned j = 0; j != 2; ++j) {
3350 SDValue SOp = SetCC->getOperand(j);
3351 if (SOp == Trunc)
3352 Ops.push_back(ExtLoad);
3353 else
3354 Ops.push_back(DAG.getNode(ISD::ANY_EXTEND,
3355 N->getDebugLoc(), VT, SOp));
3356 }
3357
3358 Ops.push_back(SetCC->getOperand(2));
3359 CombineTo(SetCC, DAG.getNode(ISD::SETCC, N->getDebugLoc(),
3360 SetCC->getValueType(0),
3361 &Ops[0], Ops.size()));
3362 }
3363
3364 return SDValue(N, 0); // Return N so it doesn't get rechecked!
3365 }
Chris Lattner5ffc0662006-05-05 05:58:59 +00003366 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003367
Chris Lattner5ffc0662006-05-05 05:58:59 +00003368 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3369 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3370 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003371 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003372 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003373 N0.hasOneUse()) {
3374 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00003375 EVT EVT = LN0->getMemoryVT();
Bill Wendling683c9572009-01-30 22:27:33 +00003376 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), N->getDebugLoc(),
3377 VT, LN0->getChain(), LN0->getBasePtr(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003378 LN0->getSrcValue(),
3379 LN0->getSrcValueOffset(), EVT,
3380 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003381 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00003382 CombineTo(N0.getNode(),
Bill Wendling683c9572009-01-30 22:27:33 +00003383 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3384 N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00003385 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003386 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003387 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003388
Chris Lattner20a35c32007-04-11 05:32:27 +00003389 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3390 if (N0.getOpcode() == ISD::SETCC) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003391 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00003392 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003393 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003394 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003395 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003396 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003397 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003398
Dan Gohman475871a2008-07-27 21:46:04 +00003399 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00003400}
3401
Chris Lattner2b4c2792007-10-13 06:35:54 +00003402/// GetDemandedBits - See if the specified operand can be simplified with the
3403/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00003404/// simpler operand, otherwise return a null SDValue.
3405SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003406 switch (V.getOpcode()) {
3407 default: break;
3408 case ISD::OR:
3409 case ISD::XOR:
3410 // If the LHS or RHS don't contribute bits to the or, drop them.
3411 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3412 return V.getOperand(1);
3413 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3414 return V.getOperand(0);
3415 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003416 case ISD::SRL:
3417 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003418 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00003419 break;
3420 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3421 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003422 unsigned Amt = RHSC->getZExtValue();
Bill Wendling8509c902009-01-30 22:33:24 +00003423
Dan Gohmancc91d632009-01-03 19:22:06 +00003424 // Watch out for shift count overflow though.
3425 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003426 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00003427 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Bill Wendling8509c902009-01-30 22:33:24 +00003428 if (SimplifyLHS.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00003429 return DAG.getNode(ISD::SRL, V.getDebugLoc(), V.getValueType(),
Chris Lattnere33544c2007-10-13 06:58:48 +00003430 SimplifyLHS, V.getOperand(1));
Chris Lattnere33544c2007-10-13 06:58:48 +00003431 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003432 }
Dan Gohman475871a2008-07-27 21:46:04 +00003433 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00003434}
3435
Evan Chengc88138f2007-03-22 01:54:19 +00003436/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3437/// bits and then truncated to a narrower type and where N is a multiple
3438/// of number of bits of the narrower type, transform it to a narrower load
3439/// from address + N / num of bits of new type. If the result is to be
3440/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00003441SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00003442 unsigned Opc = N->getOpcode();
3443 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00003444 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003445 EVT VT = N->getValueType(0);
3446 EVT ExtVT = VT;
Evan Chengc88138f2007-03-22 01:54:19 +00003447
Dan Gohman7f8613e2008-08-14 20:04:46 +00003448 // This transformation isn't valid for vector loads.
3449 if (VT.isVector())
3450 return SDValue();
3451
Evan Chenge177e302007-03-23 22:13:36 +00003452 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3453 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003454 if (Opc == ISD::SIGN_EXTEND_INREG) {
3455 ExtType = ISD::SEXTLOAD;
Owen Andersone50ed302009-08-10 22:56:29 +00003456 ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT();
3457 if (LegalOperations && !TLI.isLoadExtLegal(ISD::SEXTLOAD, ExtVT))
Dan Gohman475871a2008-07-27 21:46:04 +00003458 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003459 }
3460
Owen Andersone50ed302009-08-10 22:56:29 +00003461 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Chengc88138f2007-03-22 01:54:19 +00003462 unsigned ShAmt = 0;
3463 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3464 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003465 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003466 // Is the shift amount a multiple of size of VT?
3467 if ((ShAmt & (EVTBits-1)) == 0) {
3468 N0 = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003469 if (N0.getValueType().getSizeInBits() <= EVTBits)
Dan Gohman475871a2008-07-27 21:46:04 +00003470 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003471 }
3472 }
3473 }
3474
Duncan Sandsad205a72008-06-16 08:14:38 +00003475 // Do not generate loads of non-round integer types since these can
3476 // be expensive (and would be wrong if the type is not byte sized).
Owen Andersone50ed302009-08-10 22:56:29 +00003477 if (isa<LoadSDNode>(N0) && N0.hasOneUse() && ExtVT.isRound() &&
Dan Gohman75dcf082008-07-31 00:50:31 +00003478 cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() > EVTBits &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003479 // Do not change the width of a volatile load.
3480 !cast<LoadSDNode>(N0)->isVolatile()) {
Evan Chengc88138f2007-03-22 01:54:19 +00003481 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00003482 EVT PtrType = N0.getOperand(1).getValueType();
Bill Wendling8509c902009-01-30 22:33:24 +00003483
Evan Chengdae54ce2007-03-24 00:02:43 +00003484 // For big endian targets, we need to adjust the offset to the pointer to
3485 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003486 if (TLI.isBigEndian()) {
Dan Gohman75dcf082008-07-31 00:50:31 +00003487 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
Owen Andersone50ed302009-08-10 22:56:29 +00003488 unsigned EVTStoreBits = ExtVT.getStoreSizeInBits();
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003489 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3490 }
Bill Wendling8509c902009-01-30 22:33:24 +00003491
Evan Chengdae54ce2007-03-24 00:02:43 +00003492 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003493 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Bill Wendling9729c5a2009-01-31 03:12:48 +00003494 SDValue NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(),
Bill Wendling8509c902009-01-30 22:33:24 +00003495 PtrType, LN0->getBasePtr(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003496 DAG.getConstant(PtrOff, PtrType));
Gabor Greifba36cb52008-08-28 21:40:38 +00003497 AddToWorkList(NewPtr.getNode());
Bill Wendling8509c902009-01-30 22:33:24 +00003498
Dan Gohman475871a2008-07-27 21:46:04 +00003499 SDValue Load = (ExtType == ISD::NON_EXTLOAD)
Bill Wendling8509c902009-01-30 22:33:24 +00003500 ? DAG.getLoad(VT, N0.getDebugLoc(), LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003501 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
Duncan Sandsdc846502007-10-28 12:59:45 +00003502 LN0->isVolatile(), NewAlign)
Bill Wendling8509c902009-01-30 22:33:24 +00003503 : DAG.getExtLoad(ExtType, N0.getDebugLoc(), VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003504 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
Owen Andersone50ed302009-08-10 22:56:29 +00003505 ExtVT, LN0->isVolatile(), NewAlign);
Bill Wendling8509c902009-01-30 22:33:24 +00003506
Dan Gohman764fd0c2009-01-21 15:17:51 +00003507 // Replace the old load's chain with the new load's chain.
3508 WorkListRemover DeadNodes(*this);
3509 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3510 &DeadNodes);
Bill Wendling8509c902009-01-30 22:33:24 +00003511
Dan Gohman764fd0c2009-01-21 15:17:51 +00003512 // Return the new loaded value.
3513 return Load;
Evan Chengc88138f2007-03-22 01:54:19 +00003514 }
3515
Dan Gohman475871a2008-07-27 21:46:04 +00003516 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003517}
3518
Dan Gohman475871a2008-07-27 21:46:04 +00003519SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
3520 SDValue N0 = N->getOperand(0);
3521 SDValue N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00003522 EVT VT = N->getValueType(0);
3523 EVT EVT = cast<VTSDNode>(N1)->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003524 unsigned VTBits = VT.getSizeInBits();
3525 unsigned EVTBits = EVT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003526
Nate Begeman1d4d4142005-09-01 00:19:25 +00003527 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003528 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Bill Wendling8509c902009-01-30 22:33:24 +00003529 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00003530
Chris Lattner541a24f2006-05-06 22:43:44 +00003531 // If the input is already sign extended, just drop the extension.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003532 if (DAG.ComputeNumSignBits(N0) >= VT.getSizeInBits()-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003533 return N0;
Scott Michelfdc40a02009-02-17 22:15:04 +00003534
Nate Begeman646d7e22005-09-02 21:18:40 +00003535 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3536 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00003537 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Bill Wendling8509c902009-01-30 22:33:24 +00003538 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
3539 N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003540 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003541
Dan Gohman75dcf082008-07-31 00:50:31 +00003542 // fold (sext_in_reg (sext x)) -> (sext x)
3543 // fold (sext_in_reg (aext x)) -> (sext x)
3544 // if x is small enough.
3545 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
3546 SDValue N00 = N0.getOperand(0);
3547 if (N00.getValueType().getSizeInBits() < EVTBits)
Bill Wendling8509c902009-01-30 22:33:24 +00003548 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N00, N1);
Dan Gohman75dcf082008-07-31 00:50:31 +00003549 }
3550
Chris Lattner95a5e052007-04-17 19:03:21 +00003551 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003552 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003553 return DAG.getZeroExtendInReg(N0, N->getDebugLoc(), EVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003554
Chris Lattner95a5e052007-04-17 19:03:21 +00003555 // fold operands of sext_in_reg based on knowledge that the top bits are not
3556 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00003557 if (SimplifyDemandedBits(SDValue(N, 0)))
3558 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003559
Evan Chengc88138f2007-03-22 01:54:19 +00003560 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3561 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00003562 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003563 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00003564 return NarrowLoad;
3565
Bill Wendling8509c902009-01-30 22:33:24 +00003566 // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24)
3567 // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible.
Chris Lattner4b37e872006-05-08 21:18:59 +00003568 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3569 if (N0.getOpcode() == ISD::SRL) {
3570 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003571 if (ShAmt->getZExtValue()+EVTBits <= VT.getSizeInBits()) {
Chris Lattner4b37e872006-05-08 21:18:59 +00003572 // We can turn this into an SRA iff the input to the SRL is already sign
3573 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003574 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003575 if (VT.getSizeInBits()-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Bill Wendling8509c902009-01-30 22:33:24 +00003576 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT,
3577 N0.getOperand(0), N0.getOperand(1));
Chris Lattner4b37e872006-05-08 21:18:59 +00003578 }
3579 }
Evan Chengc88138f2007-03-22 01:54:19 +00003580
Nate Begemanded49632005-10-13 03:11:28 +00003581 // fold (sext_inreg (extload x)) -> (sextload x)
Scott Michelfdc40a02009-02-17 22:15:04 +00003582 if (ISD::isEXTLoad(N0.getNode()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003583 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003584 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003585 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003586 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003587 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendling8509c902009-01-30 22:33:24 +00003588 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
3589 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003590 LN0->getBasePtr(), LN0->getSrcValue(),
3591 LN0->getSrcValueOffset(), EVT,
3592 LN0->isVolatile(), LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003593 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003594 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003595 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003596 }
3597 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00003598 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003599 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003600 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003601 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003602 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003603 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendling8509c902009-01-30 22:33:24 +00003604 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
3605 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003606 LN0->getBasePtr(), LN0->getSrcValue(),
3607 LN0->getSrcValueOffset(), EVT,
3608 LN0->isVolatile(), LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003609 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003610 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003611 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003612 }
Dan Gohman475871a2008-07-27 21:46:04 +00003613 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003614}
3615
Dan Gohman475871a2008-07-27 21:46:04 +00003616SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
3617 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003618 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003619
3620 // noop truncate
3621 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003622 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003623 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003624 if (isa<ConstantSDNode>(N0))
Bill Wendling67a67682009-01-30 22:44:24 +00003625 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003626 // fold (truncate (truncate x)) -> (truncate x)
3627 if (N0.getOpcode() == ISD::TRUNCATE)
Bill Wendling67a67682009-01-30 22:44:24 +00003628 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003629 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003630 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3631 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00003632 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003633 // if the source is smaller than the dest, we still need an extend
Bill Wendling67a67682009-01-30 22:44:24 +00003634 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
3635 N0.getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00003636 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003637 // if the source is larger than the dest, than we just need the truncate
Bill Wendling67a67682009-01-30 22:44:24 +00003638 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003639 else
3640 // if the source and dest are the same type, we can drop both the extend
3641 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003642 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003643 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003644
Chris Lattner2b4c2792007-10-13 06:35:54 +00003645 // See if we can simplify the input to this truncate through knowledge that
3646 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3647 // -> trunc y
Dan Gohman475871a2008-07-27 21:46:04 +00003648 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003649 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00003650 VT.getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003651 if (Shorter.getNode())
Bill Wendling67a67682009-01-30 22:44:24 +00003652 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Shorter);
Chris Lattner2b4c2792007-10-13 06:35:54 +00003653
Nate Begeman3df4d522005-10-12 20:40:40 +00003654 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003655 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003656 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003657}
3658
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003659static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003660 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003661 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00003662 return Elt.getNode();
3663 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003664}
3665
3666/// CombineConsecutiveLoads - build_pair (load, load) -> load
Scott Michelfdc40a02009-02-17 22:15:04 +00003667/// if load locations are consecutive.
Owen Andersone50ed302009-08-10 22:56:29 +00003668SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003669 assert(N->getOpcode() == ISD::BUILD_PAIR);
3670
Nate Begemanabc01992009-06-05 21:37:30 +00003671 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0));
3672 LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1));
3673 if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00003674 return SDValue();
Owen Andersone50ed302009-08-10 22:56:29 +00003675 EVT LD1VT = LD1->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003676 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bill Wendling67a67682009-01-30 22:44:24 +00003677
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003678 if (ISD::isNON_EXTLoad(LD2) &&
3679 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003680 // If both are volatile this would reduce the number of volatile loads.
3681 // If one is volatile it might be ok, but play conservative and bail out.
Nate Begemanabc01992009-06-05 21:37:30 +00003682 !LD1->isVolatile() &&
3683 !LD2->isVolatile() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003684 TLI.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1, MFI)) {
Nate Begemanabc01992009-06-05 21:37:30 +00003685 unsigned Align = LD1->getAlignment();
Dan Gohman6448d912008-09-04 15:39:15 +00003686 unsigned NewAlign = TLI.getTargetData()->
Owen Andersone50ed302009-08-10 22:56:29 +00003687 getABITypeAlignment(VT.getTypeForEVT());
Bill Wendling67a67682009-01-30 22:44:24 +00003688
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003689 if (NewAlign <= Align &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003690 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Nate Begemanabc01992009-06-05 21:37:30 +00003691 return DAG.getLoad(VT, N->getDebugLoc(), LD1->getChain(),
3692 LD1->getBasePtr(), LD1->getSrcValue(),
3693 LD1->getSrcValueOffset(), false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003694 }
Bill Wendling67a67682009-01-30 22:44:24 +00003695
Dan Gohman475871a2008-07-27 21:46:04 +00003696 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003697}
3698
Dan Gohman475871a2008-07-27 21:46:04 +00003699SDValue DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3700 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003701 EVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00003702
Dan Gohman7f321562007-06-25 16:23:39 +00003703 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3704 // Only do this before legalize, since afterward the target may be depending
3705 // on the bitconvert.
3706 // First check to see if this is all constant.
Duncan Sands25cf2272008-11-24 14:53:14 +00003707 if (!LegalTypes &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003708 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003709 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003710 bool isSimple = true;
3711 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3712 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3713 N0.getOperand(i).getOpcode() != ISD::Constant &&
3714 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003715 isSimple = false;
Dan Gohman7f321562007-06-25 16:23:39 +00003716 break;
3717 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003718
Owen Andersone50ed302009-08-10 22:56:29 +00003719 EVT DestEltVT = N->getValueType(0).getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003720 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00003721 "Element type of vector ValueType must not be vector!");
Bill Wendling67a67682009-01-30 22:44:24 +00003722 if (isSimple)
Gabor Greifba36cb52008-08-28 21:40:38 +00003723 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00003724 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003725
Dan Gohman3dd168d2008-09-05 01:58:21 +00003726 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00003727 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Bill Wendling67a67682009-01-30 22:44:24 +00003728 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, N0);
Dan Gohmana407ca12009-08-10 23:15:10 +00003729 if (Res.getNode() != N) {
3730 if (!LegalOperations ||
3731 TLI.isOperationLegal(Res.getNode()->getOpcode(), VT))
3732 return Res;
3733
3734 // Folding it resulted in an illegal node, and it's too late to
3735 // do that. Clean up the old node and forego the transformation.
3736 // Ideally this won't happen very often, because instcombine
3737 // and the earlier dagcombine runs (where illegal nodes are
3738 // permitted) should have folded most of them already.
3739 DAG.DeleteNode(Res.getNode());
3740 }
Chris Lattner94683772005-12-23 05:30:37 +00003741 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003742
Bill Wendling67a67682009-01-30 22:44:24 +00003743 // (conv (conv x, t1), t2) -> (conv x, t2)
3744 if (N0.getOpcode() == ISD::BIT_CONVERT)
3745 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT,
3746 N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003747
Chris Lattner57104102005-12-23 05:44:41 +00003748 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003749 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00003750 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003751 // Do not change the width of a volatile load.
3752 !cast<LoadSDNode>(N0)->isVolatile() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003753 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003754 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman6448d912008-09-04 15:39:15 +00003755 unsigned Align = TLI.getTargetData()->
Owen Andersone50ed302009-08-10 22:56:29 +00003756 getABITypeAlignment(VT.getTypeForEVT());
Evan Cheng59d5b682007-05-07 21:27:48 +00003757 unsigned OrigAlign = LN0->getAlignment();
Bill Wendling67a67682009-01-30 22:44:24 +00003758
Evan Cheng59d5b682007-05-07 21:27:48 +00003759 if (Align <= OrigAlign) {
Bill Wendling67a67682009-01-30 22:44:24 +00003760 SDValue Load = DAG.getLoad(VT, N->getDebugLoc(), LN0->getChain(),
3761 LN0->getBasePtr(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003762 LN0->getSrcValue(), LN0->getSrcValueOffset(),
3763 LN0->isVolatile(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00003764 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00003765 CombineTo(N0.getNode(),
Bill Wendling67a67682009-01-30 22:44:24 +00003766 DAG.getNode(ISD::BIT_CONVERT, N0.getDebugLoc(),
3767 N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00003768 Load.getValue(1));
3769 return Load;
3770 }
Chris Lattner57104102005-12-23 05:44:41 +00003771 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003772
Bill Wendling67a67682009-01-30 22:44:24 +00003773 // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
3774 // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
Chris Lattner3bd39d42008-01-27 17:42:27 +00003775 // This often reduces constant pool loads.
3776 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003777 N0.getNode()->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Bill Wendling67a67682009-01-30 22:44:24 +00003778 SDValue NewConv = DAG.getNode(ISD::BIT_CONVERT, N0.getDebugLoc(), VT,
3779 N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00003780 AddToWorkList(NewConv.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00003781
Duncan Sands83ec4b62008-06-06 12:08:01 +00003782 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003783 if (N0.getOpcode() == ISD::FNEG)
Bill Wendling67a67682009-01-30 22:44:24 +00003784 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT,
3785 NewConv, DAG.getConstant(SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00003786 assert(N0.getOpcode() == ISD::FABS);
Bill Wendling67a67682009-01-30 22:44:24 +00003787 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
3788 NewConv, DAG.getConstant(~SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00003789 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003790
Bill Wendling67a67682009-01-30 22:44:24 +00003791 // fold (bitconvert (fcopysign cst, x)) ->
3792 // (or (and (bitconvert x), sign), (and cst, (not sign)))
3793 // Note that we don't handle (copysign x, cst) because this can always be
3794 // folded to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003795 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00003796 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003797 VT.isInteger() && !VT.isVector()) {
3798 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Owen Andersone50ed302009-08-10 22:56:29 +00003799 EVT IntXVT = EVT::getIntegerVT(OrigXWidth);
Duncan Sands25cf2272008-11-24 14:53:14 +00003800 if (TLI.isTypeLegal(IntXVT) || !LegalTypes) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003801 SDValue X = DAG.getNode(ISD::BIT_CONVERT, N0.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00003802 IntXVT, N0.getOperand(1));
Duncan Sands25cf2272008-11-24 14:53:14 +00003803 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003804
Duncan Sands25cf2272008-11-24 14:53:14 +00003805 // If X has a different width than the result/lhs, sext it or truncate it.
3806 unsigned VTWidth = VT.getSizeInBits();
3807 if (OrigXWidth < VTWidth) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003808 X = DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00003809 AddToWorkList(X.getNode());
3810 } else if (OrigXWidth > VTWidth) {
3811 // To get the sign bit in the right place, we have to shift it right
3812 // before truncating.
Bill Wendling9729c5a2009-01-31 03:12:48 +00003813 X = DAG.getNode(ISD::SRL, X.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00003814 X.getValueType(), X,
Duncan Sands25cf2272008-11-24 14:53:14 +00003815 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
3816 AddToWorkList(X.getNode());
Bill Wendling9729c5a2009-01-31 03:12:48 +00003817 X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00003818 AddToWorkList(X.getNode());
3819 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003820
Duncan Sands25cf2272008-11-24 14:53:14 +00003821 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Bill Wendling9729c5a2009-01-31 03:12:48 +00003822 X = DAG.getNode(ISD::AND, X.getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00003823 X, DAG.getConstant(SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00003824 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003825
Bill Wendling9729c5a2009-01-31 03:12:48 +00003826 SDValue Cst = DAG.getNode(ISD::BIT_CONVERT, N0.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00003827 VT, N0.getOperand(0));
Bill Wendling9729c5a2009-01-31 03:12:48 +00003828 Cst = DAG.getNode(ISD::AND, Cst.getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00003829 Cst, DAG.getConstant(~SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00003830 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003831
Bill Wendling67a67682009-01-30 22:44:24 +00003832 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, X, Cst);
Duncan Sands25cf2272008-11-24 14:53:14 +00003833 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003834 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003835
Scott Michelfdc40a02009-02-17 22:15:04 +00003836 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003837 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003838 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
3839 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003840 return CombineLD;
3841 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003842
Dan Gohman475871a2008-07-27 21:46:04 +00003843 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00003844}
3845
Dan Gohman475871a2008-07-27 21:46:04 +00003846SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00003847 EVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003848 return CombineConsecutiveLoads(N, VT);
3849}
3850
Dan Gohman7f321562007-06-25 16:23:39 +00003851/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Scott Michelfdc40a02009-02-17 22:15:04 +00003852/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
Chris Lattner6258fb22006-04-02 02:53:43 +00003853/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00003854SDValue DAGCombiner::
Owen Andersone50ed302009-08-10 22:56:29 +00003855ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) {
3856 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003857
Chris Lattner6258fb22006-04-02 02:53:43 +00003858 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00003859 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003860
Duncan Sands83ec4b62008-06-06 12:08:01 +00003861 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
3862 unsigned DstBitSize = DstEltVT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003863
Chris Lattner6258fb22006-04-02 02:53:43 +00003864 // If this is a conversion of N elements of one type to N elements of another
3865 // type, convert each element. This handles FP<->INT cases.
3866 if (SrcBitSize == DstBitSize) {
Dan Gohman475871a2008-07-27 21:46:04 +00003867 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003868 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Bob Wilsonb1303d02009-04-13 22:05:19 +00003869 SDValue Op = BV->getOperand(i);
3870 // If the vector element type is not legal, the BUILD_VECTOR operands
3871 // are promoted and implicitly truncated. Make that explicit here.
Bob Wilsonc8851652009-04-20 17:27:09 +00003872 if (Op.getValueType() != SrcEltVT)
3873 Op = DAG.getNode(ISD::TRUNCATE, BV->getDebugLoc(), SrcEltVT, Op);
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003874 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, BV->getDebugLoc(),
Bob Wilsonb1303d02009-04-13 22:05:19 +00003875 DstEltVT, Op));
Gabor Greifba36cb52008-08-28 21:40:38 +00003876 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00003877 }
Owen Andersone50ed302009-08-10 22:56:29 +00003878 EVT VT = EVT::getVectorVT(DstEltVT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003879 BV->getValueType(0).getVectorNumElements());
Evan Chenga87008d2009-02-25 22:49:59 +00003880 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
3881 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003882 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003883
Chris Lattner6258fb22006-04-02 02:53:43 +00003884 // Otherwise, we're growing or shrinking the elements. To avoid having to
3885 // handle annoying details of growing/shrinking FP values, we convert them to
3886 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003887 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003888 // Convert the input float vector to a int vector where the elements are the
3889 // same sizes.
Owen Anderson825b72b2009-08-11 20:47:22 +00003890 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Owen Andersone50ed302009-08-10 22:56:29 +00003891 EVT IntVT = EVT::getIntegerVT(SrcEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00003892 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00003893 SrcEltVT = IntVT;
3894 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003895
Chris Lattner6258fb22006-04-02 02:53:43 +00003896 // Now we know the input is an integer vector. If the output is a FP type,
3897 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003898 if (DstEltVT.isFloatingPoint()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003899 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Owen Andersone50ed302009-08-10 22:56:29 +00003900 EVT TmpVT = EVT::getIntegerVT(DstEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00003901 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00003902
Chris Lattner6258fb22006-04-02 02:53:43 +00003903 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003904 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003905 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003906
Chris Lattner6258fb22006-04-02 02:53:43 +00003907 // Okay, we know the src/dst types are both integers of differing types.
3908 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003909 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00003910 if (SrcBitSize < DstBitSize) {
3911 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
Scott Michelfdc40a02009-02-17 22:15:04 +00003912
Dan Gohman475871a2008-07-27 21:46:04 +00003913 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003914 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003915 i += NumInputsPerOutput) {
3916 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00003917 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003918 bool EltIsUndef = true;
3919 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3920 // Shift the previously computed bits over.
3921 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00003922 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00003923 if (Op.getOpcode() == ISD::UNDEF) continue;
3924 EltIsUndef = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00003925
Bob Wilsonb1303d02009-04-13 22:05:19 +00003926 NewBits |= (APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).
3927 zextOrTrunc(SrcBitSize).zext(DstBitSize));
Chris Lattner6258fb22006-04-02 02:53:43 +00003928 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003929
Chris Lattner6258fb22006-04-02 02:53:43 +00003930 if (EltIsUndef)
Dale Johannesene8d72302009-02-06 23:05:02 +00003931 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00003932 else
3933 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3934 }
3935
Owen Andersone50ed302009-08-10 22:56:29 +00003936 EVT VT = EVT::getVectorVT(DstEltVT, Ops.size());
Evan Chenga87008d2009-02-25 22:49:59 +00003937 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
3938 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003939 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003940
Chris Lattner6258fb22006-04-02 02:53:43 +00003941 // Finally, this must be the case where we are shrinking elements: each input
3942 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00003943 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00003944 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Owen Andersone50ed302009-08-10 22:56:29 +00003945 EVT VT = EVT::getVectorVT(DstEltVT, NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00003946 SmallVector<SDValue, 8> Ops;
Bill Wendlingb0162f52009-01-30 22:53:48 +00003947
Dan Gohman7f321562007-06-25 16:23:39 +00003948 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003949 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3950 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
Dale Johannesene8d72302009-02-06 23:05:02 +00003951 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00003952 continue;
3953 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00003954
Bob Wilsonb1303d02009-04-13 22:05:19 +00003955 APInt OpVal = APInt(cast<ConstantSDNode>(BV->getOperand(i))->
3956 getAPIntValue()).zextOrTrunc(SrcBitSize);
Bill Wendlingb0162f52009-01-30 22:53:48 +00003957
Chris Lattner6258fb22006-04-02 02:53:43 +00003958 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohman220a8232008-03-03 23:51:38 +00003959 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003960 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohman220a8232008-03-03 23:51:38 +00003961 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00003962 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
Bill Wendlingb0162f52009-01-30 22:53:48 +00003963 return DAG.getNode(ISD::SCALAR_TO_VECTOR, BV->getDebugLoc(), VT,
3964 Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00003965 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003966 }
3967
3968 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00003969 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00003970 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3971 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00003972
Evan Chenga87008d2009-02-25 22:49:59 +00003973 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
3974 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003975}
3976
Dan Gohman475871a2008-07-27 21:46:04 +00003977SDValue DAGCombiner::visitFADD(SDNode *N) {
3978 SDValue N0 = N->getOperand(0);
3979 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003980 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3981 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003982 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003983
Dan Gohman7f321562007-06-25 16:23:39 +00003984 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003985 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003986 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003987 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003988 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003989
Bill Wendlingb0162f52009-01-30 22:53:48 +00003990 // fold (fadd c1, c2) -> (fadd c1, c2)
Owen Anderson825b72b2009-08-11 20:47:22 +00003991 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlingb0162f52009-01-30 22:53:48 +00003992 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003993 // canonicalize constant to RHS
3994 if (N0CFP && !N1CFP)
Bill Wendlingb0162f52009-01-30 22:53:48 +00003995 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N1, N0);
3996 // fold (fadd A, 0) -> A
Dan Gohman760f86f2009-01-22 21:58:43 +00003997 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
3998 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00003999 // fold (fadd A, (fneg B)) -> (fsub A, B)
Duncan Sands25cf2272008-11-24 14:53:14 +00004000 if (isNegatibleForFree(N1, LegalOperations) == 2)
Bill Wendlingb0162f52009-01-30 22:53:48 +00004001 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00004002 GetNegatedExpression(N1, DAG, LegalOperations));
Bill Wendlingb0162f52009-01-30 22:53:48 +00004003 // fold (fadd (fneg A), B) -> (fsub B, A)
Duncan Sands25cf2272008-11-24 14:53:14 +00004004 if (isNegatibleForFree(N0, LegalOperations) == 2)
Bill Wendlingb0162f52009-01-30 22:53:48 +00004005 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N1,
Duncan Sands25cf2272008-11-24 14:53:14 +00004006 GetNegatedExpression(N0, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00004007
Chris Lattnerddae4bd2007-01-08 23:04:05 +00004008 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
4009 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004010 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Bill Wendlingb0162f52009-01-30 22:53:48 +00004011 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0.getOperand(0),
Bill Wendlingfc4b6772009-02-01 11:19:36 +00004012 DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
4013 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004014
Dan Gohman475871a2008-07-27 21:46:04 +00004015 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00004016}
4017
Dan Gohman475871a2008-07-27 21:46:04 +00004018SDValue DAGCombiner::visitFSUB(SDNode *N) {
4019 SDValue N0 = N->getOperand(0);
4020 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00004021 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4022 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00004023 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004024
Dan Gohman7f321562007-06-25 16:23:39 +00004025 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00004026 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004027 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00004028 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00004029 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004030
Nate Begemana0e221d2005-10-18 00:28:13 +00004031 // fold (fsub c1, c2) -> c1-c2
Owen Anderson825b72b2009-08-11 20:47:22 +00004032 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlingfc4b6772009-02-01 11:19:36 +00004033 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0, N1);
Bill Wendlingb0162f52009-01-30 22:53:48 +00004034 // fold (fsub A, 0) -> A
Dan Gohmana90c8e62009-01-23 19:10:37 +00004035 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
4036 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00004037 // fold (fsub 0, B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00004038 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Duncan Sands25cf2272008-11-24 14:53:14 +00004039 if (isNegatibleForFree(N1, LegalOperations))
4040 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman760f86f2009-01-22 21:58:43 +00004041 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendlingb0162f52009-01-30 22:53:48 +00004042 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00004043 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00004044 // fold (fsub A, (fneg B)) -> (fadd A, B)
Duncan Sands25cf2272008-11-24 14:53:14 +00004045 if (isNegatibleForFree(N1, LegalOperations))
Bill Wendlingb0162f52009-01-30 22:53:48 +00004046 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00004047 GetNegatedExpression(N1, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00004048
Dan Gohman475871a2008-07-27 21:46:04 +00004049 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00004050}
4051
Dan Gohman475871a2008-07-27 21:46:04 +00004052SDValue DAGCombiner::visitFMUL(SDNode *N) {
4053 SDValue N0 = N->getOperand(0);
4054 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00004055 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4056 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00004057 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00004058
Dan Gohman7f321562007-06-25 16:23:39 +00004059 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00004060 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004061 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00004062 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00004063 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004064
Nate Begeman11af4ea2005-10-17 20:40:11 +00004065 // fold (fmul c1, c2) -> c1*c2
Owen Anderson825b72b2009-08-11 20:47:22 +00004066 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004067 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00004068 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00004069 if (N0CFP && !N1CFP)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004070 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N1, N0);
4071 // fold (fmul A, 0) -> 0
Dan Gohman760f86f2009-01-22 21:58:43 +00004072 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
4073 return N1;
Dan Gohman77b81fe2009-06-04 17:12:12 +00004074 // fold (fmul A, 0) -> 0, vector edition.
4075 if (UnsafeFPMath && ISD::isBuildVectorAllZeros(N1.getNode()))
4076 return N1;
Nate Begeman11af4ea2005-10-17 20:40:11 +00004077 // fold (fmul X, 2.0) -> (fadd X, X)
4078 if (N1CFP && N1CFP->isExactlyValue(+2.0))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004079 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N0);
Dan Gohmaneb1fedc2009-08-10 16:50:32 +00004080 // fold (fmul X, -1.0) -> (fneg X)
Chris Lattner29446522007-05-14 22:04:50 +00004081 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman760f86f2009-01-22 21:58:43 +00004082 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004083 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004084
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004085 // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y)
Duncan Sands25cf2272008-11-24 14:53:14 +00004086 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
4087 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Chris Lattner29446522007-05-14 22:04:50 +00004088 // Both can be negated for free, check to see if at least one is cheaper
4089 // negated.
4090 if (LHSNeg == 2 || RHSNeg == 2)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004091 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00004092 GetNegatedExpression(N0, DAG, LegalOperations),
4093 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00004094 }
4095 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004096
Chris Lattnerddae4bd2007-01-08 23:04:05 +00004097 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
4098 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004099 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004100 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0.getOperand(0),
Scott Michelfdc40a02009-02-17 22:15:04 +00004101 DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
Dale Johannesende064702009-02-06 21:50:26 +00004102 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004103
Dan Gohman475871a2008-07-27 21:46:04 +00004104 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00004105}
4106
Dan Gohman475871a2008-07-27 21:46:04 +00004107SDValue DAGCombiner::visitFDIV(SDNode *N) {
4108 SDValue N0 = N->getOperand(0);
4109 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00004110 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4111 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00004112 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00004113
Dan Gohman7f321562007-06-25 16:23:39 +00004114 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00004115 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004116 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00004117 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00004118 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004119
Nate Begemana148d982006-01-18 22:35:16 +00004120 // fold (fdiv c1, c2) -> c1/c2
Owen Anderson825b72b2009-08-11 20:47:22 +00004121 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004122 return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004123
4124
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004125 // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y)
Duncan Sands25cf2272008-11-24 14:53:14 +00004126 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
4127 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Chris Lattner29446522007-05-14 22:04:50 +00004128 // Both can be negated for free, check to see if at least one is cheaper
4129 // negated.
4130 if (LHSNeg == 2 || RHSNeg == 2)
Scott Michelfdc40a02009-02-17 22:15:04 +00004131 return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00004132 GetNegatedExpression(N0, DAG, LegalOperations),
4133 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00004134 }
4135 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004136
Dan Gohman475871a2008-07-27 21:46:04 +00004137 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00004138}
4139
Dan Gohman475871a2008-07-27 21:46:04 +00004140SDValue DAGCombiner::visitFREM(SDNode *N) {
4141 SDValue N0 = N->getOperand(0);
4142 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00004143 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4144 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00004145 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00004146
Nate Begemana148d982006-01-18 22:35:16 +00004147 // fold (frem c1, c2) -> fmod(c1,c2)
Owen Anderson825b72b2009-08-11 20:47:22 +00004148 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004149 return DAG.getNode(ISD::FREM, N->getDebugLoc(), VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00004150
Dan Gohman475871a2008-07-27 21:46:04 +00004151 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00004152}
4153
Dan Gohman475871a2008-07-27 21:46:04 +00004154SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
4155 SDValue N0 = N->getOperand(0);
4156 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00004157 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4158 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00004159 EVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00004160
Owen Anderson825b72b2009-08-11 20:47:22 +00004161 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Bill Wendlingfc4b6772009-02-01 11:19:36 +00004162 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004163
Chris Lattner12d83032006-03-05 05:30:57 +00004164 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00004165 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00004166 // copysign(x, c1) -> fabs(x) iff ispos(c1)
4167 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman760f86f2009-01-22 21:58:43 +00004168 if (!V.isNegative()) {
4169 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
Bill Wendling0225a1d2009-01-30 23:15:49 +00004170 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Dan Gohman760f86f2009-01-22 21:58:43 +00004171 } else {
4172 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendling0225a1d2009-01-30 23:15:49 +00004173 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00004174 DAG.getNode(ISD::FABS, N0.getDebugLoc(), VT, N0));
Dan Gohman760f86f2009-01-22 21:58:43 +00004175 }
Chris Lattner12d83032006-03-05 05:30:57 +00004176 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004177
Chris Lattner12d83032006-03-05 05:30:57 +00004178 // copysign(fabs(x), y) -> copysign(x, y)
4179 // copysign(fneg(x), y) -> copysign(x, y)
4180 // copysign(copysign(x,z), y) -> copysign(x, y)
4181 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
4182 N0.getOpcode() == ISD::FCOPYSIGN)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004183 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
4184 N0.getOperand(0), N1);
Chris Lattner12d83032006-03-05 05:30:57 +00004185
4186 // copysign(x, abs(y)) -> abs(x)
4187 if (N1.getOpcode() == ISD::FABS)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004188 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004189
Chris Lattner12d83032006-03-05 05:30:57 +00004190 // copysign(x, copysign(y,z)) -> copysign(x, z)
4191 if (N1.getOpcode() == ISD::FCOPYSIGN)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004192 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
4193 N0, N1.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004194
Chris Lattner12d83032006-03-05 05:30:57 +00004195 // copysign(x, fp_extend(y)) -> copysign(x, y)
4196 // copysign(x, fp_round(y)) -> copysign(x, y)
4197 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004198 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
4199 N0, N1.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004200
Dan Gohman475871a2008-07-27 21:46:04 +00004201 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00004202}
4203
Dan Gohman475871a2008-07-27 21:46:04 +00004204SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
4205 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00004206 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004207 EVT VT = N->getValueType(0);
4208 EVT OpVT = N0.getValueType();
Chris Lattnercda88752008-06-26 00:16:49 +00004209
Nate Begeman1d4d4142005-09-01 00:19:25 +00004210 // fold (sint_to_fp c1) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00004211 if (N0C && OpVT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004212 return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004213
Chris Lattnercda88752008-06-26 00:16:49 +00004214 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
4215 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004216 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
4217 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004218 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00004219 if (DAG.SignBitIsZero(N0))
Bill Wendling0225a1d2009-01-30 23:15:49 +00004220 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00004221 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00004222
Dan Gohman475871a2008-07-27 21:46:04 +00004223 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004224}
4225
Dan Gohman475871a2008-07-27 21:46:04 +00004226SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
4227 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00004228 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004229 EVT VT = N->getValueType(0);
4230 EVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00004231
Nate Begeman1d4d4142005-09-01 00:19:25 +00004232 // fold (uint_to_fp c1) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00004233 if (N0C && OpVT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004234 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004235
Chris Lattnercda88752008-06-26 00:16:49 +00004236 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
4237 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004238 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
4239 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004240 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00004241 if (DAG.SignBitIsZero(N0))
Bill Wendling0225a1d2009-01-30 23:15:49 +00004242 return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00004243 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004244
Dan Gohman475871a2008-07-27 21:46:04 +00004245 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004246}
4247
Dan Gohman475871a2008-07-27 21:46:04 +00004248SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
4249 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004250 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004251 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004252
Nate Begeman1d4d4142005-09-01 00:19:25 +00004253 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00004254 if (N0CFP)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004255 return DAG.getNode(ISD::FP_TO_SINT, N->getDebugLoc(), VT, N0);
4256
Dan Gohman475871a2008-07-27 21:46:04 +00004257 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004258}
4259
Dan Gohman475871a2008-07-27 21:46:04 +00004260SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
4261 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004262 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004263 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004264
Nate Begeman1d4d4142005-09-01 00:19:25 +00004265 // fold (fp_to_uint c1fp) -> c1
Owen Anderson825b72b2009-08-11 20:47:22 +00004266 if (N0CFP && VT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004267 return DAG.getNode(ISD::FP_TO_UINT, N->getDebugLoc(), VT, N0);
4268
Dan Gohman475871a2008-07-27 21:46:04 +00004269 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004270}
4271
Dan Gohman475871a2008-07-27 21:46:04 +00004272SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
4273 SDValue N0 = N->getOperand(0);
4274 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00004275 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004276 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004277
Nate Begeman1d4d4142005-09-01 00:19:25 +00004278 // fold (fp_round c1fp) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00004279 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004280 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004281
Chris Lattner79dbea52006-03-13 06:26:26 +00004282 // fold (fp_round (fp_extend x)) -> x
4283 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
4284 return N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004285
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00004286 // fold (fp_round (fp_round x)) -> (fp_round x)
4287 if (N0.getOpcode() == ISD::FP_ROUND) {
4288 // This is a value preserving truncation if both round's are.
4289 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004290 N0.getNode()->getConstantOperandVal(1) == 1;
Bill Wendling0225a1d2009-01-30 23:15:49 +00004291 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0.getOperand(0),
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00004292 DAG.getIntPtrConstant(IsTrunc));
4293 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004294
Chris Lattner79dbea52006-03-13 06:26:26 +00004295 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00004296 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Bill Wendling0225a1d2009-01-30 23:15:49 +00004297 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(), VT,
4298 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00004299 AddToWorkList(Tmp.getNode());
Bill Wendling0225a1d2009-01-30 23:15:49 +00004300 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
4301 Tmp, N0.getOperand(1));
Chris Lattner79dbea52006-03-13 06:26:26 +00004302 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004303
Dan Gohman475871a2008-07-27 21:46:04 +00004304 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004305}
4306
Dan Gohman475871a2008-07-27 21:46:04 +00004307SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
4308 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004309 EVT VT = N->getValueType(0);
4310 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00004311 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004312
Nate Begeman1d4d4142005-09-01 00:19:25 +00004313 // fold (fp_round_inreg c1fp) -> c1fp
Duncan Sands25cf2272008-11-24 14:53:14 +00004314 if (N0CFP && (TLI.isTypeLegal(EVT) || !LegalTypes)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00004315 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Bill Wendling0225a1d2009-01-30 23:15:49 +00004316 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004317 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00004318
Dan Gohman475871a2008-07-27 21:46:04 +00004319 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004320}
4321
Dan Gohman475871a2008-07-27 21:46:04 +00004322SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
4323 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004324 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004325 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004326
Chris Lattner5938bef2007-12-29 06:55:23 +00004327 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Scott Michelfdc40a02009-02-17 22:15:04 +00004328 if (N->hasOneUse() &&
Dan Gohmane7852d02009-01-26 04:35:06 +00004329 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman475871a2008-07-27 21:46:04 +00004330 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004331
Nate Begeman1d4d4142005-09-01 00:19:25 +00004332 // fold (fp_extend c1fp) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00004333 if (N0CFP && VT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004334 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004335
4336 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
4337 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00004338 if (N0.getOpcode() == ISD::FP_ROUND
4339 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004340 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004341 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00004342 if (VT.bitsLT(In.getValueType()))
Bill Wendling0225a1d2009-01-30 23:15:49 +00004343 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT,
4344 In, N0.getOperand(1));
4345 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, In);
Chris Lattner0bd48932008-01-17 07:00:52 +00004346 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004347
Chris Lattner0bd48932008-01-17 07:00:52 +00004348 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004349 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004350 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004351 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00004352 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendling0225a1d2009-01-30 23:15:49 +00004353 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT,
4354 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004355 LN0->getBasePtr(), LN0->getSrcValue(),
4356 LN0->getSrcValueOffset(),
4357 N0.getValueType(),
4358 LN0->isVolatile(), LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00004359 CombineTo(N, ExtLoad);
Bill Wendling0225a1d2009-01-30 23:15:49 +00004360 CombineTo(N0.getNode(),
4361 DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(),
4362 N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00004363 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004364 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00004365 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004366
Dan Gohman475871a2008-07-27 21:46:04 +00004367 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004368}
4369
Dan Gohman475871a2008-07-27 21:46:04 +00004370SDValue DAGCombiner::visitFNEG(SDNode *N) {
4371 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004372
Duncan Sands25cf2272008-11-24 14:53:14 +00004373 if (isNegatibleForFree(N0, LegalOperations))
4374 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohman23ff1822007-07-02 15:48:56 +00004375
Chris Lattner3bd39d42008-01-27 17:42:27 +00004376 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
4377 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004378 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004379 N0.getOperand(0).getValueType().isInteger() &&
4380 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004381 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004382 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004383 if (IntVT.isInteger() && !IntVT.isVector()) {
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00004384 Int = DAG.getNode(ISD::XOR, N0.getDebugLoc(), IntVT, Int,
4385 DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004386 AddToWorkList(Int.getNode());
Bill Wendling0225a1d2009-01-30 23:15:49 +00004387 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(),
4388 N->getValueType(0), Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00004389 }
4390 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004391
Dan Gohman475871a2008-07-27 21:46:04 +00004392 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004393}
4394
Dan Gohman475871a2008-07-27 21:46:04 +00004395SDValue DAGCombiner::visitFABS(SDNode *N) {
4396 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004397 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004398 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004399
Nate Begeman1d4d4142005-09-01 00:19:25 +00004400 // fold (fabs c1) -> fabs(c1)
Owen Anderson825b72b2009-08-11 20:47:22 +00004401 if (N0CFP && VT != MVT::ppcf128)
Bill Wendlingc0debad2009-01-30 23:27:35 +00004402 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004403 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004404 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00004405 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004406 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004407 // fold (fabs (fcopysign x, y)) -> (fabs x)
4408 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
Bill Wendlingc0debad2009-01-30 23:27:35 +00004409 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004410
Chris Lattner3bd39d42008-01-27 17:42:27 +00004411 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
4412 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004413 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004414 N0.getOperand(0).getValueType().isInteger() &&
4415 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004416 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004417 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004418 if (IntVT.isInteger() && !IntVT.isVector()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004419 Int = DAG.getNode(ISD::AND, N0.getDebugLoc(), IntVT, Int,
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00004420 DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004421 AddToWorkList(Int.getNode());
Bill Wendlingc0debad2009-01-30 23:27:35 +00004422 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(),
4423 N->getValueType(0), Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00004424 }
4425 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004426
Dan Gohman475871a2008-07-27 21:46:04 +00004427 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004428}
4429
Dan Gohman475871a2008-07-27 21:46:04 +00004430SDValue DAGCombiner::visitBRCOND(SDNode *N) {
4431 SDValue Chain = N->getOperand(0);
4432 SDValue N1 = N->getOperand(1);
4433 SDValue N2 = N->getOperand(2);
Nate Begeman44728a72005-09-19 22:34:01 +00004434 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004435
Nate Begeman44728a72005-09-19 22:34:01 +00004436 // never taken branch, fold to chain
4437 if (N1C && N1C->isNullValue())
4438 return Chain;
4439 // unconditional branch
Dan Gohman002e5d02008-03-13 22:13:53 +00004440 if (N1C && N1C->getAPIntValue() == 1)
Owen Anderson825b72b2009-08-11 20:47:22 +00004441 return DAG.getNode(ISD::BR, N->getDebugLoc(), MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004442 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
4443 // on the target.
Scott Michelfdc40a02009-02-17 22:15:04 +00004444 if (N1.getOpcode() == ISD::SETCC &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004445 TLI.isOperationLegalOrCustom(ISD::BR_CC, MVT::Other)) {
4446 return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00004447 Chain, N1.getOperand(2),
Nate Begeman750ac1b2006-02-01 07:19:44 +00004448 N1.getOperand(0), N1.getOperand(1), N2);
4449 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00004450
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00004451 if (N1.hasOneUse() && N1.getOpcode() == ISD::SRL) {
4452 // Match this pattern so that we can generate simpler code:
4453 //
4454 // %a = ...
4455 // %b = and i32 %a, 2
4456 // %c = srl i32 %b, 1
4457 // brcond i32 %c ...
4458 //
4459 // into
4460 //
4461 // %a = ...
4462 // %b = and %a, 2
4463 // %c = setcc eq %b, 0
4464 // brcond %c ...
4465 //
4466 // This applies only when the AND constant value has one bit set and the
4467 // SRL constant is equal to the log2 of the AND constant. The back-end is
4468 // smart enough to convert the result into a TEST/JMP sequence.
4469 SDValue Op0 = N1.getOperand(0);
4470 SDValue Op1 = N1.getOperand(1);
4471
4472 if (Op0.getOpcode() == ISD::AND &&
4473 Op0.hasOneUse() &&
4474 Op1.getOpcode() == ISD::Constant) {
4475 SDValue AndOp0 = Op0.getOperand(0);
4476 SDValue AndOp1 = Op0.getOperand(1);
4477
4478 if (AndOp1.getOpcode() == ISD::Constant) {
4479 const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue();
4480
4481 if (AndConst.isPowerOf2() &&
4482 cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) {
4483 SDValue SetCC =
4484 DAG.getSetCC(N->getDebugLoc(),
4485 TLI.getSetCCResultType(Op0.getValueType()),
4486 Op0, DAG.getConstant(0, Op0.getValueType()),
4487 ISD::SETNE);
4488
4489 // Replace the uses of SRL with SETCC
4490 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
4491 removeFromWorkList(N1.getNode());
4492 DAG.DeleteNode(N1.getNode());
4493 return DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00004494 MVT::Other, Chain, SetCC, N2);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00004495 }
4496 }
4497 }
4498 }
4499
Dan Gohman475871a2008-07-27 21:46:04 +00004500 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004501}
4502
Chris Lattner3ea0b472005-10-05 06:47:48 +00004503// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
4504//
Dan Gohman475871a2008-07-27 21:46:04 +00004505SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00004506 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004507 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Scott Michelfdc40a02009-02-17 22:15:04 +00004508
Duncan Sands8eab8a22008-06-09 11:32:28 +00004509 // Use SimplifySetCC to simplify SETCC's.
Duncan Sands5480c042009-01-01 15:52:00 +00004510 SDValue Simp = SimplifySetCC(TLI.getSetCCResultType(CondLHS.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00004511 CondLHS, CondRHS, CC->get(), N->getDebugLoc(),
4512 false);
Gabor Greifba36cb52008-08-28 21:40:38 +00004513 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00004514
Gabor Greifba36cb52008-08-28 21:40:38 +00004515 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.getNode());
Nate Begemane17daeb2005-10-05 21:43:42 +00004516
4517 // fold br_cc true, dest -> br dest (unconditional branch)
Dan Gohman002e5d02008-03-13 22:13:53 +00004518 if (SCCC && !SCCC->isNullValue())
Owen Anderson825b72b2009-08-11 20:47:22 +00004519 return DAG.getNode(ISD::BR, N->getDebugLoc(), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00004520 N->getOperand(0), N->getOperand(4));
Nate Begemane17daeb2005-10-05 21:43:42 +00004521 // fold br_cc false, dest -> unconditional fall through
4522 if (SCCC && SCCC->isNullValue())
4523 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00004524
Nate Begemane17daeb2005-10-05 21:43:42 +00004525 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00004526 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Owen Anderson825b72b2009-08-11 20:47:22 +00004527 return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00004528 N->getOperand(0), Simp.getOperand(2),
4529 Simp.getOperand(0), Simp.getOperand(1),
4530 N->getOperand(4));
4531
Dan Gohman475871a2008-07-27 21:46:04 +00004532 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004533}
4534
Duncan Sandsec87aa82008-06-15 20:12:31 +00004535/// CombineToPreIndexedLoadStore - Try turning a load / store into a
4536/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00004537/// and it has other uses besides the load / store. After the
4538/// transformation, the new indexed load / store has effectively folded
4539/// the add / subtract in and all of its other uses are redirected to the
4540/// new load / store.
4541bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Duncan Sands25cf2272008-11-24 14:53:14 +00004542 if (!LegalOperations)
Chris Lattner448f2192006-11-11 00:39:41 +00004543 return false;
4544
4545 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004546 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00004547 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004548 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004549 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004550 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004551 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00004552 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00004553 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
4554 return false;
4555 Ptr = LD->getBasePtr();
4556 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004557 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004558 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004559 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004560 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
4561 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
4562 return false;
4563 Ptr = ST->getBasePtr();
4564 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00004565 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00004566 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00004567 }
Chris Lattner448f2192006-11-11 00:39:41 +00004568
Chris Lattner9f1794e2006-11-11 00:56:29 +00004569 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4570 // out. There is no reason to make this a preinc/predec.
4571 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00004572 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004573 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004574
Chris Lattner9f1794e2006-11-11 00:56:29 +00004575 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00004576 SDValue BasePtr;
4577 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004578 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4579 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4580 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00004581 // Don't create a indexed load / store with zero offset.
4582 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004583 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004584 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00004585
Chris Lattner41e53fd2006-11-11 01:00:15 +00004586 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00004587 // 1) The new base ptr is a frame index.
4588 // 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 +00004589 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00004590 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00004591 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00004592 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00004593
Chris Lattner41e53fd2006-11-11 01:00:15 +00004594 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4595 // (plus the implicit offset) to a register to preinc anyway.
Evan Chengcaab1292009-05-06 18:25:01 +00004596 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
Chris Lattner41e53fd2006-11-11 01:00:15 +00004597 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00004598
Chris Lattner41e53fd2006-11-11 01:00:15 +00004599 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004600 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004601 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004602 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004603 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004604 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004605
Evan Chengc843abe2007-05-24 02:35:39 +00004606 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004607 bool RealUse = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00004608 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4609 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004610 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004611 if (Use == N)
4612 continue;
Evan Cheng917be682008-03-04 00:41:45 +00004613 if (Use->isPredecessorOf(N))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004614 return false;
4615
4616 if (!((Use->getOpcode() == ISD::LOAD &&
4617 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004618 (Use->getOpcode() == ISD::STORE &&
4619 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004620 RealUse = true;
4621 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00004622
Chris Lattner9f1794e2006-11-11 00:56:29 +00004623 if (!RealUse)
4624 return false;
4625
Dan Gohman475871a2008-07-27 21:46:04 +00004626 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004627 if (isLoad)
Bill Wendlingc0debad2009-01-30 23:27:35 +00004628 Result = DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(),
4629 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004630 else
Bill Wendlingc0debad2009-01-30 23:27:35 +00004631 Result = DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(),
4632 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004633 ++PreIndexedNodes;
4634 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004635 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004636 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004637 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004638 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004639 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004640 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004641 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004642 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004643 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004644 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004645 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004646 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004647 }
4648
Chris Lattner9f1794e2006-11-11 00:56:29 +00004649 // Finally, since the node is now dead, remove it from the graph.
4650 DAG.DeleteNode(N);
4651
4652 // Replace the uses of Ptr with uses of the updated base value.
4653 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004654 &DeadNodes);
Gabor Greifba36cb52008-08-28 21:40:38 +00004655 removeFromWorkList(Ptr.getNode());
4656 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00004657
4658 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004659}
4660
Duncan Sandsec87aa82008-06-15 20:12:31 +00004661/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00004662/// add / sub of the base pointer node into a post-indexed load / store.
4663/// The transformation folded the add / subtract into the new indexed
4664/// load / store effectively and all of its uses are redirected to the
4665/// new load / store.
4666bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Duncan Sands25cf2272008-11-24 14:53:14 +00004667 if (!LegalOperations)
Chris Lattner448f2192006-11-11 00:39:41 +00004668 return false;
4669
4670 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004671 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00004672 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004673 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004674 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004675 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004676 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004677 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4678 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4679 return false;
4680 Ptr = LD->getBasePtr();
4681 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004682 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004683 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004684 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004685 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4686 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4687 return false;
4688 Ptr = ST->getBasePtr();
4689 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00004690 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00004691 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00004692 }
Chris Lattner448f2192006-11-11 00:39:41 +00004693
Gabor Greifba36cb52008-08-28 21:40:38 +00004694 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004695 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00004696
Gabor Greifba36cb52008-08-28 21:40:38 +00004697 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4698 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004699 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004700 if (Op == N ||
4701 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4702 continue;
4703
Dan Gohman475871a2008-07-27 21:46:04 +00004704 SDValue BasePtr;
4705 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004706 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4707 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4708 if (Ptr == Offset)
4709 std::swap(BasePtr, Offset);
4710 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00004711 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00004712 // Don't create a indexed load / store with zero offset.
4713 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004714 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004715 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004716
Chris Lattner9f1794e2006-11-11 00:56:29 +00004717 // Try turning it into a post-indexed load / store except when
4718 // 1) All uses are load / store ops that use it as base ptr.
4719 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4720 // nor a successor of N. Otherwise, if Op is folded that would
4721 // create a cycle.
4722
Evan Chengcaab1292009-05-06 18:25:01 +00004723 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
4724 continue;
4725
Chris Lattner9f1794e2006-11-11 00:56:29 +00004726 // Check for #1.
4727 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00004728 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
4729 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00004730 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00004731 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00004732 continue;
4733
Chris Lattner9f1794e2006-11-11 00:56:29 +00004734 // If all the uses are load / store addresses, then don't do the
4735 // transformation.
4736 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4737 bool RealUse = false;
4738 for (SDNode::use_iterator III = Use->use_begin(),
4739 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00004740 SDNode *UseUse = *III;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004741 if (!((UseUse->getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004742 cast<LoadSDNode>(UseUse)->getBasePtr().getNode() == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004743 (UseUse->getOpcode() == ISD::STORE &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004744 cast<StoreSDNode>(UseUse)->getBasePtr().getNode() == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004745 RealUse = true;
4746 }
Chris Lattner448f2192006-11-11 00:39:41 +00004747
Chris Lattner9f1794e2006-11-11 00:56:29 +00004748 if (!RealUse) {
4749 TryNext = true;
4750 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004751 }
4752 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004753 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00004754
Chris Lattner9f1794e2006-11-11 00:56:29 +00004755 if (TryNext)
4756 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004757
Chris Lattner9f1794e2006-11-11 00:56:29 +00004758 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00004759 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004760 SDValue Result = isLoad
Bill Wendlingc0debad2009-01-30 23:27:35 +00004761 ? DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(),
4762 BasePtr, Offset, AM)
4763 : DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(),
4764 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004765 ++PostIndexedNodes;
4766 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004767 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004768 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004769 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004770 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004771 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004772 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004773 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004774 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004775 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004776 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004777 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004778 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00004779 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004780
Chris Lattner9f1794e2006-11-11 00:56:29 +00004781 // Finally, since the node is now dead, remove it from the graph.
4782 DAG.DeleteNode(N);
4783
4784 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00004785 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Chris Lattner9f1794e2006-11-11 00:56:29 +00004786 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004787 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004788 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004789 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004790 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004791 }
4792 }
4793 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00004794
Chris Lattner448f2192006-11-11 00:39:41 +00004795 return false;
4796}
4797
Chris Lattner00161a62008-01-25 07:20:16 +00004798/// InferAlignment - If we can infer some alignment information from this
4799/// pointer, return it.
Dan Gohman475871a2008-07-27 21:46:04 +00004800static unsigned InferAlignment(SDValue Ptr, SelectionDAG &DAG) {
Chris Lattner00161a62008-01-25 07:20:16 +00004801 // If this is a direct reference to a stack slot, use information about the
4802 // stack slot's alignment.
Chris Lattner1329cb82008-01-26 19:45:50 +00004803 int FrameIdx = 1 << 31;
4804 int64_t FrameOffset = 0;
Chris Lattner00161a62008-01-25 07:20:16 +00004805 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1329cb82008-01-26 19:45:50 +00004806 FrameIdx = FI->getIndex();
Scott Michelfdc40a02009-02-17 22:15:04 +00004807 } else if (Ptr.getOpcode() == ISD::ADD &&
Chris Lattner1329cb82008-01-26 19:45:50 +00004808 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4809 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4810 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4811 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner00161a62008-01-25 07:20:16 +00004812 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004813
Chris Lattner1329cb82008-01-26 19:45:50 +00004814 if (FrameIdx != (1 << 31)) {
4815 // FIXME: Handle FI+CST.
4816 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4817 if (MFI.isFixedObjectIndex(FrameIdx)) {
Dan Gohman8cea8ff2008-08-11 18:27:03 +00004818 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx) + FrameOffset;
Chris Lattner1329cb82008-01-26 19:45:50 +00004819
4820 // The alignment of the frame index can be determined from its offset from
4821 // the incoming frame position. If the frame object is at offset 32 and
4822 // the stack is guaranteed to be 16-byte aligned, then we know that the
4823 // object is 16-byte aligned.
4824 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4825 unsigned Align = MinAlign(ObjectOffset, StackAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00004826
Chris Lattner1329cb82008-01-26 19:45:50 +00004827 // Finally, the frame object itself may have a known alignment. Factor
4828 // the alignment + offset into a new alignment. For example, if we know
4829 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4830 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4831 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
Scott Michelfdc40a02009-02-17 22:15:04 +00004832 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
Chris Lattner1329cb82008-01-26 19:45:50 +00004833 FrameOffset);
4834 return std::max(Align, FIInfoAlign);
4835 }
4836 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004837
Chris Lattner00161a62008-01-25 07:20:16 +00004838 return 0;
4839}
Chris Lattner448f2192006-11-11 00:39:41 +00004840
Dan Gohman475871a2008-07-27 21:46:04 +00004841SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004842 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004843 SDValue Chain = LD->getChain();
4844 SDValue Ptr = LD->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00004845
Chris Lattner00161a62008-01-25 07:20:16 +00004846 // Try to infer better alignment information than the load already has.
Bill Wendling98a366d2009-04-29 23:29:43 +00004847 if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) {
Chris Lattner00161a62008-01-25 07:20:16 +00004848 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4849 if (Align > LD->getAlignment())
Bill Wendlingc0debad2009-01-30 23:27:35 +00004850 return DAG.getExtLoad(LD->getExtensionType(), N->getDebugLoc(),
4851 LD->getValueType(0),
Chris Lattner00161a62008-01-25 07:20:16 +00004852 Chain, Ptr, LD->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004853 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004854 LD->isVolatile(), Align);
4855 }
4856 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004857
4858 // If load is not volatile and there are no uses of the loaded value (and
4859 // the updated indexed value in case of indexed loads), change uses of the
4860 // chain value into uses of the chain input (i.e. delete the dead load).
4861 if (!LD->isVolatile()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004862 if (N->getValueType(1) == MVT::Other) {
Evan Cheng498f5592007-05-01 08:53:39 +00004863 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004864 if (N->hasNUsesOfValue(0, 0)) {
4865 // It's not safe to use the two value CombineTo variant here. e.g.
4866 // v1, chain2 = load chain1, loc
4867 // v2, chain3 = load chain2, loc
4868 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004869 // Now we replace use of chain2 with chain1. This makes the second load
4870 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004871 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004872 DOUT << "\nWith chain: "; DEBUG(Chain.getNode()->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004873 DOUT << "\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004874 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004875 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain, &DeadNodes);
Bill Wendlingc0debad2009-01-30 23:27:35 +00004876
Chris Lattner125991a2008-01-24 07:57:06 +00004877 if (N->use_empty()) {
4878 removeFromWorkList(N);
4879 DAG.DeleteNode(N);
4880 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00004881
Dan Gohman475871a2008-07-27 21:46:04 +00004882 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00004883 }
Evan Cheng498f5592007-05-01 08:53:39 +00004884 } else {
4885 // Indexed loads.
Owen Anderson825b72b2009-08-11 20:47:22 +00004886 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
Evan Cheng498f5592007-05-01 08:53:39 +00004887 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Dale Johannesene8d72302009-02-06 23:05:02 +00004888 SDValue Undef = DAG.getUNDEF(N->getValueType(0));
Evan Cheng02c42852008-01-16 23:11:54 +00004889 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004890 DOUT << "\nWith: "; DEBUG(Undef.getNode()->dump(&DAG));
Evan Cheng02c42852008-01-16 23:11:54 +00004891 DOUT << " and 2 other values\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004892 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004893 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef, &DeadNodes);
4894 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Dale Johannesene8d72302009-02-06 23:05:02 +00004895 DAG.getUNDEF(N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004896 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004897 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004898 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00004899 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004900 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004901 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004902 }
4903 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004904
Chris Lattner01a22022005-10-10 22:04:48 +00004905 // If this load is directly stored, replace the load value with the stored
4906 // value.
4907 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004908 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohmanb061c4b2008-03-31 20:32:52 +00004909 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
4910 !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004911 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004912 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4913 if (PrevST->getBasePtr() == Ptr &&
4914 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004915 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004916 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004917 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004918
Jim Laskey7ca56af2006-10-11 13:47:09 +00004919 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004920 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00004921 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00004922
Jim Laskey6ff23e52006-10-04 16:53:27 +00004923 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004924 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00004925 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004926
Jim Laskey279f0532006-09-25 16:29:54 +00004927 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004928 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Bill Wendlingc0debad2009-01-30 23:27:35 +00004929 ReplLoad = DAG.getLoad(N->getValueType(0), LD->getDebugLoc(),
4930 BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004931 LD->getSrcValue(), LD->getSrcValueOffset(),
4932 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004933 } else {
Bill Wendlingc0debad2009-01-30 23:27:35 +00004934 ReplLoad = DAG.getExtLoad(LD->getExtensionType(), LD->getDebugLoc(),
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004935 LD->getValueType(0),
4936 BetterChain, Ptr, LD->getSrcValue(),
4937 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004938 LD->getMemoryVT(),
Scott Michelfdc40a02009-02-17 22:15:04 +00004939 LD->isVolatile(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004940 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004941 }
Jim Laskey279f0532006-09-25 16:29:54 +00004942
Jim Laskey6ff23e52006-10-04 16:53:27 +00004943 // Create token factor to keep old chain connected.
Bill Wendlingc0debad2009-01-30 23:27:35 +00004944 SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00004945 MVT::Other, Chain, ReplLoad.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004946
Jim Laskey274062c2006-10-13 23:32:28 +00004947 // Replace uses with load result and token factor. Don't add users
4948 // to work list.
4949 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004950 }
4951 }
4952
Evan Cheng7fc033a2006-11-03 03:06:21 +00004953 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004954 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00004955 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00004956
Dan Gohman475871a2008-07-27 21:46:04 +00004957 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00004958}
4959
Evan Cheng8b944d32009-05-28 00:35:15 +00004960
4961/// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is
4962/// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some
4963/// of the loaded bits, try narrowing the load and store if it would end up
4964/// being a win for performance or code size.
4965SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) {
4966 StoreSDNode *ST = cast<StoreSDNode>(N);
Evan Chengcdcecc02009-05-28 18:41:02 +00004967 if (ST->isVolatile())
4968 return SDValue();
4969
Evan Cheng8b944d32009-05-28 00:35:15 +00004970 SDValue Chain = ST->getChain();
4971 SDValue Value = ST->getValue();
4972 SDValue Ptr = ST->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +00004973 EVT VT = Value.getValueType();
Evan Cheng8b944d32009-05-28 00:35:15 +00004974
4975 if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse())
Evan Chengcdcecc02009-05-28 18:41:02 +00004976 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00004977
4978 unsigned Opc = Value.getOpcode();
4979 if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) ||
4980 Value.getOperand(1).getOpcode() != ISD::Constant)
Evan Chengcdcecc02009-05-28 18:41:02 +00004981 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00004982
4983 SDValue N0 = Value.getOperand(0);
4984 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse()) {
4985 LoadSDNode *LD = cast<LoadSDNode>(N0);
Evan Chengcdcecc02009-05-28 18:41:02 +00004986 if (LD->getBasePtr() != Ptr)
4987 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00004988
4989 // Find the type to narrow it the load / op / store to.
4990 SDValue N1 = Value.getOperand(1);
4991 unsigned BitWidth = N1.getValueSizeInBits();
4992 APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue();
4993 if (Opc == ISD::AND)
4994 Imm ^= APInt::getAllOnesValue(BitWidth);
Evan Chengd3c76bb2009-05-28 23:52:18 +00004995 if (Imm == 0 || Imm.isAllOnesValue())
4996 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00004997 unsigned ShAmt = Imm.countTrailingZeros();
4998 unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1;
4999 unsigned NewBW = NextPowerOf2(MSB - ShAmt);
Owen Andersone50ed302009-08-10 22:56:29 +00005000 EVT NewVT = EVT::getIntegerVT(NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00005001 while (NewBW < BitWidth &&
Evan Chengcdcecc02009-05-28 18:41:02 +00005002 !(TLI.isOperationLegalOrCustom(Opc, NewVT) &&
Evan Cheng8b944d32009-05-28 00:35:15 +00005003 TLI.isNarrowingProfitable(VT, NewVT))) {
5004 NewBW = NextPowerOf2(NewBW);
Owen Andersone50ed302009-08-10 22:56:29 +00005005 NewVT = EVT::getIntegerVT(NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00005006 }
Evan Chengcdcecc02009-05-28 18:41:02 +00005007 if (NewBW >= BitWidth)
5008 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00005009
5010 // If the lsb changed does not start at the type bitwidth boundary,
5011 // start at the previous one.
5012 if (ShAmt % NewBW)
5013 ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW;
5014 APInt Mask = APInt::getBitsSet(BitWidth, ShAmt, ShAmt + NewBW);
5015 if ((Imm & Mask) == Imm) {
5016 APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW);
5017 if (Opc == ISD::AND)
5018 NewImm ^= APInt::getAllOnesValue(NewBW);
5019 uint64_t PtrOff = ShAmt / 8;
5020 // For big endian targets, we need to adjust the offset to the pointer to
5021 // load the correct bytes.
5022 if (TLI.isBigEndian())
Evan Chengcdcecc02009-05-28 18:41:02 +00005023 PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff;
Evan Cheng8b944d32009-05-28 00:35:15 +00005024
5025 unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff);
Evan Chengcdcecc02009-05-28 18:41:02 +00005026 if (NewAlign <
Owen Andersone50ed302009-08-10 22:56:29 +00005027 TLI.getTargetData()->getABITypeAlignment(NewVT.getTypeForEVT()))
Evan Chengcdcecc02009-05-28 18:41:02 +00005028 return SDValue();
5029
Evan Cheng8b944d32009-05-28 00:35:15 +00005030 SDValue NewPtr = DAG.getNode(ISD::ADD, LD->getDebugLoc(),
5031 Ptr.getValueType(), Ptr,
5032 DAG.getConstant(PtrOff, Ptr.getValueType()));
5033 SDValue NewLD = DAG.getLoad(NewVT, N0.getDebugLoc(),
5034 LD->getChain(), NewPtr,
5035 LD->getSrcValue(), LD->getSrcValueOffset(),
5036 LD->isVolatile(), NewAlign);
5037 SDValue NewVal = DAG.getNode(Opc, Value.getDebugLoc(), NewVT, NewLD,
5038 DAG.getConstant(NewImm, NewVT));
5039 SDValue NewST = DAG.getStore(Chain, N->getDebugLoc(),
5040 NewVal, NewPtr,
5041 ST->getSrcValue(), ST->getSrcValueOffset(),
Evan Chengcdcecc02009-05-28 18:41:02 +00005042 false, NewAlign);
Evan Cheng8b944d32009-05-28 00:35:15 +00005043
5044 AddToWorkList(NewPtr.getNode());
5045 AddToWorkList(NewLD.getNode());
5046 AddToWorkList(NewVal.getNode());
5047 WorkListRemover DeadNodes(*this);
5048 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1),
5049 &DeadNodes);
5050 ++OpsNarrowed;
5051 return NewST;
5052 }
5053 }
5054
Evan Chengcdcecc02009-05-28 18:41:02 +00005055 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00005056}
5057
Dan Gohman475871a2008-07-27 21:46:04 +00005058SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00005059 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00005060 SDValue Chain = ST->getChain();
5061 SDValue Value = ST->getValue();
5062 SDValue Ptr = ST->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00005063
Chris Lattner00161a62008-01-25 07:20:16 +00005064 // Try to infer better alignment information than the store already has.
Bill Wendling98a366d2009-04-29 23:29:43 +00005065 if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) {
Chris Lattner00161a62008-01-25 07:20:16 +00005066 if (unsigned Align = InferAlignment(Ptr, DAG)) {
5067 if (Align > ST->getAlignment())
Bill Wendlingc144a572009-01-30 23:36:47 +00005068 return DAG.getTruncStore(Chain, N->getDebugLoc(), Value,
5069 Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005070 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00005071 ST->isVolatile(), Align);
5072 }
5073 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005074
Evan Cheng59d5b682007-05-07 21:27:48 +00005075 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00005076 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00005077 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00005078 ST->isUnindexed()) {
Dan Gohman1ba519b2009-02-20 23:29:13 +00005079 unsigned OrigAlign = ST->getAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00005080 EVT SVT = Value.getOperand(0).getValueType();
Dan Gohman1ba519b2009-02-20 23:29:13 +00005081 unsigned Align = TLI.getTargetData()->
Owen Andersone50ed302009-08-10 22:56:29 +00005082 getABITypeAlignment(SVT.getTypeForEVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005083 if (Align <= OrigAlign &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005084 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohmanf560ffa2009-01-28 17:46:25 +00005085 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Bill Wendlingc144a572009-01-30 23:36:47 +00005086 return DAG.getStore(Chain, N->getDebugLoc(), Value.getOperand(0),
5087 Ptr, ST->getSrcValue(),
Dan Gohman77455612008-06-28 00:45:22 +00005088 ST->getSrcValueOffset(), ST->isVolatile(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00005089 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005090
Nate Begeman2cbba892006-12-11 02:23:46 +00005091 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00005092 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005093 // NOTE: If the original store is volatile, this transform must not increase
5094 // the number of stores. For example, on x86-32 an f64 can be stored in one
5095 // processor operation but an i64 (which is not legal) requires two. So the
5096 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00005097 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00005098 SDValue Tmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00005099 switch (CFP->getValueType(0).getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005100 default: llvm_unreachable("Unknown FP type");
Owen Anderson825b72b2009-08-11 20:47:22 +00005101 case MVT::f80: // We don't do this for these yet.
5102 case MVT::f128:
5103 case MVT::ppcf128:
Dale Johannesenc7b21d52007-09-18 18:36:59 +00005104 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005105 case MVT::f32:
5106 if (((TLI.isTypeLegal(MVT::i32) || !LegalTypes) && !LegalOperations &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00005107 !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005108 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00005109 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Owen Anderson825b72b2009-08-11 20:47:22 +00005110 bitcastToAPInt().getZExtValue(), MVT::i32);
Bill Wendlingc144a572009-01-30 23:36:47 +00005111 return DAG.getStore(Chain, N->getDebugLoc(), Tmp,
5112 Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005113 ST->getSrcValueOffset(), ST->isVolatile(),
5114 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00005115 }
5116 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005117 case MVT::f64:
5118 if (((TLI.isTypeLegal(MVT::i64) || !LegalTypes) && !LegalOperations &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00005119 !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005120 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen7111b022008-10-09 18:53:47 +00005121 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +00005122 getZExtValue(), MVT::i64);
Bill Wendlingc144a572009-01-30 23:36:47 +00005123 return DAG.getStore(Chain, N->getDebugLoc(), Tmp,
5124 Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005125 ST->getSrcValueOffset(), ST->isVolatile(),
5126 ST->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005127 } else if (!ST->isVolatile() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005128 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00005129 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00005130 // argument passing. Since this is so common, custom legalize the
5131 // 64-bit integer store into two 32-bit stores.
Dale Johannesen7111b022008-10-09 18:53:47 +00005132 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005133 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
5134 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00005135 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00005136
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005137 int SVOffset = ST->getSrcValueOffset();
5138 unsigned Alignment = ST->getAlignment();
5139 bool isVolatile = ST->isVolatile();
5140
Bill Wendlingc144a572009-01-30 23:36:47 +00005141 SDValue St0 = DAG.getStore(Chain, ST->getDebugLoc(), Lo,
5142 Ptr, ST->getSrcValue(),
5143 ST->getSrcValueOffset(),
5144 isVolatile, ST->getAlignment());
5145 Ptr = DAG.getNode(ISD::ADD, N->getDebugLoc(), Ptr.getValueType(), Ptr,
Chris Lattner62be1a72006-12-12 04:16:14 +00005146 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005147 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00005148 Alignment = MinAlign(Alignment, 4U);
Bill Wendlingc144a572009-01-30 23:36:47 +00005149 SDValue St1 = DAG.getStore(Chain, ST->getDebugLoc(), Hi,
5150 Ptr, ST->getSrcValue(),
5151 SVOffset, isVolatile, Alignment);
Owen Anderson825b72b2009-08-11 20:47:22 +00005152 return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other,
Bill Wendlingc144a572009-01-30 23:36:47 +00005153 St0, St1);
Chris Lattner62be1a72006-12-12 04:16:14 +00005154 }
Bill Wendlingc144a572009-01-30 23:36:47 +00005155
Chris Lattner62be1a72006-12-12 04:16:14 +00005156 break;
Evan Cheng25ece662006-12-11 17:25:19 +00005157 }
Nate Begeman2cbba892006-12-11 02:23:46 +00005158 }
Nate Begeman2cbba892006-12-11 02:23:46 +00005159 }
5160
Scott Michelfdc40a02009-02-17 22:15:04 +00005161 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00005162 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00005163 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00005164
Jim Laskey6ff23e52006-10-04 16:53:27 +00005165 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00005166 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00005167 // Replace the chain to avoid dependency.
Dan Gohman475871a2008-07-27 21:46:04 +00005168 SDValue ReplStore;
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00005169 if (ST->isTruncatingStore()) {
Bill Wendlingc144a572009-01-30 23:36:47 +00005170 ReplStore = DAG.getTruncStore(BetterChain, N->getDebugLoc(), Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00005171 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005172 ST->getMemoryVT(),
Chris Lattner4626b252008-01-17 07:20:38 +00005173 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00005174 } else {
Bill Wendlingc144a572009-01-30 23:36:47 +00005175 ReplStore = DAG.getStore(BetterChain, N->getDebugLoc(), Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00005176 ST->getSrcValue(), ST->getSrcValueOffset(),
5177 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00005178 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005179
Jim Laskey279f0532006-09-25 16:29:54 +00005180 // Create token to keep both nodes around.
Bill Wendlingc144a572009-01-30 23:36:47 +00005181 SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005182 MVT::Other, Chain, ReplStore);
Bill Wendlingc144a572009-01-30 23:36:47 +00005183
Jim Laskey274062c2006-10-13 23:32:28 +00005184 // Don't add users to work list.
5185 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00005186 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00005187 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005188
Evan Cheng33dbedc2006-11-05 09:31:14 +00005189 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00005190 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00005191 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00005192
Chris Lattner3c872852007-12-29 06:26:16 +00005193 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00005194 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005195 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00005196 // See if we can simplify the input to this truncstore with knowledge that
5197 // only the low bits are being used. For example:
5198 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Scott Michelfdc40a02009-02-17 22:15:04 +00005199 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005200 GetDemandedBits(Value,
Bill Wendlingc144a572009-01-30 23:36:47 +00005201 APInt::getLowBitsSet(Value.getValueSizeInBits(),
5202 ST->getMemoryVT().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00005203 AddToWorkList(Value.getNode());
5204 if (Shorter.getNode())
Bill Wendlingc144a572009-01-30 23:36:47 +00005205 return DAG.getTruncStore(Chain, N->getDebugLoc(), Shorter,
5206 Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005207 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner2b4c2792007-10-13 06:35:54 +00005208 ST->isVolatile(), ST->getAlignment());
Scott Michelfdc40a02009-02-17 22:15:04 +00005209
Chris Lattnere33544c2007-10-13 06:58:48 +00005210 // Otherwise, see if we can simplify the operation with
5211 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00005212 if (SimplifyDemandedBits(Value,
5213 APInt::getLowBitsSet(
5214 Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00005215 ST->getMemoryVT().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00005216 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00005217 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005218
Chris Lattner3c872852007-12-29 06:26:16 +00005219 // If this is a load followed by a store to the same location, then the store
5220 // is dead/noop.
5221 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005222 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00005223 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00005224 // There can't be any side effects between the load and store, such as
5225 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00005226 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00005227 // The store is dead, remove it.
5228 return Chain;
5229 }
5230 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005231
Chris Lattnerddf89562008-01-17 19:59:44 +00005232 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
5233 // truncating store. We can do this even if this is already a truncstore.
5234 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00005235 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00005236 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005237 ST->getMemoryVT())) {
Bill Wendlingc144a572009-01-30 23:36:47 +00005238 return DAG.getTruncStore(Chain, N->getDebugLoc(), Value.getOperand(0),
5239 Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005240 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnerddf89562008-01-17 19:59:44 +00005241 ST->isVolatile(), ST->getAlignment());
5242 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005243
Evan Cheng8b944d32009-05-28 00:35:15 +00005244 return ReduceLoadOpStoreWidth(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00005245}
5246
Dan Gohman475871a2008-07-27 21:46:04 +00005247SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
5248 SDValue InVec = N->getOperand(0);
5249 SDValue InVal = N->getOperand(1);
5250 SDValue EltNo = N->getOperand(2);
Scott Michelfdc40a02009-02-17 22:15:04 +00005251
Chris Lattnerca242442006-03-19 01:27:56 +00005252 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
5253 // vector with the inserted element.
5254 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005255 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Gabor Greif12632d22008-08-30 19:29:20 +00005256 SmallVector<SDValue, 8> Ops(InVec.getNode()->op_begin(),
5257 InVec.getNode()->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00005258 if (Elt < Ops.size())
5259 Ops[Elt] = InVal;
Evan Chenga87008d2009-02-25 22:49:59 +00005260 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
5261 InVec.getValueType(), &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00005262 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005263 // If the invec is an UNDEF and if EltNo is a constant, create a new
5264 // BUILD_VECTOR with undef elements and the inserted element.
5265 if (!LegalOperations && InVec.getOpcode() == ISD::UNDEF &&
5266 isa<ConstantSDNode>(EltNo)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005267 EVT VT = InVec.getValueType();
5268 EVT EVT = VT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00005269 unsigned NElts = VT.getVectorNumElements();
5270 SmallVector<SDValue, 8> Ops(NElts, DAG.getUNDEF(EVT));
Scott Michelfdc40a02009-02-17 22:15:04 +00005271
Nate Begeman9008ca62009-04-27 18:41:29 +00005272 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5273 if (Elt < Ops.size())
5274 Ops[Elt] = InVal;
5275 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
5276 InVec.getValueType(), &Ops[0], Ops.size());
5277 }
Dan Gohman475871a2008-07-27 21:46:04 +00005278 return SDValue();
Chris Lattnerca242442006-03-19 01:27:56 +00005279}
5280
Dan Gohman475871a2008-07-27 21:46:04 +00005281SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00005282 // (vextract (scalar_to_vector val, 0) -> val
5283 SDValue InVec = N->getOperand(0);
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00005284
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00005285 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5286 // If the operand is wider than the vector element type then it is implicitly
5287 // truncated. Make that explicit here.
Owen Andersone50ed302009-08-10 22:56:29 +00005288 EVT EltVT = InVec.getValueType().getVectorElementType();
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00005289 SDValue InOp = InVec.getOperand(0);
5290 if (InOp.getValueType() != EltVT)
5291 return DAG.getNode(ISD::TRUNCATE, InVec.getDebugLoc(), EltVT, InOp);
5292 return InOp;
5293 }
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005294
5295 // Perform only after legalization to ensure build_vector / vector_shuffle
5296 // optimizations have already been done.
Duncan Sands25cf2272008-11-24 14:53:14 +00005297 if (!LegalOperations) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005298
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00005299 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
5300 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
5301 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Mon P Wange3bc6ae2009-01-18 06:43:40 +00005302 SDValue EltNo = N->getOperand(1);
Evan Cheng513da432007-10-06 08:19:55 +00005303
Evan Cheng513da432007-10-06 08:19:55 +00005304 if (isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005305 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00005306 bool NewLoad = false;
Mon P Wanga60b5232008-12-11 00:26:16 +00005307 bool BCNumEltsChanged = false;
Owen Andersone50ed302009-08-10 22:56:29 +00005308 EVT VT = InVec.getValueType();
5309 EVT ExtVT = VT.getVectorElementType();
5310 EVT LVT = ExtVT;
Bill Wendlingc144a572009-01-30 23:36:47 +00005311
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005312 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
Owen Andersone50ed302009-08-10 22:56:29 +00005313 EVT BCVT = InVec.getOperand(0).getValueType();
5314 if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005315 return SDValue();
Mon P Wanga60b5232008-12-11 00:26:16 +00005316 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
5317 BCNumEltsChanged = true;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005318 InVec = InVec.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005319 ExtVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005320 NewLoad = true;
5321 }
Evan Cheng513da432007-10-06 08:19:55 +00005322
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005323 LoadSDNode *LN0 = NULL;
Nate Begeman5a5ca152009-04-29 05:20:52 +00005324 const ShuffleVectorSDNode *SVN = NULL;
Bill Wendlingc144a572009-01-30 23:36:47 +00005325 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005326 LN0 = cast<LoadSDNode>(InVec);
Bill Wendlingc144a572009-01-30 23:36:47 +00005327 } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Owen Andersone50ed302009-08-10 22:56:29 +00005328 InVec.getOperand(0).getValueType() == ExtVT &&
Bill Wendlingc144a572009-01-30 23:36:47 +00005329 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005330 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
Nate Begeman5a5ca152009-04-29 05:20:52 +00005331 } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005332 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
5333 // =>
5334 // (load $addr+1*size)
Scott Michelfdc40a02009-02-17 22:15:04 +00005335
Mon P Wanga60b5232008-12-11 00:26:16 +00005336 // If the bit convert changed the number of elements, it is unsafe
5337 // to examine the mask.
5338 if (BCNumEltsChanged)
5339 return SDValue();
Nate Begeman5a5ca152009-04-29 05:20:52 +00005340
5341 // Select the input vector, guarding against out of range extract vector.
5342 unsigned NumElems = VT.getVectorNumElements();
5343 int Idx = (Elt > NumElems) ? -1 : SVN->getMaskElt(Elt);
5344 InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
5345
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005346 if (InVec.getOpcode() == ISD::BIT_CONVERT)
5347 InVec = InVec.getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00005348 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005349 LN0 = cast<LoadSDNode>(InVec);
Nate Begeman5a5ca152009-04-29 05:20:52 +00005350 Elt = (Idx < (int)NumElems) ? Idx : Idx - NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00005351 }
5352 }
Bill Wendlingc144a572009-01-30 23:36:47 +00005353
Duncan Sandsec87aa82008-06-15 20:12:31 +00005354 if (!LN0 || !LN0->hasOneUse() || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00005355 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005356
5357 unsigned Align = LN0->getAlignment();
5358 if (NewLoad) {
5359 // Check the resultant load doesn't need a higher alignment than the
5360 // original load.
Bill Wendlingc144a572009-01-30 23:36:47 +00005361 unsigned NewAlign =
Owen Andersone50ed302009-08-10 22:56:29 +00005362 TLI.getTargetData()->getABITypeAlignment(LVT.getTypeForEVT());
Bill Wendlingc144a572009-01-30 23:36:47 +00005363
Dan Gohmanf560ffa2009-01-28 17:46:25 +00005364 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00005365 return SDValue();
Bill Wendlingc144a572009-01-30 23:36:47 +00005366
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005367 Align = NewAlign;
5368 }
5369
Dan Gohman475871a2008-07-27 21:46:04 +00005370 SDValue NewPtr = LN0->getBasePtr();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005371 if (Elt) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005372 unsigned PtrOff = LVT.getSizeInBits() * Elt / 8;
Owen Andersone50ed302009-08-10 22:56:29 +00005373 EVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005374 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00005375 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Bill Wendlingc144a572009-01-30 23:36:47 +00005376 NewPtr = DAG.getNode(ISD::ADD, N->getDebugLoc(), PtrType, NewPtr,
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005377 DAG.getConstant(PtrOff, PtrType));
5378 }
Bill Wendlingc144a572009-01-30 23:36:47 +00005379
5380 return DAG.getLoad(LVT, N->getDebugLoc(), LN0->getChain(), NewPtr,
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005381 LN0->getSrcValue(), LN0->getSrcValueOffset(),
5382 LN0->isVolatile(), Align);
Evan Cheng513da432007-10-06 08:19:55 +00005383 }
Bill Wendlingc144a572009-01-30 23:36:47 +00005384
Dan Gohman475871a2008-07-27 21:46:04 +00005385 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00005386}
Evan Cheng513da432007-10-06 08:19:55 +00005387
Dan Gohman475871a2008-07-27 21:46:04 +00005388SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005389 unsigned NumInScalars = N->getNumOperands();
Owen Andersone50ed302009-08-10 22:56:29 +00005390 EVT VT = N->getValueType(0);
5391 EVT EltType = VT.getVectorElementType();
Chris Lattnerca242442006-03-19 01:27:56 +00005392
Dan Gohman7f321562007-06-25 16:23:39 +00005393 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
5394 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
5395 // at most two distinct vectors, turn this into a shuffle node.
Dan Gohman475871a2008-07-27 21:46:04 +00005396 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00005397 for (unsigned i = 0; i != NumInScalars; ++i) {
5398 // Ignore undef inputs.
5399 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00005400
Dan Gohman7f321562007-06-25 16:23:39 +00005401 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00005402 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00005403 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00005404 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00005405 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005406 break;
5407 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005408
Dan Gohman7f321562007-06-25 16:23:39 +00005409 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00005410 // we can't make a shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00005411 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005412 if (ExtractedFromVec.getValueType() != VT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005413 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005414 break;
5415 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005416
Chris Lattnerd7648c82006-03-28 20:28:38 +00005417 // Otherwise, remember this. We allow up to two distinct input vectors.
5418 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
5419 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00005420
Gabor Greifba36cb52008-08-28 21:40:38 +00005421 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00005422 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00005423 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00005424 VecIn2 = ExtractedFromVec;
5425 } else {
5426 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00005427 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005428 break;
5429 }
5430 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005431
Chris Lattnerd7648c82006-03-28 20:28:38 +00005432 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00005433 if (VecIn1.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005434 SmallVector<int, 8> Mask;
Chris Lattnerd7648c82006-03-28 20:28:38 +00005435 for (unsigned i = 0; i != NumInScalars; ++i) {
5436 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005437 Mask.push_back(-1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005438 continue;
5439 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005440
Rafael Espindola15684b22009-04-24 12:40:33 +00005441 // If extracting from the first vector, just use the index directly.
Nate Begeman9008ca62009-04-27 18:41:29 +00005442 SDValue Extract = N->getOperand(i);
Mon P Wang93b74152009-03-17 06:33:10 +00005443 SDValue ExtVal = Extract.getOperand(1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005444 if (Extract.getOperand(0) == VecIn1) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00005445 unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue();
5446 if (ExtIndex > VT.getVectorNumElements())
5447 return SDValue();
5448
5449 Mask.push_back(ExtIndex);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005450 continue;
5451 }
5452
5453 // Otherwise, use InIdx + VecSize
Mon P Wang93b74152009-03-17 06:33:10 +00005454 unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00005455 Mask.push_back(Idx+NumInScalars);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005456 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005457
Chris Lattnerd7648c82006-03-28 20:28:38 +00005458 // Add count and size info.
Nate Begeman9008ca62009-04-27 18:41:29 +00005459 if (!TLI.isTypeLegal(VT) && LegalTypes)
Duncan Sands25cf2272008-11-24 14:53:14 +00005460 return SDValue();
5461
Dan Gohman7f321562007-06-25 16:23:39 +00005462 // Return the new VECTOR_SHUFFLE node.
Nate Begeman9008ca62009-04-27 18:41:29 +00005463 SDValue Ops[2];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005464 Ops[0] = VecIn1;
Nate Begeman9008ca62009-04-27 18:41:29 +00005465 Ops[1] = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
5466 return DAG.getVectorShuffle(VT, N->getDebugLoc(), Ops[0], Ops[1], &Mask[0]);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005467 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005468
Dan Gohman475871a2008-07-27 21:46:04 +00005469 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00005470}
5471
Dan Gohman475871a2008-07-27 21:46:04 +00005472SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005473 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
5474 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
5475 // inputs come from at most two distinct vectors, turn this into a shuffle
5476 // node.
5477
5478 // If we only have one input vector, we don't need to do any concatenation.
Bill Wendlingc144a572009-01-30 23:36:47 +00005479 if (N->getNumOperands() == 1)
Dan Gohman7f321562007-06-25 16:23:39 +00005480 return N->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005481
Dan Gohman475871a2008-07-27 21:46:04 +00005482 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00005483}
5484
Dan Gohman475871a2008-07-27 21:46:04 +00005485SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005486 return SDValue();
5487
Owen Andersone50ed302009-08-10 22:56:29 +00005488 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005489 unsigned NumElts = VT.getVectorNumElements();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005490
Mon P Wangaeb06d22008-11-10 04:46:22 +00005491 SDValue N0 = N->getOperand(0);
5492 SDValue N1 = N->getOperand(1);
5493
5494 assert(N0.getValueType().getVectorNumElements() == NumElts &&
5495 "Vector shuffle must be normalized in DAG");
5496
Nate Begeman9008ca62009-04-27 18:41:29 +00005497 // FIXME: implement canonicalizations from DAG.getVectorShuffle()
Evan Chenge7bec0d2006-07-20 22:44:41 +00005498
Evan Cheng917ec982006-07-21 08:25:53 +00005499 // If it is a splat, check if the argument vector is a build_vector with
5500 // all scalar elements the same.
Nate Begeman9008ca62009-04-27 18:41:29 +00005501 if (cast<ShuffleVectorSDNode>(N)->isSplat()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005502 SDNode *V = N0.getNode();
Nate Begeman9008ca62009-04-27 18:41:29 +00005503
Evan Cheng917ec982006-07-21 08:25:53 +00005504
Dan Gohman7f321562007-06-25 16:23:39 +00005505 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00005506 // not the number of vector elements, look through it. Be careful not to
5507 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00005508 if (V->getOpcode() == ISD::BIT_CONVERT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005509 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00005510 if (ConvInput.getValueType().isVector() &&
5511 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +00005512 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +00005513 }
5514
Dan Gohman7f321562007-06-25 16:23:39 +00005515 if (V->getOpcode() == ISD::BUILD_VECTOR) {
5516 unsigned NumElems = V->getNumOperands();
Nate Begeman9008ca62009-04-27 18:41:29 +00005517 unsigned BaseIdx = cast<ShuffleVectorSDNode>(N)->getSplatIndex();
Evan Cheng917ec982006-07-21 08:25:53 +00005518 if (NumElems > BaseIdx) {
Dan Gohman475871a2008-07-27 21:46:04 +00005519 SDValue Base;
Evan Cheng917ec982006-07-21 08:25:53 +00005520 bool AllSame = true;
5521 for (unsigned i = 0; i != NumElems; ++i) {
5522 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
5523 Base = V->getOperand(i);
5524 break;
5525 }
5526 }
5527 // Splat of <u, u, u, u>, return <u, u, u, u>
Gabor Greifba36cb52008-08-28 21:40:38 +00005528 if (!Base.getNode())
Evan Cheng917ec982006-07-21 08:25:53 +00005529 return N0;
5530 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00005531 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00005532 AllSame = false;
5533 break;
5534 }
5535 }
5536 // Splat of <x, x, x, x>, return <x, x, x, x>
5537 if (AllSame)
5538 return N0;
5539 }
5540 }
5541 }
Dan Gohman475871a2008-07-27 21:46:04 +00005542 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005543}
5544
Evan Cheng44f1f092006-04-20 08:56:16 +00005545/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00005546/// an AND to a vector_shuffle with the destination vector and a zero vector.
5547/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00005548/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00005549SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00005550 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005551 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005552 SDValue LHS = N->getOperand(0);
5553 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00005554 if (N->getOpcode() == ISD::AND) {
5555 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00005556 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005557 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005558 SmallVector<int, 8> Indices;
5559 unsigned NumElts = RHS.getNumOperands();
Evan Cheng44f1f092006-04-20 08:56:16 +00005560 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005561 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00005562 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00005563 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005564 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00005565 Indices.push_back(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00005566 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00005567 Indices.push_back(NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00005568 else
Dan Gohman475871a2008-07-27 21:46:04 +00005569 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005570 }
5571
5572 // Let's see if the target supports this vector_shuffle.
Owen Andersone50ed302009-08-10 22:56:29 +00005573 EVT RVT = RHS.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00005574 if (!TLI.isVectorClearMaskLegal(Indices, RVT))
Dan Gohman475871a2008-07-27 21:46:04 +00005575 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005576
Dan Gohman7f321562007-06-25 16:23:39 +00005577 // Return the new VECTOR_SHUFFLE node.
Owen Andersone50ed302009-08-10 22:56:29 +00005578 EVT EVT = RVT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00005579 SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(),
5580 DAG.getConstant(0, EVT));
5581 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
5582 RVT, &ZeroOps[0], ZeroOps.size());
5583 LHS = DAG.getNode(ISD::BIT_CONVERT, dl, RVT, LHS);
5584 SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]);
5585 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Shuf);
Evan Cheng44f1f092006-04-20 08:56:16 +00005586 }
5587 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00005588
Dan Gohman475871a2008-07-27 21:46:04 +00005589 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005590}
5591
Dan Gohman7f321562007-06-25 16:23:39 +00005592/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00005593SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005594 // After legalize, the target may be depending on adds and other
5595 // binary ops to provide legal ways to construct constants or other
5596 // things. Simplifying them may result in a loss of legality.
Duncan Sands25cf2272008-11-24 14:53:14 +00005597 if (LegalOperations) return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00005598
Owen Andersone50ed302009-08-10 22:56:29 +00005599 EVT VT = N->getValueType(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005600 assert(VT.isVector() && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00005601
Owen Andersone50ed302009-08-10 22:56:29 +00005602 EVT EltType = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00005603 SDValue LHS = N->getOperand(0);
5604 SDValue RHS = N->getOperand(1);
5605 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005606 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +00005607
Dan Gohman7f321562007-06-25 16:23:39 +00005608 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00005609 // this operation.
Scott Michelfdc40a02009-02-17 22:15:04 +00005610 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
Dan Gohman7f321562007-06-25 16:23:39 +00005611 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00005612 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005613 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005614 SDValue LHSOp = LHS.getOperand(i);
5615 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00005616 // If these two elements can't be folded, bail out.
5617 if ((LHSOp.getOpcode() != ISD::UNDEF &&
5618 LHSOp.getOpcode() != ISD::Constant &&
5619 LHSOp.getOpcode() != ISD::ConstantFP) ||
5620 (RHSOp.getOpcode() != ISD::UNDEF &&
5621 RHSOp.getOpcode() != ISD::Constant &&
5622 RHSOp.getOpcode() != ISD::ConstantFP))
5623 break;
Bill Wendling836ca7d2009-01-30 23:59:18 +00005624
Evan Cheng7b336a82006-05-31 06:08:35 +00005625 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00005626 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
5627 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00005628 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005629 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +00005630 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005631 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00005632 break;
5633 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00005634
Bill Wendling9729c5a2009-01-31 03:12:48 +00005635 Ops.push_back(DAG.getNode(N->getOpcode(), LHS.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00005636 EltType, LHSOp, RHSOp));
Gabor Greifba36cb52008-08-28 21:40:38 +00005637 AddToWorkList(Ops.back().getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +00005638 assert((Ops.back().getOpcode() == ISD::UNDEF ||
5639 Ops.back().getOpcode() == ISD::Constant ||
5640 Ops.back().getOpcode() == ISD::ConstantFP) &&
5641 "Scalar binop didn't fold!");
5642 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005643
Dan Gohman7f321562007-06-25 16:23:39 +00005644 if (Ops.size() == LHS.getNumOperands()) {
Owen Andersone50ed302009-08-10 22:56:29 +00005645 EVT VT = LHS.getValueType();
Evan Chenga87008d2009-02-25 22:49:59 +00005646 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT,
5647 &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005648 }
Chris Lattneredab1b92006-04-02 03:25:57 +00005649 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005650
Dan Gohman475871a2008-07-27 21:46:04 +00005651 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00005652}
5653
Bill Wendling836ca7d2009-01-30 23:59:18 +00005654SDValue DAGCombiner::SimplifySelect(DebugLoc DL, SDValue N0,
5655 SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00005656 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005657
Bill Wendling836ca7d2009-01-30 23:59:18 +00005658 SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00005659 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Bill Wendling836ca7d2009-01-30 23:59:18 +00005660
Nate Begemanf845b452005-10-08 00:29:44 +00005661 // If we got a simplified select_cc node back from SimplifySelectCC, then
5662 // break it down into a new SETCC node, and a new SELECT node, and then return
5663 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +00005664 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005665 // Check to see if we got a select_cc back (to turn into setcc/select).
5666 // Otherwise, just return whatever node we got back, like fabs.
5667 if (SCC.getOpcode() == ISD::SELECT_CC) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00005668 SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getDebugLoc(),
5669 N0.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +00005670 SCC.getOperand(0), SCC.getOperand(1),
Bill Wendling836ca7d2009-01-30 23:59:18 +00005671 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +00005672 AddToWorkList(SETCC.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00005673 return DAG.getNode(ISD::SELECT, SCC.getDebugLoc(), SCC.getValueType(),
5674 SCC.getOperand(2), SCC.getOperand(3), SETCC);
Nate Begemanf845b452005-10-08 00:29:44 +00005675 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00005676
Nate Begemanf845b452005-10-08 00:29:44 +00005677 return SCC;
5678 }
Dan Gohman475871a2008-07-27 21:46:04 +00005679 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005680}
5681
Chris Lattner40c62d52005-10-18 06:04:22 +00005682/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
5683/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00005684/// select. Callers of this should assume that TheSelect is deleted if this
5685/// returns true. As such, they should return the appropriate thing (e.g. the
5686/// node) back to the top-level of the DAG combiner loop to avoid it being
5687/// looked at.
Scott Michelfdc40a02009-02-17 22:15:04 +00005688bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
Dan Gohman475871a2008-07-27 21:46:04 +00005689 SDValue RHS) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005690
Chris Lattner40c62d52005-10-18 06:04:22 +00005691 // If this is a select from two identical things, try to pull the operation
5692 // through the select.
5693 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00005694 // If this is a load and the token chain is identical, replace the select
5695 // of two loads with a load through a select of the address to load from.
5696 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
5697 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00005698 if (LHS.getOpcode() == ISD::LOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005699 // Do not let this transformation reduce the number of volatile loads.
5700 !cast<LoadSDNode>(LHS)->isVolatile() &&
5701 !cast<LoadSDNode>(RHS)->isVolatile() &&
Chris Lattner40c62d52005-10-18 06:04:22 +00005702 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00005703 LHS.getOperand(0) == RHS.getOperand(0)) {
5704 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5705 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5706
5707 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005708 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00005709 // FIXME: this conflates two src values, discarding one. This is not
5710 // the right thing to do, but nothing uses srcvalues now. When they do,
5711 // turn SrcValue into a list of locations.
Dan Gohman475871a2008-07-27 21:46:04 +00005712 SDValue Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005713 if (TheSelect->getOpcode() == ISD::SELECT) {
5714 // Check that the condition doesn't reach either load. If so, folding
5715 // this will induce a cycle into the DAG.
Gabor Greifba36cb52008-08-28 21:40:38 +00005716 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5717 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode())) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00005718 Addr = DAG.getNode(ISD::SELECT, TheSelect->getDebugLoc(),
5719 LLD->getBasePtr().getValueType(),
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005720 TheSelect->getOperand(0), LLD->getBasePtr(),
5721 RLD->getBasePtr());
5722 }
5723 } else {
5724 // Check that the condition doesn't reach either load. If so, folding
5725 // this will induce a cycle into the DAG.
Gabor Greifba36cb52008-08-28 21:40:38 +00005726 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5727 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5728 !LLD->isPredecessorOf(TheSelect->getOperand(1).getNode()) &&
5729 !RLD->isPredecessorOf(TheSelect->getOperand(1).getNode())) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00005730 Addr = DAG.getNode(ISD::SELECT_CC, TheSelect->getDebugLoc(),
5731 LLD->getBasePtr().getValueType(),
5732 TheSelect->getOperand(0),
Scott Michelfdc40a02009-02-17 22:15:04 +00005733 TheSelect->getOperand(1),
Bill Wendling836ca7d2009-01-30 23:59:18 +00005734 LLD->getBasePtr(), RLD->getBasePtr(),
5735 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005736 }
Evan Cheng466685d2006-10-09 20:57:25 +00005737 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005738
Gabor Greifba36cb52008-08-28 21:40:38 +00005739 if (Addr.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005740 SDValue Load;
Bill Wendling836ca7d2009-01-30 23:59:18 +00005741 if (LLD->getExtensionType() == ISD::NON_EXTLOAD) {
5742 Load = DAG.getLoad(TheSelect->getValueType(0),
5743 TheSelect->getDebugLoc(),
5744 LLD->getChain(),
Scott Michelfdc40a02009-02-17 22:15:04 +00005745 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005746 LLD->getSrcValueOffset(),
Scott Michelfdc40a02009-02-17 22:15:04 +00005747 LLD->isVolatile(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005748 LLD->getAlignment());
Bill Wendling836ca7d2009-01-30 23:59:18 +00005749 } else {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005750 Load = DAG.getExtLoad(LLD->getExtensionType(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00005751 TheSelect->getDebugLoc(),
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005752 TheSelect->getValueType(0),
5753 LLD->getChain(), Addr, LLD->getSrcValue(),
5754 LLD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005755 LLD->getMemoryVT(),
Scott Michelfdc40a02009-02-17 22:15:04 +00005756 LLD->isVolatile(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005757 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005758 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00005759
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005760 // Users of the select now use the result of the load.
5761 CombineTo(TheSelect, Load);
Scott Michelfdc40a02009-02-17 22:15:04 +00005762
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005763 // Users of the old loads now use the new load's chain. We know the
5764 // old-load value is dead now.
Gabor Greifba36cb52008-08-28 21:40:38 +00005765 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
5766 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005767 return true;
5768 }
Evan Chengc5484282006-10-04 00:56:09 +00005769 }
Chris Lattner40c62d52005-10-18 06:04:22 +00005770 }
5771 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005772
Chris Lattner40c62d52005-10-18 06:04:22 +00005773 return false;
5774}
5775
Chris Lattner600fec32009-03-11 05:08:08 +00005776/// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3
5777/// where 'cond' is the comparison specified by CC.
Scott Michelfdc40a02009-02-17 22:15:04 +00005778SDValue DAGCombiner::SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1,
Dan Gohman475871a2008-07-27 21:46:04 +00005779 SDValue N2, SDValue N3,
5780 ISD::CondCode CC, bool NotExtCompare) {
Chris Lattner600fec32009-03-11 05:08:08 +00005781 // (x ? y : y) -> y.
5782 if (N2 == N3) return N2;
5783
Owen Andersone50ed302009-08-10 22:56:29 +00005784 EVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +00005785 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
5786 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
5787 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005788
5789 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00005790 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00005791 N0, N1, CC, DL, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00005792 if (SCC.getNode()) AddToWorkList(SCC.getNode());
5793 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005794
5795 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00005796 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005797 return N2;
5798 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00005799 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005800 return N3;
Scott Michelfdc40a02009-02-17 22:15:04 +00005801
Nate Begemanf845b452005-10-08 00:29:44 +00005802 // Check to see if we can simplify the select into an fabs node
5803 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5804 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00005805 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005806 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5807 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5808 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5809 N2 == N3.getOperand(0))
Bill Wendling836ca7d2009-01-30 23:59:18 +00005810 return DAG.getNode(ISD::FABS, DL, VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005811
Nate Begemanf845b452005-10-08 00:29:44 +00005812 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5813 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5814 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5815 N2.getOperand(0) == N3)
Bill Wendling836ca7d2009-01-30 23:59:18 +00005816 return DAG.getNode(ISD::FABS, DL, VT, N3);
Nate Begemanf845b452005-10-08 00:29:44 +00005817 }
5818 }
Chris Lattner600fec32009-03-11 05:08:08 +00005819
5820 // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)"
5821 // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0
5822 // in it. This is a win when the constant is not otherwise available because
5823 // it replaces two constant pool loads with one. We only do this if the FP
5824 // type is known to be legal, because if it isn't, then we are before legalize
5825 // types an we want the other legalization to happen first (e.g. to avoid
Mon P Wang0b7a7862009-03-14 00:25:19 +00005826 // messing with soft float) and if the ConstantFP is not legal, because if
5827 // it is legal, we may not need to store the FP constant in a constant pool.
Chris Lattner600fec32009-03-11 05:08:08 +00005828 if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2))
5829 if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) {
5830 if (TLI.isTypeLegal(N2.getValueType()) &&
Mon P Wang0b7a7862009-03-14 00:25:19 +00005831 (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) !=
5832 TargetLowering::Legal) &&
Chris Lattner600fec32009-03-11 05:08:08 +00005833 // If both constants have multiple uses, then we won't need to do an
5834 // extra load, they are likely around in registers for other users.
5835 (TV->hasOneUse() || FV->hasOneUse())) {
5836 Constant *Elts[] = {
5837 const_cast<ConstantFP*>(FV->getConstantFPValue()),
5838 const_cast<ConstantFP*>(TV->getConstantFPValue())
5839 };
5840 const Type *FPTy = Elts[0]->getType();
5841 const TargetData &TD = *TLI.getTargetData();
5842
5843 // Create a ConstantArray of the two constants.
Owen Andersondebcb012009-07-29 22:17:13 +00005844 Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts, 2);
Chris Lattner600fec32009-03-11 05:08:08 +00005845 SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(),
5846 TD.getPrefTypeAlignment(FPTy));
Evan Cheng1606e8e2009-03-13 07:51:59 +00005847 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner600fec32009-03-11 05:08:08 +00005848
5849 // Get the offsets to the 0 and 1 element of the array so that we can
5850 // select between them.
5851 SDValue Zero = DAG.getIntPtrConstant(0);
Duncan Sands777d2302009-05-09 07:06:46 +00005852 unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType());
Chris Lattner600fec32009-03-11 05:08:08 +00005853 SDValue One = DAG.getIntPtrConstant(EltSize);
5854
5855 SDValue Cond = DAG.getSetCC(DL,
5856 TLI.getSetCCResultType(N0.getValueType()),
5857 N0, N1, CC);
5858 SDValue CstOffset = DAG.getNode(ISD::SELECT, DL, Zero.getValueType(),
5859 Cond, One, Zero);
5860 CPIdx = DAG.getNode(ISD::ADD, DL, TLI.getPointerTy(), CPIdx,
5861 CstOffset);
5862 return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx,
5863 PseudoSourceValue::getConstantPool(), 0, false,
5864 Alignment);
5865
5866 }
5867 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005868
Nate Begemanf845b452005-10-08 00:29:44 +00005869 // Check to see if we can perform the "gzip trick", transforming
Bill Wendling836ca7d2009-01-30 23:59:18 +00005870 // (select_cc setlt X, 0, A, 0) -> (and (sra X, (sub size(X), 1), A)
Chris Lattnere3152e52006-09-20 06:41:35 +00005871 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005872 N0.getValueType().isInteger() &&
5873 N2.getValueType().isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005874 (N1C->isNullValue() || // (a < 0) ? b : 0
5875 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Owen Andersone50ed302009-08-10 22:56:29 +00005876 EVT XType = N0.getValueType();
5877 EVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00005878 if (XType.bitsGE(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005879 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00005880 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00005881 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
5882 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005883 ShCtV = XType.getSizeInBits()-ShCtV-1;
Duncan Sands92abc622009-01-31 15:50:11 +00005884 SDValue ShCt = DAG.getConstant(ShCtV, getShiftAmountTy());
Bill Wendling9729c5a2009-01-31 03:12:48 +00005885 SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00005886 XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +00005887 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00005888
Duncan Sands8e4eb092008-06-08 20:54:56 +00005889 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00005890 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005891 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005892 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00005893
5894 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00005895 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00005896
Bill Wendling9729c5a2009-01-31 03:12:48 +00005897 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00005898 XType, N0,
5899 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00005900 getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00005901 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00005902
Duncan Sands8e4eb092008-06-08 20:54:56 +00005903 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00005904 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005905 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005906 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00005907
5908 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00005909 }
5910 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005911
Nate Begeman07ed4172005-10-10 21:26:48 +00005912 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00005913 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands03228082008-11-23 15:47:28 +00005914 TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005915
Chris Lattner1eba01e2007-04-11 06:50:51 +00005916 // If the caller doesn't want us to simplify this into a zext of a compare,
5917 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00005918 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00005919 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00005920
Nate Begeman07ed4172005-10-10 21:26:48 +00005921 // Get a SetCC of the condition
5922 // FIXME: Should probably make sure that setcc is legal if we ever have a
5923 // target where it isn't.
Dan Gohman475871a2008-07-27 21:46:04 +00005924 SDValue Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00005925 // cast from setcc result type to select result type
Duncan Sands25cf2272008-11-24 14:53:14 +00005926 if (LegalTypes) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00005927 SCC = DAG.getSetCC(DL, TLI.getSetCCResultType(N0.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00005928 N0, N1, CC);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005929 if (N2.getValueType().bitsLT(SCC.getValueType()))
Bill Wendling9729c5a2009-01-31 03:12:48 +00005930 Temp = DAG.getZeroExtendInReg(SCC, N2.getDebugLoc(), N2.getValueType());
Chris Lattner555d8d62006-12-07 22:36:47 +00005931 else
Bill Wendling9729c5a2009-01-31 03:12:48 +00005932 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00005933 N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005934 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00005935 SCC = DAG.getSetCC(N0.getDebugLoc(), MVT::i1, N0, N1, CC);
Bill Wendling9729c5a2009-01-31 03:12:48 +00005936 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00005937 N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005938 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00005939
Gabor Greifba36cb52008-08-28 21:40:38 +00005940 AddToWorkList(SCC.getNode());
5941 AddToWorkList(Temp.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005942
Dan Gohman002e5d02008-03-13 22:13:53 +00005943 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005944 return Temp;
Bill Wendling836ca7d2009-01-30 23:59:18 +00005945
Nate Begeman07ed4172005-10-10 21:26:48 +00005946 // shl setcc result by log2 n2c
Bill Wendling836ca7d2009-01-30 23:59:18 +00005947 return DAG.getNode(ISD::SHL, DL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00005948 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Duncan Sands92abc622009-01-31 15:50:11 +00005949 getShiftAmountTy()));
Nate Begeman07ed4172005-10-10 21:26:48 +00005950 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005951
Nate Begemanf845b452005-10-08 00:29:44 +00005952 // Check to see if this is the equivalent of setcc
5953 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5954 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00005955 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005956 EVT XType = N0.getValueType();
Duncan Sands25cf2272008-11-24 14:53:14 +00005957 if (!LegalOperations ||
Duncan Sands5480c042009-01-01 15:52:00 +00005958 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(XType))) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00005959 SDValue Res = DAG.getSetCC(DL, TLI.getSetCCResultType(XType), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00005960 if (Res.getValueType() != VT)
Bill Wendling836ca7d2009-01-30 23:59:18 +00005961 Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res);
Nate Begemanf845b452005-10-08 00:29:44 +00005962 return Res;
5963 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005964
Bill Wendling836ca7d2009-01-30 23:59:18 +00005965 // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X))))
Scott Michelfdc40a02009-02-17 22:15:04 +00005966 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005967 (!LegalOperations ||
Duncan Sands184a8762008-06-14 17:48:34 +00005968 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00005969 SDValue Ctlz = DAG.getNode(ISD::CTLZ, N0.getDebugLoc(), XType, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005970 return DAG.getNode(ISD::SRL, DL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005971 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Duncan Sands92abc622009-01-31 15:50:11 +00005972 getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00005973 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00005974 // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1))
Scott Michelfdc40a02009-02-17 22:15:04 +00005975 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00005976 SDValue NegN0 = DAG.getNode(ISD::SUB, N0.getDebugLoc(),
5977 XType, DAG.getConstant(0, XType), N0);
Bill Wendling7581bfa2009-01-30 23:03:19 +00005978 SDValue NotN0 = DAG.getNOT(N0.getDebugLoc(), N0, XType);
Bill Wendling836ca7d2009-01-30 23:59:18 +00005979 return DAG.getNode(ISD::SRL, DL, XType,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00005980 DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00005981 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00005982 getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00005983 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00005984 // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1))
Nate Begemanf845b452005-10-08 00:29:44 +00005985 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00005986 SDValue Sign = DAG.getNode(ISD::SRL, N0.getDebugLoc(), XType, N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +00005987 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00005988 getShiftAmountTy()));
Bill Wendling836ca7d2009-01-30 23:59:18 +00005989 return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType));
Nate Begemanf845b452005-10-08 00:29:44 +00005990 }
5991 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005992
Nate Begemanf845b452005-10-08 00:29:44 +00005993 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5994 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5995 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005996 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005997 N2.getOperand(0) == N1 && N0.getValueType().isInteger()) {
Owen Andersone50ed302009-08-10 22:56:29 +00005998 EVT XType = N0.getValueType();
Bill Wendling9729c5a2009-01-31 03:12:48 +00005999 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(), XType, N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +00006000 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00006001 getShiftAmountTy()));
Bill Wendling9729c5a2009-01-31 03:12:48 +00006002 SDValue Add = DAG.getNode(ISD::ADD, N0.getDebugLoc(), XType,
Bill Wendling836ca7d2009-01-30 23:59:18 +00006003 N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00006004 AddToWorkList(Shift.getNode());
6005 AddToWorkList(Add.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00006006 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Chris Lattner1982ef22007-04-11 05:11:38 +00006007 }
6008 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
6009 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
6010 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
6011 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
6012 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Owen Andersone50ed302009-08-10 22:56:29 +00006013 EVT XType = N0.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006014 if (SubC->isNullValue() && XType.isInteger()) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00006015 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(), XType,
Bill Wendling836ca7d2009-01-30 23:59:18 +00006016 N0,
6017 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00006018 getShiftAmountTy()));
Bill Wendling9729c5a2009-01-31 03:12:48 +00006019 SDValue Add = DAG.getNode(ISD::ADD, N0.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00006020 XType, N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00006021 AddToWorkList(Shift.getNode());
6022 AddToWorkList(Add.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00006023 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Nate Begemanf845b452005-10-08 00:29:44 +00006024 }
6025 }
6026 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006027
Dan Gohman475871a2008-07-27 21:46:04 +00006028 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00006029}
6030
Evan Chengfa1eb272007-02-08 22:13:59 +00006031/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Owen Andersone50ed302009-08-10 22:56:29 +00006032SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0,
Dan Gohman475871a2008-07-27 21:46:04 +00006033 SDValue N1, ISD::CondCode Cond,
Dale Johannesenff97d4f2009-02-03 00:47:48 +00006034 DebugLoc DL, bool foldBooleans) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006035 TargetLowering::DAGCombinerInfo
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00006036 DagCombineInfo(DAG, !LegalTypes, !LegalOperations, false, this);
Dale Johannesenff97d4f2009-02-03 00:47:48 +00006037 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL);
Nate Begeman452d7be2005-09-16 00:54:12 +00006038}
6039
Nate Begeman69575232005-10-20 02:15:44 +00006040/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
6041/// return a DAG expression to select that will generate the same value by
6042/// multiplying by a magic number. See:
6043/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00006044SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00006045 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00006046 SDValue S = TLI.BuildSDIV(N, DAG, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00006047
Andrew Lenharth232c9102006-06-12 16:07:18 +00006048 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00006049 ii != ee; ++ii)
6050 AddToWorkList(*ii);
6051 return S;
Nate Begeman69575232005-10-20 02:15:44 +00006052}
6053
6054/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
6055/// return a DAG expression to select that will generate the same value by
6056/// multiplying by a magic number. See:
6057/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00006058SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00006059 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00006060 SDValue S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00006061
Andrew Lenharth232c9102006-06-12 16:07:18 +00006062 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00006063 ii != ee; ++ii)
6064 AddToWorkList(*ii);
6065 return S;
Nate Begeman69575232005-10-20 02:15:44 +00006066}
6067
Jim Laskey71382342006-10-07 23:37:56 +00006068/// FindBaseOffset - Return true if base is known not to alias with anything
6069/// but itself. Provides base object and offset as results.
Dan Gohman475871a2008-07-27 21:46:04 +00006070static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset) {
Jim Laskey71382342006-10-07 23:37:56 +00006071 // Assume it is a primitive operation.
6072 Base = Ptr; Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00006073
Jim Laskey71382342006-10-07 23:37:56 +00006074 // If it's an adding a simple constant then integrate the offset.
6075 if (Base.getOpcode() == ISD::ADD) {
6076 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
6077 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006078 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +00006079 }
6080 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006081
Jim Laskey71382342006-10-07 23:37:56 +00006082 // If it's any of the following then it can't alias with anything but itself.
6083 return isa<FrameIndexSDNode>(Base) ||
6084 isa<ConstantPoolSDNode>(Base) ||
6085 isa<GlobalAddressSDNode>(Base);
6086}
6087
6088/// isAlias - Return true if there is any possibility that the two addresses
6089/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +00006090bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +00006091 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman475871a2008-07-27 21:46:04 +00006092 SDValue Ptr2, int64_t Size2,
Bill Wendling836ca7d2009-01-30 23:59:18 +00006093 const Value *SrcValue2, int SrcValueOffset2) const {
Jim Laskey71382342006-10-07 23:37:56 +00006094 // If they are the same then they must be aliases.
6095 if (Ptr1 == Ptr2) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00006096
Jim Laskey71382342006-10-07 23:37:56 +00006097 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +00006098 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +00006099 int64_t Offset1, Offset2;
6100 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
6101 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
Scott Michelfdc40a02009-02-17 22:15:04 +00006102
Jim Laskey71382342006-10-07 23:37:56 +00006103 // If they have a same base address then...
Bill Wendling836ca7d2009-01-30 23:59:18 +00006104 if (Base1 == Base2)
Jim Laskey71382342006-10-07 23:37:56 +00006105 // Check to see if the addresses overlap.
Bill Wendling836ca7d2009-01-30 23:59:18 +00006106 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006107
Jim Laskey096c22e2006-10-18 12:29:57 +00006108 // If we know both bases then they can't alias.
6109 if (KnownBase1 && KnownBase2) return false;
6110
Jim Laskey07a27092006-10-18 19:08:31 +00006111 if (CombinerGlobalAA) {
6112 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00006113 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
6114 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
6115 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Scott Michelfdc40a02009-02-17 22:15:04 +00006116 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00006117 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00006118 if (AAResult == AliasAnalysis::NoAlias)
6119 return false;
6120 }
Jim Laskey096c22e2006-10-18 12:29:57 +00006121
6122 // Otherwise we have to assume they alias.
6123 return true;
Jim Laskey71382342006-10-07 23:37:56 +00006124}
6125
6126/// FindAliasInfo - Extracts the relevant alias information from the memory
6127/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00006128bool DAGCombiner::FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +00006129 SDValue &Ptr, int64_t &Size,
Bill Wendling836ca7d2009-01-30 23:59:18 +00006130 const Value *&SrcValue, int &SrcValueOffset) const {
Jim Laskey7ca56af2006-10-11 13:47:09 +00006131 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
6132 Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006133 Size = LD->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00006134 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00006135 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00006136 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00006137 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00006138 Ptr = ST->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006139 Size = ST->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00006140 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00006141 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00006142 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00006143 llvm_unreachable("FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00006144 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006145
Jim Laskey71382342006-10-07 23:37:56 +00006146 return false;
6147}
6148
Jim Laskey6ff23e52006-10-04 16:53:27 +00006149/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
6150/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +00006151void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
6152 SmallVector<SDValue, 8> &Aliases) {
6153 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00006154 std::set<SDNode *> Visited; // Visited node set.
Scott Michelfdc40a02009-02-17 22:15:04 +00006155
Jim Laskey279f0532006-09-25 16:29:54 +00006156 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +00006157 SDValue Ptr;
Daniel Dunbar8c562e22009-05-18 16:43:04 +00006158 int64_t Size = 0;
6159 const Value *SrcValue = 0;
6160 int SrcValueOffset = 0;
Jim Laskey096c22e2006-10-18 12:29:57 +00006161 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00006162
Jim Laskey6ff23e52006-10-04 16:53:27 +00006163 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00006164 Chains.push_back(OriginalChain);
Scott Michelfdc40a02009-02-17 22:15:04 +00006165
Jim Laskeybc588b82006-10-05 15:07:25 +00006166 // Look at each chain and determine if it is an alias. If so, add it to the
6167 // aliases list. If not, then continue up the chain looking for the next
Scott Michelfdc40a02009-02-17 22:15:04 +00006168 // candidate.
Jim Laskeybc588b82006-10-05 15:07:25 +00006169 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006170 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +00006171 Chains.pop_back();
Scott Michelfdc40a02009-02-17 22:15:04 +00006172
Jim Laskeybc588b82006-10-05 15:07:25 +00006173 // Don't bother if we've been before.
Gabor Greifba36cb52008-08-28 21:40:38 +00006174 if (Visited.find(Chain.getNode()) != Visited.end()) continue;
6175 Visited.insert(Chain.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006176
Jim Laskeybc588b82006-10-05 15:07:25 +00006177 switch (Chain.getOpcode()) {
6178 case ISD::EntryToken:
6179 // Entry token is ideal chain operand, but handled in FindBetterChain.
6180 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006181
Jim Laskeybc588b82006-10-05 15:07:25 +00006182 case ISD::LOAD:
6183 case ISD::STORE: {
6184 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006185 SDValue OpPtr;
Daniel Dunbar8c562e22009-05-18 16:43:04 +00006186 int64_t OpSize = 0;
6187 const Value *OpSrcValue = 0;
6188 int OpSrcValueOffset = 0;
Gabor Greifba36cb52008-08-28 21:40:38 +00006189 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Jim Laskey096c22e2006-10-18 12:29:57 +00006190 OpSrcValue, OpSrcValueOffset);
Scott Michelfdc40a02009-02-17 22:15:04 +00006191
Jim Laskeybc588b82006-10-05 15:07:25 +00006192 // If chain is alias then stop here.
6193 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00006194 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
6195 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00006196 Aliases.push_back(Chain);
6197 } else {
6198 // Look further up the chain.
Scott Michelfdc40a02009-02-17 22:15:04 +00006199 Chains.push_back(Chain.getOperand(0));
Jim Laskeybc588b82006-10-05 15:07:25 +00006200 // Clean up old chain.
Gabor Greifba36cb52008-08-28 21:40:38 +00006201 AddToWorkList(Chain.getNode());
Jim Laskey279f0532006-09-25 16:29:54 +00006202 }
Jim Laskeybc588b82006-10-05 15:07:25 +00006203 break;
6204 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006205
Jim Laskeybc588b82006-10-05 15:07:25 +00006206 case ISD::TokenFactor:
6207 // We have to check each of the operands of the token factor, so we queue
6208 // then up. Adding the operands to the queue (stack) in reverse order
6209 // maintains the original order and increases the likelihood that getNode
6210 // will find a matching token factor (CSE.)
6211 for (unsigned n = Chain.getNumOperands(); n;)
6212 Chains.push_back(Chain.getOperand(--n));
6213 // Eliminate the token factor if we can.
Gabor Greifba36cb52008-08-28 21:40:38 +00006214 AddToWorkList(Chain.getNode());
Jim Laskeybc588b82006-10-05 15:07:25 +00006215 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006216
Jim Laskeybc588b82006-10-05 15:07:25 +00006217 default:
6218 // For all other instructions we will just have to take what we can get.
6219 Aliases.push_back(Chain);
6220 break;
Jim Laskey279f0532006-09-25 16:29:54 +00006221 }
6222 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00006223}
6224
6225/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
6226/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +00006227SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
6228 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00006229
Jim Laskey6ff23e52006-10-04 16:53:27 +00006230 // Accumulate all the aliases to this node.
6231 GatherAllAliases(N, OldChain, Aliases);
Scott Michelfdc40a02009-02-17 22:15:04 +00006232
Jim Laskey6ff23e52006-10-04 16:53:27 +00006233 if (Aliases.size() == 0) {
6234 // If no operands then chain to entry token.
6235 return DAG.getEntryNode();
6236 } else if (Aliases.size() == 1) {
6237 // If a single operand then chain to it. We don't need to revisit it.
6238 return Aliases[0];
6239 }
6240
6241 // Construct a custom tailored token factor.
Owen Anderson825b72b2009-08-11 20:47:22 +00006242 SDValue NewChain = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00006243 &Aliases[0], Aliases.size());
Jim Laskey6ff23e52006-10-04 16:53:27 +00006244
6245 // Make sure the old chain gets cleaned up.
Gabor Greifba36cb52008-08-28 21:40:38 +00006246 if (NewChain != OldChain) AddToWorkList(OldChain.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006247
Jim Laskey6ff23e52006-10-04 16:53:27 +00006248 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00006249}
6250
Nate Begeman1d4d4142005-09-01 00:19:25 +00006251// SelectionDAG::Combine - This is the entry point for the file.
6252//
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00006253void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA,
Bill Wendling98a366d2009-04-29 23:29:43 +00006254 CodeGenOpt::Level OptLevel) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00006255 /// run - This is the main entry point to this class.
6256 ///
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00006257 DAGCombiner(*this, AA, OptLevel).Run(Level);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006258}