blob: 9192593af11f23d94c680fa33dc49b135d033977 [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"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000034#include "llvm/Support/CommandLine.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000035#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000036#include "llvm/Support/ErrorHandling.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000037#include "llvm/Support/MathExtras.h"
Chris Lattnerbbbfa992009-08-23 06:35:02 +000038#include "llvm/Support/raw_ostream.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000039#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000040using namespace llvm;
41
Chris Lattnercd3245a2006-12-19 22:41:21 +000042STATISTIC(NodesCombined , "Number of dag nodes combined");
43STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
44STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
Evan Cheng8b944d32009-05-28 00:35:15 +000045STATISTIC(OpsNarrowed , "Number of load/op/store narrowed");
Chris Lattnercd3245a2006-12-19 22:41:21 +000046
Nate Begeman1d4d4142005-09-01 00:19:25 +000047namespace {
Jim Laskey71382342006-10-07 23:37:56 +000048 static cl::opt<bool>
49 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000050 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000051
Jim Laskey07a27092006-10-18 19:08:31 +000052 static cl::opt<bool>
53 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
54 cl::desc("Include global information in alias analysis"));
55
Jim Laskeybc588b82006-10-05 15:07:25 +000056//------------------------------ DAGCombiner ---------------------------------//
57
Nick Lewycky6726b6d2009-10-25 06:33:48 +000058 class DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000059 SelectionDAG &DAG;
Dan Gohman79ce2762009-01-15 19:20:50 +000060 const TargetLowering &TLI;
Duncan Sands25cf2272008-11-24 14:53:14 +000061 CombineLevel Level;
Bill Wendling98a366d2009-04-29 23:29:43 +000062 CodeGenOpt::Level OptLevel;
Duncan Sands25cf2272008-11-24 14:53:14 +000063 bool LegalOperations;
64 bool LegalTypes;
Nate Begeman1d4d4142005-09-01 00:19:25 +000065
66 // Worklist of all of the nodes that need to be simplified.
Evan Cheng17a568b2008-08-29 22:21:44 +000067 std::vector<SDNode*> WorkList;
Nate Begeman1d4d4142005-09-01 00:19:25 +000068
Jim Laskeyc7c3f112006-10-16 20:52:31 +000069 // AA - Used for DAG load/store alias analysis.
70 AliasAnalysis &AA;
71
Nate Begeman1d4d4142005-09-01 00:19:25 +000072 /// AddUsersToWorkList - When an instruction is simplified, add all users of
73 /// the instruction to the work lists because they might get more simplified
74 /// now.
75 ///
76 void AddUsersToWorkList(SDNode *N) {
77 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000078 UI != UE; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +000079 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000080 }
81
Dan Gohman389079b2007-10-08 17:57:15 +000082 /// visit - call the node-specific routine that knows how to fold each
83 /// particular type of node.
Dan Gohman475871a2008-07-27 21:46:04 +000084 SDValue visit(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +000085
Chris Lattner24664722006-03-01 04:53:38 +000086 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +000087 /// AddToWorkList - Add to the work list making sure it's instance is at the
88 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +000089 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000090 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +000091 WorkList.push_back(N);
92 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000093
Chris Lattnerf8dc0612008-02-03 06:49:24 +000094 /// removeFromWorkList - remove all instances of N from the worklist.
95 ///
96 void removeFromWorkList(SDNode *N) {
97 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
98 WorkList.end());
Chris Lattner01a22022005-10-10 22:04:48 +000099 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000100
Dan Gohman475871a2008-07-27 21:46:04 +0000101 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000102 bool AddTo = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000103
Dan Gohman475871a2008-07-27 21:46:04 +0000104 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Jim Laskey274062c2006-10-13 23:32:28 +0000105 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000106 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000107
Dan Gohman475871a2008-07-27 21:46:04 +0000108 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000109 bool AddTo = true) {
Dan Gohman475871a2008-07-27 21:46:04 +0000110 SDValue To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000111 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000112 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000113
114 void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO);
Scott Michelfdc40a02009-02-17 22:15:04 +0000115
116 private:
117
Chris Lattner012f2412006-02-17 21:58:01 +0000118 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000119 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000120 /// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000121 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman87862e72009-12-11 21:31:27 +0000122 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
123 APInt Demanded = APInt::getAllOnesValue(BitWidth);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000124 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
Evan Cheng95c57ea2010-04-24 04:43:44 +0000132 void ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad);
133 SDValue PromoteOperand(SDValue Op, EVT PVT, bool &Replace);
134 SDValue SExtPromoteOperand(SDValue Op, EVT PVT);
135 SDValue ZExtPromoteOperand(SDValue Op, EVT PVT);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000136 SDValue PromoteIntBinOp(SDValue Op);
Evan Cheng07c4e102010-04-22 20:19:46 +0000137 SDValue PromoteIntShiftOp(SDValue Op);
Evan Cheng4c26e932010-04-19 19:29:22 +0000138 SDValue PromoteExtend(SDValue Op);
139 bool PromoteLoad(SDValue Op);
Scott Michelfdc40a02009-02-17 22:15:04 +0000140
Dan Gohman389079b2007-10-08 17:57:15 +0000141 /// combine - call the node-specific routine that knows how to fold each
142 /// particular type of node. If that doesn't do anything, try the
143 /// target-specific DAG combines.
Dan Gohman475871a2008-07-27 21:46:04 +0000144 SDValue combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000145
146 // Visitation implementation - Implement dag node combining for different
147 // node types. The semantics are as follows:
148 // Return Value:
Evan Cheng17a568b2008-08-29 22:21:44 +0000149 // SDValue.getNode() == 0 - No change was made
150 // SDValue.getNode() == N - N was replaced, is dead and has been handled.
151 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000152 //
Dan Gohman475871a2008-07-27 21:46:04 +0000153 SDValue visitTokenFactor(SDNode *N);
154 SDValue visitMERGE_VALUES(SDNode *N);
155 SDValue visitADD(SDNode *N);
156 SDValue visitSUB(SDNode *N);
157 SDValue visitADDC(SDNode *N);
158 SDValue visitADDE(SDNode *N);
159 SDValue visitMUL(SDNode *N);
160 SDValue visitSDIV(SDNode *N);
161 SDValue visitUDIV(SDNode *N);
162 SDValue visitSREM(SDNode *N);
163 SDValue visitUREM(SDNode *N);
164 SDValue visitMULHU(SDNode *N);
165 SDValue visitMULHS(SDNode *N);
166 SDValue visitSMUL_LOHI(SDNode *N);
167 SDValue visitUMUL_LOHI(SDNode *N);
168 SDValue visitSDIVREM(SDNode *N);
169 SDValue visitUDIVREM(SDNode *N);
170 SDValue visitAND(SDNode *N);
171 SDValue visitOR(SDNode *N);
172 SDValue visitXOR(SDNode *N);
173 SDValue SimplifyVBinOp(SDNode *N);
174 SDValue visitSHL(SDNode *N);
175 SDValue visitSRA(SDNode *N);
176 SDValue visitSRL(SDNode *N);
177 SDValue visitCTLZ(SDNode *N);
178 SDValue visitCTTZ(SDNode *N);
179 SDValue visitCTPOP(SDNode *N);
180 SDValue visitSELECT(SDNode *N);
181 SDValue visitSELECT_CC(SDNode *N);
182 SDValue visitSETCC(SDNode *N);
183 SDValue visitSIGN_EXTEND(SDNode *N);
184 SDValue visitZERO_EXTEND(SDNode *N);
185 SDValue visitANY_EXTEND(SDNode *N);
186 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
187 SDValue visitTRUNCATE(SDNode *N);
188 SDValue visitBIT_CONVERT(SDNode *N);
189 SDValue visitBUILD_PAIR(SDNode *N);
190 SDValue visitFADD(SDNode *N);
191 SDValue visitFSUB(SDNode *N);
192 SDValue visitFMUL(SDNode *N);
193 SDValue visitFDIV(SDNode *N);
194 SDValue visitFREM(SDNode *N);
195 SDValue visitFCOPYSIGN(SDNode *N);
196 SDValue visitSINT_TO_FP(SDNode *N);
197 SDValue visitUINT_TO_FP(SDNode *N);
198 SDValue visitFP_TO_SINT(SDNode *N);
199 SDValue visitFP_TO_UINT(SDNode *N);
200 SDValue visitFP_ROUND(SDNode *N);
201 SDValue visitFP_ROUND_INREG(SDNode *N);
202 SDValue visitFP_EXTEND(SDNode *N);
203 SDValue visitFNEG(SDNode *N);
204 SDValue visitFABS(SDNode *N);
205 SDValue visitBRCOND(SDNode *N);
206 SDValue visitBR_CC(SDNode *N);
207 SDValue visitLOAD(SDNode *N);
208 SDValue visitSTORE(SDNode *N);
209 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
210 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
211 SDValue visitBUILD_VECTOR(SDNode *N);
212 SDValue visitCONCAT_VECTORS(SDNode *N);
213 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000214
Dan Gohman475871a2008-07-27 21:46:04 +0000215 SDValue XformToShuffleWithZero(SDNode *N);
Bill Wendling35247c32009-01-30 00:45:56 +0000216 SDValue ReassociateOps(unsigned Opc, DebugLoc DL, SDValue LHS, SDValue RHS);
Scott Michelfdc40a02009-02-17 22:15:04 +0000217
Dan Gohman475871a2008-07-27 21:46:04 +0000218 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattnere70da202007-12-06 07:33:36 +0000219
Dan Gohman475871a2008-07-27 21:46:04 +0000220 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
221 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Bill Wendling836ca7d2009-01-30 23:59:18 +0000222 SDValue SimplifySelect(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2);
Scott Michelfdc40a02009-02-17 22:15:04 +0000223 SDValue SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2,
224 SDValue N3, ISD::CondCode CC,
Bill Wendling836ca7d2009-01-30 23:59:18 +0000225 bool NotExtCompare = false);
Owen Andersone50ed302009-08-10 22:56:29 +0000226 SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond,
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000227 DebugLoc DL, bool foldBooleans = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000228 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner5eee4272008-01-26 01:09:19 +0000229 unsigned HiOp);
Owen Andersone50ed302009-08-10 22:56:29 +0000230 SDValue CombineConsecutiveLoads(SDNode *N, EVT VT);
231 SDValue ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, EVT);
Dan Gohman475871a2008-07-27 21:46:04 +0000232 SDValue BuildSDIV(SDNode *N);
233 SDValue BuildUDIV(SDNode *N);
Bill Wendling317bd702009-01-30 21:14:50 +0000234 SDNode *MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL);
Dan Gohman475871a2008-07-27 21:46:04 +0000235 SDValue ReduceLoadWidth(SDNode *N);
Evan Cheng8b944d32009-05-28 00:35:15 +0000236 SDValue ReduceLoadOpStoreWidth(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000237
Dan Gohman475871a2008-07-27 21:46:04 +0000238 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Scott Michelfdc40a02009-02-17 22:15:04 +0000239
Jim Laskey6ff23e52006-10-04 16:53:27 +0000240 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
241 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000242 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
243 SmallVector<SDValue, 8> &Aliases);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000244
Jim Laskey096c22e2006-10-18 12:29:57 +0000245 /// isAlias - Return true if there is any possibility that the two addresses
246 /// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +0000247 bool isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +0000248 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000249 unsigned SrcValueAlign1,
Dan Gohman475871a2008-07-27 21:46:04 +0000250 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000251 const Value *SrcValue2, int SrcValueOffset2,
252 unsigned SrcValueAlign2) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000253
Jim Laskey7ca56af2006-10-11 13:47:09 +0000254 /// FindAliasInfo - Extracts the relevant alias information from the memory
255 /// node. Returns true if the operand was a load.
256 bool FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000257 SDValue &Ptr, int64_t &Size,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000258 const Value *&SrcValue, int &SrcValueOffset,
259 unsigned &SrcValueAlignment) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000260
Jim Laskey279f0532006-09-25 16:29:54 +0000261 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000262 /// looking for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +0000263 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Duncan Sands92abc622009-01-31 15:50:11 +0000264
Chris Lattner2392ae72010-04-15 04:48:01 +0000265 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000266 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, CodeGenOpt::Level OL)
Chris Lattner2392ae72010-04-15 04:48:01 +0000267 : DAG(D), TLI(D.getTargetLoweringInfo()), Level(Unrestricted),
268 OptLevel(OL), LegalOperations(false), LegalTypes(false), 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);
Chris Lattner2392ae72010-04-15 04:48:01 +0000272
273 SelectionDAG &getDAG() const { return DAG; }
274
275 /// getShiftAmountTy - Returns a type large enough to hold any valid
276 /// shift amount - before type legalization these can be huge.
277 EVT getShiftAmountTy() {
278 return LegalTypes ? TLI.getShiftAmountTy() : TLI.getPointerTy();
279 }
280
281 /// isTypeLegal - This method returns true if we are running before type
282 /// legalization or if the specified VT is legal.
283 bool isTypeLegal(const EVT &VT) {
284 if (!LegalTypes) return true;
285 return TLI.isTypeLegal(VT);
286 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000287 };
288}
289
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000290
291namespace {
292/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
293/// nodes from the worklist.
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000294class WorkListRemover : public SelectionDAG::DAGUpdateListener {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000295 DAGCombiner &DC;
296public:
Dan Gohmanb5660dc2008-02-20 16:44:09 +0000297 explicit WorkListRemover(DAGCombiner &dc) : DC(dc) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000298
Duncan Sandsedfcf592008-06-11 11:42:12 +0000299 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000300 DC.removeFromWorkList(N);
301 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000302
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000303 virtual void NodeUpdated(SDNode *N) {
304 // Ignore updates.
305 }
306};
307}
308
Chris Lattner24664722006-03-01 04:53:38 +0000309//===----------------------------------------------------------------------===//
310// TargetLowering::DAGCombinerInfo implementation
311//===----------------------------------------------------------------------===//
312
313void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
314 ((DAGCombiner*)DC)->AddToWorkList(N);
315}
316
Dan Gohman475871a2008-07-27 21:46:04 +0000317SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000318CombineTo(SDNode *N, const std::vector<SDValue> &To, bool AddTo) {
319 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000320}
321
Dan Gohman475871a2008-07-27 21:46:04 +0000322SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000323CombineTo(SDNode *N, SDValue Res, bool AddTo) {
324 return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000325}
326
327
Dan Gohman475871a2008-07-27 21:46:04 +0000328SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000329CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) {
330 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000331}
332
Dan Gohmane5af2d32009-01-29 01:59:02 +0000333void TargetLowering::DAGCombinerInfo::
334CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
335 return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO);
336}
Chris Lattner24664722006-03-01 04:53:38 +0000337
Chris Lattner24664722006-03-01 04:53:38 +0000338//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000339// Helper Functions
340//===----------------------------------------------------------------------===//
341
342/// isNegatibleForFree - Return 1 if we can compute the negated form of the
343/// specified expression for the same cost as the expression itself, or 2 if we
344/// can compute the negated form more cheaply than the expression itself.
Duncan Sands25cf2272008-11-24 14:53:14 +0000345static char isNegatibleForFree(SDValue Op, bool LegalOperations,
Chris Lattner0254e702008-02-26 07:04:54 +0000346 unsigned Depth = 0) {
Dale Johannesendb44bf82007-10-16 23:38:29 +0000347 // No compile time optimizations on this type.
Owen Anderson825b72b2009-08-11 20:47:22 +0000348 if (Op.getValueType() == MVT::ppcf128)
Dale Johannesendb44bf82007-10-16 23:38:29 +0000349 return 0;
350
Chris Lattner29446522007-05-14 22:04:50 +0000351 // fneg is removable even if it has multiple uses.
352 if (Op.getOpcode() == ISD::FNEG) return 2;
Scott Michelfdc40a02009-02-17 22:15:04 +0000353
Chris Lattner29446522007-05-14 22:04:50 +0000354 // Don't allow anything with multiple uses.
355 if (!Op.hasOneUse()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000356
Chris Lattner3adf9512007-05-25 02:19:06 +0000357 // Don't recurse exponentially.
358 if (Depth > 6) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000359
Chris Lattner29446522007-05-14 22:04:50 +0000360 switch (Op.getOpcode()) {
361 default: return false;
362 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000363 // Don't invert constant FP values after legalize. The negated constant
364 // isn't necessarily legal.
Duncan Sands25cf2272008-11-24 14:53:14 +0000365 return LegalOperations ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000366 case ISD::FADD:
367 // FIXME: determine better conditions for this xform.
368 if (!UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000369
Bill Wendlingd34470c2009-01-30 23:10:18 +0000370 // fold (fsub (fadd A, B)) -> (fsub (fneg A), B)
Duncan Sands25cf2272008-11-24 14:53:14 +0000371 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000372 return V;
Bill Wendlingd34470c2009-01-30 23:10:18 +0000373 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Duncan Sands25cf2272008-11-24 14:53:14 +0000374 return isNegatibleForFree(Op.getOperand(1), LegalOperations, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000375 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000376 // We can't turn -(A-B) into B-A when we honor signed zeros.
Chris Lattner29446522007-05-14 22:04:50 +0000377 if (!UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000378
Bill Wendlingd34470c2009-01-30 23:10:18 +0000379 // fold (fneg (fsub A, B)) -> (fsub B, A)
Chris Lattner29446522007-05-14 22:04:50 +0000380 return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000381
Chris Lattner29446522007-05-14 22:04:50 +0000382 case ISD::FMUL:
383 case ISD::FDIV:
384 if (HonorSignDependentRoundingFPMath()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000385
Bill Wendlingd34470c2009-01-30 23:10:18 +0000386 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y))
Duncan Sands25cf2272008-11-24 14:53:14 +0000387 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000388 return V;
Scott Michelfdc40a02009-02-17 22:15:04 +0000389
Duncan Sands25cf2272008-11-24 14:53:14 +0000390 return isNegatibleForFree(Op.getOperand(1), LegalOperations, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000391
Chris Lattner29446522007-05-14 22:04:50 +0000392 case ISD::FP_EXTEND:
393 case ISD::FP_ROUND:
394 case ISD::FSIN:
Duncan Sands25cf2272008-11-24 14:53:14 +0000395 return isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000396 }
397}
398
399/// GetNegatedExpression - If isNegatibleForFree returns true, this function
400/// returns the newly negated expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000401static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000402 bool LegalOperations, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000403 // fneg is removable even if it has multiple uses.
404 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000405
Chris Lattner29446522007-05-14 22:04:50 +0000406 // Don't allow anything with multiple uses.
407 assert(Op.hasOneUse() && "Unknown reuse!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000408
Chris Lattner3adf9512007-05-25 02:19:06 +0000409 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000410 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000411 default: llvm_unreachable("Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000412 case ISD::ConstantFP: {
413 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
414 V.changeSign();
415 return DAG.getConstantFP(V, Op.getValueType());
416 }
Chris Lattner29446522007-05-14 22:04:50 +0000417 case ISD::FADD:
418 // FIXME: determine better conditions for this xform.
419 assert(UnsafeFPMath);
Scott Michelfdc40a02009-02-17 22:15:04 +0000420
Bill Wendlingd34470c2009-01-30 23:10:18 +0000421 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Duncan Sands25cf2272008-11-24 14:53:14 +0000422 if (isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Bill Wendling35247c32009-01-30 00:45:56 +0000423 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000424 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000425 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000426 Op.getOperand(1));
Bill Wendlingd34470c2009-01-30 23:10:18 +0000427 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Bill Wendling35247c32009-01-30 00:45:56 +0000428 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000429 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000430 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000431 Op.getOperand(0));
432 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000433 // We can't turn -(A-B) into B-A when we honor signed zeros.
Chris Lattner29446522007-05-14 22:04:50 +0000434 assert(UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000435
Bill Wendlingd34470c2009-01-30 23:10:18 +0000436 // fold (fneg (fsub 0, B)) -> B
Dan Gohman23ff1822007-07-02 15:48:56 +0000437 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000438 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000439 return Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000440
Bill Wendlingd34470c2009-01-30 23:10:18 +0000441 // fold (fneg (fsub A, B)) -> (fsub B, A)
Bill Wendling35247c32009-01-30 00:45:56 +0000442 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
443 Op.getOperand(1), Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +0000444
Chris Lattner29446522007-05-14 22:04:50 +0000445 case ISD::FMUL:
446 case ISD::FDIV:
447 assert(!HonorSignDependentRoundingFPMath());
Scott Michelfdc40a02009-02-17 22:15:04 +0000448
Bill Wendlingd34470c2009-01-30 23:10:18 +0000449 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y)
Duncan Sands25cf2272008-11-24 14:53:14 +0000450 if (isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Bill Wendling35247c32009-01-30 00:45:56 +0000451 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000452 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000453 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000454 Op.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000455
Bill Wendlingd34470c2009-01-30 23:10:18 +0000456 // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y))
Bill Wendling35247c32009-01-30 00:45:56 +0000457 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Chris Lattner29446522007-05-14 22:04:50 +0000458 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000459 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000460 LegalOperations, Depth+1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000461
Chris Lattner29446522007-05-14 22:04:50 +0000462 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000463 case ISD::FSIN:
Bill Wendling35247c32009-01-30 00:45:56 +0000464 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000465 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000466 LegalOperations, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000467 case ISD::FP_ROUND:
Bill Wendling35247c32009-01-30 00:45:56 +0000468 return DAG.getNode(ISD::FP_ROUND, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000469 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000470 LegalOperations, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000471 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000472 }
473}
Chris Lattner24664722006-03-01 04:53:38 +0000474
475
Nate Begeman4ebd8052005-09-01 23:24:04 +0000476// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
477// that selects between the values 1 and 0, making it equivalent to a setcc.
Scott Michelfdc40a02009-02-17 22:15:04 +0000478// Also, set the incoming LHS, RHS, and CC references to the appropriate
Nate Begeman646d7e22005-09-02 21:18:40 +0000479// nodes based on the type of node we are checking. This simplifies life a
480// bit for the callers.
Dan Gohman475871a2008-07-27 21:46:04 +0000481static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
482 SDValue &CC) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000483 if (N.getOpcode() == ISD::SETCC) {
484 LHS = N.getOperand(0);
485 RHS = N.getOperand(1);
486 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000487 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000488 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000489 if (N.getOpcode() == ISD::SELECT_CC &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000490 N.getOperand(2).getOpcode() == ISD::Constant &&
491 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000492 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000493 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
494 LHS = N.getOperand(0);
495 RHS = N.getOperand(1);
496 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000497 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000498 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000499 return false;
500}
501
Nate Begeman99801192005-09-07 23:25:52 +0000502// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
503// one use. If this is true, it allows the users to invert the operation for
504// free when it is profitable to do so.
Dan Gohman475871a2008-07-27 21:46:04 +0000505static bool isOneUseSetCC(SDValue N) {
506 SDValue N0, N1, N2;
Gabor Greifba36cb52008-08-28 21:40:38 +0000507 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000508 return true;
509 return false;
510}
511
Bill Wendling35247c32009-01-30 00:45:56 +0000512SDValue DAGCombiner::ReassociateOps(unsigned Opc, DebugLoc DL,
513 SDValue N0, SDValue N1) {
Owen Andersone50ed302009-08-10 22:56:29 +0000514 EVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000515 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
516 if (isa<ConstantSDNode>(N1)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000517 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000518 SDValue OpNode =
519 DAG.FoldConstantArithmetic(Opc, VT,
520 cast<ConstantSDNode>(N0.getOperand(1)),
521 cast<ConstantSDNode>(N1));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000522 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000523 } else if (N0.hasOneUse()) {
Bill Wendling35247c32009-01-30 00:45:56 +0000524 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
525 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
526 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000527 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000528 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000529 }
530 }
Bill Wendling35247c32009-01-30 00:45:56 +0000531
Nate Begemancd4d58c2006-02-03 06:46:56 +0000532 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
533 if (isa<ConstantSDNode>(N0)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000534 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000535 SDValue OpNode =
536 DAG.FoldConstantArithmetic(Opc, VT,
537 cast<ConstantSDNode>(N1.getOperand(1)),
538 cast<ConstantSDNode>(N0));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000539 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000540 } else if (N1.hasOneUse()) {
Bill Wendling35247c32009-01-30 00:45:56 +0000541 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
Bill Wendlingd69c3142009-01-30 02:23:43 +0000542 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000543 N1.getOperand(0), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000544 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000545 return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000546 }
547 }
Bill Wendling35247c32009-01-30 00:45:56 +0000548
Dan Gohman475871a2008-07-27 21:46:04 +0000549 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000550}
551
Dan Gohman475871a2008-07-27 21:46:04 +0000552SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
553 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000554 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
555 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +0000556 DEBUG(dbgs() << "\nReplacing.1 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000557 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000558 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000559 To[0].getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000560 dbgs() << " and " << NumTo-1 << " other values\n";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000561 for (unsigned i = 0, e = NumTo; i != e; ++i)
Jakob Stoklund Olesen9f0d4e62009-12-03 05:15:35 +0000562 assert((!To[i].getNode() ||
563 N->getValueType(i) == To[i].getValueType()) &&
Dan Gohman764fd0c2009-01-21 15:17:51 +0000564 "Cannot combine value to value of different type!"));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000565 WorkListRemover DeadNodes(*this);
566 DAG.ReplaceAllUsesWith(N, To, &DeadNodes);
Scott Michelfdc40a02009-02-17 22:15:04 +0000567
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000568 if (AddTo) {
569 // Push the new nodes and any users onto the worklist
570 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattnerd1980a52009-03-12 06:52:53 +0000571 if (To[i].getNode()) {
572 AddToWorkList(To[i].getNode());
573 AddUsersToWorkList(To[i].getNode());
574 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000575 }
576 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000577
Dan Gohmandbe664a2009-01-19 21:44:21 +0000578 // Finally, if the node is now dead, remove it from the graph. The node
579 // may not be dead if the replacement process recursively simplified to
580 // something else needing this node.
581 if (N->use_empty()) {
582 // Nodes can be reintroduced into the worklist. Make sure we do not
583 // process a node that has been replaced.
584 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000585
Dan Gohmandbe664a2009-01-19 21:44:21 +0000586 // Finally, since the node is now dead, remove it from the graph.
587 DAG.DeleteNode(N);
588 }
Dan Gohman475871a2008-07-27 21:46:04 +0000589 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000590}
591
Evan Chenge5b51ac2010-04-17 06:13:15 +0000592void DAGCombiner::
593CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000594 // Replace all uses. If any nodes become isomorphic to other nodes and
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000595 // are deleted, make sure to remove them from our worklist.
596 WorkListRemover DeadNodes(*this);
597 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000598
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000599 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000600 AddToWorkList(TLO.New.getNode());
601 AddUsersToWorkList(TLO.New.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000602
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000603 // Finally, if the node is now dead, remove it from the graph. The node
604 // may not be dead if the replacement process recursively simplified to
605 // something else needing this node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000606 if (TLO.Old.getNode()->use_empty()) {
607 removeFromWorkList(TLO.Old.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000608
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000609 // If the operands of this node are only used by the node, they will now
610 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greifba36cb52008-08-28 21:40:38 +0000611 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
612 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
613 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000614
Gabor Greifba36cb52008-08-28 21:40:38 +0000615 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000616 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000617}
618
619/// SimplifyDemandedBits - Check the specified integer node value to see if
620/// it can be simplified or if things it uses can be simplified by bit
621/// propagation. If so, return true.
622bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000623 TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000624 APInt KnownZero, KnownOne;
625 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
626 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000627
Dan Gohmane5af2d32009-01-29 01:59:02 +0000628 // Revisit the node.
629 AddToWorkList(Op.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000630
Dan Gohmane5af2d32009-01-29 01:59:02 +0000631 // Replace the old value with the new one.
632 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +0000633 DEBUG(dbgs() << "\nReplacing.2 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000634 TLO.Old.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000635 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000636 TLO.New.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000637 dbgs() << '\n');
Scott Michelfdc40a02009-02-17 22:15:04 +0000638
Dan Gohmane5af2d32009-01-29 01:59:02 +0000639 CommitTargetLoweringOpt(TLO);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000640 return true;
641}
642
Evan Cheng95c57ea2010-04-24 04:43:44 +0000643void DAGCombiner::ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad) {
644 DebugLoc dl = Load->getDebugLoc();
645 EVT VT = Load->getValueType(0);
646 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, VT, SDValue(ExtLoad, 0));
Evan Cheng4c26e932010-04-19 19:29:22 +0000647
Evan Cheng95c57ea2010-04-24 04:43:44 +0000648 DEBUG(dbgs() << "\nReplacing.9 ";
649 Load->dump(&DAG);
650 dbgs() << "\nWith: ";
651 Trunc.getNode()->dump(&DAG);
652 dbgs() << '\n');
653 WorkListRemover DeadNodes(*this);
654 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 0), Trunc, &DeadNodes);
655 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), SDValue(ExtLoad, 1),
656 &DeadNodes);
657 removeFromWorkList(Load);
658 DAG.DeleteNode(Load);
659}
660
661SDValue DAGCombiner::PromoteOperand(SDValue Op, EVT PVT, bool &Replace) {
662 Replace = false;
Evan Cheng4c26e932010-04-19 19:29:22 +0000663 DebugLoc dl = Op.getDebugLoc();
Evan Chenge5b51ac2010-04-17 06:13:15 +0000664 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
Evan Cheng4c26e932010-04-19 19:29:22 +0000665 ISD::LoadExtType ExtType =
666 ISD::isNON_EXTLoad(LD) ? ISD::EXTLOAD : LD->getExtensionType();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000667 Replace = true;
Evan Cheng4c26e932010-04-19 19:29:22 +0000668 return DAG.getExtLoad(ExtType, dl, PVT,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000669 LD->getChain(), LD->getBasePtr(),
670 LD->getSrcValue(), LD->getSrcValueOffset(),
671 LD->getMemoryVT(), LD->isVolatile(),
672 LD->isNonTemporal(), LD->getAlignment());
673 }
674
Evan Cheng4c26e932010-04-19 19:29:22 +0000675 unsigned Opc = Op.getOpcode();
Evan Chengcaf77402010-04-23 19:10:30 +0000676 switch (Opc) {
677 default: break;
678 case ISD::AssertSext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000679 return DAG.getNode(ISD::AssertSext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000680 SExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000681 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000682 case ISD::AssertZext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000683 return DAG.getNode(ISD::AssertZext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000684 ZExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000685 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000686 case ISD::Constant: {
687 unsigned ExtOpc =
Evan Cheng4c26e932010-04-19 19:29:22 +0000688 Op.getValueType().isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Evan Chengcaf77402010-04-23 19:10:30 +0000689 return DAG.getNode(ExtOpc, dl, PVT, Op);
690 }
691 }
692
693 if (!TLI.isOperationLegal(ISD::ANY_EXTEND, PVT))
Evan Chenge5b51ac2010-04-17 06:13:15 +0000694 return SDValue();
Evan Chengcaf77402010-04-23 19:10:30 +0000695 return DAG.getNode(ISD::ANY_EXTEND, dl, PVT, Op);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000696}
697
Evan Cheng95c57ea2010-04-24 04:43:44 +0000698SDValue DAGCombiner::SExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000699 if (!TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, PVT))
700 return SDValue();
701 EVT OldVT = Op.getValueType();
702 DebugLoc dl = Op.getDebugLoc();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000703 bool Replace = false;
704 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
705 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000706 return SDValue();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000707
708 if (Replace)
709 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
710 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NewOp.getValueType(), NewOp,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000711 DAG.getValueType(OldVT));
712}
713
Evan Cheng95c57ea2010-04-24 04:43:44 +0000714SDValue DAGCombiner::ZExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000715 EVT OldVT = Op.getValueType();
716 DebugLoc dl = Op.getDebugLoc();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000717 bool Replace = false;
718 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
719 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000720 return SDValue();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000721
722 if (Replace)
723 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
724 return DAG.getZeroExtendInReg(NewOp, dl, OldVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000725}
726
Evan Cheng64b7bf72010-04-16 06:14:10 +0000727/// PromoteIntBinOp - Promote the specified integer binary operation if the
728/// target indicates it is beneficial. e.g. On x86, it's usually better to
729/// promote i16 operations to i32 since i16 instructions are longer.
730SDValue DAGCombiner::PromoteIntBinOp(SDValue Op) {
731 if (!LegalOperations)
732 return SDValue();
733
734 EVT VT = Op.getValueType();
735 if (VT.isVector() || !VT.isInteger())
736 return SDValue();
737
Evan Chenge5b51ac2010-04-17 06:13:15 +0000738 // If operation type is 'undesirable', e.g. i16 on x86, consider
739 // promoting it.
740 unsigned Opc = Op.getOpcode();
741 if (TLI.isTypeDesirableForOp(Opc, VT))
742 return SDValue();
743
Evan Cheng64b7bf72010-04-16 06:14:10 +0000744 EVT PVT = VT;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000745 // Consult target whether it is a good idea to promote this operation and
746 // what's the right type to promote it to.
747 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
Evan Cheng64b7bf72010-04-16 06:14:10 +0000748 assert(PVT != VT && "Don't know what type to promote to!");
749
Evan Cheng95c57ea2010-04-24 04:43:44 +0000750 bool Replace0 = false;
751 SDValue N0 = Op.getOperand(0);
752 SDValue NN0 = PromoteOperand(N0, PVT, Replace0);
753 if (NN0.getNode() == 0)
Evan Cheng07c4e102010-04-22 20:19:46 +0000754 return SDValue();
755
Evan Cheng95c57ea2010-04-24 04:43:44 +0000756 bool Replace1 = false;
757 SDValue N1 = Op.getOperand(1);
758 SDValue NN1 = PromoteOperand(N1, PVT, Replace1);
759 if (NN1.getNode() == 0)
Evan Cheng07c4e102010-04-22 20:19:46 +0000760 return SDValue();
761
Evan Cheng95c57ea2010-04-24 04:43:44 +0000762 AddToWorkList(NN0.getNode());
763 AddToWorkList(NN1.getNode());
764
765 if (Replace0)
766 ReplaceLoadWithPromotedLoad(N0.getNode(), NN0.getNode());
767 if (Replace1)
768 ReplaceLoadWithPromotedLoad(N1.getNode(), NN1.getNode());
Evan Cheng07c4e102010-04-22 20:19:46 +0000769
770 DebugLoc dl = Op.getDebugLoc();
771 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000772 DAG.getNode(Opc, dl, PVT, NN0, NN1));
Evan Cheng07c4e102010-04-22 20:19:46 +0000773 }
774 return SDValue();
775}
776
777/// PromoteIntShiftOp - Promote the specified integer shift operation if the
778/// target indicates it is beneficial. e.g. On x86, it's usually better to
779/// promote i16 operations to i32 since i16 instructions are longer.
780SDValue DAGCombiner::PromoteIntShiftOp(SDValue Op) {
781 if (!LegalOperations)
782 return SDValue();
783
784 EVT VT = Op.getValueType();
785 if (VT.isVector() || !VT.isInteger())
786 return SDValue();
787
788 // If operation type is 'undesirable', e.g. i16 on x86, consider
789 // promoting it.
790 unsigned Opc = Op.getOpcode();
791 if (TLI.isTypeDesirableForOp(Opc, VT))
792 return SDValue();
793
794 EVT PVT = VT;
795 // Consult target whether it is a good idea to promote this operation and
796 // what's the right type to promote it to.
797 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
798 assert(PVT != VT && "Don't know what type to promote to!");
799
Evan Cheng95c57ea2010-04-24 04:43:44 +0000800 bool Replace = false;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000801 SDValue N0 = Op.getOperand(0);
802 if (Opc == ISD::SRA)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000803 N0 = SExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000804 else if (Opc == ISD::SRL)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000805 N0 = ZExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000806 else
Evan Cheng95c57ea2010-04-24 04:43:44 +0000807 N0 = PromoteOperand(N0, PVT, Replace);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000808 if (N0.getNode() == 0)
809 return SDValue();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000810
Evan Chenge5b51ac2010-04-17 06:13:15 +0000811 AddToWorkList(N0.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000812 if (Replace)
813 ReplaceLoadWithPromotedLoad(Op.getOperand(0).getNode(), N0.getNode());
Evan Cheng64b7bf72010-04-16 06:14:10 +0000814
815 DebugLoc dl = Op.getDebugLoc();
816 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng07c4e102010-04-22 20:19:46 +0000817 DAG.getNode(Opc, dl, PVT, N0, Op.getOperand(1)));
Evan Cheng64b7bf72010-04-16 06:14:10 +0000818 }
819 return SDValue();
820}
821
Evan Cheng4c26e932010-04-19 19:29:22 +0000822SDValue DAGCombiner::PromoteExtend(SDValue Op) {
823 if (!LegalOperations)
824 return SDValue();
825
826 EVT VT = Op.getValueType();
827 if (VT.isVector() || !VT.isInteger())
828 return SDValue();
829
830 // If operation type is 'undesirable', e.g. i16 on x86, consider
831 // promoting it.
832 unsigned Opc = Op.getOpcode();
833 if (TLI.isTypeDesirableForOp(Opc, VT))
834 return SDValue();
835
836 EVT PVT = VT;
837 // Consult target whether it is a good idea to promote this operation and
838 // what's the right type to promote it to.
839 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
840 assert(PVT != VT && "Don't know what type to promote to!");
841 // fold (aext (aext x)) -> (aext x)
842 // fold (aext (zext x)) -> (zext x)
843 // fold (aext (sext x)) -> (sext x)
844 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), VT, Op.getOperand(0));
845 }
846 return SDValue();
847}
848
849bool DAGCombiner::PromoteLoad(SDValue Op) {
850 if (!LegalOperations)
851 return false;
852
853 EVT VT = Op.getValueType();
854 if (VT.isVector() || !VT.isInteger())
855 return false;
856
857 // If operation type is 'undesirable', e.g. i16 on x86, consider
858 // promoting it.
859 unsigned Opc = Op.getOpcode();
860 if (TLI.isTypeDesirableForOp(Opc, VT))
861 return false;
862
863 EVT PVT = VT;
864 // Consult target whether it is a good idea to promote this operation and
865 // what's the right type to promote it to.
866 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
867 assert(PVT != VT && "Don't know what type to promote to!");
868
869 DebugLoc dl = Op.getDebugLoc();
870 SDNode *N = Op.getNode();
871 LoadSDNode *LD = cast<LoadSDNode>(N);
872 ISD::LoadExtType ExtType =
873 ISD::isNON_EXTLoad(LD) ? ISD::EXTLOAD : LD->getExtensionType();
874 SDValue NewLD = DAG.getExtLoad(ExtType, dl, PVT,
875 LD->getChain(), LD->getBasePtr(),
876 LD->getSrcValue(), LD->getSrcValueOffset(),
877 LD->getMemoryVT(), LD->isVolatile(),
878 LD->isNonTemporal(), LD->getAlignment());
879 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, VT, NewLD);
880
Evan Cheng95c57ea2010-04-24 04:43:44 +0000881 DEBUG(dbgs() << "\nPromoting ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000882 N->dump(&DAG);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000883 dbgs() << "\nTo: ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000884 Result.getNode()->dump(&DAG);
885 dbgs() << '\n');
886 WorkListRemover DeadNodes(*this);
887 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result, &DeadNodes);
888 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), NewLD.getValue(1), &DeadNodes);
889 removeFromWorkList(N);
890 DAG.DeleteNode(N);
891 return true;
892 }
893 return false;
894}
895
Evan Chenge5b51ac2010-04-17 06:13:15 +0000896
Chris Lattner29446522007-05-14 22:04:50 +0000897//===----------------------------------------------------------------------===//
898// Main DAG Combiner implementation
899//===----------------------------------------------------------------------===//
900
Duncan Sands25cf2272008-11-24 14:53:14 +0000901void DAGCombiner::Run(CombineLevel AtLevel) {
902 // set the instance variables, so that the various visit routines may use it.
903 Level = AtLevel;
904 LegalOperations = Level >= NoIllegalOperations;
905 LegalTypes = Level >= NoIllegalTypes;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000906
Evan Cheng17a568b2008-08-29 22:21:44 +0000907 // Add all the dag nodes to the worklist.
908 WorkList.reserve(DAG.allnodes_size());
909 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
910 E = DAG.allnodes_end(); I != E; ++I)
911 WorkList.push_back(I);
Duncan Sands25cf2272008-11-24 14:53:14 +0000912
Evan Cheng17a568b2008-08-29 22:21:44 +0000913 // Create a dummy node (which is not added to allnodes), that adds a reference
914 // to the root node, preventing it from being deleted, and tracking any
915 // changes of the root.
916 HandleSDNode Dummy(DAG.getRoot());
Scott Michelfdc40a02009-02-17 22:15:04 +0000917
Jim Laskey26f7fa72006-10-17 19:33:52 +0000918 // The root of the dag may dangle to deleted nodes until the dag combiner is
919 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +0000920 DAG.setRoot(SDValue());
Scott Michelfdc40a02009-02-17 22:15:04 +0000921
Evan Cheng17a568b2008-08-29 22:21:44 +0000922 // while the worklist isn't empty, inspect the node on the end of it and
923 // try and combine it.
924 while (!WorkList.empty()) {
925 SDNode *N = WorkList.back();
926 WorkList.pop_back();
Scott Michelfdc40a02009-02-17 22:15:04 +0000927
Evan Cheng17a568b2008-08-29 22:21:44 +0000928 // If N has no uses, it is dead. Make sure to revisit all N's operands once
929 // N is deleted from the DAG, since they too may now be dead or may have a
930 // reduced number of uses, allowing other xforms.
931 if (N->use_empty() && N != &Dummy) {
932 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
933 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000934
Evan Cheng17a568b2008-08-29 22:21:44 +0000935 DAG.DeleteNode(N);
936 continue;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000937 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000938
Evan Cheng17a568b2008-08-29 22:21:44 +0000939 SDValue RV = combine(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000940
Evan Cheng17a568b2008-08-29 22:21:44 +0000941 if (RV.getNode() == 0)
942 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +0000943
Evan Cheng17a568b2008-08-29 22:21:44 +0000944 ++NodesCombined;
Scott Michelfdc40a02009-02-17 22:15:04 +0000945
Evan Cheng17a568b2008-08-29 22:21:44 +0000946 // If we get back the same node we passed in, rather than a new node or
947 // zero, we know that the node must have defined multiple values and
Scott Michelfdc40a02009-02-17 22:15:04 +0000948 // CombineTo was used. Since CombineTo takes care of the worklist
Evan Cheng17a568b2008-08-29 22:21:44 +0000949 // mechanics for us, we have no work to do in this case.
950 if (RV.getNode() == N)
951 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +0000952
Evan Cheng17a568b2008-08-29 22:21:44 +0000953 assert(N->getOpcode() != ISD::DELETED_NODE &&
954 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
955 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +0000956
David Greenef1090292010-01-05 01:25:00 +0000957 DEBUG(dbgs() << "\nReplacing.3 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000958 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000959 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000960 RV.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000961 dbgs() << '\n');
Evan Cheng17a568b2008-08-29 22:21:44 +0000962 WorkListRemover DeadNodes(*this);
963 if (N->getNumValues() == RV.getNode()->getNumValues())
964 DAG.ReplaceAllUsesWith(N, RV.getNode(), &DeadNodes);
965 else {
966 assert(N->getValueType(0) == RV.getValueType() &&
967 N->getNumValues() == 1 && "Type mismatch");
968 SDValue OpV = RV;
969 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
970 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000971
Evan Cheng17a568b2008-08-29 22:21:44 +0000972 // Push the new node and any users onto the worklist
973 AddToWorkList(RV.getNode());
974 AddUsersToWorkList(RV.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000975
Evan Cheng17a568b2008-08-29 22:21:44 +0000976 // Add any uses of the old node to the worklist in case this node is the
977 // last one that uses them. They may become dead after this node is
978 // deleted.
979 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
980 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000981
Dan Gohmandbe664a2009-01-19 21:44:21 +0000982 // Finally, if the node is now dead, remove it from the graph. The node
983 // may not be dead if the replacement process recursively simplified to
984 // something else needing this node.
985 if (N->use_empty()) {
986 // Nodes can be reintroduced into the worklist. Make sure we do not
987 // process a node that has been replaced.
988 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000989
Dan Gohmandbe664a2009-01-19 21:44:21 +0000990 // Finally, since the node is now dead, remove it from the graph.
991 DAG.DeleteNode(N);
992 }
Evan Cheng17a568b2008-08-29 22:21:44 +0000993 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000994
Chris Lattner95038592005-10-05 06:35:28 +0000995 // If the root changed (e.g. it was a dead load, update the root).
996 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000997}
998
Dan Gohman475871a2008-07-27 21:46:04 +0000999SDValue DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001000 switch(N->getOpcode()) {
1001 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +00001002 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001003 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001004 case ISD::ADD: return visitADD(N);
1005 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +00001006 case ISD::ADDC: return visitADDC(N);
1007 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001008 case ISD::MUL: return visitMUL(N);
1009 case ISD::SDIV: return visitSDIV(N);
1010 case ISD::UDIV: return visitUDIV(N);
1011 case ISD::SREM: return visitSREM(N);
1012 case ISD::UREM: return visitUREM(N);
1013 case ISD::MULHU: return visitMULHU(N);
1014 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001015 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
1016 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
1017 case ISD::SDIVREM: return visitSDIVREM(N);
1018 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001019 case ISD::AND: return visitAND(N);
1020 case ISD::OR: return visitOR(N);
1021 case ISD::XOR: return visitXOR(N);
1022 case ISD::SHL: return visitSHL(N);
1023 case ISD::SRA: return visitSRA(N);
1024 case ISD::SRL: return visitSRL(N);
1025 case ISD::CTLZ: return visitCTLZ(N);
1026 case ISD::CTTZ: return visitCTTZ(N);
1027 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +00001028 case ISD::SELECT: return visitSELECT(N);
1029 case ISD::SELECT_CC: return visitSELECT_CC(N);
1030 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001031 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
1032 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001033 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001034 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
1035 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +00001036 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00001037 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001038 case ISD::FADD: return visitFADD(N);
1039 case ISD::FSUB: return visitFSUB(N);
1040 case ISD::FMUL: return visitFMUL(N);
1041 case ISD::FDIV: return visitFDIV(N);
1042 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +00001043 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001044 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
1045 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
1046 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
1047 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
1048 case ISD::FP_ROUND: return visitFP_ROUND(N);
1049 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
1050 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
1051 case ISD::FNEG: return visitFNEG(N);
1052 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001053 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001054 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +00001055 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00001056 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +00001057 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +00001058 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +00001059 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
1060 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +00001061 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001062 }
Dan Gohman475871a2008-07-27 21:46:04 +00001063 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001064}
1065
Dan Gohman475871a2008-07-27 21:46:04 +00001066SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00001067 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001068
1069 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +00001070 if (RV.getNode() == 0) {
Dan Gohman389079b2007-10-08 17:57:15 +00001071 assert(N->getOpcode() != ISD::DELETED_NODE &&
1072 "Node was deleted but visit returned NULL!");
1073
1074 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
1075 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
1076
1077 // Expose the DAG combiner to the target combiner impls.
Scott Michelfdc40a02009-02-17 22:15:04 +00001078 TargetLowering::DAGCombinerInfo
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00001079 DagCombineInfo(DAG, !LegalTypes, !LegalOperations, false, this);
Dan Gohman389079b2007-10-08 17:57:15 +00001080
1081 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
1082 }
1083 }
1084
Scott Michelfdc40a02009-02-17 22:15:04 +00001085 // If N is a commutative binary node, try commuting it to enable more
Evan Cheng08b11732008-03-22 01:55:50 +00001086 // sdisel CSE.
Scott Michelfdc40a02009-02-17 22:15:04 +00001087 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +00001088 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
1089 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00001090 SDValue N0 = N->getOperand(0);
1091 SDValue N1 = N->getOperand(1);
Bill Wendling5c71acf2009-01-30 01:13:16 +00001092
Evan Cheng08b11732008-03-22 01:55:50 +00001093 // Constant operands are canonicalized to RHS.
1094 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001095 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +00001096 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
1097 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +00001098 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +00001099 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +00001100 }
1101 }
1102
Dan Gohman389079b2007-10-08 17:57:15 +00001103 return RV;
Scott Michelfdc40a02009-02-17 22:15:04 +00001104}
Dan Gohman389079b2007-10-08 17:57:15 +00001105
Chris Lattner6270f682006-10-08 22:57:01 +00001106/// getInputChainForNode - Given a node, return its input chain if it has one,
1107/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +00001108static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001109 if (unsigned NumOps = N->getNumOperands()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001110 if (N->getOperand(0).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001111 return N->getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001112 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001113 return N->getOperand(NumOps-1);
1114 for (unsigned i = 1; i < NumOps-1; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +00001115 if (N->getOperand(i).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001116 return N->getOperand(i);
1117 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001118 return SDValue();
Chris Lattner6270f682006-10-08 22:57:01 +00001119}
1120
Dan Gohman475871a2008-07-27 21:46:04 +00001121SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001122 // If N has two operands, where one has an input chain equal to the other,
1123 // the 'other' chain is redundant.
1124 if (N->getNumOperands() == 2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001125 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +00001126 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001127 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +00001128 return N->getOperand(1);
1129 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001130
Chris Lattnerc76d4412007-05-16 06:37:59 +00001131 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +00001132 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001133 SmallPtrSet<SDNode*, 16> SeenOps;
Chris Lattnerc76d4412007-05-16 06:37:59 +00001134 bool Changed = false; // If we should replace this token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001135
Jim Laskey6ff23e52006-10-04 16:53:27 +00001136 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00001137 TFs.push_back(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001138
Jim Laskey71382342006-10-07 23:37:56 +00001139 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +00001140 // encountered.
1141 for (unsigned i = 0; i < TFs.size(); ++i) {
1142 SDNode *TF = TFs[i];
Scott Michelfdc40a02009-02-17 22:15:04 +00001143
Jim Laskey6ff23e52006-10-04 16:53:27 +00001144 // Check each of the operands.
1145 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001146 SDValue Op = TF->getOperand(i);
Scott Michelfdc40a02009-02-17 22:15:04 +00001147
Jim Laskey6ff23e52006-10-04 16:53:27 +00001148 switch (Op.getOpcode()) {
1149 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +00001150 // Entry tokens don't need to be added to the list. They are
1151 // rededundant.
1152 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001153 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001154
Jim Laskey6ff23e52006-10-04 16:53:27 +00001155 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +00001156 if (Op.hasOneUse() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001157 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001158 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +00001159 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001160 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +00001161 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001162 Changed = true;
1163 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001164 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001165 // Fall thru
Scott Michelfdc40a02009-02-17 22:15:04 +00001166
Jim Laskey6ff23e52006-10-04 16:53:27 +00001167 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +00001168 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +00001169 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +00001170 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +00001171 else
1172 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001173 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001174 }
1175 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001176 }
Nate Begemanb6aef5c2009-09-15 00:18:30 +00001177
Dan Gohman475871a2008-07-27 21:46:04 +00001178 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001179
1180 // If we've change things around then replace token factor.
1181 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +00001182 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001183 // The entry token is the only possible outcome.
1184 Result = DAG.getEntryNode();
1185 } else {
1186 // New and improved token factor.
Bill Wendling5c71acf2009-01-30 01:13:16 +00001187 Result = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001188 MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +00001189 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001190
Jim Laskey274062c2006-10-13 23:32:28 +00001191 // Don't add users to work list.
1192 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +00001193 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001194
Jim Laskey6ff23e52006-10-04 16:53:27 +00001195 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001196}
1197
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001198/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +00001199SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001200 WorkListRemover DeadNodes(*this);
Dan Gohman00edf392009-08-10 23:43:19 +00001201 // Replacing results may cause a different MERGE_VALUES to suddenly
1202 // be CSE'd with N, and carry its uses with it. Iterate until no
1203 // uses remain, to ensure that the node can be safely deleted.
1204 do {
1205 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1206 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i),
1207 &DeadNodes);
1208 } while (!N->use_empty());
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001209 removeFromWorkList(N);
1210 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001211 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001212}
1213
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001214static
Bill Wendlingd69c3142009-01-30 02:23:43 +00001215SDValue combineShlAddConstant(DebugLoc DL, SDValue N0, SDValue N1,
1216 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001217 EVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001218 SDValue N00 = N0.getOperand(0);
1219 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001220 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Bill Wendlingd69c3142009-01-30 02:23:43 +00001221
Gabor Greifba36cb52008-08-28 21:40:38 +00001222 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001223 isa<ConstantSDNode>(N00.getOperand(1))) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001224 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
1225 N0 = DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT,
1226 DAG.getNode(ISD::SHL, N00.getDebugLoc(), VT,
1227 N00.getOperand(0), N01),
1228 DAG.getNode(ISD::SHL, N01.getDebugLoc(), VT,
1229 N00.getOperand(1), N01));
1230 return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001231 }
Bill Wendlingd69c3142009-01-30 02:23:43 +00001232
Dan Gohman475871a2008-07-27 21:46:04 +00001233 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001234}
1235
Dan Gohman475871a2008-07-27 21:46:04 +00001236SDValue DAGCombiner::visitADD(SDNode *N) {
1237 SDValue N0 = N->getOperand(0);
1238 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001239 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1240 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001241 EVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00001242
1243 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001244 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001245 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001246 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001247 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001248
Dan Gohman613e0d82007-07-03 14:03:57 +00001249 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001250 if (N0.getOpcode() == ISD::UNDEF)
1251 return N0;
1252 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001253 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001254 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001255 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001256 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001257 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001258 if (N0C && !N1C)
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001259 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001260 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001261 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001262 return N0;
Dan Gohman6520e202008-10-18 02:06:02 +00001263 // fold (add Sym, c) -> Sym+c
1264 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001265 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman6520e202008-10-18 02:06:02 +00001266 GA->getOpcode() == ISD::GlobalAddress)
1267 return DAG.getGlobalAddress(GA->getGlobal(), VT,
1268 GA->getOffset() +
1269 (uint64_t)N1C->getSExtValue());
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001270 // fold ((c1-A)+c2) -> (c1+c2)-A
1271 if (N1C && N0.getOpcode() == ISD::SUB)
1272 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001273 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
Dan Gohman002e5d02008-03-13 22:13:53 +00001274 DAG.getConstant(N1C->getAPIntValue()+
1275 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001276 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +00001277 // reassociate add
Bill Wendling35247c32009-01-30 00:45:56 +00001278 SDValue RADD = ReassociateOps(ISD::ADD, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001279 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001280 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001281 // fold ((0-A) + B) -> B-A
1282 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1283 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001284 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001285 // fold (A + (0-B)) -> A-B
1286 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1287 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001288 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001289 // fold (A+(B-A)) -> B
1290 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001291 return N1.getOperand(0);
Dale Johannesen56eca912008-11-27 00:43:21 +00001292 // fold ((B-A)+A) -> B
1293 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1294 return N0.getOperand(0);
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001295 // fold (A+(B-(A+C))) to (B-C)
1296 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001297 N0 == N1.getOperand(1).getOperand(0))
1298 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001299 N1.getOperand(1).getOperand(1));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001300 // fold (A+(B-(C+A))) to (B-C)
1301 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001302 N0 == N1.getOperand(1).getOperand(1))
1303 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001304 N1.getOperand(1).getOperand(0));
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001305 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesen34d79852008-12-02 18:40:40 +00001306 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1307 N1.getOperand(0).getOpcode() == ISD::SUB &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001308 N0 == N1.getOperand(0).getOperand(1))
1309 return DAG.getNode(N1.getOpcode(), N->getDebugLoc(), VT,
1310 N1.getOperand(0).getOperand(0), N1.getOperand(1));
Dale Johannesen34d79852008-12-02 18:40:40 +00001311
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001312 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1313 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1314 SDValue N00 = N0.getOperand(0);
1315 SDValue N01 = N0.getOperand(1);
1316 SDValue N10 = N1.getOperand(0);
1317 SDValue N11 = N1.getOperand(1);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001318
1319 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10))
1320 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1321 DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT, N00, N10),
1322 DAG.getNode(ISD::ADD, N1.getDebugLoc(), VT, N01, N11));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001323 }
Chris Lattner947c2892006-03-13 06:51:27 +00001324
Dan Gohman475871a2008-07-27 21:46:04 +00001325 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1326 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001327
Chris Lattner947c2892006-03-13 06:51:27 +00001328 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001329 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001330 APInt LHSZero, LHSOne;
1331 APInt RHSZero, RHSOne;
Dan Gohman5b870af2010-03-02 02:14:38 +00001332 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001333 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001334
Dan Gohman948d8ea2008-02-20 16:33:30 +00001335 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001336 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001337
Chris Lattner947c2892006-03-13 06:51:27 +00001338 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1339 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1340 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1341 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001342 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1);
Chris Lattner947c2892006-03-13 06:51:27 +00001343 }
1344 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001345
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001346 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001347 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001348 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001349 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001350 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001351 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001352 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001353 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001354 }
1355
Dan Gohmancd9e1552010-01-19 23:30:49 +00001356 // fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n))
1357 if (N1.getOpcode() == ISD::SHL &&
1358 N1.getOperand(0).getOpcode() == ISD::SUB)
1359 if (ConstantSDNode *C =
1360 dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(0)))
1361 if (C->getAPIntValue() == 0)
1362 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0,
1363 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1364 N1.getOperand(0).getOperand(1),
1365 N1.getOperand(1)));
1366 if (N0.getOpcode() == ISD::SHL &&
1367 N0.getOperand(0).getOpcode() == ISD::SUB)
1368 if (ConstantSDNode *C =
1369 dyn_cast<ConstantSDNode>(N0.getOperand(0).getOperand(0)))
1370 if (C->getAPIntValue() == 0)
1371 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1,
1372 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1373 N0.getOperand(0).getOperand(1),
1374 N0.getOperand(1)));
1375
Evan Cheng64b7bf72010-04-16 06:14:10 +00001376 return PromoteIntBinOp(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001377}
1378
Dan Gohman475871a2008-07-27 21:46:04 +00001379SDValue DAGCombiner::visitADDC(SDNode *N) {
1380 SDValue N0 = N->getOperand(0);
1381 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001382 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1383 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001384 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001385
Chris Lattner91153682007-03-04 20:03:15 +00001386 // If the flag result is dead, turn this into an ADD.
1387 if (N->hasNUsesOfValue(0, 1))
Bill Wendling14036c02009-01-30 02:38:00 +00001388 return CombineTo(N, DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0),
Dale Johannesen874ae252009-06-02 03:12:52 +00001389 DAG.getNode(ISD::CARRY_FALSE,
Owen Anderson825b72b2009-08-11 20:47:22 +00001390 N->getDebugLoc(), MVT::Flag));
Scott Michelfdc40a02009-02-17 22:15:04 +00001391
Chris Lattner91153682007-03-04 20:03:15 +00001392 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001393 if (N0C && !N1C)
Bill Wendling14036c02009-01-30 02:38:00 +00001394 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001395
Chris Lattnerb6541762007-03-04 20:40:38 +00001396 // fold (addc x, 0) -> x + no carry out
1397 if (N1C && N1C->isNullValue())
Dale Johannesen874ae252009-06-02 03:12:52 +00001398 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE,
Owen Anderson825b72b2009-08-11 20:47:22 +00001399 N->getDebugLoc(), MVT::Flag));
Scott Michelfdc40a02009-02-17 22:15:04 +00001400
Dale Johannesen874ae252009-06-02 03:12:52 +00001401 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001402 APInt LHSZero, LHSOne;
1403 APInt RHSZero, RHSOne;
Dan Gohman5b870af2010-03-02 02:14:38 +00001404 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001405 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Bill Wendling14036c02009-01-30 02:38:00 +00001406
Dan Gohman948d8ea2008-02-20 16:33:30 +00001407 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001408 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001409
Chris Lattnerb6541762007-03-04 20:40:38 +00001410 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1411 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1412 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1413 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
Bill Wendling14036c02009-01-30 02:38:00 +00001414 return CombineTo(N, DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001415 DAG.getNode(ISD::CARRY_FALSE,
Owen Anderson825b72b2009-08-11 20:47:22 +00001416 N->getDebugLoc(), MVT::Flag));
Chris Lattnerb6541762007-03-04 20:40:38 +00001417 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001418
Dan Gohman475871a2008-07-27 21:46:04 +00001419 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001420}
1421
Dan Gohman475871a2008-07-27 21:46:04 +00001422SDValue DAGCombiner::visitADDE(SDNode *N) {
1423 SDValue N0 = N->getOperand(0);
1424 SDValue N1 = N->getOperand(1);
1425 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001426 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1427 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001428
Chris Lattner91153682007-03-04 20:03:15 +00001429 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001430 if (N0C && !N1C)
Bill Wendling14036c02009-01-30 02:38:00 +00001431 return DAG.getNode(ISD::ADDE, N->getDebugLoc(), N->getVTList(),
1432 N1, N0, CarryIn);
Scott Michelfdc40a02009-02-17 22:15:04 +00001433
Chris Lattnerb6541762007-03-04 20:40:38 +00001434 // fold (adde x, y, false) -> (addc x, y)
Dale Johannesen874ae252009-06-02 03:12:52 +00001435 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
1436 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001437
Dan Gohman475871a2008-07-27 21:46:04 +00001438 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001439}
1440
Dan Gohman475871a2008-07-27 21:46:04 +00001441SDValue DAGCombiner::visitSUB(SDNode *N) {
1442 SDValue N0 = N->getOperand(0);
1443 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001444 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1445 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001446 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001447
Dan Gohman7f321562007-06-25 16:23:39 +00001448 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001449 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001450 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001451 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001452 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001453
Chris Lattner854077d2005-10-17 01:07:11 +00001454 // fold (sub x, x) -> 0
Evan Chengc8e3b142008-03-12 07:02:50 +00001455 if (N0 == N1)
Chris Lattner854077d2005-10-17 01:07:11 +00001456 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001457 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001458 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001459 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattner05b57432005-10-11 06:07:15 +00001460 // fold (sub x, c) -> (add x, -c)
1461 if (N1C)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001462 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001463 DAG.getConstant(-N1C->getAPIntValue(), VT));
Evan Cheng1ad0e8b2010-01-18 21:38:44 +00001464 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1)
1465 if (N0C && N0C->isAllOnesValue())
1466 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001467 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001468 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001469 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001470 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001471 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Scott Michelfdc40a02009-02-17 22:15:04 +00001472 return N0.getOperand(0);
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001473 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001474 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001475 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1476 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001477 N0.getOperand(1).getOperand(0) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001478 return DAG.getNode(N0.getOperand(1).getOpcode(), N->getDebugLoc(), VT,
1479 N0.getOperand(0), N0.getOperand(1).getOperand(1));
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001480 // fold ((A+(C+B))-B) -> A+C
1481 if (N0.getOpcode() == ISD::ADD &&
1482 N0.getOperand(1).getOpcode() == ISD::ADD &&
1483 N0.getOperand(1).getOperand(1) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001484 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1485 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dale Johannesen58e39b02008-12-23 01:59:54 +00001486 // fold ((A-(B-C))-C) -> A-B
1487 if (N0.getOpcode() == ISD::SUB &&
1488 N0.getOperand(1).getOpcode() == ISD::SUB &&
1489 N0.getOperand(1).getOperand(1) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001490 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1491 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Bill Wendlingb0702e02009-01-30 02:42:10 +00001492
Dan Gohman613e0d82007-07-03 14:03:57 +00001493 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001494 if (N0.getOpcode() == ISD::UNDEF)
1495 return N0;
1496 if (N1.getOpcode() == ISD::UNDEF)
1497 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001498
Dan Gohman6520e202008-10-18 02:06:02 +00001499 // If the relocation model supports it, consider symbol offsets.
1500 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001501 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman6520e202008-10-18 02:06:02 +00001502 // fold (sub Sym, c) -> Sym-c
1503 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
1504 return DAG.getGlobalAddress(GA->getGlobal(), VT,
1505 GA->getOffset() -
1506 (uint64_t)N1C->getSExtValue());
1507 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1508 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1509 if (GA->getGlobal() == GB->getGlobal())
1510 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1511 VT);
1512 }
1513
Evan Cheng64b7bf72010-04-16 06:14:10 +00001514 return PromoteIntBinOp(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001515}
1516
Dan Gohman475871a2008-07-27 21:46:04 +00001517SDValue DAGCombiner::visitMUL(SDNode *N) {
1518 SDValue N0 = N->getOperand(0);
1519 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001520 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1521 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001522 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001523
Dan Gohman7f321562007-06-25 16:23:39 +00001524 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001525 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001526 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001527 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001528 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001529
Dan Gohman613e0d82007-07-03 14:03:57 +00001530 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001531 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001532 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001533 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001534 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001535 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001536 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001537 if (N0C && !N1C)
Bill Wendling9c8148a2009-01-30 02:45:56 +00001538 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001539 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001540 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001541 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001542 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001543 if (N1C && N1C->isAllOnesValue())
Bill Wendling9c8148a2009-01-30 02:45:56 +00001544 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1545 DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001546 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001547 if (N1C && N1C->getAPIntValue().isPowerOf2())
Bill Wendling9c8148a2009-01-30 02:45:56 +00001548 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001549 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Duncan Sands92abc622009-01-31 15:50:11 +00001550 getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001551 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Chris Lattner66b8bc32009-03-09 20:22:18 +00001552 if (N1C && (-N1C->getAPIntValue()).isPowerOf2()) {
1553 unsigned Log2Val = (-N1C->getAPIntValue()).logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00001554 // FIXME: If the input is something that is easily negated (e.g. a
Chris Lattner3e6099b2005-10-30 06:41:49 +00001555 // single-use add), we should put the negate there.
Bill Wendling9c8148a2009-01-30 02:45:56 +00001556 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1557 DAG.getConstant(0, VT),
Bill Wendling73e16b22009-01-30 02:49:26 +00001558 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Chris Lattner66b8bc32009-03-09 20:22:18 +00001559 DAG.getConstant(Log2Val, getShiftAmountTy())));
1560 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001561 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
Bill Wendling73e16b22009-01-30 02:49:26 +00001562 if (N1C && N0.getOpcode() == ISD::SHL &&
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001563 isa<ConstantSDNode>(N0.getOperand(1))) {
Bill Wendling9c8148a2009-01-30 02:45:56 +00001564 SDValue C3 = DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1565 N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001566 AddToWorkList(C3.getNode());
Bill Wendling9c8148a2009-01-30 02:45:56 +00001567 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1568 N0.getOperand(0), C3);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001569 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001570
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001571 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1572 // use.
1573 {
Dan Gohman475871a2008-07-27 21:46:04 +00001574 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001575 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1576 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001577 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001578 Sh = N0; Y = N1;
Scott Michelfdc40a02009-02-17 22:15:04 +00001579 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001580 isa<ConstantSDNode>(N1.getOperand(1)) &&
1581 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001582 Sh = N1; Y = N0;
1583 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001584
Gabor Greifba36cb52008-08-28 21:40:38 +00001585 if (Sh.getNode()) {
Bill Wendling9c8148a2009-01-30 02:45:56 +00001586 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1587 Sh.getOperand(0), Y);
1588 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1589 Mul, Sh.getOperand(1));
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001590 }
1591 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001592
Chris Lattnera1deca32006-03-04 23:33:26 +00001593 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Scott Michelfdc40a02009-02-17 22:15:04 +00001594 if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
Bill Wendling9c8148a2009-01-30 02:45:56 +00001595 isa<ConstantSDNode>(N0.getOperand(1)))
1596 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1597 DAG.getNode(ISD::MUL, N0.getDebugLoc(), VT,
1598 N0.getOperand(0), N1),
1599 DAG.getNode(ISD::MUL, N1.getDebugLoc(), VT,
1600 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001601
Nate Begemancd4d58c2006-02-03 06:46:56 +00001602 // reassociate mul
Bill Wendling35247c32009-01-30 00:45:56 +00001603 SDValue RMUL = ReassociateOps(ISD::MUL, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001604 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001605 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001606
Evan Cheng64b7bf72010-04-16 06:14:10 +00001607 return PromoteIntBinOp(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001608}
1609
Dan Gohman475871a2008-07-27 21:46:04 +00001610SDValue DAGCombiner::visitSDIV(SDNode *N) {
1611 SDValue N0 = N->getOperand(0);
1612 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001613 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1614 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001615 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001616
Dan Gohman7f321562007-06-25 16:23:39 +00001617 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001618 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001619 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001620 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001621 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001622
Nate Begeman1d4d4142005-09-01 00:19:25 +00001623 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001624 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001625 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001626 // fold (sdiv X, 1) -> X
Dan Gohman7810bfe2008-09-26 21:54:37 +00001627 if (N1C && N1C->getSExtValue() == 1LL)
Nate Begeman405e3ec2005-10-21 00:02:42 +00001628 return N0;
1629 // fold (sdiv X, -1) -> 0-X
1630 if (N1C && N1C->isAllOnesValue())
Bill Wendling944d34b2009-01-30 02:52:17 +00001631 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1632 DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001633 // If we know the sign bits of both operands are zero, strength reduce to a
1634 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001635 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001636 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendling944d34b2009-01-30 02:52:17 +00001637 return DAG.getNode(ISD::UDIV, N->getDebugLoc(), N1.getValueType(),
1638 N0, N1);
Chris Lattnerf32aac32008-01-27 23:32:17 +00001639 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001640 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman002e5d02008-03-13 22:13:53 +00001641 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Scott Michelfdc40a02009-02-17 22:15:04 +00001642 (isPowerOf2_64(N1C->getSExtValue()) ||
Dan Gohman7810bfe2008-09-26 21:54:37 +00001643 isPowerOf2_64(-N1C->getSExtValue()))) {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001644 // If dividing by powers of two is cheap, then don't perform the following
1645 // fold.
1646 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001647 return SDValue();
Bill Wendling944d34b2009-01-30 02:52:17 +00001648
Dan Gohman7810bfe2008-09-26 21:54:37 +00001649 int64_t pow2 = N1C->getSExtValue();
Nate Begeman405e3ec2005-10-21 00:02:42 +00001650 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001651 unsigned lg2 = Log2_64(abs2);
Bill Wendling944d34b2009-01-30 02:52:17 +00001652
Chris Lattner8f4880b2006-02-16 08:02:36 +00001653 // Splat the sign bit into the register
Bill Wendling944d34b2009-01-30 02:52:17 +00001654 SDValue SGN = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
1655 DAG.getConstant(VT.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00001656 getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00001657 AddToWorkList(SGN.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001658
Chris Lattner8f4880b2006-02-16 08:02:36 +00001659 // Add (N0 < 0) ? abs2 - 1 : 0;
Bill Wendling944d34b2009-01-30 02:52:17 +00001660 SDValue SRL = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, SGN,
1661 DAG.getConstant(VT.getSizeInBits() - lg2,
Duncan Sands92abc622009-01-31 15:50:11 +00001662 getShiftAmountTy()));
Bill Wendling944d34b2009-01-30 02:52:17 +00001663 SDValue ADD = DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001664 AddToWorkList(SRL.getNode());
1665 AddToWorkList(ADD.getNode()); // Divide by pow2
Bill Wendling944d34b2009-01-30 02:52:17 +00001666 SDValue SRA = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, ADD,
Duncan Sands92abc622009-01-31 15:50:11 +00001667 DAG.getConstant(lg2, getShiftAmountTy()));
Bill Wendling944d34b2009-01-30 02:52:17 +00001668
Nate Begeman405e3ec2005-10-21 00:02:42 +00001669 // If we're dividing by a positive value, we're done. Otherwise, we must
1670 // negate the result.
1671 if (pow2 > 0)
1672 return SRA;
Bill Wendling944d34b2009-01-30 02:52:17 +00001673
Gabor Greifba36cb52008-08-28 21:40:38 +00001674 AddToWorkList(SRA.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001675 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1676 DAG.getConstant(0, VT), SRA);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001677 }
Bill Wendling944d34b2009-01-30 02:52:17 +00001678
Nate Begeman69575232005-10-20 02:15:44 +00001679 // if integer divide is expensive and we satisfy the requirements, emit an
1680 // alternate sequence.
Scott Michelfdc40a02009-02-17 22:15:04 +00001681 if (N1C && (N1C->getSExtValue() < -1 || N1C->getSExtValue() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001682 !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001683 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001684 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001685 }
Dan Gohman7f321562007-06-25 16:23:39 +00001686
Dan Gohman613e0d82007-07-03 14:03:57 +00001687 // undef / X -> 0
1688 if (N0.getOpcode() == ISD::UNDEF)
1689 return DAG.getConstant(0, VT);
1690 // X / undef -> undef
1691 if (N1.getOpcode() == ISD::UNDEF)
1692 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001693
Dan Gohman475871a2008-07-27 21:46:04 +00001694 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001695}
1696
Dan Gohman475871a2008-07-27 21:46:04 +00001697SDValue DAGCombiner::visitUDIV(SDNode *N) {
1698 SDValue N0 = N->getOperand(0);
1699 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001700 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1701 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001702 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001703
Dan Gohman7f321562007-06-25 16:23:39 +00001704 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001705 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001706 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001707 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001708 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001709
Nate Begeman1d4d4142005-09-01 00:19:25 +00001710 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001711 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001712 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001713 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001714 if (N1C && N1C->getAPIntValue().isPowerOf2())
Scott Michelfdc40a02009-02-17 22:15:04 +00001715 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001716 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Duncan Sands92abc622009-01-31 15:50:11 +00001717 getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001718 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1719 if (N1.getOpcode() == ISD::SHL) {
1720 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001721 if (SHC->getAPIntValue().isPowerOf2()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001722 EVT ADDVT = N1.getOperand(1).getValueType();
Bill Wendling07d85142009-01-30 02:55:25 +00001723 SDValue Add = DAG.getNode(ISD::ADD, N->getDebugLoc(), ADDVT,
1724 N1.getOperand(1),
1725 DAG.getConstant(SHC->getAPIntValue()
1726 .logBase2(),
1727 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001728 AddToWorkList(Add.getNode());
Bill Wendling07d85142009-01-30 02:55:25 +00001729 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001730 }
1731 }
1732 }
Nate Begeman69575232005-10-20 02:15:44 +00001733 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001734 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001735 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001736 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00001737 }
Dan Gohman7f321562007-06-25 16:23:39 +00001738
Dan Gohman613e0d82007-07-03 14:03:57 +00001739 // undef / X -> 0
1740 if (N0.getOpcode() == ISD::UNDEF)
1741 return DAG.getConstant(0, VT);
1742 // X / undef -> undef
1743 if (N1.getOpcode() == ISD::UNDEF)
1744 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001745
Dan Gohman475871a2008-07-27 21:46:04 +00001746 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001747}
1748
Dan Gohman475871a2008-07-27 21:46:04 +00001749SDValue DAGCombiner::visitSREM(SDNode *N) {
1750 SDValue N0 = N->getOperand(0);
1751 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001752 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1753 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001754 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001755
Nate Begeman1d4d4142005-09-01 00:19:25 +00001756 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001757 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001758 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001759 // If we know the sign bits of both operands are zero, strength reduce to a
1760 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00001761 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001762 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001763 return DAG.getNode(ISD::UREM, N->getDebugLoc(), VT, N0, N1);
Chris Lattneree339f42008-01-27 23:21:58 +00001764 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001765
Dan Gohman77003042007-11-26 23:46:11 +00001766 // If X/C can be simplified by the division-by-constant logic, lower
1767 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001768 if (N1C && !N1C->isNullValue()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001769 SDValue Div = DAG.getNode(ISD::SDIV, N->getDebugLoc(), VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001770 AddToWorkList(Div.getNode());
1771 SDValue OptimizedDiv = combine(Div.getNode());
1772 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001773 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1774 OptimizedDiv, N1);
1775 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001776 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001777 return Sub;
1778 }
Chris Lattner26d29902006-10-12 20:58:32 +00001779 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001780
Dan Gohman613e0d82007-07-03 14:03:57 +00001781 // undef % X -> 0
1782 if (N0.getOpcode() == ISD::UNDEF)
1783 return DAG.getConstant(0, VT);
1784 // X % undef -> undef
1785 if (N1.getOpcode() == ISD::UNDEF)
1786 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001787
Dan Gohman475871a2008-07-27 21:46:04 +00001788 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001789}
1790
Dan Gohman475871a2008-07-27 21:46:04 +00001791SDValue DAGCombiner::visitUREM(SDNode *N) {
1792 SDValue N0 = N->getOperand(0);
1793 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001794 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1795 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001796 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001797
Nate Begeman1d4d4142005-09-01 00:19:25 +00001798 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001799 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001800 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001801 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001802 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001803 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001804 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001805 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1806 if (N1.getOpcode() == ISD::SHL) {
1807 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001808 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001809 SDValue Add =
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001810 DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001811 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00001812 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001813 AddToWorkList(Add.getNode());
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001814 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, Add);
Nate Begemanc031e332006-02-05 07:36:48 +00001815 }
1816 }
1817 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001818
Dan Gohman77003042007-11-26 23:46:11 +00001819 // If X/C can be simplified by the division-by-constant logic, lower
1820 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001821 if (N1C && !N1C->isNullValue()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001822 SDValue Div = DAG.getNode(ISD::UDIV, N->getDebugLoc(), VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00001823 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00001824 SDValue OptimizedDiv = combine(Div.getNode());
1825 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001826 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1827 OptimizedDiv, N1);
1828 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001829 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001830 return Sub;
1831 }
Chris Lattner26d29902006-10-12 20:58:32 +00001832 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001833
Dan Gohman613e0d82007-07-03 14:03:57 +00001834 // undef % X -> 0
1835 if (N0.getOpcode() == ISD::UNDEF)
1836 return DAG.getConstant(0, VT);
1837 // X % undef -> undef
1838 if (N1.getOpcode() == ISD::UNDEF)
1839 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001840
Dan Gohman475871a2008-07-27 21:46:04 +00001841 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001842}
1843
Dan Gohman475871a2008-07-27 21:46:04 +00001844SDValue DAGCombiner::visitMULHS(SDNode *N) {
1845 SDValue N0 = N->getOperand(0);
1846 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001847 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001848 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001849
Nate Begeman1d4d4142005-09-01 00:19:25 +00001850 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001851 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001852 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001853 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001854 if (N1C && N1C->getAPIntValue() == 1)
Bill Wendling326411d2009-01-30 03:00:18 +00001855 return DAG.getNode(ISD::SRA, N->getDebugLoc(), N0.getValueType(), N0,
1856 DAG.getConstant(N0.getValueType().getSizeInBits() - 1,
Duncan Sands92abc622009-01-31 15:50:11 +00001857 getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001858 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001859 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001860 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001861
Dan Gohman475871a2008-07-27 21:46:04 +00001862 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001863}
1864
Dan Gohman475871a2008-07-27 21:46:04 +00001865SDValue DAGCombiner::visitMULHU(SDNode *N) {
1866 SDValue N0 = N->getOperand(0);
1867 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001868 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001869 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001870
Nate Begeman1d4d4142005-09-01 00:19:25 +00001871 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001872 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001873 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001874 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00001875 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001876 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001877 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001878 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001879 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001880
Dan Gohman475871a2008-07-27 21:46:04 +00001881 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001882}
1883
Dan Gohman389079b2007-10-08 17:57:15 +00001884/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1885/// compute two values. LoOp and HiOp give the opcodes for the two computations
1886/// that are being performed. Return true if a simplification was made.
1887///
Scott Michelfdc40a02009-02-17 22:15:04 +00001888SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Dan Gohman475871a2008-07-27 21:46:04 +00001889 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001890 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001891 bool HiExists = N->hasAnyUseOfValue(1);
1892 if (!HiExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001893 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00001894 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Bill Wendling826d1142009-01-30 03:08:40 +00001895 SDValue Res = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
1896 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001897 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001898 }
1899
1900 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001901 bool LoExists = N->hasAnyUseOfValue(0);
1902 if (!LoExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001903 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00001904 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Bill Wendling826d1142009-01-30 03:08:40 +00001905 SDValue Res = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
1906 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001907 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001908 }
1909
Evan Cheng44711942007-11-08 09:25:29 +00001910 // If both halves are used, return as it is.
1911 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00001912 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00001913
1914 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001915 if (LoExists) {
Bill Wendling826d1142009-01-30 03:08:40 +00001916 SDValue Lo = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
1917 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001918 AddToWorkList(Lo.getNode());
1919 SDValue LoOpt = combine(Lo.getNode());
1920 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001921 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001922 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001923 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001924 }
1925
Evan Cheng44711942007-11-08 09:25:29 +00001926 if (HiExists) {
Bill Wendling826d1142009-01-30 03:08:40 +00001927 SDValue Hi = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
Duncan Sands25cf2272008-11-24 14:53:14 +00001928 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001929 AddToWorkList(Hi.getNode());
1930 SDValue HiOpt = combine(Hi.getNode());
1931 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001932 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001933 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001934 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001935 }
Bill Wendling826d1142009-01-30 03:08:40 +00001936
Dan Gohman475871a2008-07-27 21:46:04 +00001937 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001938}
1939
Dan Gohman475871a2008-07-27 21:46:04 +00001940SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
1941 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00001942 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001943
Dan Gohman475871a2008-07-27 21:46:04 +00001944 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001945}
1946
Dan Gohman475871a2008-07-27 21:46:04 +00001947SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
1948 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00001949 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001950
Dan Gohman475871a2008-07-27 21:46:04 +00001951 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001952}
1953
Dan Gohman475871a2008-07-27 21:46:04 +00001954SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
1955 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001956 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00001957
Dan Gohman475871a2008-07-27 21:46:04 +00001958 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001959}
1960
Dan Gohman475871a2008-07-27 21:46:04 +00001961SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
1962 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001963 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00001964
Dan Gohman475871a2008-07-27 21:46:04 +00001965 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001966}
1967
Chris Lattner35e5c142006-05-05 05:51:50 +00001968/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1969/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00001970SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1971 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00001972 EVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00001973 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001974
Dan Gohmanff00a552010-01-14 03:08:49 +00001975 // Bail early if none of these transforms apply.
1976 if (N0.getNode()->getNumOperands() == 0) return SDValue();
1977
Chris Lattner540121f2006-05-05 06:31:05 +00001978 // For each of OP in AND/OR/XOR:
1979 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1980 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1981 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Evan Chengd40d03e2010-01-06 19:38:29 +00001982 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Nate Begeman93e0ed32009-12-03 07:11:29 +00001983 //
1984 // do not sink logical op inside of a vector extend, since it may combine
1985 // into a vsetcc.
Evan Chengd40d03e2010-01-06 19:38:29 +00001986 EVT Op0VT = N0.getOperand(0).getValueType();
1987 if ((N0.getOpcode() == ISD::ZERO_EXTEND ||
Dan Gohman97121ba2009-04-08 00:15:30 +00001988 N0.getOpcode() == ISD::SIGN_EXTEND ||
Evan Chenge5b51ac2010-04-17 06:13:15 +00001989 // Avoid infinite looping with PromoteIntBinOp.
1990 (N0.getOpcode() == ISD::ANY_EXTEND &&
1991 (!LegalTypes || TLI.isTypeDesirableForOp(N->getOpcode(), Op0VT))) ||
Evan Chengd40d03e2010-01-06 19:38:29 +00001992 (N0.getOpcode() == ISD::TRUNCATE && TLI.isTypeLegal(Op0VT))) &&
Nate Begeman93e0ed32009-12-03 07:11:29 +00001993 !VT.isVector() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00001994 Op0VT == N1.getOperand(0).getValueType() &&
1995 (!LegalOperations || TLI.isOperationLegal(N->getOpcode(), Op0VT))) {
Bill Wendlingb74c8672009-01-30 19:25:47 +00001996 SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(),
1997 N0.getOperand(0).getValueType(),
1998 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001999 AddToWorkList(ORNode.getNode());
Bill Wendlingb74c8672009-01-30 19:25:47 +00002000 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00002001 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002002
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002003 // For each of OP in SHL/SRL/SRA/AND...
2004 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
2005 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
2006 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00002007 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002008 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00002009 N0.getOperand(1) == N1.getOperand(1)) {
Bill Wendlingb74c8672009-01-30 19:25:47 +00002010 SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(),
2011 N0.getOperand(0).getValueType(),
2012 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002013 AddToWorkList(ORNode.getNode());
Bill Wendlingb74c8672009-01-30 19:25:47 +00002014 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
2015 ORNode, N0.getOperand(1));
Chris Lattner35e5c142006-05-05 05:51:50 +00002016 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002017
Dan Gohman475871a2008-07-27 21:46:04 +00002018 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00002019}
2020
Dan Gohman475871a2008-07-27 21:46:04 +00002021SDValue DAGCombiner::visitAND(SDNode *N) {
2022 SDValue N0 = N->getOperand(0);
2023 SDValue N1 = N->getOperand(1);
2024 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00002025 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2026 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002027 EVT VT = N1.getValueType();
Dan Gohman6900a392010-03-04 00:23:16 +00002028 unsigned BitWidth = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002029
Dan Gohman7f321562007-06-25 16:23:39 +00002030 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002031 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002032 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002033 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002034 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002035
Dan Gohman613e0d82007-07-03 14:03:57 +00002036 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002037 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002038 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002039 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002040 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002041 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002042 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002043 if (N0C && !N1C)
Bill Wendlingfc4b6772009-02-01 11:19:36 +00002044 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002045 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002046 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002047 return N0;
2048 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002049 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002050 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002051 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00002052 // reassociate and
Bill Wendling35247c32009-01-30 00:45:56 +00002053 SDValue RAND = ReassociateOps(ISD::AND, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002054 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002055 return RAND;
Bill Wendling7d9f2b92010-03-03 00:35:56 +00002056 // fold (and (or x, C), D) -> D if (C & D) == D
Nate Begeman5dc7e862005-11-02 18:42:59 +00002057 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002058 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002059 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002060 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00002061 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
2062 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00002063 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002064 APInt Mask = ~N1C->getAPIntValue();
2065 Mask.trunc(N0Op0.getValueSizeInBits());
2066 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Bill Wendling2627a882009-01-30 20:43:18 +00002067 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(),
2068 N0.getValueType(), N0Op0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002069
Chris Lattner1ec05d12006-03-01 21:47:21 +00002070 // Replace uses of the AND with uses of the Zero extend node.
2071 CombineTo(N, Zext);
Scott Michelfdc40a02009-02-17 22:15:04 +00002072
Chris Lattner3603cd62006-02-02 07:17:31 +00002073 // We actually want to replace all uses of the any_extend with the
2074 // zero_extend, to avoid duplicating things. This will later cause this
2075 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00002076 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00002077 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00002078 }
2079 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002080 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
2081 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
2082 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
2083 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00002084
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002085 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002086 LL.getValueType().isInteger()) {
Bill Wendling2627a882009-01-30 20:43:18 +00002087 // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00002088 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Bill Wendling2627a882009-01-30 20:43:18 +00002089 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
2090 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002091 AddToWorkList(ORNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00002092 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002093 }
Bill Wendling2627a882009-01-30 20:43:18 +00002094 // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002095 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Bill Wendling2627a882009-01-30 20:43:18 +00002096 SDValue ANDNode = DAG.getNode(ISD::AND, N0.getDebugLoc(),
2097 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002098 AddToWorkList(ANDNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00002099 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002100 }
Bill Wendling2627a882009-01-30 20:43:18 +00002101 // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002102 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Bill Wendling2627a882009-01-30 20:43:18 +00002103 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
2104 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002105 AddToWorkList(ORNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00002106 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002107 }
2108 }
2109 // canonicalize equivalent to ll == rl
2110 if (LL == RR && LR == RL) {
2111 Op1 = ISD::getSetCCSwappedOperands(Op1);
2112 std::swap(RL, RR);
2113 }
2114 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002115 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002116 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00002117 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002118 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Bill Wendling2627a882009-01-30 20:43:18 +00002119 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
2120 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002121 }
2122 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002123
Bill Wendling2627a882009-01-30 20:43:18 +00002124 // Simplify: (and (op x...), (op y...)) -> (op (and x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00002125 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002126 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002127 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002128 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002129
Nate Begemande996292006-02-03 22:24:05 +00002130 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
2131 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002132 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002133 SimplifyDemandedBits(SDValue(N, 0)))
2134 return SDValue(N, 0);
Evan Chengd40d03e2010-01-06 19:38:29 +00002135
Nate Begemanded49632005-10-13 03:11:28 +00002136 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002137 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00002138 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002139 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002140 // If we zero all the possible extended bits, then we can turn this into
2141 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002142 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002143 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002144 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002145 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002146 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Bill Wendling2627a882009-01-30 20:43:18 +00002147 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
2148 LN0->getChain(), LN0->getBasePtr(),
2149 LN0->getSrcValue(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00002150 LN0->getSrcValueOffset(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00002151 LN0->isVolatile(), LN0->isNonTemporal(),
2152 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002153 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002154 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002155 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002156 }
2157 }
2158 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00002159 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00002160 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002161 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002162 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002163 // If we zero all the possible extended bits, then we can turn this into
2164 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002165 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002166 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002167 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002168 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002169 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Bill Wendling2627a882009-01-30 20:43:18 +00002170 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
2171 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00002172 LN0->getBasePtr(), LN0->getSrcValue(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00002173 LN0->getSrcValueOffset(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00002174 LN0->isVolatile(), LN0->isNonTemporal(),
2175 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002176 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002177 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002178 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002179 }
2180 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002181
Chris Lattner35a9f5a2006-02-28 06:49:37 +00002182 // fold (and (load x), 255) -> (zextload x, i8)
2183 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Chengd40d03e2010-01-06 19:38:29 +00002184 // fold (and (any_ext (extload x, i16)), 255) -> (zextload x, i8)
2185 if (N1C && (N0.getOpcode() == ISD::LOAD ||
2186 (N0.getOpcode() == ISD::ANY_EXTEND &&
2187 N0.getOperand(0).getOpcode() == ISD::LOAD))) {
2188 bool HasAnyExt = N0.getOpcode() == ISD::ANY_EXTEND;
2189 LoadSDNode *LN0 = HasAnyExt
2190 ? cast<LoadSDNode>(N0.getOperand(0))
2191 : cast<LoadSDNode>(N0);
Evan Cheng466685d2006-10-09 20:57:25 +00002192 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Chris Lattnerbd1fccf2010-01-07 21:59:23 +00002193 LN0->isUnindexed() && N0.hasOneUse() && LN0->hasOneUse()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00002194 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
Evan Chengd40d03e2010-01-06 19:38:29 +00002195 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue())){
2196 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits);
2197 EVT LoadedVT = LN0->getMemoryVT();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002198
Evan Chengd40d03e2010-01-06 19:38:29 +00002199 if (ExtVT == LoadedVT &&
2200 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
Chris Lattneref7634c2010-01-07 21:53:27 +00002201 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
2202
2203 SDValue NewLoad =
2204 DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), LoadResultTy,
2205 LN0->getChain(), LN0->getBasePtr(),
2206 LN0->getSrcValue(), LN0->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00002207 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2208 LN0->getAlignment());
Chris Lattneref7634c2010-01-07 21:53:27 +00002209 AddToWorkList(N);
2210 CombineTo(LN0, NewLoad, NewLoad.getValue(1));
2211 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2212 }
2213
2214 // Do not change the width of a volatile load.
2215 // Do not generate loads of non-round integer types since these can
2216 // be expensive (and would be wrong if the type is not byte sized).
2217 if (!LN0->isVolatile() && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() &&
2218 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
2219 EVT PtrType = LN0->getOperand(1).getValueType();
Bill Wendling2627a882009-01-30 20:43:18 +00002220
Chris Lattneref7634c2010-01-07 21:53:27 +00002221 unsigned Alignment = LN0->getAlignment();
2222 SDValue NewPtr = LN0->getBasePtr();
2223
2224 // For big endian targets, we need to add an offset to the pointer
2225 // to load the correct bytes. For little endian systems, we merely
2226 // need to read fewer bytes from the same pointer.
2227 if (TLI.isBigEndian()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00002228 unsigned LVTStoreBytes = LoadedVT.getStoreSize();
2229 unsigned EVTStoreBytes = ExtVT.getStoreSize();
2230 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Chris Lattneref7634c2010-01-07 21:53:27 +00002231 NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(), PtrType,
2232 NewPtr, DAG.getConstant(PtrOff, PtrType));
2233 Alignment = MinAlign(Alignment, PtrOff);
Evan Chengd40d03e2010-01-06 19:38:29 +00002234 }
Chris Lattneref7634c2010-01-07 21:53:27 +00002235
2236 AddToWorkList(NewPtr.getNode());
2237
2238 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
2239 SDValue Load =
2240 DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), LoadResultTy,
2241 LN0->getChain(), NewPtr,
2242 LN0->getSrcValue(), LN0->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00002243 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2244 Alignment);
Chris Lattneref7634c2010-01-07 21:53:27 +00002245 AddToWorkList(N);
2246 CombineTo(LN0, Load, Load.getValue(1));
2247 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsdc846502007-10-28 12:59:45 +00002248 }
Evan Cheng466685d2006-10-09 20:57:25 +00002249 }
Chris Lattner15045b62006-02-28 06:35:35 +00002250 }
2251 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002252
Evan Cheng64b7bf72010-04-16 06:14:10 +00002253 return PromoteIntBinOp(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002254}
2255
Dan Gohman475871a2008-07-27 21:46:04 +00002256SDValue DAGCombiner::visitOR(SDNode *N) {
2257 SDValue N0 = N->getOperand(0);
2258 SDValue N1 = N->getOperand(1);
2259 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00002260 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2261 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002262 EVT VT = N1.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00002263
Dan Gohman7f321562007-06-25 16:23:39 +00002264 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002265 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002266 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002267 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002268 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002269
Dan Gohman613e0d82007-07-03 14:03:57 +00002270 // fold (or x, undef) -> -1
Nate Begeman93e0ed32009-12-03 07:11:29 +00002271 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF) {
2272 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
2273 return DAG.getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
2274 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00002275 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002276 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002277 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002278 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002279 if (N0C && !N1C)
Bill Wendling09025642009-01-30 20:59:34 +00002280 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002281 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002282 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002283 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002284 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002285 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002286 return N1;
2287 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002288 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002289 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002290 // reassociate or
Bill Wendling35247c32009-01-30 00:45:56 +00002291 SDValue ROR = ReassociateOps(ISD::OR, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002292 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002293 return ROR;
2294 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Bill Wendling7d9f2b92010-03-03 00:35:56 +00002295 // iff (c1 & c2) == 0.
Gabor Greifba36cb52008-08-28 21:40:38 +00002296 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00002297 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00002298 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
Bill Wendling32f9eb22010-03-03 01:58:01 +00002299 if ((C1->getAPIntValue() & N1C->getAPIntValue()) != 0)
Bill Wendling7d9f2b92010-03-03 00:35:56 +00002300 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
2301 DAG.getNode(ISD::OR, N0.getDebugLoc(), VT,
2302 N0.getOperand(0), N1),
2303 DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1));
Nate Begeman223df222005-09-08 20:18:10 +00002304 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002305 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
2306 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
2307 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
2308 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00002309
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002310 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002311 LL.getValueType().isInteger()) {
Bill Wendling09025642009-01-30 20:59:34 +00002312 // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0)
2313 // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002314 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002315 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Bill Wendling09025642009-01-30 20:59:34 +00002316 SDValue ORNode = DAG.getNode(ISD::OR, LR.getDebugLoc(),
2317 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002318 AddToWorkList(ORNode.getNode());
Bill Wendling09025642009-01-30 20:59:34 +00002319 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002320 }
Bill Wendling09025642009-01-30 20:59:34 +00002321 // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1)
2322 // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1)
Scott Michelfdc40a02009-02-17 22:15:04 +00002323 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002324 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Bill Wendling09025642009-01-30 20:59:34 +00002325 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getDebugLoc(),
2326 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002327 AddToWorkList(ANDNode.getNode());
Bill Wendling09025642009-01-30 20:59:34 +00002328 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002329 }
2330 }
2331 // canonicalize equivalent to ll == rl
2332 if (LL == RR && LR == RL) {
2333 Op1 = ISD::getSetCCSwappedOperands(Op1);
2334 std::swap(RL, RR);
2335 }
2336 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002337 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002338 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00002339 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002340 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Bill Wendling09025642009-01-30 20:59:34 +00002341 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
2342 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002343 }
2344 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002345
Bill Wendling09025642009-01-30 20:59:34 +00002346 // Simplify: (or (op x...), (op y...)) -> (op (or x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00002347 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002348 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002349 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002350 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002351
Bill Wendling09025642009-01-30 20:59:34 +00002352 // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible.
Chris Lattner1ec72732006-09-14 21:11:37 +00002353 if (N0.getOpcode() == ISD::AND &&
2354 N1.getOpcode() == ISD::AND &&
2355 N0.getOperand(1).getOpcode() == ISD::Constant &&
2356 N1.getOperand(1).getOpcode() == ISD::Constant &&
2357 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00002358 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00002359 // We can only do this xform if we know that bits from X that are set in C2
2360 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002361 const APInt &LHSMask =
2362 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
2363 const APInt &RHSMask =
2364 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00002365
Dan Gohmanea859be2007-06-22 14:59:07 +00002366 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
2367 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Bill Wendling09025642009-01-30 20:59:34 +00002368 SDValue X = DAG.getNode(ISD::OR, N0.getDebugLoc(), VT,
2369 N0.getOperand(0), N1.getOperand(0));
2370 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, X,
2371 DAG.getConstant(LHSMask | RHSMask, VT));
Chris Lattner1ec72732006-09-14 21:11:37 +00002372 }
2373 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002374
Chris Lattner516b9622006-09-14 20:50:57 +00002375 // See if this is some rotate idiom.
Bill Wendling317bd702009-01-30 21:14:50 +00002376 if (SDNode *Rot = MatchRotate(N0, N1, N->getDebugLoc()))
Dan Gohman475871a2008-07-27 21:46:04 +00002377 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00002378
Evan Cheng64b7bf72010-04-16 06:14:10 +00002379 return PromoteIntBinOp(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002380}
2381
Chris Lattner516b9622006-09-14 20:50:57 +00002382/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00002383static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00002384 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00002385 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00002386 Mask = Op.getOperand(1);
2387 Op = Op.getOperand(0);
2388 } else {
2389 return false;
2390 }
2391 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002392
Chris Lattner516b9622006-09-14 20:50:57 +00002393 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
2394 Shift = Op;
2395 return true;
2396 }
Bill Wendling09025642009-01-30 20:59:34 +00002397
Scott Michelfdc40a02009-02-17 22:15:04 +00002398 return false;
Chris Lattner516b9622006-09-14 20:50:57 +00002399}
2400
Chris Lattner516b9622006-09-14 20:50:57 +00002401// MatchRotate - Handle an 'or' of two operands. If this is one of the many
2402// idioms for rotate, and if the target supports rotation instructions, generate
2403// a rot[lr].
Bill Wendling317bd702009-01-30 21:14:50 +00002404SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002405 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Owen Andersone50ed302009-08-10 22:56:29 +00002406 EVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00002407 if (!TLI.isTypeLegal(VT)) return 0;
2408
2409 // The target must have at least one rotate flavor.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002410 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
2411 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Chris Lattner516b9622006-09-14 20:50:57 +00002412 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002413
Chris Lattner516b9622006-09-14 20:50:57 +00002414 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00002415 SDValue LHSShift; // The shift.
2416 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00002417 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
2418 return 0; // Not part of a rotate.
2419
Dan Gohman475871a2008-07-27 21:46:04 +00002420 SDValue RHSShift; // The shift.
2421 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00002422 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
2423 return 0; // Not part of a rotate.
Scott Michelfdc40a02009-02-17 22:15:04 +00002424
Chris Lattner516b9622006-09-14 20:50:57 +00002425 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
2426 return 0; // Not shifting the same value.
2427
2428 if (LHSShift.getOpcode() == RHSShift.getOpcode())
2429 return 0; // Shifts must disagree.
Scott Michelfdc40a02009-02-17 22:15:04 +00002430
Chris Lattner516b9622006-09-14 20:50:57 +00002431 // Canonicalize shl to left side in a shl/srl pair.
2432 if (RHSShift.getOpcode() == ISD::SHL) {
2433 std::swap(LHS, RHS);
2434 std::swap(LHSShift, RHSShift);
2435 std::swap(LHSMask , RHSMask );
2436 }
2437
Duncan Sands83ec4b62008-06-06 12:08:01 +00002438 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00002439 SDValue LHSShiftArg = LHSShift.getOperand(0);
2440 SDValue LHSShiftAmt = LHSShift.getOperand(1);
2441 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00002442
2443 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
2444 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00002445 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
2446 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002447 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
2448 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00002449 if ((LShVal + RShVal) != OpSizeInBits)
2450 return 0;
2451
Dan Gohman475871a2008-07-27 21:46:04 +00002452 SDValue Rot;
Chris Lattner516b9622006-09-14 20:50:57 +00002453 if (HasROTL)
Bill Wendling317bd702009-01-30 21:14:50 +00002454 Rot = DAG.getNode(ISD::ROTL, DL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002455 else
Bill Wendling317bd702009-01-30 21:14:50 +00002456 Rot = DAG.getNode(ISD::ROTR, DL, VT, LHSShiftArg, RHSShiftAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00002457
Chris Lattner516b9622006-09-14 20:50:57 +00002458 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00002459 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002460 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00002461
Gabor Greifba36cb52008-08-28 21:40:38 +00002462 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002463 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2464 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002465 }
Gabor Greifba36cb52008-08-28 21:40:38 +00002466 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002467 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2468 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002469 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002470
Bill Wendling317bd702009-01-30 21:14:50 +00002471 Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT));
Chris Lattner516b9622006-09-14 20:50:57 +00002472 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002473
Gabor Greifba36cb52008-08-28 21:40:38 +00002474 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002475 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002476
Chris Lattner516b9622006-09-14 20:50:57 +00002477 // If there is a mask here, and we have a variable shift, we can't be sure
2478 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00002479 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00002480 return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00002481
Chris Lattner516b9622006-09-14 20:50:57 +00002482 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2483 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002484 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2485 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002486 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002487 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002488 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002489 if (HasROTL)
Bill Wendling317bd702009-01-30 21:14:50 +00002490 return DAG.getNode(ISD::ROTL, DL, VT,
2491 LHSShiftArg, LHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002492 else
Bill Wendling317bd702009-01-30 21:14:50 +00002493 return DAG.getNode(ISD::ROTR, DL, VT,
2494 LHSShiftArg, RHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002495 }
Chris Lattner516b9622006-09-14 20:50:57 +00002496 }
2497 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002498
Chris Lattner516b9622006-09-14 20:50:57 +00002499 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2500 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002501 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2502 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002503 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002504 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002505 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling2692d592008-08-31 01:13:31 +00002506 if (HasROTR)
Bill Wendling317bd702009-01-30 21:14:50 +00002507 return DAG.getNode(ISD::ROTR, DL, VT,
2508 LHSShiftArg, RHSShiftAmt).getNode();
Bill Wendling2692d592008-08-31 01:13:31 +00002509 else
Bill Wendling317bd702009-01-30 21:14:50 +00002510 return DAG.getNode(ISD::ROTL, DL, VT,
2511 LHSShiftArg, LHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002512 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002513 }
2514 }
2515
Dan Gohman74feef22008-10-17 01:23:35 +00002516 // Look for sign/zext/any-extended or truncate cases:
Scott Michelc9dc1142007-04-02 21:36:32 +00002517 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2518 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00002519 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2520 || LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
Scott Michelc9dc1142007-04-02 21:36:32 +00002521 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2522 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00002523 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2524 || RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002525 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
2526 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +00002527 if (RExtOp0.getOpcode() == ISD::SUB &&
2528 RExtOp0.getOperand(1) == LExtOp0) {
2529 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002530 // (rotl x, y)
Scott Michelc9dc1142007-04-02 21:36:32 +00002531 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002532 // (rotr x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00002533 if (ConstantSDNode *SUBC =
2534 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002535 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00002536 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
2537 LHSShiftArg,
Gabor Greif12632d22008-08-30 19:29:20 +00002538 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002539 }
2540 }
2541 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2542 RExtOp0 == LExtOp0.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002543 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002544 // (rotr x, y)
Bill Wendling353dea22008-08-31 01:04:56 +00002545 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002546 // (rotl x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00002547 if (ConstantSDNode *SUBC =
2548 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002549 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00002550 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT,
2551 LHSShiftArg,
Bill Wendling353dea22008-08-31 01:04:56 +00002552 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002553 }
2554 }
Chris Lattner516b9622006-09-14 20:50:57 +00002555 }
2556 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002557
Chris Lattner516b9622006-09-14 20:50:57 +00002558 return 0;
2559}
2560
Dan Gohman475871a2008-07-27 21:46:04 +00002561SDValue DAGCombiner::visitXOR(SDNode *N) {
2562 SDValue N0 = N->getOperand(0);
2563 SDValue N1 = N->getOperand(1);
2564 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00002565 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2566 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002567 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00002568
Dan Gohman7f321562007-06-25 16:23:39 +00002569 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002570 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002571 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002572 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002573 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002574
Evan Cheng26471c42008-03-25 20:08:07 +00002575 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
2576 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
2577 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00002578 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002579 if (N0.getOpcode() == ISD::UNDEF)
2580 return N0;
2581 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002582 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002583 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002584 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002585 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002586 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002587 if (N0C && !N1C)
Bill Wendling317bd702009-01-30 21:14:50 +00002588 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002589 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002590 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002591 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002592 // reassociate xor
Bill Wendling35247c32009-01-30 00:45:56 +00002593 SDValue RXOR = ReassociateOps(ISD::XOR, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002594 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002595 return RXOR;
Bill Wendlingae89bb12008-11-11 08:25:46 +00002596
Nate Begeman1d4d4142005-09-01 00:19:25 +00002597 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00002598 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002599 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00002600 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2601 isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +00002602
Duncan Sands25cf2272008-11-24 14:53:14 +00002603 if (!LegalOperations || TLI.isCondCodeLegal(NotCC, LHS.getValueType())) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00002604 switch (N0.getOpcode()) {
2605 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002606 llvm_unreachable("Unhandled SetCC Equivalent!");
Bill Wendlingae89bb12008-11-11 08:25:46 +00002607 case ISD::SETCC:
Bill Wendling317bd702009-01-30 21:14:50 +00002608 return DAG.getSetCC(N->getDebugLoc(), VT, LHS, RHS, NotCC);
Bill Wendlingae89bb12008-11-11 08:25:46 +00002609 case ISD::SELECT_CC:
Bill Wendling317bd702009-01-30 21:14:50 +00002610 return DAG.getSelectCC(N->getDebugLoc(), LHS, RHS, N0.getOperand(2),
Bill Wendlingae89bb12008-11-11 08:25:46 +00002611 N0.getOperand(3), NotCC);
2612 }
2613 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00002614 }
Bill Wendlingae89bb12008-11-11 08:25:46 +00002615
Chris Lattner61c5ff42007-09-10 21:39:07 +00002616 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002617 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00002618 N0.getNode()->hasOneUse() &&
2619 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00002620 SDValue V = N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002621 V = DAG.getNode(ISD::XOR, N0.getDebugLoc(), V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002622 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002623 AddToWorkList(V.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00002624 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, V);
Chris Lattner61c5ff42007-09-10 21:39:07 +00002625 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002626
Bill Wendling317bd702009-01-30 21:14:50 +00002627 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc
Owen Anderson825b72b2009-08-11 20:47:22 +00002628 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002629 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002630 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002631 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2632 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling317bd702009-01-30 21:14:50 +00002633 LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS
2634 RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002635 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00002636 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002637 }
2638 }
Bill Wendling317bd702009-01-30 21:14:50 +00002639 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants
Scott Michelfdc40a02009-02-17 22:15:04 +00002640 if (N1C && N1C->isAllOnesValue() &&
Nate Begeman99801192005-09-07 23:25:52 +00002641 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002642 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002643 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2644 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling317bd702009-01-30 21:14:50 +00002645 LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS
2646 RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002647 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00002648 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002649 }
2650 }
Bill Wendling317bd702009-01-30 21:14:50 +00002651 // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2))
Nate Begeman223df222005-09-08 20:18:10 +00002652 if (N1C && N0.getOpcode() == ISD::XOR) {
2653 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2654 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2655 if (N00C)
Bill Wendling317bd702009-01-30 21:14:50 +00002656 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(1),
2657 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00002658 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002659 if (N01C)
Bill Wendling317bd702009-01-30 21:14:50 +00002660 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(0),
2661 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00002662 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002663 }
2664 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002665 if (N0 == N1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002666 if (!VT.isVector()) {
Chris Lattner4fbdd592006-03-28 19:11:05 +00002667 return DAG.getConstant(0, VT);
Duncan Sands25cf2272008-11-24 14:53:14 +00002668 } else if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)){
Chris Lattner4fbdd592006-03-28 19:11:05 +00002669 // Produce a vector of zeros.
Dan Gohman475871a2008-07-27 21:46:04 +00002670 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
2671 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
Evan Chenga87008d2009-02-25 22:49:59 +00002672 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT,
2673 &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002674 }
2675 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002676
Chris Lattner35e5c142006-05-05 05:51:50 +00002677 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2678 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002679 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002680 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002681 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002682
Chris Lattner3e104b12006-04-08 04:15:24 +00002683 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002684 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002685 SimplifyDemandedBits(SDValue(N, 0)))
2686 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002687
Evan Cheng64b7bf72010-04-16 06:14:10 +00002688 return PromoteIntBinOp(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002689}
2690
Chris Lattnere70da202007-12-06 07:33:36 +00002691/// visitShiftByConstant - Handle transforms common to the three shifts, when
2692/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00002693SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002694 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00002695 if (!LHS->hasOneUse()) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00002696
Chris Lattnere70da202007-12-06 07:33:36 +00002697 // We want to pull some binops through shifts, so that we have (and (shift))
2698 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2699 // thing happens with address calculations, so it's important to canonicalize
2700 // it.
2701 bool HighBitSet = false; // Can we transform this if the high bit is set?
Scott Michelfdc40a02009-02-17 22:15:04 +00002702
Chris Lattnere70da202007-12-06 07:33:36 +00002703 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002704 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002705 case ISD::OR:
2706 case ISD::XOR:
2707 HighBitSet = false; // We can only transform sra if the high bit is clear.
2708 break;
2709 case ISD::AND:
2710 HighBitSet = true; // We can only transform sra if the high bit is set.
2711 break;
2712 case ISD::ADD:
Scott Michelfdc40a02009-02-17 22:15:04 +00002713 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00002714 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00002715 HighBitSet = false; // We can only transform sra if the high bit is clear.
2716 break;
2717 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002718
Chris Lattnere70da202007-12-06 07:33:36 +00002719 // We require the RHS of the binop to be a constant as well.
2720 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002721 if (!BinOpCst) return SDValue();
Bill Wendling88103372009-01-30 21:37:17 +00002722
2723 // FIXME: disable this unless the input to the binop is a shift by a constant.
2724 // If it is not a shift, it pessimizes some common cases like:
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002725 //
Bill Wendling88103372009-01-30 21:37:17 +00002726 // void foo(int *X, int i) { X[i & 1235] = 1; }
2727 // int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00002728 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00002729 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002730 BinOpLHSVal->getOpcode() != ISD::SRA &&
2731 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2732 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00002733 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00002734
Owen Andersone50ed302009-08-10 22:56:29 +00002735 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002736
Bill Wendling88103372009-01-30 21:37:17 +00002737 // If this is a signed shift right, and the high bit is modified by the
2738 // logical operation, do not perform the transformation. The highBitSet
2739 // boolean indicates the value of the high bit of the constant which would
2740 // cause it to be modified for this operation.
Chris Lattnere70da202007-12-06 07:33:36 +00002741 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00002742 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2743 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00002744 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002745 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002746
Chris Lattnere70da202007-12-06 07:33:36 +00002747 // Fold the constants, shifting the binop RHS by the shift amount.
Bill Wendling88103372009-01-30 21:37:17 +00002748 SDValue NewRHS = DAG.getNode(N->getOpcode(), LHS->getOperand(1).getDebugLoc(),
2749 N->getValueType(0),
2750 LHS->getOperand(1), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00002751
2752 // Create the new shift.
Bill Wendling88103372009-01-30 21:37:17 +00002753 SDValue NewShift = DAG.getNode(N->getOpcode(), LHS->getOperand(0).getDebugLoc(),
2754 VT, LHS->getOperand(0), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00002755
2756 // Create the new binop.
Bill Wendling88103372009-01-30 21:37:17 +00002757 return DAG.getNode(LHS->getOpcode(), N->getDebugLoc(), VT, NewShift, NewRHS);
Chris Lattnere70da202007-12-06 07:33:36 +00002758}
2759
Dan Gohman475871a2008-07-27 21:46:04 +00002760SDValue DAGCombiner::visitSHL(SDNode *N) {
2761 SDValue N0 = N->getOperand(0);
2762 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002763 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2764 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002765 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00002766 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002767
Nate Begeman1d4d4142005-09-01 00:19:25 +00002768 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002769 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002770 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002771 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002772 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002773 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002774 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002775 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00002776 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002777 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002778 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002779 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002780 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002781 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman87862e72009-12-11 21:31:27 +00002782 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002783 return DAG.getConstant(0, VT);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002784 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00002785 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002786 N1.getOperand(0).getOpcode() == ISD::AND &&
2787 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002788 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002789 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00002790 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002791 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002792 APInt TruncC = N101C->getAPIntValue();
2793 TruncC.trunc(TruncVT.getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00002794 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00002795 DAG.getNode(ISD::AND, N->getDebugLoc(), TruncVT,
2796 DAG.getNode(ISD::TRUNCATE,
2797 N->getDebugLoc(),
2798 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00002799 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002800 }
2801 }
2802
Dan Gohman475871a2008-07-27 21:46:04 +00002803 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2804 return SDValue(N, 0);
Bill Wendling88103372009-01-30 21:37:17 +00002805
2806 // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00002807 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002808 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002809 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2810 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002811 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002812 return DAG.getConstant(0, VT);
Bill Wendling88103372009-01-30 21:37:17 +00002813 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002814 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002815 }
Bill Wendling88103372009-01-30 21:37:17 +00002816 // fold (shl (srl x, c1), c2) -> (shl (and x, (shl -1, c1)), (sub c2, c1)) or
2817 // (srl (and x, (shl -1, c1)), (sub c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00002818 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002819 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002820 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
Evan Chengd101a722009-07-21 05:40:15 +00002821 if (c1 < VT.getSizeInBits()) {
2822 uint64_t c2 = N1C->getZExtValue();
Dan Gohman5cbd37e2009-08-06 09:18:59 +00002823 SDValue HiBitsMask =
2824 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
2825 VT.getSizeInBits() - c1),
2826 VT);
Evan Chengd101a722009-07-21 05:40:15 +00002827 SDValue Mask = DAG.getNode(ISD::AND, N0.getDebugLoc(), VT,
2828 N0.getOperand(0),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00002829 HiBitsMask);
Evan Chengd101a722009-07-21 05:40:15 +00002830 if (c2 > c1)
2831 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, Mask,
2832 DAG.getConstant(c2-c1, N1.getValueType()));
2833 else
2834 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, Mask,
2835 DAG.getConstant(c1-c2, N1.getValueType()));
2836 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00002837 }
Bill Wendling88103372009-01-30 21:37:17 +00002838 // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1))
Dan Gohman5cbd37e2009-08-06 09:18:59 +00002839 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) {
2840 SDValue HiBitsMask =
2841 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
2842 VT.getSizeInBits() -
2843 N1C->getZExtValue()),
2844 VT);
Bill Wendling88103372009-01-30 21:37:17 +00002845 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0.getOperand(0),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00002846 HiBitsMask);
2847 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002848
Evan Chenge5b51ac2010-04-17 06:13:15 +00002849 if (N1C) {
2850 SDValue NewSHL = visitShiftByConstant(N, N1C->getZExtValue());
2851 if (NewSHL.getNode())
2852 return NewSHL;
2853 }
2854
Evan Cheng07c4e102010-04-22 20:19:46 +00002855 return PromoteIntShiftOp(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002856}
2857
Dan Gohman475871a2008-07-27 21:46:04 +00002858SDValue DAGCombiner::visitSRA(SDNode *N) {
2859 SDValue N0 = N->getOperand(0);
2860 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002861 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2862 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002863 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00002864 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002865
Bill Wendling88103372009-01-30 21:37:17 +00002866 // fold (sra c1, c2) -> (sra c1, c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002867 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002868 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002869 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002870 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002871 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002872 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002873 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002874 return N0;
Bill Wendling88103372009-01-30 21:37:17 +00002875 // fold (sra x, (setge c, size(x))) -> undef
Dan Gohman87862e72009-12-11 21:31:27 +00002876 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00002877 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002878 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002879 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002880 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002881 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2882 // sext_inreg.
2883 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohman87862e72009-12-11 21:31:27 +00002884 unsigned LowBits = OpSizeInBits - (unsigned)N1C->getZExtValue();
Dan Gohmand1996362010-01-09 02:13:55 +00002885 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), LowBits);
2886 if (VT.isVector())
2887 ExtVT = EVT::getVectorVT(*DAG.getContext(),
2888 ExtVT, VT.getVectorNumElements());
2889 if ((!LegalOperations ||
2890 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, ExtVT)))
Bill Wendling88103372009-01-30 21:37:17 +00002891 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
Dan Gohmand1996362010-01-09 02:13:55 +00002892 N0.getOperand(0), DAG.getValueType(ExtVT));
Nate Begemanfb7217b2006-02-17 19:54:08 +00002893 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002894
Bill Wendling88103372009-01-30 21:37:17 +00002895 // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2))
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002896 if (N1C && N0.getOpcode() == ISD::SRA) {
2897 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002898 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Dan Gohman87862e72009-12-11 21:31:27 +00002899 if (Sum >= OpSizeInBits) Sum = OpSizeInBits-1;
Bill Wendling88103372009-01-30 21:37:17 +00002900 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0.getOperand(0),
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002901 DAG.getConstant(Sum, N1C->getValueType(0)));
2902 }
2903 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00002904
Bill Wendling88103372009-01-30 21:37:17 +00002905 // fold (sra (shl X, m), (sub result_size, n))
2906 // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for
Scott Michelfdc40a02009-02-17 22:15:04 +00002907 // result_size - n != m.
2908 // If truncate is free for the target sext(shl) is likely to result in better
Christopher Lambb9b04282008-03-20 04:31:39 +00002909 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002910 if (N0.getOpcode() == ISD::SHL) {
2911 // Get the two constanst of the shifts, CN0 = m, CN = n.
2912 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2913 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002914 // Determine what the truncate's result bitsize and type would be.
Owen Andersone50ed302009-08-10 22:56:29 +00002915 EVT TruncVT =
Dan Gohman87862e72009-12-11 21:31:27 +00002916 EVT::getIntegerVT(*DAG.getContext(), OpSizeInBits - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00002917 // Determine the residual right-shift amount.
Torok Edwin6bb49582009-05-23 17:29:48 +00002918 signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002919
Scott Michelfdc40a02009-02-17 22:15:04 +00002920 // If the shift is not a no-op (in which case this should be just a sign
2921 // extend already), the truncated to type is legal, sign_extend is legal
Dan Gohmanf451cb82010-02-10 16:03:48 +00002922 // on that type, and the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00002923 // perform the transform.
Torok Edwin6bb49582009-05-23 17:29:48 +00002924 if ((ShiftAmt > 0) &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002925 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
2926 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00002927 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002928
Duncan Sands92abc622009-01-31 15:50:11 +00002929 SDValue Amt = DAG.getConstant(ShiftAmt, getShiftAmountTy());
Bill Wendling88103372009-01-30 21:37:17 +00002930 SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT,
2931 N0.getOperand(0), Amt);
2932 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), TruncVT,
2933 Shift);
2934 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(),
2935 N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00002936 }
2937 }
2938 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002939
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002940 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00002941 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002942 N1.getOperand(0).getOpcode() == ISD::AND &&
2943 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002944 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002945 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00002946 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002947 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002948 APInt TruncC = N101C->getAPIntValue();
Dan Gohman87862e72009-12-11 21:31:27 +00002949 TruncC.trunc(TruncVT.getScalarType().getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00002950 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
Bill Wendling9729c5a2009-01-31 03:12:48 +00002951 DAG.getNode(ISD::AND, N->getDebugLoc(),
Bill Wendling88103372009-01-30 21:37:17 +00002952 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00002953 DAG.getNode(ISD::TRUNCATE,
2954 N->getDebugLoc(),
2955 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00002956 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002957 }
2958 }
2959
Scott Michelfdc40a02009-02-17 22:15:04 +00002960 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00002961 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2962 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002963
2964
Nate Begeman1d4d4142005-09-01 00:19:25 +00002965 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002966 if (DAG.SignBitIsZero(N0))
Bill Wendling88103372009-01-30 21:37:17 +00002967 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002968
Evan Chenge5b51ac2010-04-17 06:13:15 +00002969 if (N1C) {
2970 SDValue NewSRA = visitShiftByConstant(N, N1C->getZExtValue());
2971 if (NewSRA.getNode())
2972 return NewSRA;
2973 }
2974
Evan Cheng07c4e102010-04-22 20:19:46 +00002975 return PromoteIntShiftOp(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002976}
2977
Dan Gohman475871a2008-07-27 21:46:04 +00002978SDValue DAGCombiner::visitSRL(SDNode *N) {
2979 SDValue N0 = N->getOperand(0);
2980 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002981 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2982 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002983 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00002984 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002985
Nate Begeman1d4d4142005-09-01 00:19:25 +00002986 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002987 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002988 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002989 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002990 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002991 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002992 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002993 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00002994 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002995 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002996 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002997 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002998 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002999 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003000 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003001 return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003002
Bill Wendling88103372009-01-30 21:37:17 +00003003 // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00003004 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003005 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003006 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3007 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003008 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003009 return DAG.getConstant(0, VT);
Bill Wendling88103372009-01-30 21:37:17 +00003010 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00003011 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003012 }
Chris Lattnerefcddc32010-04-15 05:28:43 +00003013
3014 // fold (srl (shl x, c), c) -> (and x, cst2)
3015 if (N1C && N0.getOpcode() == ISD::SHL && N0.getOperand(1) == N1 &&
3016 N0.getValueSizeInBits() <= 64) {
3017 uint64_t ShAmt = N1C->getZExtValue()+64-N0.getValueSizeInBits();
3018 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0.getOperand(0),
3019 DAG.getConstant(~0ULL >> ShAmt, VT));
3020 }
3021
Scott Michelfdc40a02009-02-17 22:15:04 +00003022
Chris Lattner06afe072006-05-05 22:53:17 +00003023 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
3024 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
3025 // Shifting in all undef bits?
Owen Andersone50ed302009-08-10 22:56:29 +00003026 EVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003027 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Dale Johannesene8d72302009-02-06 23:05:02 +00003028 return DAG.getUNDEF(VT);
Chris Lattner06afe072006-05-05 22:53:17 +00003029
Evan Chenge5b51ac2010-04-17 06:13:15 +00003030 if (!LegalTypes || TLI.isTypeDesirableForOp(ISD::SRL, SmallVT)) {
3031 SDValue SmallShift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), SmallVT,
3032 N0.getOperand(0), N1);
3033 AddToWorkList(SmallShift.getNode());
3034 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, SmallShift);
3035 }
Chris Lattner06afe072006-05-05 22:53:17 +00003036 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003037
Chris Lattner3657ffe2006-10-12 20:23:19 +00003038 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
3039 // bit, which is unmodified by sra.
Bill Wendling88103372009-01-30 21:37:17 +00003040 if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00003041 if (N0.getOpcode() == ISD::SRA)
Bill Wendling88103372009-01-30 21:37:17 +00003042 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0), N1);
Chris Lattner3657ffe2006-10-12 20:23:19 +00003043 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003044
Chris Lattner350bec02006-04-02 06:11:11 +00003045 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
Scott Michelfdc40a02009-02-17 22:15:04 +00003046 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003047 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00003048 APInt KnownZero, KnownOne;
Dan Gohman5b870af2010-03-02 02:14:38 +00003049 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00003050 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00003051
Chris Lattner350bec02006-04-02 06:11:11 +00003052 // If any of the input bits are KnownOne, then the input couldn't be all
3053 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00003054 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003055
Chris Lattner350bec02006-04-02 06:11:11 +00003056 // If all of the bits input the to ctlz node are known to be zero, then
3057 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00003058 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00003059 if (UnknownBits == 0) return DAG.getConstant(1, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003060
Chris Lattner350bec02006-04-02 06:11:11 +00003061 // Otherwise, check to see if there is exactly one bit input to the ctlz.
Bill Wendling88103372009-01-30 21:37:17 +00003062 if ((UnknownBits & (UnknownBits - 1)) == 0) {
Chris Lattner350bec02006-04-02 06:11:11 +00003063 // Okay, we know that only that the single bit specified by UnknownBits
Bill Wendling88103372009-01-30 21:37:17 +00003064 // could be set on input to the CTLZ node. If this bit is set, the SRL
3065 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
3066 // to an SRL/XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00003067 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00003068 SDValue Op = N0.getOperand(0);
Bill Wendling88103372009-01-30 21:37:17 +00003069
Chris Lattner350bec02006-04-02 06:11:11 +00003070 if (ShAmt) {
Bill Wendling88103372009-01-30 21:37:17 +00003071 Op = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT, Op,
Duncan Sands92abc622009-01-31 15:50:11 +00003072 DAG.getConstant(ShAmt, getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003073 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00003074 }
Bill Wendling88103372009-01-30 21:37:17 +00003075
3076 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT,
3077 Op, DAG.getConstant(1, VT));
Chris Lattner350bec02006-04-02 06:11:11 +00003078 }
3079 }
Evan Chengeb9f8922008-08-30 02:03:58 +00003080
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003081 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003082 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003083 N1.getOperand(0).getOpcode() == ISD::AND &&
3084 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003085 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003086 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003087 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003088 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003089 APInt TruncC = N101C->getAPIntValue();
3090 TruncC.trunc(TruncVT.getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00003091 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003092 DAG.getNode(ISD::AND, N->getDebugLoc(),
Bill Wendling88103372009-01-30 21:37:17 +00003093 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003094 DAG.getNode(ISD::TRUNCATE,
3095 N->getDebugLoc(),
3096 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003097 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003098 }
3099 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003100
Chris Lattner61a4c072007-04-18 03:06:49 +00003101 // fold operands of srl based on knowledge that the low bits are not
3102 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00003103 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3104 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003105
Evan Cheng9ab2b982009-12-18 21:31:31 +00003106 if (N1C) {
3107 SDValue NewSRL = visitShiftByConstant(N, N1C->getZExtValue());
3108 if (NewSRL.getNode())
3109 return NewSRL;
3110 }
3111
3112 // Here is a common situation. We want to optimize:
3113 //
3114 // %a = ...
3115 // %b = and i32 %a, 2
3116 // %c = srl i32 %b, 1
3117 // brcond i32 %c ...
3118 //
3119 // into
3120 //
3121 // %a = ...
3122 // %b = and %a, 2
3123 // %c = setcc eq %b, 0
3124 // brcond %c ...
3125 //
3126 // However when after the source operand of SRL is optimized into AND, the SRL
3127 // itself may not be optimized further. Look for it and add the BRCOND into
3128 // the worklist.
Evan Chengd40d03e2010-01-06 19:38:29 +00003129 if (N->hasOneUse()) {
3130 SDNode *Use = *N->use_begin();
3131 if (Use->getOpcode() == ISD::BRCOND)
3132 AddToWorkList(Use);
3133 else if (Use->getOpcode() == ISD::TRUNCATE && Use->hasOneUse()) {
3134 // Also look pass the truncate.
3135 Use = *Use->use_begin();
3136 if (Use->getOpcode() == ISD::BRCOND)
3137 AddToWorkList(Use);
3138 }
3139 }
Evan Cheng9ab2b982009-12-18 21:31:31 +00003140
Evan Cheng07c4e102010-04-22 20:19:46 +00003141 return PromoteIntShiftOp(SDValue(N, 0));
Evan Cheng4c26e932010-04-19 19:29:22 +00003142}
3143
Dan Gohman475871a2008-07-27 21:46:04 +00003144SDValue DAGCombiner::visitCTLZ(SDNode *N) {
3145 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003146 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003147
3148 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00003149 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00003150 return DAG.getNode(ISD::CTLZ, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003151 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003152}
3153
Dan Gohman475871a2008-07-27 21:46:04 +00003154SDValue DAGCombiner::visitCTTZ(SDNode *N) {
3155 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003156 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003157
Nate Begeman1d4d4142005-09-01 00:19:25 +00003158 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00003159 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00003160 return DAG.getNode(ISD::CTTZ, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003161 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003162}
3163
Dan Gohman475871a2008-07-27 21:46:04 +00003164SDValue DAGCombiner::visitCTPOP(SDNode *N) {
3165 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003166 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003167
Nate Begeman1d4d4142005-09-01 00:19:25 +00003168 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00003169 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00003170 return DAG.getNode(ISD::CTPOP, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003171 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003172}
3173
Dan Gohman475871a2008-07-27 21:46:04 +00003174SDValue DAGCombiner::visitSELECT(SDNode *N) {
3175 SDValue N0 = N->getOperand(0);
3176 SDValue N1 = N->getOperand(1);
3177 SDValue N2 = N->getOperand(2);
Nate Begeman452d7be2005-09-16 00:54:12 +00003178 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3179 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
3180 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Owen Andersone50ed302009-08-10 22:56:29 +00003181 EVT VT = N->getValueType(0);
3182 EVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00003183
Bill Wendling34584e62009-01-30 22:02:18 +00003184 // fold (select C, X, X) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00003185 if (N1 == N2)
3186 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00003187 // fold (select true, X, Y) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00003188 if (N0C && !N0C->isNullValue())
3189 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00003190 // fold (select false, X, Y) -> Y
Nate Begeman452d7be2005-09-16 00:54:12 +00003191 if (N0C && N0C->isNullValue())
3192 return N2;
Bill Wendling34584e62009-01-30 22:02:18 +00003193 // fold (select C, 1, X) -> (or C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00003194 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Bill Wendling34584e62009-01-30 22:02:18 +00003195 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2);
3196 // fold (select C, 0, 1) -> (xor C, 1)
Bob Wilson67ba2232009-01-22 22:05:48 +00003197 if (VT.isInteger() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00003198 (VT0 == MVT::i1 ||
Bob Wilson67ba2232009-01-22 22:05:48 +00003199 (VT0.isInteger() &&
3200 TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00003201 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00003202 SDValue XORNode;
Evan Cheng571c4782007-08-18 05:57:05 +00003203 if (VT == VT0)
Bill Wendling34584e62009-01-30 22:02:18 +00003204 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT0,
3205 N0, DAG.getConstant(1, VT0));
3206 XORNode = DAG.getNode(ISD::XOR, N0.getDebugLoc(), VT0,
3207 N0, DAG.getConstant(1, VT0));
Gabor Greifba36cb52008-08-28 21:40:38 +00003208 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00003209 if (VT.bitsGT(VT0))
Bill Wendling34584e62009-01-30 22:02:18 +00003210 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, XORNode);
3211 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, XORNode);
Evan Cheng571c4782007-08-18 05:57:05 +00003212 }
Bill Wendling34584e62009-01-30 22:02:18 +00003213 // fold (select C, 0, X) -> (and (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00003214 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Bill Wendling7581bfa2009-01-30 23:03:19 +00003215 SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00003216 AddToWorkList(NOTNode.getNode());
Bill Wendling7581bfa2009-01-30 23:03:19 +00003217 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, NOTNode, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00003218 }
Bill Wendling34584e62009-01-30 22:02:18 +00003219 // fold (select C, X, 1) -> (or (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00003220 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00003221 SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00003222 AddToWorkList(NOTNode.getNode());
Bill Wendling7581bfa2009-01-30 23:03:19 +00003223 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, NOTNode, N1);
Nate Begeman452d7be2005-09-16 00:54:12 +00003224 }
Bill Wendling34584e62009-01-30 22:02:18 +00003225 // fold (select C, X, 0) -> (and C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00003226 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Bill Wendling34584e62009-01-30 22:02:18 +00003227 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1);
3228 // fold (select X, X, Y) -> (or X, Y)
3229 // fold (select X, 1, Y) -> (or X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00003230 if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1)))
Bill Wendling34584e62009-01-30 22:02:18 +00003231 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2);
3232 // fold (select X, Y, X) -> (and X, Y)
3233 // fold (select X, Y, 0) -> (and X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00003234 if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0)))
Bill Wendling34584e62009-01-30 22:02:18 +00003235 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00003236
Chris Lattner40c62d52005-10-18 06:04:22 +00003237 // If we can fold this based on the true/false value, do so.
3238 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00003239 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003240
Nate Begeman44728a72005-09-19 22:34:01 +00003241 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003242 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00003243 // FIXME:
Owen Anderson825b72b2009-08-11 20:47:22 +00003244 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
Nate Begeman750ac1b2006-02-01 07:19:44 +00003245 // having to say they don't support SELECT_CC on every type the DAG knows
3246 // about, since there is no way to mark an opcode illegal at all value types
Owen Anderson825b72b2009-08-11 20:47:22 +00003247 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) &&
Dan Gohman4ea48042009-08-02 16:19:38 +00003248 TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT))
Bill Wendling34584e62009-01-30 22:02:18 +00003249 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT,
3250 N0.getOperand(0), N0.getOperand(1),
Nate Begeman750ac1b2006-02-01 07:19:44 +00003251 N1, N2, N0.getOperand(2));
Chris Lattner600fec32009-03-11 05:08:08 +00003252 return SimplifySelect(N->getDebugLoc(), N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003253 }
Bill Wendling34584e62009-01-30 22:02:18 +00003254
Dan Gohman475871a2008-07-27 21:46:04 +00003255 return SDValue();
Nate Begeman452d7be2005-09-16 00:54:12 +00003256}
3257
Dan Gohman475871a2008-07-27 21:46:04 +00003258SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
3259 SDValue N0 = N->getOperand(0);
3260 SDValue N1 = N->getOperand(1);
3261 SDValue N2 = N->getOperand(2);
3262 SDValue N3 = N->getOperand(3);
3263 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00003264 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00003265
Nate Begeman44728a72005-09-19 22:34:01 +00003266 // fold select_cc lhs, rhs, x, x, cc -> x
3267 if (N2 == N3)
3268 return N2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003269
Chris Lattner5f42a242006-09-20 06:19:26 +00003270 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00003271 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00003272 N0, N1, CC, N->getDebugLoc(), false);
Gabor Greifba36cb52008-08-28 21:40:38 +00003273 if (SCC.getNode()) AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00003274
Gabor Greifba36cb52008-08-28 21:40:38 +00003275 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003276 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00003277 return N2; // cond always true -> true val
3278 else
3279 return N3; // cond always false -> false val
3280 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003281
Chris Lattner5f42a242006-09-20 06:19:26 +00003282 // Fold to a simpler select_cc
Gabor Greifba36cb52008-08-28 21:40:38 +00003283 if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC)
Scott Michelfdc40a02009-02-17 22:15:04 +00003284 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), N2.getValueType(),
3285 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
Chris Lattner5f42a242006-09-20 06:19:26 +00003286 SCC.getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00003287
Chris Lattner40c62d52005-10-18 06:04:22 +00003288 // If we can fold this based on the true/false value, do so.
3289 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00003290 return SDValue(N, 0); // Don't revisit N.
Scott Michelfdc40a02009-02-17 22:15:04 +00003291
Nate Begeman44728a72005-09-19 22:34:01 +00003292 // fold select_cc into other things, such as min/max/abs
Bill Wendling836ca7d2009-01-30 23:59:18 +00003293 return SimplifySelectCC(N->getDebugLoc(), N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00003294}
3295
Dan Gohman475871a2008-07-27 21:46:04 +00003296SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003297 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00003298 cast<CondCodeSDNode>(N->getOperand(2))->get(),
3299 N->getDebugLoc());
Nate Begeman452d7be2005-09-16 00:54:12 +00003300}
3301
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003302// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
Dan Gohman57fc82d2009-04-09 03:51:29 +00003303// "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))"
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003304// transformation. Returns true if extension are possible and the above
Scott Michelfdc40a02009-02-17 22:15:04 +00003305// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00003306static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003307 unsigned ExtOpc,
3308 SmallVector<SDNode*, 4> &ExtendNodes,
Dan Gohman79ce2762009-01-15 19:20:50 +00003309 const TargetLowering &TLI) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003310 bool HasCopyToRegUses = false;
3311 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00003312 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
3313 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003314 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00003315 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003316 if (User == N)
3317 continue;
Dan Gohman57fc82d2009-04-09 03:51:29 +00003318 if (UI.getUse().getResNo() != N0.getResNo())
3319 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003320 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
Dan Gohman57fc82d2009-04-09 03:51:29 +00003321 if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003322 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
3323 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
3324 // Sign bits will be lost after a zext.
3325 return false;
3326 bool Add = false;
3327 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003328 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003329 if (UseOp == N0)
3330 continue;
3331 if (!isa<ConstantSDNode>(UseOp))
3332 return false;
3333 Add = true;
3334 }
3335 if (Add)
3336 ExtendNodes.push_back(User);
Dan Gohman57fc82d2009-04-09 03:51:29 +00003337 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003338 }
Dan Gohman57fc82d2009-04-09 03:51:29 +00003339 // If truncates aren't free and there are users we can't
3340 // extend, it isn't worthwhile.
3341 if (!isTruncFree)
3342 return false;
3343 // Remember if this value is live-out.
3344 if (User->getOpcode() == ISD::CopyToReg)
3345 HasCopyToRegUses = true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003346 }
3347
3348 if (HasCopyToRegUses) {
3349 bool BothLiveOut = false;
3350 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
3351 UI != UE; ++UI) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00003352 SDUse &Use = UI.getUse();
3353 if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) {
3354 BothLiveOut = true;
3355 break;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003356 }
3357 }
3358 if (BothLiveOut)
3359 // Both unextended and extended values are live out. There had better be
3360 // good a reason for the transformation.
3361 return ExtendNodes.size();
3362 }
3363 return true;
3364}
3365
Dan Gohman475871a2008-07-27 21:46:04 +00003366SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
3367 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003368 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003369
Nate Begeman1d4d4142005-09-01 00:19:25 +00003370 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00003371 if (isa<ConstantSDNode>(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00003372 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003373
Nate Begeman1d4d4142005-09-01 00:19:25 +00003374 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00003375 // fold (sext (aext x)) -> (sext x)
3376 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Bill Wendling6ce610f2009-01-30 22:23:15 +00003377 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT,
3378 N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00003379
Chris Lattner22558872007-02-26 03:13:59 +00003380 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00003381 // fold (sext (truncate (load x))) -> (sext (smaller load x))
3382 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003383 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3384 if (NarrowLoad.getNode()) {
3385 if (NarrowLoad.getNode() != N0.getNode())
3386 CombineTo(N0.getNode(), NarrowLoad);
Dan Gohmanc7b34442009-04-27 02:00:55 +00003387 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00003388 }
Evan Chengc88138f2007-03-22 01:54:19 +00003389
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00003390 // See if the value being truncated is already sign extended. If so, just
3391 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00003392 SDValue Op = N0.getOperand(0);
Dan Gohmand1996362010-01-09 02:13:55 +00003393 unsigned OpBits = Op.getValueType().getScalarType().getSizeInBits();
3394 unsigned MidBits = N0.getValueType().getScalarType().getSizeInBits();
3395 unsigned DestBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00003396 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00003397
Chris Lattner22558872007-02-26 03:13:59 +00003398 if (OpBits == DestBits) {
3399 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
3400 // bits, it is already ready.
3401 if (NumSignBits > DestBits-MidBits)
3402 return Op;
3403 } else if (OpBits < DestBits) {
3404 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
3405 // bits, just sext from i32.
3406 if (NumSignBits > OpBits-MidBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00003407 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, Op);
Chris Lattner22558872007-02-26 03:13:59 +00003408 } else {
3409 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
3410 // bits, just truncate to i32.
3411 if (NumSignBits > OpBits-MidBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00003412 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00003413 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003414
Chris Lattner22558872007-02-26 03:13:59 +00003415 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sands25cf2272008-11-24 14:53:14 +00003416 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
3417 N0.getValueType())) {
Dan Gohmand1996362010-01-09 02:13:55 +00003418 if (OpBits < DestBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00003419 Op = DAG.getNode(ISD::ANY_EXTEND, N0.getDebugLoc(), VT, Op);
Dan Gohmand1996362010-01-09 02:13:55 +00003420 else if (OpBits > DestBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00003421 Op = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), VT, Op);
3422 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, Op,
Dan Gohmand1996362010-01-09 02:13:55 +00003423 DAG.getValueType(N0.getValueType()));
Chris Lattner22558872007-02-26 03:13:59 +00003424 }
Chris Lattner6007b842006-09-21 06:00:20 +00003425 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003426
Evan Cheng110dec22005-12-14 02:19:23 +00003427 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003428 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003429 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003430 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003431 bool DoXform = true;
3432 SmallVector<SDNode*, 4> SetCCs;
3433 if (!N0.hasOneUse())
3434 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
3435 if (DoXform) {
3436 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman57fc82d2009-04-09 03:51:29 +00003437 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
3438 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003439 LN0->getBasePtr(), LN0->getSrcValue(),
3440 LN0->getSrcValueOffset(),
3441 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00003442 LN0->isVolatile(), LN0->isNonTemporal(),
3443 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003444 CombineTo(N, ExtLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00003445 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3446 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003447 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling6ce610f2009-01-30 22:23:15 +00003448
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003449 // Extend SetCC uses if necessary.
3450 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3451 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00003452 SmallVector<SDValue, 4> Ops;
Bill Wendling6ce610f2009-01-30 22:23:15 +00003453
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003454 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00003455 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003456 if (SOp == Trunc)
3457 Ops.push_back(ExtLoad);
3458 else
Dan Gohman57fc82d2009-04-09 03:51:29 +00003459 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND,
3460 N->getDebugLoc(), VT, SOp));
Bill Wendling6ce610f2009-01-30 22:23:15 +00003461 }
3462
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003463 Ops.push_back(SetCC->getOperand(2));
Bill Wendling9729c5a2009-01-31 03:12:48 +00003464 CombineTo(SetCC, DAG.getNode(ISD::SETCC, N->getDebugLoc(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00003465 SetCC->getValueType(0),
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003466 &Ops[0], Ops.size()));
3467 }
Bill Wendling6ce610f2009-01-30 22:23:15 +00003468
Dan Gohman475871a2008-07-27 21:46:04 +00003469 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003470 }
Nate Begeman3df4d522005-10-12 20:40:40 +00003471 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003472
3473 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
3474 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003475 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3476 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003477 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00003478 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00003479 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00003480 TLI.isLoadExtLegal(ISD::SEXTLOAD, MemVT)) {
Bill Wendling6ce610f2009-01-30 22:23:15 +00003481 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
3482 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003483 LN0->getBasePtr(), LN0->getSrcValue(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00003484 LN0->getSrcValueOffset(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00003485 LN0->isVolatile(), LN0->isNonTemporal(),
3486 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003487 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00003488 CombineTo(N0.getNode(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00003489 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3490 N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003491 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003492 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003493 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003494 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003495
Chris Lattner20a35c32007-04-11 05:32:27 +00003496 if (N0.getOpcode() == ISD::SETCC) {
Chris Lattner2b7a2712009-07-08 00:31:33 +00003497 // sext(setcc) -> sext_in_reg(vsetcc) for vectors.
3498 if (VT.isVector() &&
3499 // We know that the # elements of the results is the same as the
3500 // # elements of the compare (and the # elements of the compare result
3501 // for that matter). Check to see that they are the same size. If so,
3502 // we know that the element size of the sext'd result matches the
3503 // element size of the compare operands.
3504 VT.getSizeInBits() == N0.getOperand(0).getValueType().getSizeInBits() &&
3505
3506 // Only do this before legalize for now.
3507 !LegalOperations) {
3508 return DAG.getVSetCC(N->getDebugLoc(), VT, N0.getOperand(0),
3509 N0.getOperand(1),
3510 cast<CondCodeSDNode>(N0.getOperand(2))->get());
3511 }
3512
3513 // sext(setcc x, y, cc) -> (select_cc x, y, -1, 0, cc)
Dan Gohmana7bcef12010-04-24 01:17:30 +00003514 unsigned ElementWidth = VT.getScalarType().getSizeInBits();
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003515 SDValue NegOne =
Dan Gohmana7bcef12010-04-24 01:17:30 +00003516 DAG.getConstant(APInt::getAllOnesValue(ElementWidth), VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003517 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00003518 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003519 NegOne, DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003520 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003521 if (SCC.getNode()) return SCC;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00003522 if (!LegalOperations ||
3523 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(VT)))
3524 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3525 DAG.getSetCC(N->getDebugLoc(),
3526 TLI.getSetCCResultType(VT),
3527 N0.getOperand(0), N0.getOperand(1),
3528 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
3529 NegOne, DAG.getConstant(0, VT));
Chris Lattner20a35c32007-04-11 05:32:27 +00003530 }
Chris Lattner2b7a2712009-07-08 00:31:33 +00003531
3532
Scott Michelfdc40a02009-02-17 22:15:04 +00003533
Dan Gohman8f0ad582008-04-28 16:58:24 +00003534 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sands25cf2272008-11-24 14:53:14 +00003535 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman187db7b2008-04-28 18:47:17 +00003536 DAG.SignBitIsZero(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00003537 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003538
Evan Cheng4c26e932010-04-19 19:29:22 +00003539 return PromoteExtend(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003540}
3541
Dan Gohman475871a2008-07-27 21:46:04 +00003542SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
3543 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003544 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003545
Nate Begeman1d4d4142005-09-01 00:19:25 +00003546 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00003547 if (isa<ConstantSDNode>(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00003548 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003549 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00003550 // fold (zext (aext x)) -> (zext x)
3551 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Bill Wendling6ce610f2009-01-30 22:23:15 +00003552 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT,
3553 N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00003554
Evan Chengc88138f2007-03-22 01:54:19 +00003555 // fold (zext (truncate (load x))) -> (zext (smaller load x))
3556 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00003557 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003558 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3559 if (NarrowLoad.getNode()) {
3560 if (NarrowLoad.getNode() != N0.getNode())
3561 CombineTo(N0.getNode(), NarrowLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00003562 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003563 }
Evan Chengc88138f2007-03-22 01:54:19 +00003564 }
3565
Chris Lattner6007b842006-09-21 06:00:20 +00003566 // fold (zext (truncate x)) -> (and x, mask)
3567 if (N0.getOpcode() == ISD::TRUNCATE &&
Evan Chengd40d03e2010-01-06 19:38:29 +00003568 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT)) &&
3569 (!TLI.isTruncateFree(N0.getOperand(0).getValueType(),
3570 N0.getValueType()) ||
3571 !TLI.isZExtFree(N0.getValueType(), VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00003572 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003573 if (Op.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003574 Op = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003575 } else if (Op.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003576 Op = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00003577 }
Dan Gohman87862e72009-12-11 21:31:27 +00003578 return DAG.getZeroExtendInReg(Op, N->getDebugLoc(),
3579 N0.getValueType().getScalarType());
Chris Lattner6007b842006-09-21 06:00:20 +00003580 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003581
Dan Gohman97121ba2009-04-08 00:15:30 +00003582 // Fold (zext (and (trunc x), cst)) -> (and x, cst),
3583 // if either of the casts is not free.
Chris Lattner111c2282006-09-21 06:14:31 +00003584 if (N0.getOpcode() == ISD::AND &&
3585 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00003586 N0.getOperand(1).getOpcode() == ISD::Constant &&
3587 (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
3588 N0.getValueType()) ||
3589 !TLI.isZExtFree(N0.getValueType(), VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00003590 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003591 if (X.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003592 X = DAG.getNode(ISD::ANY_EXTEND, X.getDebugLoc(), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003593 } else if (X.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003594 X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X);
Chris Lattner111c2282006-09-21 06:14:31 +00003595 }
Dan Gohman220a8232008-03-03 23:51:38 +00003596 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003597 Mask.zext(VT.getSizeInBits());
Bill Wendling6ce610f2009-01-30 22:23:15 +00003598 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
3599 X, DAG.getConstant(Mask, VT));
Chris Lattner111c2282006-09-21 06:14:31 +00003600 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003601
Evan Cheng110dec22005-12-14 02:19:23 +00003602 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003603 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003604 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003605 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003606 bool DoXform = true;
3607 SmallVector<SDNode*, 4> SetCCs;
3608 if (!N0.hasOneUse())
3609 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
3610 if (DoXform) {
3611 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendling6ce610f2009-01-30 22:23:15 +00003612 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT,
3613 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003614 LN0->getBasePtr(), LN0->getSrcValue(),
3615 LN0->getSrcValueOffset(),
3616 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00003617 LN0->isVolatile(), LN0->isNonTemporal(),
3618 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003619 CombineTo(N, ExtLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00003620 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3621 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003622 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling6ce610f2009-01-30 22:23:15 +00003623
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003624 // Extend SetCC uses if necessary.
3625 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3626 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00003627 SmallVector<SDValue, 4> Ops;
Bill Wendling6ce610f2009-01-30 22:23:15 +00003628
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003629 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00003630 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003631 if (SOp == Trunc)
3632 Ops.push_back(ExtLoad);
3633 else
Bill Wendling9729c5a2009-01-31 03:12:48 +00003634 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND,
3635 N->getDebugLoc(), VT, SOp));
Bill Wendling6ce610f2009-01-30 22:23:15 +00003636 }
3637
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003638 Ops.push_back(SetCC->getOperand(2));
Bill Wendling9729c5a2009-01-31 03:12:48 +00003639 CombineTo(SetCC, DAG.getNode(ISD::SETCC, N->getDebugLoc(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00003640 SetCC->getValueType(0),
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003641 &Ops[0], Ops.size()));
3642 }
Bill Wendling6ce610f2009-01-30 22:23:15 +00003643
Dan Gohman475871a2008-07-27 21:46:04 +00003644 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003645 }
Evan Cheng110dec22005-12-14 02:19:23 +00003646 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003647
3648 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
3649 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003650 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3651 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003652 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00003653 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00003654 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00003655 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT)) {
Bill Wendling6ce610f2009-01-30 22:23:15 +00003656 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT,
3657 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003658 LN0->getBasePtr(), LN0->getSrcValue(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00003659 LN0->getSrcValueOffset(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00003660 LN0->isVolatile(), LN0->isNonTemporal(),
3661 LN0->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003662 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00003663 CombineTo(N0.getNode(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00003664 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), N0.getValueType(),
3665 ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003666 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003667 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003668 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003669 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003670
Chris Lattner20a35c32007-04-11 05:32:27 +00003671 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3672 if (N0.getOpcode() == ISD::SETCC) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003673 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00003674 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Chris Lattner20a35c32007-04-11 05:32:27 +00003675 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003676 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003677 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003678 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003679
Evan Cheng9818c042009-12-15 03:00:32 +00003680 // (zext (shl (zext x), cst)) -> (shl (zext x), cst)
Evan Cheng99b653c2009-12-15 00:41:36 +00003681 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL) &&
Evan Cheng9818c042009-12-15 03:00:32 +00003682 isa<ConstantSDNode>(N0.getOperand(1)) &&
Evan Cheng99b653c2009-12-15 00:41:36 +00003683 N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND &&
3684 N0.hasOneUse()) {
Evan Cheng9818c042009-12-15 03:00:32 +00003685 if (N0.getOpcode() == ISD::SHL) {
3686 // If the original shl may be shifting out bits, do not perform this
3687 // transformation.
3688 unsigned ShAmt = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3689 unsigned KnownZeroBits = N0.getOperand(0).getValueType().getSizeInBits() -
3690 N0.getOperand(0).getOperand(0).getValueType().getSizeInBits();
3691 if (ShAmt > KnownZeroBits)
3692 return SDValue();
3693 }
Evan Cheng99b653c2009-12-15 00:41:36 +00003694 DebugLoc dl = N->getDebugLoc();
3695 return DAG.getNode(N0.getOpcode(), dl, VT,
3696 DAG.getNode(ISD::ZERO_EXTEND, dl, VT, N0.getOperand(0)),
Bill Wendling9f7c5c02010-01-05 22:39:10 +00003697 DAG.getNode(ISD::ZERO_EXTEND, dl,
3698 N0.getOperand(1).getValueType(),
3699 N0.getOperand(1)));
Evan Cheng99b653c2009-12-15 00:41:36 +00003700 }
3701
Evan Cheng4c26e932010-04-19 19:29:22 +00003702 return PromoteExtend(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003703}
3704
Dan Gohman475871a2008-07-27 21:46:04 +00003705SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
3706 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003707 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003708
Chris Lattner5ffc0662006-05-05 05:58:59 +00003709 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003710 if (isa<ConstantSDNode>(N0))
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003711 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, N0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00003712 // fold (aext (aext x)) -> (aext x)
3713 // fold (aext (zext x)) -> (zext x)
3714 // fold (aext (sext x)) -> (sext x)
3715 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3716 N0.getOpcode() == ISD::ZERO_EXTEND ||
3717 N0.getOpcode() == ISD::SIGN_EXTEND)
Bill Wendling683c9572009-01-30 22:27:33 +00003718 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00003719
Evan Chengc88138f2007-03-22 01:54:19 +00003720 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3721 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3722 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003723 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3724 if (NarrowLoad.getNode()) {
3725 if (NarrowLoad.getNode() != N0.getNode())
3726 CombineTo(N0.getNode(), NarrowLoad);
Bill Wendling683c9572009-01-30 22:27:33 +00003727 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003728 }
Evan Chengc88138f2007-03-22 01:54:19 +00003729 }
3730
Chris Lattner84750582006-09-20 06:29:17 +00003731 // fold (aext (truncate x))
3732 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00003733 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00003734 if (TruncOp.getValueType() == VT)
3735 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003736 if (TruncOp.getValueType().bitsGT(VT))
Bill Wendling683c9572009-01-30 22:27:33 +00003737 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, TruncOp);
3738 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, TruncOp);
Chris Lattner84750582006-09-20 06:29:17 +00003739 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003740
Dan Gohman97121ba2009-04-08 00:15:30 +00003741 // Fold (aext (and (trunc x), cst)) -> (and x, cst)
3742 // if the trunc is not free.
Chris Lattner0e4b9222006-09-21 06:40:43 +00003743 if (N0.getOpcode() == ISD::AND &&
3744 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00003745 N0.getOperand(1).getOpcode() == ISD::Constant &&
3746 !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
3747 N0.getValueType())) {
Dan Gohman475871a2008-07-27 21:46:04 +00003748 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003749 if (X.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003750 X = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003751 } else if (X.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003752 X = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, X);
Chris Lattner0e4b9222006-09-21 06:40:43 +00003753 }
Dan Gohman220a8232008-03-03 23:51:38 +00003754 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003755 Mask.zext(VT.getSizeInBits());
Bill Wendling683c9572009-01-30 22:27:33 +00003756 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
3757 X, DAG.getConstant(Mask, VT));
Chris Lattner0e4b9222006-09-21 06:40:43 +00003758 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003759
Chris Lattner5ffc0662006-05-05 05:58:59 +00003760 // fold (aext (load x)) -> (aext (truncate (extload x)))
Dan Gohman57fc82d2009-04-09 03:51:29 +00003761 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003762 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003763 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00003764 bool DoXform = true;
3765 SmallVector<SDNode*, 4> SetCCs;
3766 if (!N0.hasOneUse())
3767 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI);
3768 if (DoXform) {
3769 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3770 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT,
3771 LN0->getChain(),
3772 LN0->getBasePtr(), LN0->getSrcValue(),
3773 LN0->getSrcValueOffset(),
3774 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00003775 LN0->isVolatile(), LN0->isNonTemporal(),
3776 LN0->getAlignment());
Dan Gohman57fc82d2009-04-09 03:51:29 +00003777 CombineTo(N, ExtLoad);
3778 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3779 N0.getValueType(), ExtLoad);
3780 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
3781
3782 // Extend SetCC uses if necessary.
3783 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3784 SDNode *SetCC = SetCCs[i];
3785 SmallVector<SDValue, 4> Ops;
3786
3787 for (unsigned j = 0; j != 2; ++j) {
3788 SDValue SOp = SetCC->getOperand(j);
3789 if (SOp == Trunc)
3790 Ops.push_back(ExtLoad);
3791 else
3792 Ops.push_back(DAG.getNode(ISD::ANY_EXTEND,
3793 N->getDebugLoc(), VT, SOp));
3794 }
3795
3796 Ops.push_back(SetCC->getOperand(2));
3797 CombineTo(SetCC, DAG.getNode(ISD::SETCC, N->getDebugLoc(),
3798 SetCC->getValueType(0),
3799 &Ops[0], Ops.size()));
3800 }
3801
3802 return SDValue(N, 0); // Return N so it doesn't get rechecked!
3803 }
Chris Lattner5ffc0662006-05-05 05:58:59 +00003804 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003805
Chris Lattner5ffc0662006-05-05 05:58:59 +00003806 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3807 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3808 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003809 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003810 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003811 N0.hasOneUse()) {
3812 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00003813 EVT MemVT = LN0->getMemoryVT();
Bill Wendling683c9572009-01-30 22:27:33 +00003814 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), N->getDebugLoc(),
3815 VT, LN0->getChain(), LN0->getBasePtr(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003816 LN0->getSrcValue(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00003817 LN0->getSrcValueOffset(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00003818 LN0->isVolatile(), LN0->isNonTemporal(),
3819 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003820 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00003821 CombineTo(N0.getNode(),
Bill Wendling683c9572009-01-30 22:27:33 +00003822 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3823 N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00003824 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003825 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003826 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003827
Chris Lattner20a35c32007-04-11 05:32:27 +00003828 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3829 if (N0.getOpcode() == ISD::SETCC) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003830 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00003831 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003832 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003833 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003834 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003835 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003836 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003837
Evan Cheng4c26e932010-04-19 19:29:22 +00003838 return PromoteExtend(SDValue(N, 0));
Chris Lattner5ffc0662006-05-05 05:58:59 +00003839}
3840
Chris Lattner2b4c2792007-10-13 06:35:54 +00003841/// GetDemandedBits - See if the specified operand can be simplified with the
3842/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00003843/// simpler operand, otherwise return a null SDValue.
3844SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003845 switch (V.getOpcode()) {
3846 default: break;
3847 case ISD::OR:
3848 case ISD::XOR:
3849 // If the LHS or RHS don't contribute bits to the or, drop them.
3850 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3851 return V.getOperand(1);
3852 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3853 return V.getOperand(0);
3854 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003855 case ISD::SRL:
3856 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003857 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00003858 break;
3859 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3860 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003861 unsigned Amt = RHSC->getZExtValue();
Bill Wendling8509c902009-01-30 22:33:24 +00003862
Dan Gohmancc91d632009-01-03 19:22:06 +00003863 // Watch out for shift count overflow though.
3864 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003865 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00003866 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Bill Wendling8509c902009-01-30 22:33:24 +00003867 if (SimplifyLHS.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00003868 return DAG.getNode(ISD::SRL, V.getDebugLoc(), V.getValueType(),
Chris Lattnere33544c2007-10-13 06:58:48 +00003869 SimplifyLHS, V.getOperand(1));
Chris Lattnere33544c2007-10-13 06:58:48 +00003870 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003871 }
Dan Gohman475871a2008-07-27 21:46:04 +00003872 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00003873}
3874
Evan Chengc88138f2007-03-22 01:54:19 +00003875/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3876/// bits and then truncated to a narrower type and where N is a multiple
3877/// of number of bits of the narrower type, transform it to a narrower load
3878/// from address + N / num of bits of new type. If the result is to be
3879/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00003880SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00003881 unsigned Opc = N->getOpcode();
3882 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00003883 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003884 EVT VT = N->getValueType(0);
3885 EVT ExtVT = VT;
Evan Chengc88138f2007-03-22 01:54:19 +00003886
Dan Gohman7f8613e2008-08-14 20:04:46 +00003887 // This transformation isn't valid for vector loads.
3888 if (VT.isVector())
3889 return SDValue();
3890
Dan Gohmand1996362010-01-09 02:13:55 +00003891 // Special case: SIGN_EXTEND_INREG is basically truncating to ExtVT then
Evan Chenge177e302007-03-23 22:13:36 +00003892 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003893 if (Opc == ISD::SIGN_EXTEND_INREG) {
3894 ExtType = ISD::SEXTLOAD;
Owen Andersone50ed302009-08-10 22:56:29 +00003895 ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT();
3896 if (LegalOperations && !TLI.isLoadExtLegal(ISD::SEXTLOAD, ExtVT))
Dan Gohman475871a2008-07-27 21:46:04 +00003897 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003898 }
3899
Owen Andersone50ed302009-08-10 22:56:29 +00003900 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Chengc88138f2007-03-22 01:54:19 +00003901 unsigned ShAmt = 0;
Eli Friedman5aba5c02009-08-23 00:14:19 +00003902 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse() && ExtVT.isRound()) {
Evan Chengc88138f2007-03-22 01:54:19 +00003903 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003904 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003905 // Is the shift amount a multiple of size of VT?
3906 if ((ShAmt & (EVTBits-1)) == 0) {
3907 N0 = N0.getOperand(0);
Eli Friedmand68eea22009-08-19 08:46:10 +00003908 // Is the load width a multiple of size of VT?
3909 if ((N0.getValueType().getSizeInBits() & (EVTBits-1)) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00003910 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003911 }
3912 }
3913 }
3914
Duncan Sandsad205a72008-06-16 08:14:38 +00003915 // Do not generate loads of non-round integer types since these can
3916 // be expensive (and would be wrong if the type is not byte sized).
Owen Andersone50ed302009-08-10 22:56:29 +00003917 if (isa<LoadSDNode>(N0) && N0.hasOneUse() && ExtVT.isRound() &&
Chris Lattner6dc86852010-04-15 05:40:59 +00003918 cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() >= EVTBits &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003919 // Do not change the width of a volatile load.
3920 !cast<LoadSDNode>(N0)->isVolatile()) {
Evan Chengc88138f2007-03-22 01:54:19 +00003921 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00003922 EVT PtrType = N0.getOperand(1).getValueType();
Bill Wendling8509c902009-01-30 22:33:24 +00003923
Evan Chengdae54ce2007-03-24 00:02:43 +00003924 // For big endian targets, we need to adjust the offset to the pointer to
3925 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003926 if (TLI.isBigEndian()) {
Dan Gohman75dcf082008-07-31 00:50:31 +00003927 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
Owen Andersone50ed302009-08-10 22:56:29 +00003928 unsigned EVTStoreBits = ExtVT.getStoreSizeInBits();
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003929 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3930 }
Bill Wendling8509c902009-01-30 22:33:24 +00003931
Evan Chengdae54ce2007-03-24 00:02:43 +00003932 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003933 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Bill Wendling9729c5a2009-01-31 03:12:48 +00003934 SDValue NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(),
Bill Wendling8509c902009-01-30 22:33:24 +00003935 PtrType, LN0->getBasePtr(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003936 DAG.getConstant(PtrOff, PtrType));
Gabor Greifba36cb52008-08-28 21:40:38 +00003937 AddToWorkList(NewPtr.getNode());
Bill Wendling8509c902009-01-30 22:33:24 +00003938
Dan Gohman475871a2008-07-27 21:46:04 +00003939 SDValue Load = (ExtType == ISD::NON_EXTLOAD)
Bill Wendling8509c902009-01-30 22:33:24 +00003940 ? DAG.getLoad(VT, N0.getDebugLoc(), LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003941 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
David Greene1e559442010-02-15 17:00:31 +00003942 LN0->isVolatile(), LN0->isNonTemporal(), NewAlign)
Bill Wendling8509c902009-01-30 22:33:24 +00003943 : DAG.getExtLoad(ExtType, N0.getDebugLoc(), VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003944 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
David Greene1e559442010-02-15 17:00:31 +00003945 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
3946 NewAlign);
Bill Wendling8509c902009-01-30 22:33:24 +00003947
Dan Gohman764fd0c2009-01-21 15:17:51 +00003948 // Replace the old load's chain with the new load's chain.
3949 WorkListRemover DeadNodes(*this);
3950 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3951 &DeadNodes);
Bill Wendling8509c902009-01-30 22:33:24 +00003952
Dan Gohman764fd0c2009-01-21 15:17:51 +00003953 // Return the new loaded value.
3954 return Load;
Evan Chengc88138f2007-03-22 01:54:19 +00003955 }
3956
Dan Gohman475871a2008-07-27 21:46:04 +00003957 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003958}
3959
Dan Gohman475871a2008-07-27 21:46:04 +00003960SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
3961 SDValue N0 = N->getOperand(0);
3962 SDValue N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00003963 EVT VT = N->getValueType(0);
3964 EVT EVT = cast<VTSDNode>(N1)->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00003965 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmand1996362010-01-09 02:13:55 +00003966 unsigned EVTBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003967
Nate Begeman1d4d4142005-09-01 00:19:25 +00003968 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003969 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Bill Wendling8509c902009-01-30 22:33:24 +00003970 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00003971
Chris Lattner541a24f2006-05-06 22:43:44 +00003972 // If the input is already sign extended, just drop the extension.
Dan Gohman87862e72009-12-11 21:31:27 +00003973 if (DAG.ComputeNumSignBits(N0) >= VTBits-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003974 return N0;
Scott Michelfdc40a02009-02-17 22:15:04 +00003975
Nate Begeman646d7e22005-09-02 21:18:40 +00003976 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3977 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00003978 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Bill Wendling8509c902009-01-30 22:33:24 +00003979 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
3980 N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003981 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003982
Dan Gohman75dcf082008-07-31 00:50:31 +00003983 // fold (sext_in_reg (sext x)) -> (sext x)
3984 // fold (sext_in_reg (aext x)) -> (sext x)
3985 // if x is small enough.
3986 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
3987 SDValue N00 = N0.getOperand(0);
Evan Cheng003d7c42010-04-16 22:26:19 +00003988 if (N00.getValueType().getScalarType().getSizeInBits() <= EVTBits &&
3989 (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT)))
Bill Wendling8509c902009-01-30 22:33:24 +00003990 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N00, N1);
Dan Gohman75dcf082008-07-31 00:50:31 +00003991 }
3992
Chris Lattner95a5e052007-04-17 19:03:21 +00003993 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003994 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003995 return DAG.getZeroExtendInReg(N0, N->getDebugLoc(), EVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003996
Chris Lattner95a5e052007-04-17 19:03:21 +00003997 // fold operands of sext_in_reg based on knowledge that the top bits are not
3998 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00003999 if (SimplifyDemandedBits(SDValue(N, 0)))
4000 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004001
Evan Chengc88138f2007-03-22 01:54:19 +00004002 // fold (sext_in_reg (load x)) -> (smaller sextload x)
4003 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00004004 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00004005 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00004006 return NarrowLoad;
4007
Bill Wendling8509c902009-01-30 22:33:24 +00004008 // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24)
4009 // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible.
Chris Lattner4b37e872006-05-08 21:18:59 +00004010 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
4011 if (N0.getOpcode() == ISD::SRL) {
4012 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman87862e72009-12-11 21:31:27 +00004013 if (ShAmt->getZExtValue()+EVTBits <= VTBits) {
Chris Lattner4b37e872006-05-08 21:18:59 +00004014 // We can turn this into an SRA iff the input to the SRL is already sign
4015 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00004016 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohman87862e72009-12-11 21:31:27 +00004017 if (VTBits-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Bill Wendling8509c902009-01-30 22:33:24 +00004018 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT,
4019 N0.getOperand(0), N0.getOperand(1));
Chris Lattner4b37e872006-05-08 21:18:59 +00004020 }
4021 }
Evan Chengc88138f2007-03-22 01:54:19 +00004022
Nate Begemanded49632005-10-13 03:11:28 +00004023 // fold (sext_inreg (extload x)) -> (sextload x)
Scott Michelfdc40a02009-02-17 22:15:04 +00004024 if (ISD::isEXTLoad(N0.getNode()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004025 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004026 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004027 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004028 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00004029 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendling8509c902009-01-30 22:33:24 +00004030 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
4031 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004032 LN0->getBasePtr(), LN0->getSrcValue(),
4033 LN0->getSrcValueOffset(), EVT,
David Greene1e559442010-02-15 17:00:31 +00004034 LN0->isVolatile(), LN0->isNonTemporal(),
4035 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00004036 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004037 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004038 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00004039 }
4040 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00004041 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00004042 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004043 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004044 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004045 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00004046 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendling8509c902009-01-30 22:33:24 +00004047 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
4048 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004049 LN0->getBasePtr(), LN0->getSrcValue(),
4050 LN0->getSrcValueOffset(), EVT,
David Greene1e559442010-02-15 17:00:31 +00004051 LN0->isVolatile(), LN0->isNonTemporal(),
4052 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00004053 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004054 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004055 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00004056 }
Dan Gohman475871a2008-07-27 21:46:04 +00004057 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004058}
4059
Dan Gohman475871a2008-07-27 21:46:04 +00004060SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
4061 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004062 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004063
4064 // noop truncate
4065 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00004066 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00004067 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00004068 if (isa<ConstantSDNode>(N0))
Bill Wendling67a67682009-01-30 22:44:24 +00004069 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004070 // fold (truncate (truncate x)) -> (truncate x)
4071 if (N0.getOpcode() == ISD::TRUNCATE)
Bill Wendling67a67682009-01-30 22:44:24 +00004072 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00004073 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattner7f893c02010-04-07 18:13:33 +00004074 if (N0.getOpcode() == ISD::ZERO_EXTEND ||
4075 N0.getOpcode() == ISD::SIGN_EXTEND ||
Chris Lattnerb72773b2006-05-05 22:56:26 +00004076 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004077 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00004078 // if the source is smaller than the dest, we still need an extend
Bill Wendling67a67682009-01-30 22:44:24 +00004079 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
4080 N0.getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00004081 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00004082 // if the source is larger than the dest, than we just need the truncate
Bill Wendling67a67682009-01-30 22:44:24 +00004083 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00004084 else
4085 // if the source and dest are the same type, we can drop both the extend
Evan Chengd40d03e2010-01-06 19:38:29 +00004086 // and the truncate.
Nate Begeman83e75ec2005-09-06 04:43:02 +00004087 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004088 }
Evan Cheng007b69e2007-03-21 20:14:05 +00004089
Chris Lattner2b4c2792007-10-13 06:35:54 +00004090 // See if we can simplify the input to this truncate through knowledge that
4091 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
4092 // -> trunc y
Dan Gohman475871a2008-07-27 21:46:04 +00004093 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004094 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004095 VT.getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00004096 if (Shorter.getNode())
Bill Wendling67a67682009-01-30 22:44:24 +00004097 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Shorter);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004098
Nate Begeman3df4d522005-10-12 20:40:40 +00004099 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00004100 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chenge5b51ac2010-04-17 06:13:15 +00004101 if (!LegalTypes || TLI.isTypeDesirableForOp(N0.getOpcode(), VT))
4102 return ReduceLoadWidth(N);
4103 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004104}
4105
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004106static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004107 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004108 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00004109 return Elt.getNode();
4110 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004111}
4112
4113/// CombineConsecutiveLoads - build_pair (load, load) -> load
Scott Michelfdc40a02009-02-17 22:15:04 +00004114/// if load locations are consecutive.
Owen Andersone50ed302009-08-10 22:56:29 +00004115SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004116 assert(N->getOpcode() == ISD::BUILD_PAIR);
4117
Nate Begemanabc01992009-06-05 21:37:30 +00004118 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0));
4119 LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1));
4120 if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004121 return SDValue();
Owen Andersone50ed302009-08-10 22:56:29 +00004122 EVT LD1VT = LD1->getValueType(0);
Bill Wendling67a67682009-01-30 22:44:24 +00004123
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004124 if (ISD::isNON_EXTLoad(LD2) &&
4125 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004126 // If both are volatile this would reduce the number of volatile loads.
4127 // If one is volatile it might be ok, but play conservative and bail out.
Nate Begemanabc01992009-06-05 21:37:30 +00004128 !LD1->isVolatile() &&
4129 !LD2->isVolatile() &&
Evan Cheng64fa4a92009-12-09 01:36:00 +00004130 DAG.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1)) {
Nate Begemanabc01992009-06-05 21:37:30 +00004131 unsigned Align = LD1->getAlignment();
Dan Gohman6448d912008-09-04 15:39:15 +00004132 unsigned NewAlign = TLI.getTargetData()->
Owen Anderson23b9b192009-08-12 00:36:31 +00004133 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Bill Wendling67a67682009-01-30 22:44:24 +00004134
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004135 if (NewAlign <= Align &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004136 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Nate Begemanabc01992009-06-05 21:37:30 +00004137 return DAG.getLoad(VT, N->getDebugLoc(), LD1->getChain(),
4138 LD1->getBasePtr(), LD1->getSrcValue(),
David Greene1e559442010-02-15 17:00:31 +00004139 LD1->getSrcValueOffset(), false, false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004140 }
Bill Wendling67a67682009-01-30 22:44:24 +00004141
Dan Gohman475871a2008-07-27 21:46:04 +00004142 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004143}
4144
Dan Gohman475871a2008-07-27 21:46:04 +00004145SDValue DAGCombiner::visitBIT_CONVERT(SDNode *N) {
4146 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004147 EVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00004148
Dan Gohman7f321562007-06-25 16:23:39 +00004149 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
4150 // Only do this before legalize, since afterward the target may be depending
4151 // on the bitconvert.
4152 // First check to see if this is all constant.
Duncan Sands25cf2272008-11-24 14:53:14 +00004153 if (!LegalTypes &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004154 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004155 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00004156 bool isSimple = true;
4157 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
4158 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
4159 N0.getOperand(i).getOpcode() != ISD::Constant &&
4160 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004161 isSimple = false;
Dan Gohman7f321562007-06-25 16:23:39 +00004162 break;
4163 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004164
Owen Andersone50ed302009-08-10 22:56:29 +00004165 EVT DestEltVT = N->getValueType(0).getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004166 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00004167 "Element type of vector ValueType must not be vector!");
Bill Wendling67a67682009-01-30 22:44:24 +00004168 if (isSimple)
Gabor Greifba36cb52008-08-28 21:40:38 +00004169 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00004170 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004171
Dan Gohman3dd168d2008-09-05 01:58:21 +00004172 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00004173 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Bill Wendling67a67682009-01-30 22:44:24 +00004174 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, N0);
Dan Gohmana407ca12009-08-10 23:15:10 +00004175 if (Res.getNode() != N) {
4176 if (!LegalOperations ||
4177 TLI.isOperationLegal(Res.getNode()->getOpcode(), VT))
4178 return Res;
4179
4180 // Folding it resulted in an illegal node, and it's too late to
4181 // do that. Clean up the old node and forego the transformation.
4182 // Ideally this won't happen very often, because instcombine
4183 // and the earlier dagcombine runs (where illegal nodes are
4184 // permitted) should have folded most of them already.
4185 DAG.DeleteNode(Res.getNode());
4186 }
Chris Lattner94683772005-12-23 05:30:37 +00004187 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004188
Bill Wendling67a67682009-01-30 22:44:24 +00004189 // (conv (conv x, t1), t2) -> (conv x, t2)
4190 if (N0.getOpcode() == ISD::BIT_CONVERT)
4191 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT,
4192 N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00004193
Chris Lattner57104102005-12-23 05:44:41 +00004194 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00004195 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00004196 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004197 // Do not change the width of a volatile load.
4198 !cast<LoadSDNode>(N0)->isVolatile() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004199 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00004200 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman6448d912008-09-04 15:39:15 +00004201 unsigned Align = TLI.getTargetData()->
Owen Anderson23b9b192009-08-12 00:36:31 +00004202 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Evan Cheng59d5b682007-05-07 21:27:48 +00004203 unsigned OrigAlign = LN0->getAlignment();
Bill Wendling67a67682009-01-30 22:44:24 +00004204
Evan Cheng59d5b682007-05-07 21:27:48 +00004205 if (Align <= OrigAlign) {
Bill Wendling67a67682009-01-30 22:44:24 +00004206 SDValue Load = DAG.getLoad(VT, N->getDebugLoc(), LN0->getChain(),
4207 LN0->getBasePtr(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004208 LN0->getSrcValue(), LN0->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00004209 LN0->isVolatile(), LN0->isNonTemporal(),
4210 OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00004211 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00004212 CombineTo(N0.getNode(),
Bill Wendling67a67682009-01-30 22:44:24 +00004213 DAG.getNode(ISD::BIT_CONVERT, N0.getDebugLoc(),
4214 N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00004215 Load.getValue(1));
4216 return Load;
4217 }
Chris Lattner57104102005-12-23 05:44:41 +00004218 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004219
Bill Wendling67a67682009-01-30 22:44:24 +00004220 // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
4221 // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
Chris Lattner3bd39d42008-01-27 17:42:27 +00004222 // This often reduces constant pool loads.
4223 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004224 N0.getNode()->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Bill Wendling67a67682009-01-30 22:44:24 +00004225 SDValue NewConv = DAG.getNode(ISD::BIT_CONVERT, N0.getDebugLoc(), VT,
4226 N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00004227 AddToWorkList(NewConv.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004228
Duncan Sands83ec4b62008-06-06 12:08:01 +00004229 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004230 if (N0.getOpcode() == ISD::FNEG)
Bill Wendling67a67682009-01-30 22:44:24 +00004231 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT,
4232 NewConv, DAG.getConstant(SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00004233 assert(N0.getOpcode() == ISD::FABS);
Bill Wendling67a67682009-01-30 22:44:24 +00004234 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
4235 NewConv, DAG.getConstant(~SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00004236 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004237
Bill Wendling67a67682009-01-30 22:44:24 +00004238 // fold (bitconvert (fcopysign cst, x)) ->
4239 // (or (and (bitconvert x), sign), (and cst, (not sign)))
4240 // Note that we don't handle (copysign x, cst) because this can always be
4241 // folded to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00004242 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00004243 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004244 VT.isInteger() && !VT.isVector()) {
4245 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Owen Anderson23b9b192009-08-12 00:36:31 +00004246 EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth);
Chris Lattner2392ae72010-04-15 04:48:01 +00004247 if (isTypeLegal(IntXVT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004248 SDValue X = DAG.getNode(ISD::BIT_CONVERT, N0.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00004249 IntXVT, N0.getOperand(1));
Duncan Sands25cf2272008-11-24 14:53:14 +00004250 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004251
Duncan Sands25cf2272008-11-24 14:53:14 +00004252 // If X has a different width than the result/lhs, sext it or truncate it.
4253 unsigned VTWidth = VT.getSizeInBits();
4254 if (OrigXWidth < VTWidth) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004255 X = DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00004256 AddToWorkList(X.getNode());
4257 } else if (OrigXWidth > VTWidth) {
4258 // To get the sign bit in the right place, we have to shift it right
4259 // before truncating.
Bill Wendling9729c5a2009-01-31 03:12:48 +00004260 X = DAG.getNode(ISD::SRL, X.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00004261 X.getValueType(), X,
Duncan Sands25cf2272008-11-24 14:53:14 +00004262 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
4263 AddToWorkList(X.getNode());
Bill Wendling9729c5a2009-01-31 03:12:48 +00004264 X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00004265 AddToWorkList(X.getNode());
4266 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004267
Duncan Sands25cf2272008-11-24 14:53:14 +00004268 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Bill Wendling9729c5a2009-01-31 03:12:48 +00004269 X = DAG.getNode(ISD::AND, X.getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00004270 X, DAG.getConstant(SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00004271 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004272
Bill Wendling9729c5a2009-01-31 03:12:48 +00004273 SDValue Cst = DAG.getNode(ISD::BIT_CONVERT, N0.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00004274 VT, N0.getOperand(0));
Bill Wendling9729c5a2009-01-31 03:12:48 +00004275 Cst = DAG.getNode(ISD::AND, Cst.getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00004276 Cst, DAG.getConstant(~SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00004277 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004278
Bill Wendling67a67682009-01-30 22:44:24 +00004279 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, X, Cst);
Duncan Sands25cf2272008-11-24 14:53:14 +00004280 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00004281 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004282
Scott Michelfdc40a02009-02-17 22:15:04 +00004283 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004284 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004285 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
4286 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004287 return CombineLD;
4288 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004289
Dan Gohman475871a2008-07-27 21:46:04 +00004290 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00004291}
4292
Dan Gohman475871a2008-07-27 21:46:04 +00004293SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00004294 EVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004295 return CombineConsecutiveLoads(N, VT);
4296}
4297
Dan Gohman7f321562007-06-25 16:23:39 +00004298/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Scott Michelfdc40a02009-02-17 22:15:04 +00004299/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
Chris Lattner6258fb22006-04-02 02:53:43 +00004300/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00004301SDValue DAGCombiner::
Owen Andersone50ed302009-08-10 22:56:29 +00004302ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) {
4303 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00004304
Chris Lattner6258fb22006-04-02 02:53:43 +00004305 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00004306 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004307
Duncan Sands83ec4b62008-06-06 12:08:01 +00004308 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
4309 unsigned DstBitSize = DstEltVT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004310
Chris Lattner6258fb22006-04-02 02:53:43 +00004311 // If this is a conversion of N elements of one type to N elements of another
4312 // type, convert each element. This handles FP<->INT cases.
4313 if (SrcBitSize == DstBitSize) {
Dan Gohman475871a2008-07-27 21:46:04 +00004314 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004315 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Bob Wilsonb1303d02009-04-13 22:05:19 +00004316 SDValue Op = BV->getOperand(i);
4317 // If the vector element type is not legal, the BUILD_VECTOR operands
4318 // are promoted and implicitly truncated. Make that explicit here.
Bob Wilsonc8851652009-04-20 17:27:09 +00004319 if (Op.getValueType() != SrcEltVT)
4320 Op = DAG.getNode(ISD::TRUNCATE, BV->getDebugLoc(), SrcEltVT, Op);
Bill Wendlingfc4b6772009-02-01 11:19:36 +00004321 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, BV->getDebugLoc(),
Bob Wilsonb1303d02009-04-13 22:05:19 +00004322 DstEltVT, Op));
Gabor Greifba36cb52008-08-28 21:40:38 +00004323 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00004324 }
Owen Anderson23b9b192009-08-12 00:36:31 +00004325 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004326 BV->getValueType(0).getVectorNumElements());
Evan Chenga87008d2009-02-25 22:49:59 +00004327 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
4328 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00004329 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004330
Chris Lattner6258fb22006-04-02 02:53:43 +00004331 // Otherwise, we're growing or shrinking the elements. To avoid having to
4332 // handle annoying details of growing/shrinking FP values, we convert them to
4333 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004334 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00004335 // Convert the input float vector to a int vector where the elements are the
4336 // same sizes.
Owen Anderson825b72b2009-08-11 20:47:22 +00004337 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00004338 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00004339 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00004340 SrcEltVT = IntVT;
4341 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004342
Chris Lattner6258fb22006-04-02 02:53:43 +00004343 // Now we know the input is an integer vector. If the output is a FP type,
4344 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004345 if (DstEltVT.isFloatingPoint()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004346 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00004347 EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00004348 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00004349
Chris Lattner6258fb22006-04-02 02:53:43 +00004350 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00004351 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00004352 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004353
Chris Lattner6258fb22006-04-02 02:53:43 +00004354 // Okay, we know the src/dst types are both integers of differing types.
4355 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004356 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00004357 if (SrcBitSize < DstBitSize) {
4358 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
Scott Michelfdc40a02009-02-17 22:15:04 +00004359
Dan Gohman475871a2008-07-27 21:46:04 +00004360 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004361 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00004362 i += NumInputsPerOutput) {
4363 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00004364 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00004365 bool EltIsUndef = true;
4366 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
4367 // Shift the previously computed bits over.
4368 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00004369 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00004370 if (Op.getOpcode() == ISD::UNDEF) continue;
4371 EltIsUndef = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00004372
Dan Gohman58c25872010-04-12 02:24:01 +00004373 NewBits |= APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).
4374 zextOrTrunc(SrcBitSize).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00004375 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004376
Chris Lattner6258fb22006-04-02 02:53:43 +00004377 if (EltIsUndef)
Dale Johannesene8d72302009-02-06 23:05:02 +00004378 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00004379 else
4380 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
4381 }
4382
Owen Anderson23b9b192009-08-12 00:36:31 +00004383 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size());
Evan Chenga87008d2009-02-25 22:49:59 +00004384 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
4385 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00004386 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004387
Chris Lattner6258fb22006-04-02 02:53:43 +00004388 // Finally, this must be the case where we are shrinking elements: each input
4389 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00004390 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00004391 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Owen Anderson23b9b192009-08-12 00:36:31 +00004392 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
4393 NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00004394 SmallVector<SDValue, 8> Ops;
Bill Wendlingb0162f52009-01-30 22:53:48 +00004395
Dan Gohman7f321562007-06-25 16:23:39 +00004396 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00004397 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
4398 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
Dale Johannesene8d72302009-02-06 23:05:02 +00004399 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00004400 continue;
4401 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00004402
Bob Wilsonb1303d02009-04-13 22:05:19 +00004403 APInt OpVal = APInt(cast<ConstantSDNode>(BV->getOperand(i))->
4404 getAPIntValue()).zextOrTrunc(SrcBitSize);
Bill Wendlingb0162f52009-01-30 22:53:48 +00004405
Chris Lattner6258fb22006-04-02 02:53:43 +00004406 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohman220a8232008-03-03 23:51:38 +00004407 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00004408 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohman220a8232008-03-03 23:51:38 +00004409 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00004410 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
Bill Wendlingb0162f52009-01-30 22:53:48 +00004411 return DAG.getNode(ISD::SCALAR_TO_VECTOR, BV->getDebugLoc(), VT,
4412 Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00004413 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00004414 }
4415
4416 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00004417 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00004418 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
4419 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00004420
Evan Chenga87008d2009-02-25 22:49:59 +00004421 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
4422 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00004423}
4424
Dan Gohman475871a2008-07-27 21:46:04 +00004425SDValue DAGCombiner::visitFADD(SDNode *N) {
4426 SDValue N0 = N->getOperand(0);
4427 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00004428 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4429 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00004430 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004431
Dan Gohman7f321562007-06-25 16:23:39 +00004432 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00004433 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004434 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00004435 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00004436 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004437
Bill Wendlingb0162f52009-01-30 22:53:48 +00004438 // fold (fadd c1, c2) -> (fadd c1, c2)
Owen Anderson825b72b2009-08-11 20:47:22 +00004439 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlingb0162f52009-01-30 22:53:48 +00004440 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00004441 // canonicalize constant to RHS
4442 if (N0CFP && !N1CFP)
Bill Wendlingb0162f52009-01-30 22:53:48 +00004443 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N1, N0);
4444 // fold (fadd A, 0) -> A
Dan Gohman760f86f2009-01-22 21:58:43 +00004445 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
4446 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00004447 // fold (fadd A, (fneg B)) -> (fsub A, B)
Duncan Sands25cf2272008-11-24 14:53:14 +00004448 if (isNegatibleForFree(N1, LegalOperations) == 2)
Bill Wendlingb0162f52009-01-30 22:53:48 +00004449 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00004450 GetNegatedExpression(N1, DAG, LegalOperations));
Bill Wendlingb0162f52009-01-30 22:53:48 +00004451 // fold (fadd (fneg A), B) -> (fsub B, A)
Duncan Sands25cf2272008-11-24 14:53:14 +00004452 if (isNegatibleForFree(N0, LegalOperations) == 2)
Bill Wendlingb0162f52009-01-30 22:53:48 +00004453 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N1,
Duncan Sands25cf2272008-11-24 14:53:14 +00004454 GetNegatedExpression(N0, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00004455
Chris Lattnerddae4bd2007-01-08 23:04:05 +00004456 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
4457 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004458 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Bill Wendlingb0162f52009-01-30 22:53:48 +00004459 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0.getOperand(0),
Bill Wendlingfc4b6772009-02-01 11:19:36 +00004460 DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
4461 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004462
Dan Gohman475871a2008-07-27 21:46:04 +00004463 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00004464}
4465
Dan Gohman475871a2008-07-27 21:46:04 +00004466SDValue DAGCombiner::visitFSUB(SDNode *N) {
4467 SDValue N0 = N->getOperand(0);
4468 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00004469 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4470 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00004471 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004472
Dan Gohman7f321562007-06-25 16:23:39 +00004473 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00004474 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004475 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00004476 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00004477 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004478
Nate Begemana0e221d2005-10-18 00:28:13 +00004479 // fold (fsub c1, c2) -> c1-c2
Owen Anderson825b72b2009-08-11 20:47:22 +00004480 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlingfc4b6772009-02-01 11:19:36 +00004481 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0, N1);
Bill Wendlingb0162f52009-01-30 22:53:48 +00004482 // fold (fsub A, 0) -> A
Dan Gohmana90c8e62009-01-23 19:10:37 +00004483 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
4484 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00004485 // fold (fsub 0, B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00004486 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Duncan Sands25cf2272008-11-24 14:53:14 +00004487 if (isNegatibleForFree(N1, LegalOperations))
4488 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman760f86f2009-01-22 21:58:43 +00004489 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendlingb0162f52009-01-30 22:53:48 +00004490 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00004491 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00004492 // fold (fsub A, (fneg B)) -> (fadd A, B)
Duncan Sands25cf2272008-11-24 14:53:14 +00004493 if (isNegatibleForFree(N1, LegalOperations))
Bill Wendlingb0162f52009-01-30 22:53:48 +00004494 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00004495 GetNegatedExpression(N1, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00004496
Dan Gohman475871a2008-07-27 21:46:04 +00004497 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00004498}
4499
Dan Gohman475871a2008-07-27 21:46:04 +00004500SDValue DAGCombiner::visitFMUL(SDNode *N) {
4501 SDValue N0 = N->getOperand(0);
4502 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00004503 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4504 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00004505 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00004506
Dan Gohman7f321562007-06-25 16:23:39 +00004507 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00004508 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004509 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00004510 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00004511 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004512
Nate Begeman11af4ea2005-10-17 20:40:11 +00004513 // fold (fmul c1, c2) -> c1*c2
Owen Anderson825b72b2009-08-11 20:47:22 +00004514 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004515 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00004516 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00004517 if (N0CFP && !N1CFP)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004518 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N1, N0);
4519 // fold (fmul A, 0) -> 0
Dan Gohman760f86f2009-01-22 21:58:43 +00004520 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
4521 return N1;
Dan Gohman77b81fe2009-06-04 17:12:12 +00004522 // fold (fmul A, 0) -> 0, vector edition.
4523 if (UnsafeFPMath && ISD::isBuildVectorAllZeros(N1.getNode()))
4524 return N1;
Nate Begeman11af4ea2005-10-17 20:40:11 +00004525 // fold (fmul X, 2.0) -> (fadd X, X)
4526 if (N1CFP && N1CFP->isExactlyValue(+2.0))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004527 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N0);
Dan Gohmaneb1fedc2009-08-10 16:50:32 +00004528 // fold (fmul X, -1.0) -> (fneg X)
Chris Lattner29446522007-05-14 22:04:50 +00004529 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman760f86f2009-01-22 21:58:43 +00004530 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004531 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004532
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004533 // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y)
Duncan Sands25cf2272008-11-24 14:53:14 +00004534 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
4535 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Chris Lattner29446522007-05-14 22:04:50 +00004536 // Both can be negated for free, check to see if at least one is cheaper
4537 // negated.
4538 if (LHSNeg == 2 || RHSNeg == 2)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004539 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00004540 GetNegatedExpression(N0, DAG, LegalOperations),
4541 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00004542 }
4543 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004544
Chris Lattnerddae4bd2007-01-08 23:04:05 +00004545 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
4546 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004547 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004548 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0.getOperand(0),
Scott Michelfdc40a02009-02-17 22:15:04 +00004549 DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
Dale Johannesende064702009-02-06 21:50:26 +00004550 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004551
Dan Gohman475871a2008-07-27 21:46:04 +00004552 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00004553}
4554
Dan Gohman475871a2008-07-27 21:46:04 +00004555SDValue DAGCombiner::visitFDIV(SDNode *N) {
4556 SDValue N0 = N->getOperand(0);
4557 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00004558 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4559 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00004560 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00004561
Dan Gohman7f321562007-06-25 16:23:39 +00004562 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00004563 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004564 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00004565 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00004566 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004567
Nate Begemana148d982006-01-18 22:35:16 +00004568 // fold (fdiv c1, c2) -> c1/c2
Owen Anderson825b72b2009-08-11 20:47:22 +00004569 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004570 return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004571
4572
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004573 // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y)
Duncan Sands25cf2272008-11-24 14:53:14 +00004574 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
4575 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Chris Lattner29446522007-05-14 22:04:50 +00004576 // Both can be negated for free, check to see if at least one is cheaper
4577 // negated.
4578 if (LHSNeg == 2 || RHSNeg == 2)
Scott Michelfdc40a02009-02-17 22:15:04 +00004579 return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00004580 GetNegatedExpression(N0, DAG, LegalOperations),
4581 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00004582 }
4583 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004584
Dan Gohman475871a2008-07-27 21:46:04 +00004585 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00004586}
4587
Dan Gohman475871a2008-07-27 21:46:04 +00004588SDValue DAGCombiner::visitFREM(SDNode *N) {
4589 SDValue N0 = N->getOperand(0);
4590 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00004591 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4592 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00004593 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00004594
Nate Begemana148d982006-01-18 22:35:16 +00004595 // fold (frem c1, c2) -> fmod(c1,c2)
Owen Anderson825b72b2009-08-11 20:47:22 +00004596 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004597 return DAG.getNode(ISD::FREM, N->getDebugLoc(), VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00004598
Dan Gohman475871a2008-07-27 21:46:04 +00004599 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00004600}
4601
Dan Gohman475871a2008-07-27 21:46:04 +00004602SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
4603 SDValue N0 = N->getOperand(0);
4604 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00004605 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4606 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00004607 EVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00004608
Owen Anderson825b72b2009-08-11 20:47:22 +00004609 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Bill Wendlingfc4b6772009-02-01 11:19:36 +00004610 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004611
Chris Lattner12d83032006-03-05 05:30:57 +00004612 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00004613 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00004614 // copysign(x, c1) -> fabs(x) iff ispos(c1)
4615 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman760f86f2009-01-22 21:58:43 +00004616 if (!V.isNegative()) {
4617 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
Bill Wendling0225a1d2009-01-30 23:15:49 +00004618 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Dan Gohman760f86f2009-01-22 21:58:43 +00004619 } else {
4620 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendling0225a1d2009-01-30 23:15:49 +00004621 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00004622 DAG.getNode(ISD::FABS, N0.getDebugLoc(), VT, N0));
Dan Gohman760f86f2009-01-22 21:58:43 +00004623 }
Chris Lattner12d83032006-03-05 05:30:57 +00004624 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004625
Chris Lattner12d83032006-03-05 05:30:57 +00004626 // copysign(fabs(x), y) -> copysign(x, y)
4627 // copysign(fneg(x), y) -> copysign(x, y)
4628 // copysign(copysign(x,z), y) -> copysign(x, y)
4629 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
4630 N0.getOpcode() == ISD::FCOPYSIGN)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004631 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
4632 N0.getOperand(0), N1);
Chris Lattner12d83032006-03-05 05:30:57 +00004633
4634 // copysign(x, abs(y)) -> abs(x)
4635 if (N1.getOpcode() == ISD::FABS)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004636 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004637
Chris Lattner12d83032006-03-05 05:30:57 +00004638 // copysign(x, copysign(y,z)) -> copysign(x, z)
4639 if (N1.getOpcode() == ISD::FCOPYSIGN)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004640 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
4641 N0, N1.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004642
Chris Lattner12d83032006-03-05 05:30:57 +00004643 // copysign(x, fp_extend(y)) -> copysign(x, y)
4644 // copysign(x, fp_round(y)) -> copysign(x, y)
4645 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004646 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
4647 N0, N1.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004648
Dan Gohman475871a2008-07-27 21:46:04 +00004649 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00004650}
4651
Dan Gohman475871a2008-07-27 21:46:04 +00004652SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
4653 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00004654 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004655 EVT VT = N->getValueType(0);
4656 EVT OpVT = N0.getValueType();
Chris Lattnercda88752008-06-26 00:16:49 +00004657
Nate Begeman1d4d4142005-09-01 00:19:25 +00004658 // fold (sint_to_fp c1) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00004659 if (N0C && OpVT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004660 return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004661
Chris Lattnercda88752008-06-26 00:16:49 +00004662 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
4663 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004664 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
4665 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004666 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00004667 if (DAG.SignBitIsZero(N0))
Bill Wendling0225a1d2009-01-30 23:15:49 +00004668 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00004669 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00004670
Dan Gohman475871a2008-07-27 21:46:04 +00004671 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004672}
4673
Dan Gohman475871a2008-07-27 21:46:04 +00004674SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
4675 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00004676 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004677 EVT VT = N->getValueType(0);
4678 EVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00004679
Nate Begeman1d4d4142005-09-01 00:19:25 +00004680 // fold (uint_to_fp c1) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00004681 if (N0C && OpVT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004682 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004683
Chris Lattnercda88752008-06-26 00:16:49 +00004684 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
4685 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004686 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
4687 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004688 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00004689 if (DAG.SignBitIsZero(N0))
Bill Wendling0225a1d2009-01-30 23:15:49 +00004690 return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00004691 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004692
Dan Gohman475871a2008-07-27 21:46:04 +00004693 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004694}
4695
Dan Gohman475871a2008-07-27 21:46:04 +00004696SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
4697 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004698 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004699 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004700
Nate Begeman1d4d4142005-09-01 00:19:25 +00004701 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00004702 if (N0CFP)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004703 return DAG.getNode(ISD::FP_TO_SINT, N->getDebugLoc(), VT, N0);
4704
Dan Gohman475871a2008-07-27 21:46:04 +00004705 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004706}
4707
Dan Gohman475871a2008-07-27 21:46:04 +00004708SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
4709 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004710 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004711 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004712
Nate Begeman1d4d4142005-09-01 00:19:25 +00004713 // fold (fp_to_uint c1fp) -> c1
Owen Anderson825b72b2009-08-11 20:47:22 +00004714 if (N0CFP && VT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004715 return DAG.getNode(ISD::FP_TO_UINT, N->getDebugLoc(), VT, N0);
4716
Dan Gohman475871a2008-07-27 21:46:04 +00004717 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004718}
4719
Dan Gohman475871a2008-07-27 21:46:04 +00004720SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
4721 SDValue N0 = N->getOperand(0);
4722 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00004723 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004724 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004725
Nate Begeman1d4d4142005-09-01 00:19:25 +00004726 // fold (fp_round c1fp) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00004727 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004728 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004729
Chris Lattner79dbea52006-03-13 06:26:26 +00004730 // fold (fp_round (fp_extend x)) -> x
4731 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
4732 return N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004733
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00004734 // fold (fp_round (fp_round x)) -> (fp_round x)
4735 if (N0.getOpcode() == ISD::FP_ROUND) {
4736 // This is a value preserving truncation if both round's are.
4737 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004738 N0.getNode()->getConstantOperandVal(1) == 1;
Bill Wendling0225a1d2009-01-30 23:15:49 +00004739 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0.getOperand(0),
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00004740 DAG.getIntPtrConstant(IsTrunc));
4741 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004742
Chris Lattner79dbea52006-03-13 06:26:26 +00004743 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00004744 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Bill Wendling0225a1d2009-01-30 23:15:49 +00004745 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(), VT,
4746 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00004747 AddToWorkList(Tmp.getNode());
Bill Wendling0225a1d2009-01-30 23:15:49 +00004748 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
4749 Tmp, N0.getOperand(1));
Chris Lattner79dbea52006-03-13 06:26:26 +00004750 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004751
Dan Gohman475871a2008-07-27 21:46:04 +00004752 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004753}
4754
Dan Gohman475871a2008-07-27 21:46:04 +00004755SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
4756 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004757 EVT VT = N->getValueType(0);
4758 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00004759 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004760
Nate Begeman1d4d4142005-09-01 00:19:25 +00004761 // fold (fp_round_inreg c1fp) -> c1fp
Chris Lattner2392ae72010-04-15 04:48:01 +00004762 if (N0CFP && isTypeLegal(EVT)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00004763 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Bill Wendling0225a1d2009-01-30 23:15:49 +00004764 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004765 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00004766
Dan Gohman475871a2008-07-27 21:46:04 +00004767 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004768}
4769
Dan Gohman475871a2008-07-27 21:46:04 +00004770SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
4771 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004772 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004773 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004774
Chris Lattner5938bef2007-12-29 06:55:23 +00004775 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Scott Michelfdc40a02009-02-17 22:15:04 +00004776 if (N->hasOneUse() &&
Dan Gohmane7852d02009-01-26 04:35:06 +00004777 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman475871a2008-07-27 21:46:04 +00004778 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004779
Nate Begeman1d4d4142005-09-01 00:19:25 +00004780 // fold (fp_extend c1fp) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00004781 if (N0CFP && VT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004782 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004783
4784 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
4785 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00004786 if (N0.getOpcode() == ISD::FP_ROUND
4787 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004788 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004789 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00004790 if (VT.bitsLT(In.getValueType()))
Bill Wendling0225a1d2009-01-30 23:15:49 +00004791 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT,
4792 In, N0.getOperand(1));
4793 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, In);
Chris Lattner0bd48932008-01-17 07:00:52 +00004794 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004795
Chris Lattner0bd48932008-01-17 07:00:52 +00004796 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004797 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004798 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004799 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00004800 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendling0225a1d2009-01-30 23:15:49 +00004801 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT,
4802 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004803 LN0->getBasePtr(), LN0->getSrcValue(),
4804 LN0->getSrcValueOffset(),
4805 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004806 LN0->isVolatile(), LN0->isNonTemporal(),
4807 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00004808 CombineTo(N, ExtLoad);
Bill Wendling0225a1d2009-01-30 23:15:49 +00004809 CombineTo(N0.getNode(),
4810 DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(),
4811 N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00004812 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004813 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00004814 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004815
Dan Gohman475871a2008-07-27 21:46:04 +00004816 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004817}
4818
Dan Gohman475871a2008-07-27 21:46:04 +00004819SDValue DAGCombiner::visitFNEG(SDNode *N) {
4820 SDValue N0 = N->getOperand(0);
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00004821 EVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00004822
Duncan Sands25cf2272008-11-24 14:53:14 +00004823 if (isNegatibleForFree(N0, LegalOperations))
4824 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohman23ff1822007-07-02 15:48:56 +00004825
Chris Lattner3bd39d42008-01-27 17:42:27 +00004826 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
4827 // constant pool values.
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00004828 if (N0.getOpcode() == ISD::BIT_CONVERT &&
4829 !VT.isVector() &&
4830 N0.getNode()->hasOneUse() &&
4831 N0.getOperand(0).getValueType().isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004832 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004833 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004834 if (IntVT.isInteger() && !IntVT.isVector()) {
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00004835 Int = DAG.getNode(ISD::XOR, N0.getDebugLoc(), IntVT, Int,
4836 DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004837 AddToWorkList(Int.getNode());
Bill Wendling0225a1d2009-01-30 23:15:49 +00004838 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(),
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00004839 VT, Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00004840 }
4841 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004842
Dan Gohman475871a2008-07-27 21:46:04 +00004843 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004844}
4845
Dan Gohman475871a2008-07-27 21:46:04 +00004846SDValue DAGCombiner::visitFABS(SDNode *N) {
4847 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004848 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004849 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004850
Nate Begeman1d4d4142005-09-01 00:19:25 +00004851 // fold (fabs c1) -> fabs(c1)
Owen Anderson825b72b2009-08-11 20:47:22 +00004852 if (N0CFP && VT != MVT::ppcf128)
Bill Wendlingc0debad2009-01-30 23:27:35 +00004853 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004854 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004855 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00004856 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004857 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004858 // fold (fabs (fcopysign x, y)) -> (fabs x)
4859 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
Bill Wendlingc0debad2009-01-30 23:27:35 +00004860 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004861
Chris Lattner3bd39d42008-01-27 17:42:27 +00004862 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
4863 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004864 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004865 N0.getOperand(0).getValueType().isInteger() &&
4866 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004867 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004868 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004869 if (IntVT.isInteger() && !IntVT.isVector()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004870 Int = DAG.getNode(ISD::AND, N0.getDebugLoc(), IntVT, Int,
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00004871 DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004872 AddToWorkList(Int.getNode());
Bill Wendlingc0debad2009-01-30 23:27:35 +00004873 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(),
4874 N->getValueType(0), Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00004875 }
4876 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004877
Dan Gohman475871a2008-07-27 21:46:04 +00004878 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004879}
4880
Dan Gohman475871a2008-07-27 21:46:04 +00004881SDValue DAGCombiner::visitBRCOND(SDNode *N) {
4882 SDValue Chain = N->getOperand(0);
4883 SDValue N1 = N->getOperand(1);
4884 SDValue N2 = N->getOperand(2);
Scott Michelfdc40a02009-02-17 22:15:04 +00004885
Dan Gohmane0f06c72009-11-17 00:47:23 +00004886 // If N is a constant we could fold this into a fallthrough or unconditional
4887 // branch. However that doesn't happen very often in normal code, because
4888 // Instcombine/SimplifyCFG should have handled the available opportunities.
4889 // If we did this folding here, it would be necessary to update the
4890 // MachineBasicBlock CFG, which is awkward.
4891
Nate Begeman750ac1b2006-02-01 07:19:44 +00004892 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
4893 // on the target.
Scott Michelfdc40a02009-02-17 22:15:04 +00004894 if (N1.getOpcode() == ISD::SETCC &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004895 TLI.isOperationLegalOrCustom(ISD::BR_CC, MVT::Other)) {
4896 return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00004897 Chain, N1.getOperand(2),
Nate Begeman750ac1b2006-02-01 07:19:44 +00004898 N1.getOperand(0), N1.getOperand(1), N2);
4899 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00004900
Evan Chengd40d03e2010-01-06 19:38:29 +00004901 SDNode *Trunc = 0;
4902 if (N1.getOpcode() == ISD::TRUNCATE && N1.hasOneUse()) {
Chris Lattnerf2f64e92010-03-10 23:46:44 +00004903 // Look past truncate.
Evan Chengd40d03e2010-01-06 19:38:29 +00004904 Trunc = N1.getNode();
4905 N1 = N1.getOperand(0);
4906 }
4907
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00004908 if (N1.hasOneUse() && N1.getOpcode() == ISD::SRL) {
4909 // Match this pattern so that we can generate simpler code:
4910 //
4911 // %a = ...
4912 // %b = and i32 %a, 2
4913 // %c = srl i32 %b, 1
4914 // brcond i32 %c ...
4915 //
4916 // into
4917 //
4918 // %a = ...
Evan Chengd40d03e2010-01-06 19:38:29 +00004919 // %b = and i32 %a, 2
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00004920 // %c = setcc eq %b, 0
4921 // brcond %c ...
4922 //
4923 // This applies only when the AND constant value has one bit set and the
4924 // SRL constant is equal to the log2 of the AND constant. The back-end is
4925 // smart enough to convert the result into a TEST/JMP sequence.
4926 SDValue Op0 = N1.getOperand(0);
4927 SDValue Op1 = N1.getOperand(1);
4928
4929 if (Op0.getOpcode() == ISD::AND &&
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00004930 Op1.getOpcode() == ISD::Constant) {
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00004931 SDValue AndOp1 = Op0.getOperand(1);
4932
4933 if (AndOp1.getOpcode() == ISD::Constant) {
4934 const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue();
4935
4936 if (AndConst.isPowerOf2() &&
4937 cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) {
4938 SDValue SetCC =
4939 DAG.getSetCC(N->getDebugLoc(),
4940 TLI.getSetCCResultType(Op0.getValueType()),
4941 Op0, DAG.getConstant(0, Op0.getValueType()),
4942 ISD::SETNE);
4943
Evan Chengd40d03e2010-01-06 19:38:29 +00004944 SDValue NewBRCond = DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
4945 MVT::Other, Chain, SetCC, N2);
4946 // Don't add the new BRCond into the worklist or else SimplifySelectCC
4947 // will convert it back to (X & C1) >> C2.
4948 CombineTo(N, NewBRCond, false);
4949 // Truncate is dead.
4950 if (Trunc) {
4951 removeFromWorkList(Trunc);
4952 DAG.DeleteNode(Trunc);
4953 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00004954 // Replace the uses of SRL with SETCC
Evan Cheng2c755ba2010-02-27 07:36:59 +00004955 WorkListRemover DeadNodes(*this);
4956 DAG.ReplaceAllUsesOfValueWith(N1, SetCC, &DeadNodes);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00004957 removeFromWorkList(N1.getNode());
4958 DAG.DeleteNode(N1.getNode());
Evan Chengd40d03e2010-01-06 19:38:29 +00004959 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00004960 }
4961 }
4962 }
4963 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00004964
4965 // Transform br(xor(x, y)) -> br(x != y)
4966 // Transform br(xor(xor(x,y), 1)) -> br (x == y)
4967 if (N1.hasOneUse() && N1.getOpcode() == ISD::XOR) {
4968 SDNode *TheXor = N1.getNode();
4969 SDValue Op0 = TheXor->getOperand(0);
4970 SDValue Op1 = TheXor->getOperand(1);
4971 if (Op0.getOpcode() == Op1.getOpcode()) {
4972 // Avoid missing important xor optimizations.
4973 SDValue Tmp = visitXOR(TheXor);
Bill Wendling86c5abb2010-04-20 01:25:01 +00004974 if (Tmp.getNode() && Tmp.getNode() != TheXor) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00004975 DEBUG(dbgs() << "\nReplacing.8 ";
4976 TheXor->dump(&DAG);
4977 dbgs() << "\nWith: ";
4978 Tmp.getNode()->dump(&DAG);
4979 dbgs() << '\n');
4980 WorkListRemover DeadNodes(*this);
4981 DAG.ReplaceAllUsesOfValueWith(N1, Tmp, &DeadNodes);
4982 removeFromWorkList(TheXor);
4983 DAG.DeleteNode(TheXor);
4984 return DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
4985 MVT::Other, Chain, Tmp, N2);
4986 }
4987 }
4988
4989 if (Op0.getOpcode() != ISD::SETCC && Op1.getOpcode() != ISD::SETCC) {
4990 bool Equal = false;
4991 if (ConstantSDNode *RHSCI = dyn_cast<ConstantSDNode>(Op0))
4992 if (RHSCI->getAPIntValue() == 1 && Op0.hasOneUse() &&
4993 Op0.getOpcode() == ISD::XOR) {
4994 TheXor = Op0.getNode();
4995 Equal = true;
4996 }
4997
Chris Lattnerf2f64e92010-03-10 23:46:44 +00004998 SDValue NodeToReplace = Trunc ? SDValue(Trunc, 0) : N1;
4999
5000 EVT SetCCVT = NodeToReplace.getValueType();
Evan Cheng2c755ba2010-02-27 07:36:59 +00005001 if (LegalTypes)
5002 SetCCVT = TLI.getSetCCResultType(SetCCVT);
5003 SDValue SetCC = DAG.getSetCC(TheXor->getDebugLoc(),
5004 SetCCVT,
5005 Op0, Op1,
5006 Equal ? ISD::SETEQ : ISD::SETNE);
5007 // Replace the uses of XOR with SETCC
5008 WorkListRemover DeadNodes(*this);
Chris Lattnerf2f64e92010-03-10 23:46:44 +00005009 DAG.ReplaceAllUsesOfValueWith(NodeToReplace, SetCC, &DeadNodes);
5010 removeFromWorkList(NodeToReplace.getNode());
5011 DAG.DeleteNode(NodeToReplace.getNode());
Evan Cheng2c755ba2010-02-27 07:36:59 +00005012 return DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
5013 MVT::Other, Chain, SetCC, N2);
5014 }
5015 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00005016
Dan Gohman475871a2008-07-27 21:46:04 +00005017 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005018}
5019
Chris Lattner3ea0b472005-10-05 06:47:48 +00005020// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
5021//
Dan Gohman475871a2008-07-27 21:46:04 +00005022SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00005023 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005024 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Scott Michelfdc40a02009-02-17 22:15:04 +00005025
Dan Gohmane0f06c72009-11-17 00:47:23 +00005026 // If N is a constant we could fold this into a fallthrough or unconditional
5027 // branch. However that doesn't happen very often in normal code, because
5028 // Instcombine/SimplifyCFG should have handled the available opportunities.
5029 // If we did this folding here, it would be necessary to update the
5030 // MachineBasicBlock CFG, which is awkward.
5031
Duncan Sands8eab8a22008-06-09 11:32:28 +00005032 // Use SimplifySetCC to simplify SETCC's.
Duncan Sands5480c042009-01-01 15:52:00 +00005033 SDValue Simp = SimplifySetCC(TLI.getSetCCResultType(CondLHS.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00005034 CondLHS, CondRHS, CC->get(), N->getDebugLoc(),
5035 false);
Gabor Greifba36cb52008-08-28 21:40:38 +00005036 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00005037
Nate Begemane17daeb2005-10-05 21:43:42 +00005038 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00005039 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Owen Anderson825b72b2009-08-11 20:47:22 +00005040 return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00005041 N->getOperand(0), Simp.getOperand(2),
5042 Simp.getOperand(0), Simp.getOperand(1),
5043 N->getOperand(4));
5044
Dan Gohman475871a2008-07-27 21:46:04 +00005045 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005046}
5047
Duncan Sandsec87aa82008-06-15 20:12:31 +00005048/// CombineToPreIndexedLoadStore - Try turning a load / store into a
5049/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00005050/// and it has other uses besides the load / store. After the
5051/// transformation, the new indexed load / store has effectively folded
5052/// the add / subtract in and all of its other uses are redirected to the
5053/// new load / store.
5054bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Duncan Sands25cf2272008-11-24 14:53:14 +00005055 if (!LegalOperations)
Chris Lattner448f2192006-11-11 00:39:41 +00005056 return false;
5057
5058 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005059 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00005060 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00005061 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00005062 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00005063 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005064 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00005065 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00005066 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
5067 return false;
5068 Ptr = LD->getBasePtr();
5069 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00005070 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00005071 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005072 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00005073 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
5074 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
5075 return false;
5076 Ptr = ST->getBasePtr();
5077 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00005078 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00005079 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00005080 }
Chris Lattner448f2192006-11-11 00:39:41 +00005081
Chris Lattner9f1794e2006-11-11 00:56:29 +00005082 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
5083 // out. There is no reason to make this a preinc/predec.
5084 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00005085 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00005086 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00005087
Chris Lattner9f1794e2006-11-11 00:56:29 +00005088 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00005089 SDValue BasePtr;
5090 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00005091 ISD::MemIndexedMode AM = ISD::UNINDEXED;
5092 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
5093 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00005094 // Don't create a indexed load / store with zero offset.
5095 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005096 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00005097 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005098
Chris Lattner41e53fd2006-11-11 01:00:15 +00005099 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00005100 // 1) The new base ptr is a frame index.
5101 // 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 +00005102 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00005103 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00005104 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00005105 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00005106
Chris Lattner41e53fd2006-11-11 01:00:15 +00005107 // Check #1. Preinc'ing a frame index would require copying the stack pointer
5108 // (plus the implicit offset) to a register to preinc anyway.
Evan Chengcaab1292009-05-06 18:25:01 +00005109 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
Chris Lattner41e53fd2006-11-11 01:00:15 +00005110 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005111
Chris Lattner41e53fd2006-11-11 01:00:15 +00005112 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00005113 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00005114 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005115 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00005116 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00005117 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00005118
Evan Chengc843abe2007-05-24 02:35:39 +00005119 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00005120 bool RealUse = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00005121 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
5122 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00005123 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00005124 if (Use == N)
5125 continue;
Evan Cheng917be682008-03-04 00:41:45 +00005126 if (Use->isPredecessorOf(N))
Chris Lattner9f1794e2006-11-11 00:56:29 +00005127 return false;
5128
5129 if (!((Use->getOpcode() == ISD::LOAD &&
5130 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00005131 (Use->getOpcode() == ISD::STORE &&
5132 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00005133 RealUse = true;
5134 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00005135
Chris Lattner9f1794e2006-11-11 00:56:29 +00005136 if (!RealUse)
5137 return false;
5138
Dan Gohman475871a2008-07-27 21:46:04 +00005139 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00005140 if (isLoad)
Bill Wendlingc0debad2009-01-30 23:27:35 +00005141 Result = DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(),
5142 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005143 else
Bill Wendlingc0debad2009-01-30 23:27:35 +00005144 Result = DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(),
5145 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005146 ++PreIndexedNodes;
5147 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00005148 DEBUG(dbgs() << "\nReplacing.4 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00005149 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00005150 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00005151 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00005152 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005153 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005154 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00005155 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005156 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00005157 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005158 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005159 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00005160 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005161 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005162 }
5163
Chris Lattner9f1794e2006-11-11 00:56:29 +00005164 // Finally, since the node is now dead, remove it from the graph.
5165 DAG.DeleteNode(N);
5166
5167 // Replace the uses of Ptr with uses of the updated base value.
5168 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005169 &DeadNodes);
Gabor Greifba36cb52008-08-28 21:40:38 +00005170 removeFromWorkList(Ptr.getNode());
5171 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00005172
5173 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00005174}
5175
Duncan Sandsec87aa82008-06-15 20:12:31 +00005176/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00005177/// add / sub of the base pointer node into a post-indexed load / store.
5178/// The transformation folded the add / subtract into the new indexed
5179/// load / store effectively and all of its uses are redirected to the
5180/// new load / store.
5181bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Duncan Sands25cf2272008-11-24 14:53:14 +00005182 if (!LegalOperations)
Chris Lattner448f2192006-11-11 00:39:41 +00005183 return false;
5184
5185 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005186 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00005187 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00005188 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00005189 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00005190 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005191 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00005192 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
5193 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
5194 return false;
5195 Ptr = LD->getBasePtr();
5196 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00005197 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00005198 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005199 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00005200 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
5201 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
5202 return false;
5203 Ptr = ST->getBasePtr();
5204 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00005205 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00005206 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00005207 }
Chris Lattner448f2192006-11-11 00:39:41 +00005208
Gabor Greifba36cb52008-08-28 21:40:38 +00005209 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00005210 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005211
Gabor Greifba36cb52008-08-28 21:40:38 +00005212 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
5213 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00005214 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00005215 if (Op == N ||
5216 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
5217 continue;
5218
Dan Gohman475871a2008-07-27 21:46:04 +00005219 SDValue BasePtr;
5220 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00005221 ISD::MemIndexedMode AM = ISD::UNINDEXED;
5222 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
Bob Wilson92ad3632009-09-10 22:09:31 +00005223 if (Ptr == Offset && Op->getOpcode() == ISD::ADD)
Chris Lattner9f1794e2006-11-11 00:56:29 +00005224 std::swap(BasePtr, Offset);
5225 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00005226 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00005227 // Don't create a indexed load / store with zero offset.
5228 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005229 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00005230 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00005231
Chris Lattner9f1794e2006-11-11 00:56:29 +00005232 // Try turning it into a post-indexed load / store except when
5233 // 1) All uses are load / store ops that use it as base ptr.
5234 // 2) Op must be independent of N, i.e. Op is neither a predecessor
5235 // nor a successor of N. Otherwise, if Op is folded that would
5236 // create a cycle.
5237
Evan Chengcaab1292009-05-06 18:25:01 +00005238 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
5239 continue;
5240
Chris Lattner9f1794e2006-11-11 00:56:29 +00005241 // Check for #1.
5242 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00005243 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
5244 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00005245 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00005246 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00005247 continue;
5248
Chris Lattner9f1794e2006-11-11 00:56:29 +00005249 // If all the uses are load / store addresses, then don't do the
5250 // transformation.
5251 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
5252 bool RealUse = false;
5253 for (SDNode::use_iterator III = Use->use_begin(),
5254 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00005255 SDNode *UseUse = *III;
Chris Lattner9f1794e2006-11-11 00:56:29 +00005256 if (!((UseUse->getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005257 cast<LoadSDNode>(UseUse)->getBasePtr().getNode() == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00005258 (UseUse->getOpcode() == ISD::STORE &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005259 cast<StoreSDNode>(UseUse)->getBasePtr().getNode() == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00005260 RealUse = true;
5261 }
Chris Lattner448f2192006-11-11 00:39:41 +00005262
Chris Lattner9f1794e2006-11-11 00:56:29 +00005263 if (!RealUse) {
5264 TryNext = true;
5265 break;
Chris Lattner448f2192006-11-11 00:39:41 +00005266 }
5267 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00005268 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00005269
Chris Lattner9f1794e2006-11-11 00:56:29 +00005270 if (TryNext)
5271 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00005272
Chris Lattner9f1794e2006-11-11 00:56:29 +00005273 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00005274 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00005275 SDValue Result = isLoad
Bill Wendlingc0debad2009-01-30 23:27:35 +00005276 ? DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(),
5277 BasePtr, Offset, AM)
5278 : DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(),
5279 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005280 ++PostIndexedNodes;
5281 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00005282 DEBUG(dbgs() << "\nReplacing.5 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00005283 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00005284 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00005285 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00005286 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005287 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005288 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00005289 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005290 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00005291 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005292 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005293 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00005294 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005295 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00005296 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00005297
Chris Lattner9f1794e2006-11-11 00:56:29 +00005298 // Finally, since the node is now dead, remove it from the graph.
5299 DAG.DeleteNode(N);
5300
5301 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00005302 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Chris Lattner9f1794e2006-11-11 00:56:29 +00005303 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005304 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005305 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005306 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005307 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00005308 }
5309 }
5310 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00005311
Chris Lattner448f2192006-11-11 00:39:41 +00005312 return false;
5313}
5314
Dan Gohman475871a2008-07-27 21:46:04 +00005315SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00005316 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00005317 SDValue Chain = LD->getChain();
5318 SDValue Ptr = LD->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00005319
Evan Cheng45a7ca92007-05-01 00:38:21 +00005320 // If load is not volatile and there are no uses of the loaded value (and
5321 // the updated indexed value in case of indexed loads), change uses of the
5322 // chain value into uses of the chain input (i.e. delete the dead load).
5323 if (!LD->isVolatile()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005324 if (N->getValueType(1) == MVT::Other) {
Evan Cheng498f5592007-05-01 08:53:39 +00005325 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00005326 if (N->hasNUsesOfValue(0, 0)) {
5327 // It's not safe to use the two value CombineTo variant here. e.g.
5328 // v1, chain2 = load chain1, loc
5329 // v2, chain3 = load chain2, loc
5330 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00005331 // Now we replace use of chain2 with chain1. This makes the second load
5332 // isomorphic to the one we are deleting, and thus makes this load live.
David Greenef1090292010-01-05 01:25:00 +00005333 DEBUG(dbgs() << "\nReplacing.6 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00005334 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00005335 dbgs() << "\nWith chain: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00005336 Chain.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00005337 dbgs() << "\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005338 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00005339 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain, &DeadNodes);
Bill Wendlingc0debad2009-01-30 23:27:35 +00005340
Chris Lattner125991a2008-01-24 07:57:06 +00005341 if (N->use_empty()) {
5342 removeFromWorkList(N);
5343 DAG.DeleteNode(N);
5344 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00005345
Dan Gohman475871a2008-07-27 21:46:04 +00005346 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00005347 }
Evan Cheng498f5592007-05-01 08:53:39 +00005348 } else {
5349 // Indexed loads.
Owen Anderson825b72b2009-08-11 20:47:22 +00005350 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
Evan Cheng498f5592007-05-01 08:53:39 +00005351 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Dale Johannesene8d72302009-02-06 23:05:02 +00005352 SDValue Undef = DAG.getUNDEF(N->getValueType(0));
Evan Cheng2c755ba2010-02-27 07:36:59 +00005353 DEBUG(dbgs() << "\nReplacing.7 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00005354 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00005355 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00005356 Undef.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00005357 dbgs() << " and 2 other values\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005358 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00005359 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef, &DeadNodes);
5360 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Dale Johannesene8d72302009-02-06 23:05:02 +00005361 DAG.getUNDEF(N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005362 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00005363 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00005364 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00005365 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00005366 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00005367 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00005368 }
5369 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005370
Chris Lattner01a22022005-10-10 22:04:48 +00005371 // If this load is directly stored, replace the load value with the stored
5372 // value.
5373 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00005374 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohmanb061c4b2008-03-31 20:32:52 +00005375 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
5376 !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005377 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00005378 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
5379 if (PrevST->getBasePtr() == Ptr &&
5380 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00005381 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00005382 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00005383 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005384
Evan Cheng255f20f2010-04-01 06:04:33 +00005385 // Try to infer better alignment information than the load already has.
5386 if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) {
5387 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
5388 if (Align > LD->getAlignment())
5389 return DAG.getExtLoad(LD->getExtensionType(), N->getDebugLoc(),
5390 LD->getValueType(0),
5391 Chain, Ptr, LD->getSrcValue(),
5392 LD->getSrcValueOffset(), LD->getMemoryVT(),
5393 LD->isVolatile(), LD->isNonTemporal(), Align);
5394 }
5395 }
5396
Jim Laskey7ca56af2006-10-11 13:47:09 +00005397 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00005398 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00005399 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00005400
Jim Laskey6ff23e52006-10-04 16:53:27 +00005401 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00005402 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00005403 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00005404
Jim Laskey279f0532006-09-25 16:29:54 +00005405 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00005406 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Bill Wendlingc0debad2009-01-30 23:27:35 +00005407 ReplLoad = DAG.getLoad(N->getValueType(0), LD->getDebugLoc(),
5408 BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00005409 LD->getSrcValue(), LD->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00005410 LD->isVolatile(), LD->isNonTemporal(),
5411 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00005412 } else {
Bill Wendlingc0debad2009-01-30 23:27:35 +00005413 ReplLoad = DAG.getExtLoad(LD->getExtensionType(), LD->getDebugLoc(),
Jim Laskeyc2b19f32006-10-11 17:47:52 +00005414 LD->getValueType(0),
5415 BetterChain, Ptr, LD->getSrcValue(),
5416 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005417 LD->getMemoryVT(),
Scott Michelfdc40a02009-02-17 22:15:04 +00005418 LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00005419 LD->isNonTemporal(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005420 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00005421 }
Jim Laskey279f0532006-09-25 16:29:54 +00005422
Jim Laskey6ff23e52006-10-04 16:53:27 +00005423 // Create token factor to keep old chain connected.
Bill Wendlingc0debad2009-01-30 23:27:35 +00005424 SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005425 MVT::Other, Chain, ReplLoad.getValue(1));
Nate Begemanb6aef5c2009-09-15 00:18:30 +00005426
5427 // Make sure the new and old chains are cleaned up.
5428 AddToWorkList(Token.getNode());
5429
Jim Laskey274062c2006-10-13 23:32:28 +00005430 // Replace uses with load result and token factor. Don't add users
5431 // to work list.
5432 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00005433 }
5434 }
5435
Evan Cheng7fc033a2006-11-03 03:06:21 +00005436 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00005437 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00005438 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00005439
Evan Cheng4c26e932010-04-19 19:29:22 +00005440 if (PromoteLoad(SDValue(N, 0)))
5441 return SDValue(N, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00005442 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00005443}
5444
Chris Lattner2392ae72010-04-15 04:48:01 +00005445/// CheckForMaskedLoad - Check to see if V is (and load (ptr), imm), where the
5446/// load is having specific bytes cleared out. If so, return the byte size
5447/// being masked out and the shift amount.
5448static std::pair<unsigned, unsigned>
5449CheckForMaskedLoad(SDValue V, SDValue Ptr, SDValue Chain) {
5450 std::pair<unsigned, unsigned> Result(0, 0);
5451
5452 // Check for the structure we're looking for.
5453 if (V->getOpcode() != ISD::AND ||
5454 !isa<ConstantSDNode>(V->getOperand(1)) ||
5455 !ISD::isNormalLoad(V->getOperand(0).getNode()))
5456 return Result;
5457
Chris Lattnere6987582010-04-15 06:10:49 +00005458 // Check the chain and pointer.
Chris Lattner2392ae72010-04-15 04:48:01 +00005459 LoadSDNode *LD = cast<LoadSDNode>(V->getOperand(0));
Chris Lattnere6987582010-04-15 06:10:49 +00005460 if (LD->getBasePtr() != Ptr) return Result; // Not from same pointer.
5461
5462 // The store should be chained directly to the load or be an operand of a
5463 // tokenfactor.
5464 if (LD == Chain.getNode())
5465 ; // ok.
5466 else if (Chain->getOpcode() != ISD::TokenFactor)
5467 return Result; // Fail.
5468 else {
5469 bool isOk = false;
5470 for (unsigned i = 0, e = Chain->getNumOperands(); i != e; ++i)
5471 if (Chain->getOperand(i).getNode() == LD) {
5472 isOk = true;
5473 break;
5474 }
5475 if (!isOk) return Result;
5476 }
Chris Lattner2392ae72010-04-15 04:48:01 +00005477
5478 // This only handles simple types.
5479 if (V.getValueType() != MVT::i16 &&
5480 V.getValueType() != MVT::i32 &&
5481 V.getValueType() != MVT::i64)
5482 return Result;
5483
5484 // Check the constant mask. Invert it so that the bits being masked out are
5485 // 0 and the bits being kept are 1. Use getSExtValue so that leading bits
5486 // follow the sign bit for uniformity.
5487 uint64_t NotMask = ~cast<ConstantSDNode>(V->getOperand(1))->getSExtValue();
5488 unsigned NotMaskLZ = CountLeadingZeros_64(NotMask);
5489 if (NotMaskLZ & 7) return Result; // Must be multiple of a byte.
5490 unsigned NotMaskTZ = CountTrailingZeros_64(NotMask);
5491 if (NotMaskTZ & 7) return Result; // Must be multiple of a byte.
5492 if (NotMaskLZ == 64) return Result; // All zero mask.
5493
5494 // See if we have a continuous run of bits. If so, we have 0*1+0*
5495 if (CountTrailingOnes_64(NotMask >> NotMaskTZ)+NotMaskTZ+NotMaskLZ != 64)
5496 return Result;
5497
5498 // Adjust NotMaskLZ down to be from the actual size of the int instead of i64.
5499 if (V.getValueType() != MVT::i64 && NotMaskLZ)
5500 NotMaskLZ -= 64-V.getValueSizeInBits();
5501
5502 unsigned MaskedBytes = (V.getValueSizeInBits()-NotMaskLZ-NotMaskTZ)/8;
5503 switch (MaskedBytes) {
5504 case 1:
5505 case 2:
5506 case 4: break;
5507 default: return Result; // All one mask, or 5-byte mask.
5508 }
5509
5510 // Verify that the first bit starts at a multiple of mask so that the access
5511 // is aligned the same as the access width.
5512 if (NotMaskTZ && NotMaskTZ/8 % MaskedBytes) return Result;
5513
5514 Result.first = MaskedBytes;
5515 Result.second = NotMaskTZ/8;
5516 return Result;
5517}
5518
5519
5520/// ShrinkLoadReplaceStoreWithStore - Check to see if IVal is something that
5521/// provides a value as specified by MaskInfo. If so, replace the specified
5522/// store with a narrower store of truncated IVal.
5523static SDNode *
5524ShrinkLoadReplaceStoreWithStore(const std::pair<unsigned, unsigned> &MaskInfo,
5525 SDValue IVal, StoreSDNode *St,
5526 DAGCombiner *DC) {
5527 unsigned NumBytes = MaskInfo.first;
5528 unsigned ByteShift = MaskInfo.second;
5529 SelectionDAG &DAG = DC->getDAG();
5530
5531 // Check to see if IVal is all zeros in the part being masked in by the 'or'
5532 // that uses this. If not, this is not a replacement.
5533 APInt Mask = ~APInt::getBitsSet(IVal.getValueSizeInBits(),
5534 ByteShift*8, (ByteShift+NumBytes)*8);
5535 if (!DAG.MaskedValueIsZero(IVal, Mask)) return 0;
5536
5537 // Check that it is legal on the target to do this. It is legal if the new
5538 // VT we're shrinking to (i8/i16/i32) is legal or we're still before type
5539 // legalization.
5540 MVT VT = MVT::getIntegerVT(NumBytes*8);
5541 if (!DC->isTypeLegal(VT))
5542 return 0;
5543
5544 // Okay, we can do this! Replace the 'St' store with a store of IVal that is
5545 // shifted by ByteShift and truncated down to NumBytes.
5546 if (ByteShift)
5547 IVal = DAG.getNode(ISD::SRL, IVal->getDebugLoc(), IVal.getValueType(), IVal,
5548 DAG.getConstant(ByteShift*8, DC->getShiftAmountTy()));
5549
5550 // Figure out the offset for the store and the alignment of the access.
5551 unsigned StOffset;
5552 unsigned NewAlign = St->getAlignment();
5553
5554 if (DAG.getTargetLoweringInfo().isLittleEndian())
5555 StOffset = ByteShift;
5556 else
5557 StOffset = IVal.getValueType().getStoreSize() - ByteShift - NumBytes;
5558
5559 SDValue Ptr = St->getBasePtr();
5560 if (StOffset) {
5561 Ptr = DAG.getNode(ISD::ADD, IVal->getDebugLoc(), Ptr.getValueType(),
5562 Ptr, DAG.getConstant(StOffset, Ptr.getValueType()));
5563 NewAlign = MinAlign(NewAlign, StOffset);
5564 }
5565
5566 // Truncate down to the new size.
5567 IVal = DAG.getNode(ISD::TRUNCATE, IVal->getDebugLoc(), VT, IVal);
5568
5569 ++OpsNarrowed;
5570 return DAG.getStore(St->getChain(), St->getDebugLoc(), IVal, Ptr,
5571 St->getSrcValue(), St->getSrcValueOffset()+StOffset,
5572 false, false, NewAlign).getNode();
5573}
5574
Evan Cheng8b944d32009-05-28 00:35:15 +00005575
5576/// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is
5577/// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some
5578/// of the loaded bits, try narrowing the load and store if it would end up
5579/// being a win for performance or code size.
5580SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) {
5581 StoreSDNode *ST = cast<StoreSDNode>(N);
Evan Chengcdcecc02009-05-28 18:41:02 +00005582 if (ST->isVolatile())
5583 return SDValue();
5584
Evan Cheng8b944d32009-05-28 00:35:15 +00005585 SDValue Chain = ST->getChain();
5586 SDValue Value = ST->getValue();
5587 SDValue Ptr = ST->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +00005588 EVT VT = Value.getValueType();
Evan Cheng8b944d32009-05-28 00:35:15 +00005589
5590 if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse())
Evan Chengcdcecc02009-05-28 18:41:02 +00005591 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00005592
5593 unsigned Opc = Value.getOpcode();
Chris Lattner2392ae72010-04-15 04:48:01 +00005594
5595 // If this is "store (or X, Y), P" and X is "(and (load P), cst)", where cst
5596 // is a byte mask indicating a consecutive number of bytes, check to see if
5597 // Y is known to provide just those bytes. If so, we try to replace the
5598 // load + replace + store sequence with a single (narrower) store, which makes
5599 // the load dead.
5600 if (Opc == ISD::OR) {
5601 std::pair<unsigned, unsigned> MaskedLoad;
5602 MaskedLoad = CheckForMaskedLoad(Value.getOperand(0), Ptr, Chain);
5603 if (MaskedLoad.first)
5604 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
5605 Value.getOperand(1), ST,this))
5606 return SDValue(NewST, 0);
5607
5608 // Or is commutative, so try swapping X and Y.
5609 MaskedLoad = CheckForMaskedLoad(Value.getOperand(1), Ptr, Chain);
5610 if (MaskedLoad.first)
5611 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
5612 Value.getOperand(0), ST,this))
5613 return SDValue(NewST, 0);
5614 }
5615
Evan Cheng8b944d32009-05-28 00:35:15 +00005616 if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) ||
5617 Value.getOperand(1).getOpcode() != ISD::Constant)
Evan Chengcdcecc02009-05-28 18:41:02 +00005618 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00005619
5620 SDValue N0 = Value.getOperand(0);
5621 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse()) {
5622 LoadSDNode *LD = cast<LoadSDNode>(N0);
Evan Chengcdcecc02009-05-28 18:41:02 +00005623 if (LD->getBasePtr() != Ptr)
5624 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00005625
5626 // Find the type to narrow it the load / op / store to.
5627 SDValue N1 = Value.getOperand(1);
5628 unsigned BitWidth = N1.getValueSizeInBits();
5629 APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue();
5630 if (Opc == ISD::AND)
5631 Imm ^= APInt::getAllOnesValue(BitWidth);
Evan Chengd3c76bb2009-05-28 23:52:18 +00005632 if (Imm == 0 || Imm.isAllOnesValue())
5633 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00005634 unsigned ShAmt = Imm.countTrailingZeros();
5635 unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1;
5636 unsigned NewBW = NextPowerOf2(MSB - ShAmt);
Owen Anderson23b9b192009-08-12 00:36:31 +00005637 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00005638 while (NewBW < BitWidth &&
Evan Chengcdcecc02009-05-28 18:41:02 +00005639 !(TLI.isOperationLegalOrCustom(Opc, NewVT) &&
Evan Cheng8b944d32009-05-28 00:35:15 +00005640 TLI.isNarrowingProfitable(VT, NewVT))) {
5641 NewBW = NextPowerOf2(NewBW);
Owen Anderson23b9b192009-08-12 00:36:31 +00005642 NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00005643 }
Evan Chengcdcecc02009-05-28 18:41:02 +00005644 if (NewBW >= BitWidth)
5645 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00005646
5647 // If the lsb changed does not start at the type bitwidth boundary,
5648 // start at the previous one.
5649 if (ShAmt % NewBW)
5650 ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW;
5651 APInt Mask = APInt::getBitsSet(BitWidth, ShAmt, ShAmt + NewBW);
5652 if ((Imm & Mask) == Imm) {
5653 APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW);
5654 if (Opc == ISD::AND)
5655 NewImm ^= APInt::getAllOnesValue(NewBW);
5656 uint64_t PtrOff = ShAmt / 8;
5657 // For big endian targets, we need to adjust the offset to the pointer to
5658 // load the correct bytes.
5659 if (TLI.isBigEndian())
Evan Chengcdcecc02009-05-28 18:41:02 +00005660 PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff;
Evan Cheng8b944d32009-05-28 00:35:15 +00005661
5662 unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff);
Chris Lattner2392ae72010-04-15 04:48:01 +00005663 const Type *NewVTTy = NewVT.getTypeForEVT(*DAG.getContext());
5664 if (NewAlign < TLI.getTargetData()->getABITypeAlignment(NewVTTy))
Evan Chengcdcecc02009-05-28 18:41:02 +00005665 return SDValue();
5666
Evan Cheng8b944d32009-05-28 00:35:15 +00005667 SDValue NewPtr = DAG.getNode(ISD::ADD, LD->getDebugLoc(),
5668 Ptr.getValueType(), Ptr,
5669 DAG.getConstant(PtrOff, Ptr.getValueType()));
5670 SDValue NewLD = DAG.getLoad(NewVT, N0.getDebugLoc(),
5671 LD->getChain(), NewPtr,
5672 LD->getSrcValue(), LD->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00005673 LD->isVolatile(), LD->isNonTemporal(),
5674 NewAlign);
Evan Cheng8b944d32009-05-28 00:35:15 +00005675 SDValue NewVal = DAG.getNode(Opc, Value.getDebugLoc(), NewVT, NewLD,
5676 DAG.getConstant(NewImm, NewVT));
5677 SDValue NewST = DAG.getStore(Chain, N->getDebugLoc(),
5678 NewVal, NewPtr,
5679 ST->getSrcValue(), ST->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00005680 false, false, NewAlign);
Evan Cheng8b944d32009-05-28 00:35:15 +00005681
5682 AddToWorkList(NewPtr.getNode());
5683 AddToWorkList(NewLD.getNode());
5684 AddToWorkList(NewVal.getNode());
5685 WorkListRemover DeadNodes(*this);
5686 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1),
5687 &DeadNodes);
5688 ++OpsNarrowed;
5689 return NewST;
5690 }
5691 }
5692
Evan Chengcdcecc02009-05-28 18:41:02 +00005693 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00005694}
5695
Dan Gohman475871a2008-07-27 21:46:04 +00005696SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00005697 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00005698 SDValue Chain = ST->getChain();
5699 SDValue Value = ST->getValue();
5700 SDValue Ptr = ST->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00005701
Evan Cheng59d5b682007-05-07 21:27:48 +00005702 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00005703 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00005704 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00005705 ST->isUnindexed()) {
Dan Gohman1ba519b2009-02-20 23:29:13 +00005706 unsigned OrigAlign = ST->getAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00005707 EVT SVT = Value.getOperand(0).getValueType();
Dan Gohman1ba519b2009-02-20 23:29:13 +00005708 unsigned Align = TLI.getTargetData()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005709 getABITypeAlignment(SVT.getTypeForEVT(*DAG.getContext()));
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005710 if (Align <= OrigAlign &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005711 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohmanf560ffa2009-01-28 17:46:25 +00005712 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Bill Wendlingc144a572009-01-30 23:36:47 +00005713 return DAG.getStore(Chain, N->getDebugLoc(), Value.getOperand(0),
5714 Ptr, ST->getSrcValue(),
David Greene1e559442010-02-15 17:00:31 +00005715 ST->getSrcValueOffset(), ST->isVolatile(),
5716 ST->isNonTemporal(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00005717 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005718
Nate Begeman2cbba892006-12-11 02:23:46 +00005719 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00005720 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005721 // NOTE: If the original store is volatile, this transform must not increase
5722 // the number of stores. For example, on x86-32 an f64 can be stored in one
5723 // processor operation but an i64 (which is not legal) requires two. So the
5724 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00005725 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00005726 SDValue Tmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00005727 switch (CFP->getValueType(0).getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005728 default: llvm_unreachable("Unknown FP type");
Owen Anderson825b72b2009-08-11 20:47:22 +00005729 case MVT::f80: // We don't do this for these yet.
5730 case MVT::f128:
5731 case MVT::ppcf128:
Dale Johannesenc7b21d52007-09-18 18:36:59 +00005732 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005733 case MVT::f32:
Chris Lattner2392ae72010-04-15 04:48:01 +00005734 if ((isTypeLegal(MVT::i32) && !LegalOperations && !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005735 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00005736 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Owen Anderson825b72b2009-08-11 20:47:22 +00005737 bitcastToAPInt().getZExtValue(), MVT::i32);
Bill Wendlingc144a572009-01-30 23:36:47 +00005738 return DAG.getStore(Chain, N->getDebugLoc(), Tmp,
5739 Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005740 ST->getSrcValueOffset(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00005741 ST->isNonTemporal(), ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00005742 }
5743 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005744 case MVT::f64:
Chris Lattner2392ae72010-04-15 04:48:01 +00005745 if ((TLI.isTypeLegal(MVT::i64) && !LegalOperations &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00005746 !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005747 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen7111b022008-10-09 18:53:47 +00005748 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +00005749 getZExtValue(), MVT::i64);
Bill Wendlingc144a572009-01-30 23:36:47 +00005750 return DAG.getStore(Chain, N->getDebugLoc(), Tmp,
5751 Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005752 ST->getSrcValueOffset(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00005753 ST->isNonTemporal(), ST->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005754 } else if (!ST->isVolatile() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005755 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00005756 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00005757 // argument passing. Since this is so common, custom legalize the
5758 // 64-bit integer store into two 32-bit stores.
Dale Johannesen7111b022008-10-09 18:53:47 +00005759 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005760 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
5761 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00005762 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00005763
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005764 int SVOffset = ST->getSrcValueOffset();
5765 unsigned Alignment = ST->getAlignment();
5766 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00005767 bool isNonTemporal = ST->isNonTemporal();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005768
Bill Wendlingc144a572009-01-30 23:36:47 +00005769 SDValue St0 = DAG.getStore(Chain, ST->getDebugLoc(), Lo,
5770 Ptr, ST->getSrcValue(),
5771 ST->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00005772 isVolatile, isNonTemporal,
5773 ST->getAlignment());
Bill Wendlingc144a572009-01-30 23:36:47 +00005774 Ptr = DAG.getNode(ISD::ADD, N->getDebugLoc(), Ptr.getValueType(), Ptr,
Chris Lattner62be1a72006-12-12 04:16:14 +00005775 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005776 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00005777 Alignment = MinAlign(Alignment, 4U);
Bill Wendlingc144a572009-01-30 23:36:47 +00005778 SDValue St1 = DAG.getStore(Chain, ST->getDebugLoc(), Hi,
5779 Ptr, ST->getSrcValue(),
David Greene1e559442010-02-15 17:00:31 +00005780 SVOffset, isVolatile, isNonTemporal,
5781 Alignment);
Owen Anderson825b72b2009-08-11 20:47:22 +00005782 return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other,
Bill Wendlingc144a572009-01-30 23:36:47 +00005783 St0, St1);
Chris Lattner62be1a72006-12-12 04:16:14 +00005784 }
Bill Wendlingc144a572009-01-30 23:36:47 +00005785
Chris Lattner62be1a72006-12-12 04:16:14 +00005786 break;
Evan Cheng25ece662006-12-11 17:25:19 +00005787 }
Nate Begeman2cbba892006-12-11 02:23:46 +00005788 }
Nate Begeman2cbba892006-12-11 02:23:46 +00005789 }
5790
Evan Cheng255f20f2010-04-01 06:04:33 +00005791 // Try to infer better alignment information than the store already has.
5792 if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) {
5793 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
5794 if (Align > ST->getAlignment())
5795 return DAG.getTruncStore(Chain, N->getDebugLoc(), Value,
5796 Ptr, ST->getSrcValue(),
5797 ST->getSrcValueOffset(), ST->getMemoryVT(),
5798 ST->isVolatile(), ST->isNonTemporal(), Align);
5799 }
5800 }
5801
Scott Michelfdc40a02009-02-17 22:15:04 +00005802 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00005803 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00005804 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00005805
Jim Laskey6ff23e52006-10-04 16:53:27 +00005806 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00005807 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00005808 SDValue ReplStore;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00005809
5810 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00005811 if (ST->isTruncatingStore()) {
Bill Wendlingc144a572009-01-30 23:36:47 +00005812 ReplStore = DAG.getTruncStore(BetterChain, N->getDebugLoc(), Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00005813 ST->getSrcValue(),ST->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00005814 ST->getMemoryVT(), ST->isVolatile(),
5815 ST->isNonTemporal(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00005816 } else {
Bill Wendlingc144a572009-01-30 23:36:47 +00005817 ReplStore = DAG.getStore(BetterChain, N->getDebugLoc(), Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00005818 ST->getSrcValue(), ST->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00005819 ST->isVolatile(), ST->isNonTemporal(),
5820 ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00005821 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005822
Jim Laskey279f0532006-09-25 16:29:54 +00005823 // Create token to keep both nodes around.
Bill Wendlingc144a572009-01-30 23:36:47 +00005824 SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005825 MVT::Other, Chain, ReplStore);
Bill Wendlingc144a572009-01-30 23:36:47 +00005826
Nate Begemanb6aef5c2009-09-15 00:18:30 +00005827 // Make sure the new and old chains are cleaned up.
5828 AddToWorkList(Token.getNode());
5829
Jim Laskey274062c2006-10-13 23:32:28 +00005830 // Don't add users to work list.
5831 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00005832 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00005833 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005834
Evan Cheng33dbedc2006-11-05 09:31:14 +00005835 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00005836 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00005837 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00005838
Chris Lattner3c872852007-12-29 06:26:16 +00005839 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00005840 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005841 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00005842 // See if we can simplify the input to this truncstore with knowledge that
5843 // only the low bits are being used. For example:
5844 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Scott Michelfdc40a02009-02-17 22:15:04 +00005845 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005846 GetDemandedBits(Value,
Bill Wendlingc144a572009-01-30 23:36:47 +00005847 APInt::getLowBitsSet(Value.getValueSizeInBits(),
5848 ST->getMemoryVT().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00005849 AddToWorkList(Value.getNode());
5850 if (Shorter.getNode())
Bill Wendlingc144a572009-01-30 23:36:47 +00005851 return DAG.getTruncStore(Chain, N->getDebugLoc(), Shorter,
5852 Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005853 ST->getSrcValueOffset(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00005854 ST->isVolatile(), ST->isNonTemporal(),
5855 ST->getAlignment());
Scott Michelfdc40a02009-02-17 22:15:04 +00005856
Chris Lattnere33544c2007-10-13 06:58:48 +00005857 // Otherwise, see if we can simplify the operation with
5858 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00005859 if (SimplifyDemandedBits(Value,
5860 APInt::getLowBitsSet(
Dan Gohman2e141d72009-12-14 23:40:38 +00005861 Value.getValueType().getScalarType().getSizeInBits(),
Dan Gohman8f78e3c2010-03-10 21:04:53 +00005862 ST->getMemoryVT().getScalarType().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00005863 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00005864 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005865
Chris Lattner3c872852007-12-29 06:26:16 +00005866 // If this is a load followed by a store to the same location, then the store
5867 // is dead/noop.
5868 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005869 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00005870 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00005871 // There can't be any side effects between the load and store, such as
5872 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00005873 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00005874 // The store is dead, remove it.
5875 return Chain;
5876 }
5877 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005878
Chris Lattnerddf89562008-01-17 19:59:44 +00005879 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
5880 // truncating store. We can do this even if this is already a truncstore.
5881 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00005882 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00005883 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005884 ST->getMemoryVT())) {
Bill Wendlingc144a572009-01-30 23:36:47 +00005885 return DAG.getTruncStore(Chain, N->getDebugLoc(), Value.getOperand(0),
5886 Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005887 ST->getSrcValueOffset(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00005888 ST->isVolatile(), ST->isNonTemporal(),
5889 ST->getAlignment());
Chris Lattnerddf89562008-01-17 19:59:44 +00005890 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005891
Evan Cheng8b944d32009-05-28 00:35:15 +00005892 return ReduceLoadOpStoreWidth(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00005893}
5894
Dan Gohman475871a2008-07-27 21:46:04 +00005895SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
5896 SDValue InVec = N->getOperand(0);
5897 SDValue InVal = N->getOperand(1);
5898 SDValue EltNo = N->getOperand(2);
Scott Michelfdc40a02009-02-17 22:15:04 +00005899
Chris Lattnerca242442006-03-19 01:27:56 +00005900 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
5901 // vector with the inserted element.
5902 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005903 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Gabor Greif12632d22008-08-30 19:29:20 +00005904 SmallVector<SDValue, 8> Ops(InVec.getNode()->op_begin(),
5905 InVec.getNode()->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00005906 if (Elt < Ops.size())
5907 Ops[Elt] = InVal;
Evan Chenga87008d2009-02-25 22:49:59 +00005908 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
5909 InVec.getValueType(), &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00005910 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005911 // If the invec is an UNDEF and if EltNo is a constant, create a new
5912 // BUILD_VECTOR with undef elements and the inserted element.
5913 if (!LegalOperations && InVec.getOpcode() == ISD::UNDEF &&
5914 isa<ConstantSDNode>(EltNo)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005915 EVT VT = InVec.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005916 EVT EltVT = VT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00005917 unsigned NElts = VT.getVectorNumElements();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005918 SmallVector<SDValue, 8> Ops(NElts, DAG.getUNDEF(EltVT));
Scott Michelfdc40a02009-02-17 22:15:04 +00005919
Nate Begeman9008ca62009-04-27 18:41:29 +00005920 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5921 if (Elt < Ops.size())
5922 Ops[Elt] = InVal;
5923 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
5924 InVec.getValueType(), &Ops[0], Ops.size());
5925 }
Dan Gohman475871a2008-07-27 21:46:04 +00005926 return SDValue();
Chris Lattnerca242442006-03-19 01:27:56 +00005927}
5928
Dan Gohman475871a2008-07-27 21:46:04 +00005929SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00005930 // (vextract (scalar_to_vector val, 0) -> val
5931 SDValue InVec = N->getOperand(0);
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00005932
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00005933 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Mon P Wangc6654ec42010-02-01 19:03:18 +00005934 // Check if the result type doesn't match the inserted element type. A
5935 // SCALAR_TO_VECTOR may truncate the inserted element and the
5936 // EXTRACT_VECTOR_ELT may widen the extracted vector.
Owen Andersone50ed302009-08-10 22:56:29 +00005937 EVT EltVT = InVec.getValueType().getVectorElementType();
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00005938 SDValue InOp = InVec.getOperand(0);
Mon P Wangc6654ec42010-02-01 19:03:18 +00005939 EVT NVT = N->getValueType(0);
5940 if (InOp.getValueType() != NVT) {
5941 assert(InOp.getValueType().isInteger() && NVT.isInteger());
Mon P Wang87c46d82010-02-01 22:15:09 +00005942 return DAG.getSExtOrTrunc(InOp, InVec.getDebugLoc(), NVT);
Mon P Wangc6654ec42010-02-01 19:03:18 +00005943 }
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00005944 return InOp;
5945 }
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005946
5947 // Perform only after legalization to ensure build_vector / vector_shuffle
5948 // optimizations have already been done.
Duncan Sands25cf2272008-11-24 14:53:14 +00005949 if (!LegalOperations) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005950
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00005951 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
5952 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
5953 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Mon P Wange3bc6ae2009-01-18 06:43:40 +00005954 SDValue EltNo = N->getOperand(1);
Evan Cheng513da432007-10-06 08:19:55 +00005955
Evan Cheng513da432007-10-06 08:19:55 +00005956 if (isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005957 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00005958 bool NewLoad = false;
Mon P Wanga60b5232008-12-11 00:26:16 +00005959 bool BCNumEltsChanged = false;
Owen Andersone50ed302009-08-10 22:56:29 +00005960 EVT VT = InVec.getValueType();
5961 EVT ExtVT = VT.getVectorElementType();
5962 EVT LVT = ExtVT;
Bill Wendlingc144a572009-01-30 23:36:47 +00005963
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005964 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
Owen Andersone50ed302009-08-10 22:56:29 +00005965 EVT BCVT = InVec.getOperand(0).getValueType();
5966 if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005967 return SDValue();
Mon P Wanga60b5232008-12-11 00:26:16 +00005968 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
5969 BCNumEltsChanged = true;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005970 InVec = InVec.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005971 ExtVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005972 NewLoad = true;
5973 }
Evan Cheng513da432007-10-06 08:19:55 +00005974
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005975 LoadSDNode *LN0 = NULL;
Nate Begeman5a5ca152009-04-29 05:20:52 +00005976 const ShuffleVectorSDNode *SVN = NULL;
Bill Wendlingc144a572009-01-30 23:36:47 +00005977 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005978 LN0 = cast<LoadSDNode>(InVec);
Bill Wendlingc144a572009-01-30 23:36:47 +00005979 } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Owen Andersone50ed302009-08-10 22:56:29 +00005980 InVec.getOperand(0).getValueType() == ExtVT &&
Bill Wendlingc144a572009-01-30 23:36:47 +00005981 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005982 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
Nate Begeman5a5ca152009-04-29 05:20:52 +00005983 } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005984 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
5985 // =>
5986 // (load $addr+1*size)
Scott Michelfdc40a02009-02-17 22:15:04 +00005987
Mon P Wanga60b5232008-12-11 00:26:16 +00005988 // If the bit convert changed the number of elements, it is unsafe
5989 // to examine the mask.
5990 if (BCNumEltsChanged)
5991 return SDValue();
Nate Begeman5a5ca152009-04-29 05:20:52 +00005992
5993 // Select the input vector, guarding against out of range extract vector.
5994 unsigned NumElems = VT.getVectorNumElements();
5995 int Idx = (Elt > NumElems) ? -1 : SVN->getMaskElt(Elt);
5996 InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
5997
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005998 if (InVec.getOpcode() == ISD::BIT_CONVERT)
5999 InVec = InVec.getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00006000 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00006001 LN0 = cast<LoadSDNode>(InVec);
Ted Kremenekd0e88f32010-04-08 18:49:30 +00006002 Elt = (Idx < (int)NumElems) ? Idx : Idx - (int)NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00006003 }
6004 }
Bill Wendlingc144a572009-01-30 23:36:47 +00006005
Duncan Sandsec87aa82008-06-15 20:12:31 +00006006 if (!LN0 || !LN0->hasOneUse() || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00006007 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00006008
6009 unsigned Align = LN0->getAlignment();
6010 if (NewLoad) {
6011 // Check the resultant load doesn't need a higher alignment than the
6012 // original load.
Bill Wendlingc144a572009-01-30 23:36:47 +00006013 unsigned NewAlign =
Owen Anderson23b9b192009-08-12 00:36:31 +00006014 TLI.getTargetData()->getABITypeAlignment(LVT.getTypeForEVT(*DAG.getContext()));
Bill Wendlingc144a572009-01-30 23:36:47 +00006015
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006016 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00006017 return SDValue();
Bill Wendlingc144a572009-01-30 23:36:47 +00006018
Evan Cheng77f0b7a2008-05-13 08:35:03 +00006019 Align = NewAlign;
6020 }
6021
Dan Gohman475871a2008-07-27 21:46:04 +00006022 SDValue NewPtr = LN0->getBasePtr();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00006023 if (Elt) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006024 unsigned PtrOff = LVT.getSizeInBits() * Elt / 8;
Owen Andersone50ed302009-08-10 22:56:29 +00006025 EVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00006026 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00006027 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Bill Wendlingc144a572009-01-30 23:36:47 +00006028 NewPtr = DAG.getNode(ISD::ADD, N->getDebugLoc(), PtrType, NewPtr,
Evan Cheng77f0b7a2008-05-13 08:35:03 +00006029 DAG.getConstant(PtrOff, PtrType));
6030 }
Bill Wendlingc144a572009-01-30 23:36:47 +00006031
6032 return DAG.getLoad(LVT, N->getDebugLoc(), LN0->getChain(), NewPtr,
Evan Cheng77f0b7a2008-05-13 08:35:03 +00006033 LN0->getSrcValue(), LN0->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00006034 LN0->isVolatile(), LN0->isNonTemporal(), Align);
Evan Cheng513da432007-10-06 08:19:55 +00006035 }
Bill Wendlingc144a572009-01-30 23:36:47 +00006036
Dan Gohman475871a2008-07-27 21:46:04 +00006037 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00006038}
Evan Cheng513da432007-10-06 08:19:55 +00006039
Dan Gohman475871a2008-07-27 21:46:04 +00006040SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00006041 unsigned NumInScalars = N->getNumOperands();
Owen Andersone50ed302009-08-10 22:56:29 +00006042 EVT VT = N->getValueType(0);
Chris Lattnerca242442006-03-19 01:27:56 +00006043
Dan Gohman7f321562007-06-25 16:23:39 +00006044 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
6045 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
6046 // at most two distinct vectors, turn this into a shuffle node.
Dan Gohman475871a2008-07-27 21:46:04 +00006047 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00006048 for (unsigned i = 0; i != NumInScalars; ++i) {
6049 // Ignore undef inputs.
6050 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00006051
Dan Gohman7f321562007-06-25 16:23:39 +00006052 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00006053 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00006054 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00006055 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00006056 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00006057 break;
6058 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006059
Dan Gohman7f321562007-06-25 16:23:39 +00006060 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00006061 // we can't make a shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00006062 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00006063 if (ExtractedFromVec.getValueType() != VT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006064 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00006065 break;
6066 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006067
Chris Lattnerd7648c82006-03-28 20:28:38 +00006068 // Otherwise, remember this. We allow up to two distinct input vectors.
6069 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
6070 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00006071
Gabor Greifba36cb52008-08-28 21:40:38 +00006072 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00006073 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00006074 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00006075 VecIn2 = ExtractedFromVec;
6076 } else {
6077 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00006078 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00006079 break;
6080 }
6081 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006082
Chris Lattnerd7648c82006-03-28 20:28:38 +00006083 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00006084 if (VecIn1.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006085 SmallVector<int, 8> Mask;
Chris Lattnerd7648c82006-03-28 20:28:38 +00006086 for (unsigned i = 0; i != NumInScalars; ++i) {
6087 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006088 Mask.push_back(-1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00006089 continue;
6090 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006091
Rafael Espindola15684b22009-04-24 12:40:33 +00006092 // If extracting from the first vector, just use the index directly.
Nate Begeman9008ca62009-04-27 18:41:29 +00006093 SDValue Extract = N->getOperand(i);
Mon P Wang93b74152009-03-17 06:33:10 +00006094 SDValue ExtVal = Extract.getOperand(1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00006095 if (Extract.getOperand(0) == VecIn1) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00006096 unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue();
6097 if (ExtIndex > VT.getVectorNumElements())
6098 return SDValue();
6099
6100 Mask.push_back(ExtIndex);
Chris Lattnerd7648c82006-03-28 20:28:38 +00006101 continue;
6102 }
6103
6104 // Otherwise, use InIdx + VecSize
Mon P Wang93b74152009-03-17 06:33:10 +00006105 unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00006106 Mask.push_back(Idx+NumInScalars);
Chris Lattnerd7648c82006-03-28 20:28:38 +00006107 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006108
Chris Lattnerd7648c82006-03-28 20:28:38 +00006109 // Add count and size info.
Chris Lattner2392ae72010-04-15 04:48:01 +00006110 if (!isTypeLegal(VT))
Duncan Sands25cf2272008-11-24 14:53:14 +00006111 return SDValue();
6112
Dan Gohman7f321562007-06-25 16:23:39 +00006113 // Return the new VECTOR_SHUFFLE node.
Nate Begeman9008ca62009-04-27 18:41:29 +00006114 SDValue Ops[2];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006115 Ops[0] = VecIn1;
Nate Begeman9008ca62009-04-27 18:41:29 +00006116 Ops[1] = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
6117 return DAG.getVectorShuffle(VT, N->getDebugLoc(), Ops[0], Ops[1], &Mask[0]);
Chris Lattnerd7648c82006-03-28 20:28:38 +00006118 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006119
Dan Gohman475871a2008-07-27 21:46:04 +00006120 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00006121}
6122
Dan Gohman475871a2008-07-27 21:46:04 +00006123SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00006124 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
6125 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
6126 // inputs come from at most two distinct vectors, turn this into a shuffle
6127 // node.
6128
6129 // If we only have one input vector, we don't need to do any concatenation.
Bill Wendlingc144a572009-01-30 23:36:47 +00006130 if (N->getNumOperands() == 1)
Dan Gohman7f321562007-06-25 16:23:39 +00006131 return N->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00006132
Dan Gohman475871a2008-07-27 21:46:04 +00006133 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00006134}
6135
Dan Gohman475871a2008-07-27 21:46:04 +00006136SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006137 return SDValue();
6138
Owen Andersone50ed302009-08-10 22:56:29 +00006139 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00006140 unsigned NumElts = VT.getVectorNumElements();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00006141
Mon P Wangaeb06d22008-11-10 04:46:22 +00006142 SDValue N0 = N->getOperand(0);
Mon P Wangaeb06d22008-11-10 04:46:22 +00006143
6144 assert(N0.getValueType().getVectorNumElements() == NumElts &&
6145 "Vector shuffle must be normalized in DAG");
6146
Nate Begeman9008ca62009-04-27 18:41:29 +00006147 // FIXME: implement canonicalizations from DAG.getVectorShuffle()
Evan Chenge7bec0d2006-07-20 22:44:41 +00006148
Evan Cheng917ec982006-07-21 08:25:53 +00006149 // If it is a splat, check if the argument vector is a build_vector with
6150 // all scalar elements the same.
Nate Begeman9008ca62009-04-27 18:41:29 +00006151 if (cast<ShuffleVectorSDNode>(N)->isSplat()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006152 SDNode *V = N0.getNode();
Nate Begeman9008ca62009-04-27 18:41:29 +00006153
Evan Cheng917ec982006-07-21 08:25:53 +00006154
Dan Gohman7f321562007-06-25 16:23:39 +00006155 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00006156 // not the number of vector elements, look through it. Be careful not to
6157 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00006158 if (V->getOpcode() == ISD::BIT_CONVERT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006159 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00006160 if (ConvInput.getValueType().isVector() &&
6161 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +00006162 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +00006163 }
6164
Dan Gohman7f321562007-06-25 16:23:39 +00006165 if (V->getOpcode() == ISD::BUILD_VECTOR) {
6166 unsigned NumElems = V->getNumOperands();
Nate Begeman9008ca62009-04-27 18:41:29 +00006167 unsigned BaseIdx = cast<ShuffleVectorSDNode>(N)->getSplatIndex();
Evan Cheng917ec982006-07-21 08:25:53 +00006168 if (NumElems > BaseIdx) {
Dan Gohman475871a2008-07-27 21:46:04 +00006169 SDValue Base;
Evan Cheng917ec982006-07-21 08:25:53 +00006170 bool AllSame = true;
6171 for (unsigned i = 0; i != NumElems; ++i) {
6172 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
6173 Base = V->getOperand(i);
6174 break;
6175 }
6176 }
6177 // Splat of <u, u, u, u>, return <u, u, u, u>
Gabor Greifba36cb52008-08-28 21:40:38 +00006178 if (!Base.getNode())
Evan Cheng917ec982006-07-21 08:25:53 +00006179 return N0;
6180 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00006181 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00006182 AllSame = false;
6183 break;
6184 }
6185 }
6186 // Splat of <x, x, x, x>, return <x, x, x, x>
6187 if (AllSame)
6188 return N0;
6189 }
6190 }
6191 }
Dan Gohman475871a2008-07-27 21:46:04 +00006192 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00006193}
6194
Evan Cheng44f1f092006-04-20 08:56:16 +00006195/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00006196/// an AND to a vector_shuffle with the destination vector and a zero vector.
6197/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00006198/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00006199SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00006200 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00006201 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006202 SDValue LHS = N->getOperand(0);
6203 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00006204 if (N->getOpcode() == ISD::AND) {
6205 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00006206 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00006207 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006208 SmallVector<int, 8> Indices;
6209 unsigned NumElts = RHS.getNumOperands();
Evan Cheng44f1f092006-04-20 08:56:16 +00006210 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00006211 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00006212 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00006213 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00006214 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00006215 Indices.push_back(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00006216 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00006217 Indices.push_back(NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00006218 else
Dan Gohman475871a2008-07-27 21:46:04 +00006219 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00006220 }
6221
6222 // Let's see if the target supports this vector_shuffle.
Owen Andersone50ed302009-08-10 22:56:29 +00006223 EVT RVT = RHS.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00006224 if (!TLI.isVectorClearMaskLegal(Indices, RVT))
Dan Gohman475871a2008-07-27 21:46:04 +00006225 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00006226
Dan Gohman7f321562007-06-25 16:23:39 +00006227 // Return the new VECTOR_SHUFFLE node.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006228 EVT EltVT = RVT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00006229 SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00006230 DAG.getConstant(0, EltVT));
Nate Begeman9008ca62009-04-27 18:41:29 +00006231 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
6232 RVT, &ZeroOps[0], ZeroOps.size());
6233 LHS = DAG.getNode(ISD::BIT_CONVERT, dl, RVT, LHS);
6234 SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]);
6235 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Shuf);
Evan Cheng44f1f092006-04-20 08:56:16 +00006236 }
6237 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006238
Dan Gohman475871a2008-07-27 21:46:04 +00006239 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00006240}
6241
Dan Gohman7f321562007-06-25 16:23:39 +00006242/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00006243SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00006244 // After legalize, the target may be depending on adds and other
6245 // binary ops to provide legal ways to construct constants or other
6246 // things. Simplifying them may result in a loss of legality.
Duncan Sands25cf2272008-11-24 14:53:14 +00006247 if (LegalOperations) return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00006248
Owen Andersone50ed302009-08-10 22:56:29 +00006249 EVT VT = N->getValueType(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006250 assert(VT.isVector() && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00006251
Owen Andersone50ed302009-08-10 22:56:29 +00006252 EVT EltType = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00006253 SDValue LHS = N->getOperand(0);
6254 SDValue RHS = N->getOperand(1);
6255 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006256 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +00006257
Dan Gohman7f321562007-06-25 16:23:39 +00006258 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00006259 // this operation.
Scott Michelfdc40a02009-02-17 22:15:04 +00006260 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
Dan Gohman7f321562007-06-25 16:23:39 +00006261 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00006262 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00006263 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00006264 SDValue LHSOp = LHS.getOperand(i);
6265 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00006266 // If these two elements can't be folded, bail out.
6267 if ((LHSOp.getOpcode() != ISD::UNDEF &&
6268 LHSOp.getOpcode() != ISD::Constant &&
6269 LHSOp.getOpcode() != ISD::ConstantFP) ||
6270 (RHSOp.getOpcode() != ISD::UNDEF &&
6271 RHSOp.getOpcode() != ISD::Constant &&
6272 RHSOp.getOpcode() != ISD::ConstantFP))
6273 break;
Bill Wendling836ca7d2009-01-30 23:59:18 +00006274
Evan Cheng7b336a82006-05-31 06:08:35 +00006275 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00006276 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
6277 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00006278 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006279 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +00006280 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006281 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00006282 break;
6283 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006284
Bill Wendling9729c5a2009-01-31 03:12:48 +00006285 Ops.push_back(DAG.getNode(N->getOpcode(), LHS.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00006286 EltType, LHSOp, RHSOp));
Gabor Greifba36cb52008-08-28 21:40:38 +00006287 AddToWorkList(Ops.back().getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +00006288 assert((Ops.back().getOpcode() == ISD::UNDEF ||
6289 Ops.back().getOpcode() == ISD::Constant ||
6290 Ops.back().getOpcode() == ISD::ConstantFP) &&
6291 "Scalar binop didn't fold!");
6292 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006293
Dan Gohman7f321562007-06-25 16:23:39 +00006294 if (Ops.size() == LHS.getNumOperands()) {
Owen Andersone50ed302009-08-10 22:56:29 +00006295 EVT VT = LHS.getValueType();
Evan Chenga87008d2009-02-25 22:49:59 +00006296 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT,
6297 &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00006298 }
Chris Lattneredab1b92006-04-02 03:25:57 +00006299 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006300
Dan Gohman475871a2008-07-27 21:46:04 +00006301 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00006302}
6303
Bill Wendling836ca7d2009-01-30 23:59:18 +00006304SDValue DAGCombiner::SimplifySelect(DebugLoc DL, SDValue N0,
6305 SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00006306 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006307
Bill Wendling836ca7d2009-01-30 23:59:18 +00006308 SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00006309 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Bill Wendling836ca7d2009-01-30 23:59:18 +00006310
Nate Begemanf845b452005-10-08 00:29:44 +00006311 // If we got a simplified select_cc node back from SimplifySelectCC, then
6312 // break it down into a new SETCC node, and a new SELECT node, and then return
6313 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +00006314 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +00006315 // Check to see if we got a select_cc back (to turn into setcc/select).
6316 // Otherwise, just return whatever node we got back, like fabs.
6317 if (SCC.getOpcode() == ISD::SELECT_CC) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00006318 SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getDebugLoc(),
6319 N0.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006320 SCC.getOperand(0), SCC.getOperand(1),
Bill Wendling836ca7d2009-01-30 23:59:18 +00006321 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +00006322 AddToWorkList(SETCC.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00006323 return DAG.getNode(ISD::SELECT, SCC.getDebugLoc(), SCC.getValueType(),
6324 SCC.getOperand(2), SCC.getOperand(3), SETCC);
Nate Begemanf845b452005-10-08 00:29:44 +00006325 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006326
Nate Begemanf845b452005-10-08 00:29:44 +00006327 return SCC;
6328 }
Dan Gohman475871a2008-07-27 21:46:04 +00006329 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00006330}
6331
Chris Lattner40c62d52005-10-18 06:04:22 +00006332/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
6333/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00006334/// select. Callers of this should assume that TheSelect is deleted if this
6335/// returns true. As such, they should return the appropriate thing (e.g. the
6336/// node) back to the top-level of the DAG combiner loop to avoid it being
6337/// looked at.
Scott Michelfdc40a02009-02-17 22:15:04 +00006338bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
Dan Gohman475871a2008-07-27 21:46:04 +00006339 SDValue RHS) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006340
Chris Lattner40c62d52005-10-18 06:04:22 +00006341 // If this is a select from two identical things, try to pull the operation
6342 // through the select.
6343 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00006344 // If this is a load and the token chain is identical, replace the select
6345 // of two loads with a load through a select of the address to load from.
6346 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
6347 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00006348 if (LHS.getOpcode() == ISD::LOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00006349 // Do not let this transformation reduce the number of volatile loads.
6350 !cast<LoadSDNode>(LHS)->isVolatile() &&
6351 !cast<LoadSDNode>(RHS)->isVolatile() &&
Chris Lattner40c62d52005-10-18 06:04:22 +00006352 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00006353 LHS.getOperand(0) == RHS.getOperand(0)) {
6354 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
6355 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
6356
6357 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006358 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Dan Gohman75832d72009-10-31 14:14:04 +00006359 // FIXME: this discards src value information. This is
6360 // over-conservative. It would be beneficial to be able to remember
Mon P Wangfe240b12010-01-11 20:12:49 +00006361 // both potential memory locations. Since we are discarding
6362 // src value info, don't do the transformation if the memory
6363 // locations are not in the default address space.
6364 unsigned LLDAddrSpace = 0, RLDAddrSpace = 0;
6365 if (const Value *LLDVal = LLD->getMemOperand()->getValue()) {
6366 if (const PointerType *PT = dyn_cast<PointerType>(LLDVal->getType()))
6367 LLDAddrSpace = PT->getAddressSpace();
6368 }
6369 if (const Value *RLDVal = RLD->getMemOperand()->getValue()) {
6370 if (const PointerType *PT = dyn_cast<PointerType>(RLDVal->getType()))
6371 RLDAddrSpace = PT->getAddressSpace();
6372 }
Dan Gohman475871a2008-07-27 21:46:04 +00006373 SDValue Addr;
Mon P Wangfe240b12010-01-11 20:12:49 +00006374 if (LLDAddrSpace == 0 && RLDAddrSpace == 0) {
6375 if (TheSelect->getOpcode() == ISD::SELECT) {
6376 // Check that the condition doesn't reach either load. If so, folding
6377 // this will induce a cycle into the DAG.
6378 if ((!LLD->hasAnyUseOfValue(1) ||
6379 !LLD->isPredecessorOf(TheSelect->getOperand(0).getNode())) &&
6380 (!RLD->hasAnyUseOfValue(1) ||
6381 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode()))) {
6382 Addr = DAG.getNode(ISD::SELECT, TheSelect->getDebugLoc(),
6383 LLD->getBasePtr().getValueType(),
6384 TheSelect->getOperand(0), LLD->getBasePtr(),
6385 RLD->getBasePtr());
6386 }
6387 } else {
6388 // Check that the condition doesn't reach either load. If so, folding
6389 // this will induce a cycle into the DAG.
6390 if ((!LLD->hasAnyUseOfValue(1) ||
6391 (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
6392 !LLD->isPredecessorOf(TheSelect->getOperand(1).getNode()))) &&
6393 (!RLD->hasAnyUseOfValue(1) ||
6394 (!RLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
6395 !RLD->isPredecessorOf(TheSelect->getOperand(1).getNode())))) {
6396 Addr = DAG.getNode(ISD::SELECT_CC, TheSelect->getDebugLoc(),
6397 LLD->getBasePtr().getValueType(),
6398 TheSelect->getOperand(0),
6399 TheSelect->getOperand(1),
6400 LLD->getBasePtr(), RLD->getBasePtr(),
6401 TheSelect->getOperand(4));
6402 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00006403 }
Evan Cheng466685d2006-10-09 20:57:25 +00006404 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006405
Gabor Greifba36cb52008-08-28 21:40:38 +00006406 if (Addr.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006407 SDValue Load;
Bill Wendling836ca7d2009-01-30 23:59:18 +00006408 if (LLD->getExtensionType() == ISD::NON_EXTLOAD) {
6409 Load = DAG.getLoad(TheSelect->getValueType(0),
6410 TheSelect->getDebugLoc(),
6411 LLD->getChain(),
Dan Gohman75832d72009-10-31 14:14:04 +00006412 Addr, 0, 0,
Scott Michelfdc40a02009-02-17 22:15:04 +00006413 LLD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00006414 LLD->isNonTemporal(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00006415 LLD->getAlignment());
Bill Wendling836ca7d2009-01-30 23:59:18 +00006416 } else {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00006417 Load = DAG.getExtLoad(LLD->getExtensionType(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00006418 TheSelect->getDebugLoc(),
Chris Lattnerc4e664b2007-01-16 05:59:59 +00006419 TheSelect->getValueType(0),
Dan Gohman75832d72009-10-31 14:14:04 +00006420 LLD->getChain(), Addr, 0, 0,
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006421 LLD->getMemoryVT(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006422 LLD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00006423 LLD->isNonTemporal(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00006424 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00006425 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006426
Chris Lattnerc4e664b2007-01-16 05:59:59 +00006427 // Users of the select now use the result of the load.
6428 CombineTo(TheSelect, Load);
Scott Michelfdc40a02009-02-17 22:15:04 +00006429
Chris Lattnerc4e664b2007-01-16 05:59:59 +00006430 // Users of the old loads now use the new load's chain. We know the
6431 // old-load value is dead now.
Gabor Greifba36cb52008-08-28 21:40:38 +00006432 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
6433 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00006434 return true;
6435 }
Evan Chengc5484282006-10-04 00:56:09 +00006436 }
Chris Lattner40c62d52005-10-18 06:04:22 +00006437 }
6438 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006439
Chris Lattner40c62d52005-10-18 06:04:22 +00006440 return false;
6441}
6442
Chris Lattner600fec32009-03-11 05:08:08 +00006443/// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3
6444/// where 'cond' is the comparison specified by CC.
Scott Michelfdc40a02009-02-17 22:15:04 +00006445SDValue DAGCombiner::SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1,
Dan Gohman475871a2008-07-27 21:46:04 +00006446 SDValue N2, SDValue N3,
6447 ISD::CondCode CC, bool NotExtCompare) {
Chris Lattner600fec32009-03-11 05:08:08 +00006448 // (x ? y : y) -> y.
6449 if (N2 == N3) return N2;
6450
Owen Andersone50ed302009-08-10 22:56:29 +00006451 EVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +00006452 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
6453 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
6454 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00006455
6456 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00006457 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00006458 N0, N1, CC, DL, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00006459 if (SCC.getNode()) AddToWorkList(SCC.getNode());
6460 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00006461
6462 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00006463 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00006464 return N2;
6465 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00006466 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00006467 return N3;
Scott Michelfdc40a02009-02-17 22:15:04 +00006468
Nate Begemanf845b452005-10-08 00:29:44 +00006469 // Check to see if we can simplify the select into an fabs node
6470 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
6471 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00006472 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00006473 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
6474 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
6475 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
6476 N2 == N3.getOperand(0))
Bill Wendling836ca7d2009-01-30 23:59:18 +00006477 return DAG.getNode(ISD::FABS, DL, VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006478
Nate Begemanf845b452005-10-08 00:29:44 +00006479 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
6480 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
6481 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
6482 N2.getOperand(0) == N3)
Bill Wendling836ca7d2009-01-30 23:59:18 +00006483 return DAG.getNode(ISD::FABS, DL, VT, N3);
Nate Begemanf845b452005-10-08 00:29:44 +00006484 }
6485 }
Chris Lattner600fec32009-03-11 05:08:08 +00006486
6487 // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)"
6488 // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0
6489 // in it. This is a win when the constant is not otherwise available because
6490 // it replaces two constant pool loads with one. We only do this if the FP
6491 // type is known to be legal, because if it isn't, then we are before legalize
6492 // types an we want the other legalization to happen first (e.g. to avoid
Mon P Wang0b7a7862009-03-14 00:25:19 +00006493 // messing with soft float) and if the ConstantFP is not legal, because if
6494 // it is legal, we may not need to store the FP constant in a constant pool.
Chris Lattner600fec32009-03-11 05:08:08 +00006495 if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2))
6496 if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) {
6497 if (TLI.isTypeLegal(N2.getValueType()) &&
Mon P Wang0b7a7862009-03-14 00:25:19 +00006498 (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) !=
6499 TargetLowering::Legal) &&
Chris Lattner600fec32009-03-11 05:08:08 +00006500 // If both constants have multiple uses, then we won't need to do an
6501 // extra load, they are likely around in registers for other users.
6502 (TV->hasOneUse() || FV->hasOneUse())) {
6503 Constant *Elts[] = {
6504 const_cast<ConstantFP*>(FV->getConstantFPValue()),
6505 const_cast<ConstantFP*>(TV->getConstantFPValue())
6506 };
6507 const Type *FPTy = Elts[0]->getType();
6508 const TargetData &TD = *TLI.getTargetData();
6509
6510 // Create a ConstantArray of the two constants.
Owen Andersondebcb012009-07-29 22:17:13 +00006511 Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts, 2);
Chris Lattner600fec32009-03-11 05:08:08 +00006512 SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(),
6513 TD.getPrefTypeAlignment(FPTy));
Evan Cheng1606e8e2009-03-13 07:51:59 +00006514 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner600fec32009-03-11 05:08:08 +00006515
6516 // Get the offsets to the 0 and 1 element of the array so that we can
6517 // select between them.
6518 SDValue Zero = DAG.getIntPtrConstant(0);
Duncan Sands777d2302009-05-09 07:06:46 +00006519 unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType());
Chris Lattner600fec32009-03-11 05:08:08 +00006520 SDValue One = DAG.getIntPtrConstant(EltSize);
6521
6522 SDValue Cond = DAG.getSetCC(DL,
6523 TLI.getSetCCResultType(N0.getValueType()),
6524 N0, N1, CC);
6525 SDValue CstOffset = DAG.getNode(ISD::SELECT, DL, Zero.getValueType(),
6526 Cond, One, Zero);
6527 CPIdx = DAG.getNode(ISD::ADD, DL, TLI.getPointerTy(), CPIdx,
6528 CstOffset);
6529 return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx,
6530 PseudoSourceValue::getConstantPool(), 0, false,
David Greene1e559442010-02-15 17:00:31 +00006531 false, Alignment);
Chris Lattner600fec32009-03-11 05:08:08 +00006532
6533 }
6534 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006535
Nate Begemanf845b452005-10-08 00:29:44 +00006536 // Check to see if we can perform the "gzip trick", transforming
Bill Wendling836ca7d2009-01-30 23:59:18 +00006537 // (select_cc setlt X, 0, A, 0) -> (and (sra X, (sub size(X), 1), A)
Chris Lattnere3152e52006-09-20 06:41:35 +00006538 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00006539 N0.getValueType().isInteger() &&
6540 N2.getValueType().isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00006541 (N1C->isNullValue() || // (a < 0) ? b : 0
6542 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Owen Andersone50ed302009-08-10 22:56:29 +00006543 EVT XType = N0.getValueType();
6544 EVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00006545 if (XType.bitsGE(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00006546 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00006547 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00006548 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
6549 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006550 ShCtV = XType.getSizeInBits()-ShCtV-1;
Duncan Sands92abc622009-01-31 15:50:11 +00006551 SDValue ShCt = DAG.getConstant(ShCtV, getShiftAmountTy());
Bill Wendling9729c5a2009-01-31 03:12:48 +00006552 SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00006553 XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +00006554 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00006555
Duncan Sands8e4eb092008-06-08 20:54:56 +00006556 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00006557 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00006558 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00006559 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006560
6561 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00006562 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006563
Bill Wendling9729c5a2009-01-31 03:12:48 +00006564 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00006565 XType, N0,
6566 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00006567 getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00006568 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00006569
Duncan Sands8e4eb092008-06-08 20:54:56 +00006570 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00006571 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00006572 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00006573 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006574
6575 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00006576 }
6577 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006578
Nate Begeman07ed4172005-10-10 21:26:48 +00006579 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00006580 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands03228082008-11-23 15:47:28 +00006581 TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006582
Chris Lattner1eba01e2007-04-11 06:50:51 +00006583 // If the caller doesn't want us to simplify this into a zext of a compare,
6584 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00006585 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00006586 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00006587
Nate Begeman07ed4172005-10-10 21:26:48 +00006588 // Get a SetCC of the condition
6589 // FIXME: Should probably make sure that setcc is legal if we ever have a
6590 // target where it isn't.
Dan Gohman475871a2008-07-27 21:46:04 +00006591 SDValue Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00006592 // cast from setcc result type to select result type
Duncan Sands25cf2272008-11-24 14:53:14 +00006593 if (LegalTypes) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00006594 SCC = DAG.getSetCC(DL, TLI.getSetCCResultType(N0.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00006595 N0, N1, CC);
Duncan Sands8e4eb092008-06-08 20:54:56 +00006596 if (N2.getValueType().bitsLT(SCC.getValueType()))
Bill Wendling9729c5a2009-01-31 03:12:48 +00006597 Temp = DAG.getZeroExtendInReg(SCC, N2.getDebugLoc(), N2.getValueType());
Chris Lattner555d8d62006-12-07 22:36:47 +00006598 else
Bill Wendling9729c5a2009-01-31 03:12:48 +00006599 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00006600 N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00006601 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00006602 SCC = DAG.getSetCC(N0.getDebugLoc(), MVT::i1, N0, N1, CC);
Bill Wendling9729c5a2009-01-31 03:12:48 +00006603 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00006604 N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00006605 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006606
Gabor Greifba36cb52008-08-28 21:40:38 +00006607 AddToWorkList(SCC.getNode());
6608 AddToWorkList(Temp.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006609
Dan Gohman002e5d02008-03-13 22:13:53 +00006610 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00006611 return Temp;
Bill Wendling836ca7d2009-01-30 23:59:18 +00006612
Nate Begeman07ed4172005-10-10 21:26:48 +00006613 // shl setcc result by log2 n2c
Bill Wendling836ca7d2009-01-30 23:59:18 +00006614 return DAG.getNode(ISD::SHL, DL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00006615 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Duncan Sands92abc622009-01-31 15:50:11 +00006616 getShiftAmountTy()));
Nate Begeman07ed4172005-10-10 21:26:48 +00006617 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006618
Nate Begemanf845b452005-10-08 00:29:44 +00006619 // Check to see if this is the equivalent of setcc
6620 // FIXME: Turn all of these into setcc if setcc if setcc is legal
6621 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00006622 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Owen Andersone50ed302009-08-10 22:56:29 +00006623 EVT XType = N0.getValueType();
Duncan Sands25cf2272008-11-24 14:53:14 +00006624 if (!LegalOperations ||
Duncan Sands5480c042009-01-01 15:52:00 +00006625 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(XType))) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00006626 SDValue Res = DAG.getSetCC(DL, TLI.getSetCCResultType(XType), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00006627 if (Res.getValueType() != VT)
Bill Wendling836ca7d2009-01-30 23:59:18 +00006628 Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res);
Nate Begemanf845b452005-10-08 00:29:44 +00006629 return Res;
6630 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006631
Bill Wendling836ca7d2009-01-30 23:59:18 +00006632 // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X))))
Scott Michelfdc40a02009-02-17 22:15:04 +00006633 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands25cf2272008-11-24 14:53:14 +00006634 (!LegalOperations ||
Duncan Sands184a8762008-06-14 17:48:34 +00006635 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00006636 SDValue Ctlz = DAG.getNode(ISD::CTLZ, N0.getDebugLoc(), XType, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006637 return DAG.getNode(ISD::SRL, DL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006638 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Duncan Sands92abc622009-01-31 15:50:11 +00006639 getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00006640 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006641 // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1))
Scott Michelfdc40a02009-02-17 22:15:04 +00006642 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00006643 SDValue NegN0 = DAG.getNode(ISD::SUB, N0.getDebugLoc(),
6644 XType, DAG.getConstant(0, XType), N0);
Bill Wendling7581bfa2009-01-30 23:03:19 +00006645 SDValue NotN0 = DAG.getNOT(N0.getDebugLoc(), N0, XType);
Bill Wendling836ca7d2009-01-30 23:59:18 +00006646 return DAG.getNode(ISD::SRL, DL, XType,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00006647 DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00006648 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00006649 getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00006650 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006651 // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1))
Nate Begemanf845b452005-10-08 00:29:44 +00006652 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00006653 SDValue Sign = DAG.getNode(ISD::SRL, N0.getDebugLoc(), XType, N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +00006654 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00006655 getShiftAmountTy()));
Bill Wendling836ca7d2009-01-30 23:59:18 +00006656 return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType));
Nate Begemanf845b452005-10-08 00:29:44 +00006657 }
6658 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006659
Nate Begemanf845b452005-10-08 00:29:44 +00006660 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
6661 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
6662 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00006663 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00006664 N2.getOperand(0) == N1 && N0.getValueType().isInteger()) {
Owen Andersone50ed302009-08-10 22:56:29 +00006665 EVT XType = N0.getValueType();
Bill Wendling9729c5a2009-01-31 03:12:48 +00006666 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(), XType, N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +00006667 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00006668 getShiftAmountTy()));
Bill Wendling9729c5a2009-01-31 03:12:48 +00006669 SDValue Add = DAG.getNode(ISD::ADD, N0.getDebugLoc(), XType,
Bill Wendling836ca7d2009-01-30 23:59:18 +00006670 N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00006671 AddToWorkList(Shift.getNode());
6672 AddToWorkList(Add.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00006673 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Chris Lattner1982ef22007-04-11 05:11:38 +00006674 }
6675 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
6676 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
6677 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
6678 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
6679 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Owen Andersone50ed302009-08-10 22:56:29 +00006680 EVT XType = N0.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006681 if (SubC->isNullValue() && XType.isInteger()) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00006682 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(), XType,
Bill Wendling836ca7d2009-01-30 23:59:18 +00006683 N0,
6684 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00006685 getShiftAmountTy()));
Bill Wendling9729c5a2009-01-31 03:12:48 +00006686 SDValue Add = DAG.getNode(ISD::ADD, N0.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00006687 XType, N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00006688 AddToWorkList(Shift.getNode());
6689 AddToWorkList(Add.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00006690 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Nate Begemanf845b452005-10-08 00:29:44 +00006691 }
6692 }
6693 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006694
Dan Gohman475871a2008-07-27 21:46:04 +00006695 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00006696}
6697
Evan Chengfa1eb272007-02-08 22:13:59 +00006698/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Owen Andersone50ed302009-08-10 22:56:29 +00006699SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0,
Dan Gohman475871a2008-07-27 21:46:04 +00006700 SDValue N1, ISD::CondCode Cond,
Dale Johannesenff97d4f2009-02-03 00:47:48 +00006701 DebugLoc DL, bool foldBooleans) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006702 TargetLowering::DAGCombinerInfo
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00006703 DagCombineInfo(DAG, !LegalTypes, !LegalOperations, false, this);
Dale Johannesenff97d4f2009-02-03 00:47:48 +00006704 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL);
Nate Begeman452d7be2005-09-16 00:54:12 +00006705}
6706
Nate Begeman69575232005-10-20 02:15:44 +00006707/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
6708/// return a DAG expression to select that will generate the same value by
6709/// multiplying by a magic number. See:
6710/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00006711SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00006712 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00006713 SDValue S = TLI.BuildSDIV(N, DAG, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00006714
Andrew Lenharth232c9102006-06-12 16:07:18 +00006715 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00006716 ii != ee; ++ii)
6717 AddToWorkList(*ii);
6718 return S;
Nate Begeman69575232005-10-20 02:15:44 +00006719}
6720
6721/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
6722/// return a DAG expression to select that will generate the same value by
6723/// multiplying by a magic number. See:
6724/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00006725SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00006726 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00006727 SDValue S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00006728
Andrew Lenharth232c9102006-06-12 16:07:18 +00006729 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00006730 ii != ee; ++ii)
6731 AddToWorkList(*ii);
6732 return S;
Nate Begeman69575232005-10-20 02:15:44 +00006733}
6734
Nate Begemancc66cdd2009-09-25 06:05:26 +00006735/// FindBaseOffset - Return true if base is a frame index, which is known not
6736// to alias with anything but itself. Provides base object and offset as results.
6737static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset,
Dan Gohman46510a72010-04-15 01:51:59 +00006738 const GlobalValue *&GV, void *&CV) {
Jim Laskey71382342006-10-07 23:37:56 +00006739 // Assume it is a primitive operation.
Nate Begemancc66cdd2009-09-25 06:05:26 +00006740 Base = Ptr; Offset = 0; GV = 0; CV = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00006741
Jim Laskey71382342006-10-07 23:37:56 +00006742 // If it's an adding a simple constant then integrate the offset.
6743 if (Base.getOpcode() == ISD::ADD) {
6744 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
6745 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006746 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +00006747 }
6748 }
Nate Begemancc66cdd2009-09-25 06:05:26 +00006749
6750 // Return the underlying GlobalValue, and update the Offset. Return false
6751 // for GlobalAddressSDNode since the same GlobalAddress may be represented
6752 // by multiple nodes with different offsets.
6753 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Base)) {
6754 GV = G->getGlobal();
6755 Offset += G->getOffset();
6756 return false;
6757 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006758
Nate Begemancc66cdd2009-09-25 06:05:26 +00006759 // Return the underlying Constant value, and update the Offset. Return false
6760 // for ConstantSDNodes since the same constant pool entry may be represented
6761 // by multiple nodes with different offsets.
6762 if (ConstantPoolSDNode *C = dyn_cast<ConstantPoolSDNode>(Base)) {
6763 CV = C->isMachineConstantPoolEntry() ? (void *)C->getMachineCPVal()
6764 : (void *)C->getConstVal();
6765 Offset += C->getOffset();
6766 return false;
6767 }
Jim Laskey71382342006-10-07 23:37:56 +00006768 // If it's any of the following then it can't alias with anything but itself.
Nate Begemancc66cdd2009-09-25 06:05:26 +00006769 return isa<FrameIndexSDNode>(Base);
Jim Laskey71382342006-10-07 23:37:56 +00006770}
6771
6772/// isAlias - Return true if there is any possibility that the two addresses
6773/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +00006774bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +00006775 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006776 unsigned SrcValueAlign1,
Dan Gohman475871a2008-07-27 21:46:04 +00006777 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006778 const Value *SrcValue2, int SrcValueOffset2,
6779 unsigned SrcValueAlign2) const {
Jim Laskey71382342006-10-07 23:37:56 +00006780 // If they are the same then they must be aliases.
6781 if (Ptr1 == Ptr2) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00006782
Jim Laskey71382342006-10-07 23:37:56 +00006783 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +00006784 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +00006785 int64_t Offset1, Offset2;
Dan Gohman46510a72010-04-15 01:51:59 +00006786 const GlobalValue *GV1, *GV2;
Nate Begemancc66cdd2009-09-25 06:05:26 +00006787 void *CV1, *CV2;
6788 bool isFrameIndex1 = FindBaseOffset(Ptr1, Base1, Offset1, GV1, CV1);
6789 bool isFrameIndex2 = FindBaseOffset(Ptr2, Base2, Offset2, GV2, CV2);
Scott Michelfdc40a02009-02-17 22:15:04 +00006790
Nate Begemancc66cdd2009-09-25 06:05:26 +00006791 // If they have a same base address then check to see if they overlap.
6792 if (Base1 == Base2 || (GV1 && (GV1 == GV2)) || (CV1 && (CV1 == CV2)))
Bill Wendling836ca7d2009-01-30 23:59:18 +00006793 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006794
Nate Begemancc66cdd2009-09-25 06:05:26 +00006795 // If we know what the bases are, and they aren't identical, then we know they
6796 // cannot alias.
6797 if ((isFrameIndex1 || CV1 || GV1) && (isFrameIndex2 || CV2 || GV2))
6798 return false;
Jim Laskey096c22e2006-10-18 12:29:57 +00006799
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006800 // If we know required SrcValue1 and SrcValue2 have relatively large alignment
6801 // compared to the size and offset of the access, we may be able to prove they
6802 // do not alias. This check is conservative for now to catch cases created by
6803 // splitting vector types.
6804 if ((SrcValueAlign1 == SrcValueAlign2) &&
6805 (SrcValueOffset1 != SrcValueOffset2) &&
6806 (Size1 == Size2) && (SrcValueAlign1 > Size1)) {
6807 int64_t OffAlign1 = SrcValueOffset1 % SrcValueAlign1;
6808 int64_t OffAlign2 = SrcValueOffset2 % SrcValueAlign1;
6809
6810 // There is no overlap between these relatively aligned accesses of similar
6811 // size, return no alias.
6812 if ((OffAlign1 + Size1) <= OffAlign2 || (OffAlign2 + Size2) <= OffAlign1)
6813 return false;
6814 }
6815
Jim Laskey07a27092006-10-18 19:08:31 +00006816 if (CombinerGlobalAA) {
6817 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00006818 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
6819 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
6820 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Scott Michelfdc40a02009-02-17 22:15:04 +00006821 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00006822 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00006823 if (AAResult == AliasAnalysis::NoAlias)
6824 return false;
6825 }
Jim Laskey096c22e2006-10-18 12:29:57 +00006826
6827 // Otherwise we have to assume they alias.
6828 return true;
Jim Laskey71382342006-10-07 23:37:56 +00006829}
6830
6831/// FindAliasInfo - Extracts the relevant alias information from the memory
6832/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00006833bool DAGCombiner::FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +00006834 SDValue &Ptr, int64_t &Size,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006835 const Value *&SrcValue,
6836 int &SrcValueOffset,
6837 unsigned &SrcValueAlign) const {
Jim Laskey7ca56af2006-10-11 13:47:09 +00006838 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
6839 Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006840 Size = LD->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00006841 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00006842 SrcValueOffset = LD->getSrcValueOffset();
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006843 SrcValueAlign = LD->getOriginalAlignment();
Jim Laskey71382342006-10-07 23:37:56 +00006844 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00006845 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00006846 Ptr = ST->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006847 Size = ST->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00006848 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00006849 SrcValueOffset = ST->getSrcValueOffset();
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006850 SrcValueAlign = ST->getOriginalAlignment();
Jim Laskey7ca56af2006-10-11 13:47:09 +00006851 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00006852 llvm_unreachable("FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00006853 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006854
Jim Laskey71382342006-10-07 23:37:56 +00006855 return false;
6856}
6857
Jim Laskey6ff23e52006-10-04 16:53:27 +00006858/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
6859/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +00006860void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
6861 SmallVector<SDValue, 8> &Aliases) {
6862 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006863 SmallPtrSet<SDNode *, 16> Visited; // Visited node set.
Scott Michelfdc40a02009-02-17 22:15:04 +00006864
Jim Laskey279f0532006-09-25 16:29:54 +00006865 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +00006866 SDValue Ptr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006867 int64_t Size;
6868 const Value *SrcValue;
6869 int SrcValueOffset;
6870 unsigned SrcValueAlign;
6871 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset,
6872 SrcValueAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00006873
Jim Laskey6ff23e52006-10-04 16:53:27 +00006874 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00006875 Chains.push_back(OriginalChain);
Nate Begeman677c89d2009-10-12 05:53:58 +00006876 unsigned Depth = 0;
6877
Jim Laskeybc588b82006-10-05 15:07:25 +00006878 // Look at each chain and determine if it is an alias. If so, add it to the
6879 // aliases list. If not, then continue up the chain looking for the next
Scott Michelfdc40a02009-02-17 22:15:04 +00006880 // candidate.
Jim Laskeybc588b82006-10-05 15:07:25 +00006881 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006882 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +00006883 Chains.pop_back();
Nate Begeman677c89d2009-10-12 05:53:58 +00006884
6885 // For TokenFactor nodes, look at each operand and only continue up the
6886 // chain until we find two aliases. If we've seen two aliases, assume we'll
6887 // find more and revert to original chain since the xform is unlikely to be
6888 // profitable.
6889 //
6890 // FIXME: The depth check could be made to return the last non-aliasing
6891 // chain we found before we hit a tokenfactor rather than the original
6892 // chain.
6893 if (Depth > 6 || Aliases.size() == 2) {
6894 Aliases.clear();
6895 Aliases.push_back(OriginalChain);
6896 break;
6897 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006898
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006899 // Don't bother if we've been before.
6900 if (!Visited.insert(Chain.getNode()))
6901 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00006902
Jim Laskeybc588b82006-10-05 15:07:25 +00006903 switch (Chain.getOpcode()) {
6904 case ISD::EntryToken:
6905 // Entry token is ideal chain operand, but handled in FindBetterChain.
6906 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006907
Jim Laskeybc588b82006-10-05 15:07:25 +00006908 case ISD::LOAD:
6909 case ISD::STORE: {
6910 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006911 SDValue OpPtr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006912 int64_t OpSize;
6913 const Value *OpSrcValue;
6914 int OpSrcValueOffset;
6915 unsigned OpSrcValueAlign;
Gabor Greifba36cb52008-08-28 21:40:38 +00006916 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006917 OpSrcValue, OpSrcValueOffset,
6918 OpSrcValueAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00006919
Jim Laskeybc588b82006-10-05 15:07:25 +00006920 // If chain is alias then stop here.
6921 if (!(IsLoad && IsOpLoad) &&
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006922 isAlias(Ptr, Size, SrcValue, SrcValueOffset, SrcValueAlign,
6923 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset,
6924 OpSrcValueAlign)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00006925 Aliases.push_back(Chain);
6926 } else {
6927 // Look further up the chain.
Scott Michelfdc40a02009-02-17 22:15:04 +00006928 Chains.push_back(Chain.getOperand(0));
Nate Begeman677c89d2009-10-12 05:53:58 +00006929 ++Depth;
Jim Laskey279f0532006-09-25 16:29:54 +00006930 }
Jim Laskeybc588b82006-10-05 15:07:25 +00006931 break;
6932 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006933
Jim Laskeybc588b82006-10-05 15:07:25 +00006934 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006935 // We have to check each of the operands of the token factor for "small"
6936 // token factors, so we queue them up. Adding the operands to the queue
6937 // (stack) in reverse order maintains the original order and increases the
6938 // likelihood that getNode will find a matching token factor (CSE.)
6939 if (Chain.getNumOperands() > 16) {
6940 Aliases.push_back(Chain);
6941 break;
6942 }
Jim Laskeybc588b82006-10-05 15:07:25 +00006943 for (unsigned n = Chain.getNumOperands(); n;)
6944 Chains.push_back(Chain.getOperand(--n));
Nate Begeman677c89d2009-10-12 05:53:58 +00006945 ++Depth;
Jim Laskeybc588b82006-10-05 15:07:25 +00006946 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006947
Jim Laskeybc588b82006-10-05 15:07:25 +00006948 default:
6949 // For all other instructions we will just have to take what we can get.
6950 Aliases.push_back(Chain);
6951 break;
Jim Laskey279f0532006-09-25 16:29:54 +00006952 }
6953 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00006954}
6955
6956/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
6957/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +00006958SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
6959 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00006960
Jim Laskey6ff23e52006-10-04 16:53:27 +00006961 // Accumulate all the aliases to this node.
6962 GatherAllAliases(N, OldChain, Aliases);
Scott Michelfdc40a02009-02-17 22:15:04 +00006963
Jim Laskey6ff23e52006-10-04 16:53:27 +00006964 if (Aliases.size() == 0) {
6965 // If no operands then chain to entry token.
6966 return DAG.getEntryNode();
6967 } else if (Aliases.size() == 1) {
6968 // If a single operand then chain to it. We don't need to revisit it.
6969 return Aliases[0];
6970 }
Nate Begeman677c89d2009-10-12 05:53:58 +00006971
Jim Laskey6ff23e52006-10-04 16:53:27 +00006972 // Construct a custom tailored token factor.
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006973 return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other,
6974 &Aliases[0], Aliases.size());
Jim Laskey279f0532006-09-25 16:29:54 +00006975}
6976
Nate Begeman1d4d4142005-09-01 00:19:25 +00006977// SelectionDAG::Combine - This is the entry point for the file.
6978//
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00006979void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA,
Bill Wendling98a366d2009-04-29 23:29:43 +00006980 CodeGenOpt::Level OptLevel) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00006981 /// run - This is the main entry point to this class.
6982 ///
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00006983 DAGCombiner(*this, AA, OptLevel).Run(Level);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006984}