blob: b355cc9a6483a3efcc8756157ce648fb14561017 [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 Lattnerc76d4412007-05-16 06:37:59 +000021#include "llvm/ADT/SmallPtrSet.h"
22#include "llvm/ADT/Statistic.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/Analysis/AliasAnalysis.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000026#include "llvm/IR/DataLayout.h"
27#include "llvm/IR/DerivedTypes.h"
28#include "llvm/IR/Function.h"
29#include "llvm/IR/LLVMContext.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000030#include "llvm/Support/CommandLine.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000031#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000032#include "llvm/Support/ErrorHandling.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000033#include "llvm/Support/MathExtras.h"
Chris Lattnerbbbfa992009-08-23 06:35:02 +000034#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000035#include "llvm/Target/TargetLowering.h"
36#include "llvm/Target/TargetMachine.h"
37#include "llvm/Target/TargetOptions.h"
Hal Finkel253acef2013-08-29 03:29:55 +000038#include "llvm/Target/TargetSubtargetInfo.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");
Evan Cheng31959b12011-02-02 01:06:55 +000046STATISTIC(LdStFP2Int , "Number of fp load/store pairs transformed to int");
Chris Lattnercd3245a2006-12-19 22:41:21 +000047
Nate Begeman1d4d4142005-09-01 00:19:25 +000048namespace {
Jim Laskey71382342006-10-07 23:37:56 +000049 static cl::opt<bool>
Owen Anderson0dcc8142010-09-19 21:01:26 +000050 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000051 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000052
Jim Laskey07a27092006-10-18 19:08:31 +000053 static cl::opt<bool>
54 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
55 cl::desc("Include global information in alias analysis"));
56
Jim Laskeybc588b82006-10-05 15:07:25 +000057//------------------------------ DAGCombiner ---------------------------------//
58
Nick Lewycky6726b6d2009-10-25 06:33:48 +000059 class DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000060 SelectionDAG &DAG;
Dan Gohman79ce2762009-01-15 19:20:50 +000061 const TargetLowering &TLI;
Duncan Sands25cf2272008-11-24 14:53:14 +000062 CombineLevel Level;
Bill Wendling98a366d2009-04-29 23:29:43 +000063 CodeGenOpt::Level OptLevel;
Duncan Sands25cf2272008-11-24 14:53:14 +000064 bool LegalOperations;
65 bool LegalTypes;
Nate Begeman1d4d4142005-09-01 00:19:25 +000066
67 // Worklist of all of the nodes that need to be simplified.
James Molloy6660c052012-02-16 09:17:04 +000068 //
69 // This has the semantics that when adding to the worklist,
70 // the item added must be next to be processed. It should
71 // also only appear once. The naive approach to this takes
72 // linear time.
73 //
74 // To reduce the insert/remove time to logarithmic, we use
75 // a set and a vector to maintain our worklist.
76 //
77 // The set contains the items on the worklist, but does not
78 // maintain the order they should be visited.
79 //
80 // The vector maintains the order nodes should be visited, but may
81 // contain duplicate or removed nodes. When choosing a node to
82 // visit, we pop off the order stack until we find an item that is
83 // also in the contents set. All operations are O(log N).
84 SmallPtrSet<SDNode*, 64> WorkListContents;
Benjamin Kramerd5f76902012-03-10 00:23:58 +000085 SmallVector<SDNode*, 64> WorkListOrder;
Nate Begeman1d4d4142005-09-01 00:19:25 +000086
Jim Laskeyc7c3f112006-10-16 20:52:31 +000087 // AA - Used for DAG load/store alias analysis.
88 AliasAnalysis &AA;
89
Nate Begeman1d4d4142005-09-01 00:19:25 +000090 /// AddUsersToWorkList - When an instruction is simplified, add all users of
91 /// the instruction to the work lists because they might get more simplified
92 /// now.
93 ///
94 void AddUsersToWorkList(SDNode *N) {
95 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000096 UI != UE; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +000097 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000098 }
99
Dan Gohman389079b2007-10-08 17:57:15 +0000100 /// visit - call the node-specific routine that knows how to fold each
101 /// particular type of node.
Dan Gohman475871a2008-07-27 21:46:04 +0000102 SDValue visit(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +0000103
Chris Lattner24664722006-03-01 04:53:38 +0000104 public:
James Molloy6afa3f72012-02-16 09:48:07 +0000105 /// AddToWorkList - Add to the work list making sure its instance is at the
James Molloy6660c052012-02-16 09:17:04 +0000106 /// back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +0000107 void AddToWorkList(SDNode *N) {
James Molloy6660c052012-02-16 09:17:04 +0000108 WorkListContents.insert(N);
109 WorkListOrder.push_back(N);
Chris Lattner5750df92006-03-01 04:03:14 +0000110 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000111
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000112 /// removeFromWorkList - remove all instances of N from the worklist.
113 ///
114 void removeFromWorkList(SDNode *N) {
James Molloy6660c052012-02-16 09:17:04 +0000115 WorkListContents.erase(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000116 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000117
Dan Gohman475871a2008-07-27 21:46:04 +0000118 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000119 bool AddTo = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000120
Dan Gohman475871a2008-07-27 21:46:04 +0000121 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Jim Laskey274062c2006-10-13 23:32:28 +0000122 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000123 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000124
Dan Gohman475871a2008-07-27 21:46:04 +0000125 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000126 bool AddTo = true) {
Dan Gohman475871a2008-07-27 21:46:04 +0000127 SDValue To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000128 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000129 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000130
131 void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO);
Scott Michelfdc40a02009-02-17 22:15:04 +0000132
133 private:
134
Chris Lattner012f2412006-02-17 21:58:01 +0000135 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000136 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000137 /// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000138 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman87862e72009-12-11 21:31:27 +0000139 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
140 APInt Demanded = APInt::getAllOnesValue(BitWidth);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000141 return SimplifyDemandedBits(Op, Demanded);
142 }
143
Dan Gohman475871a2008-07-27 21:46:04 +0000144 bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded);
Chris Lattner87514ca2005-10-10 22:31:19 +0000145
Chris Lattner448f2192006-11-11 00:39:41 +0000146 bool CombineToPreIndexedLoadStore(SDNode *N);
147 bool CombineToPostIndexedLoadStore(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000148
Evan Cheng95c57ea2010-04-24 04:43:44 +0000149 void ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad);
150 SDValue PromoteOperand(SDValue Op, EVT PVT, bool &Replace);
151 SDValue SExtPromoteOperand(SDValue Op, EVT PVT);
152 SDValue ZExtPromoteOperand(SDValue Op, EVT PVT);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000153 SDValue PromoteIntBinOp(SDValue Op);
Evan Cheng07c4e102010-04-22 20:19:46 +0000154 SDValue PromoteIntShiftOp(SDValue Op);
Evan Cheng4c26e932010-04-19 19:29:22 +0000155 SDValue PromoteExtend(SDValue Op);
156 bool PromoteLoad(SDValue Op);
Scott Michelfdc40a02009-02-17 22:15:04 +0000157
Craig Topper6c64fba2013-07-13 07:43:40 +0000158 void ExtendSetCCUses(const SmallVectorImpl<SDNode *> &SetCCs,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000159 SDValue Trunc, SDValue ExtLoad, SDLoc DL,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +0000160 ISD::NodeType ExtType);
161
Dan Gohman389079b2007-10-08 17:57:15 +0000162 /// combine - call the node-specific routine that knows how to fold each
163 /// particular type of node. If that doesn't do anything, try the
164 /// target-specific DAG combines.
Dan Gohman475871a2008-07-27 21:46:04 +0000165 SDValue combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000166
167 // Visitation implementation - Implement dag node combining for different
168 // node types. The semantics are as follows:
169 // Return Value:
Evan Cheng17a568b2008-08-29 22:21:44 +0000170 // SDValue.getNode() == 0 - No change was made
171 // SDValue.getNode() == N - N was replaced, is dead and has been handled.
172 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000173 //
Dan Gohman475871a2008-07-27 21:46:04 +0000174 SDValue visitTokenFactor(SDNode *N);
175 SDValue visitMERGE_VALUES(SDNode *N);
176 SDValue visitADD(SDNode *N);
177 SDValue visitSUB(SDNode *N);
178 SDValue visitADDC(SDNode *N);
Craig Toppercc274522012-01-07 09:06:39 +0000179 SDValue visitSUBC(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000180 SDValue visitADDE(SDNode *N);
Craig Toppercc274522012-01-07 09:06:39 +0000181 SDValue visitSUBE(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000182 SDValue visitMUL(SDNode *N);
183 SDValue visitSDIV(SDNode *N);
184 SDValue visitUDIV(SDNode *N);
185 SDValue visitSREM(SDNode *N);
186 SDValue visitUREM(SDNode *N);
187 SDValue visitMULHU(SDNode *N);
188 SDValue visitMULHS(SDNode *N);
189 SDValue visitSMUL_LOHI(SDNode *N);
190 SDValue visitUMUL_LOHI(SDNode *N);
Benjamin Kramerf55d26e2011-05-21 18:31:55 +0000191 SDValue visitSMULO(SDNode *N);
192 SDValue visitUMULO(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000193 SDValue visitSDIVREM(SDNode *N);
194 SDValue visitUDIVREM(SDNode *N);
195 SDValue visitAND(SDNode *N);
196 SDValue visitOR(SDNode *N);
197 SDValue visitXOR(SDNode *N);
198 SDValue SimplifyVBinOp(SDNode *N);
Craig Topperdd201ff2012-09-11 01:45:21 +0000199 SDValue SimplifyVUnaryOp(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000200 SDValue visitSHL(SDNode *N);
201 SDValue visitSRA(SDNode *N);
202 SDValue visitSRL(SDNode *N);
203 SDValue visitCTLZ(SDNode *N);
Chandler Carruth63974b22011-12-13 01:56:10 +0000204 SDValue visitCTLZ_ZERO_UNDEF(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000205 SDValue visitCTTZ(SDNode *N);
Chandler Carruth63974b22011-12-13 01:56:10 +0000206 SDValue visitCTTZ_ZERO_UNDEF(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000207 SDValue visitCTPOP(SDNode *N);
208 SDValue visitSELECT(SDNode *N);
Benjamin Kramer6242fda2013-04-26 09:19:19 +0000209 SDValue visitVSELECT(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000210 SDValue visitSELECT_CC(SDNode *N);
211 SDValue visitSETCC(SDNode *N);
212 SDValue visitSIGN_EXTEND(SDNode *N);
213 SDValue visitZERO_EXTEND(SDNode *N);
214 SDValue visitANY_EXTEND(SDNode *N);
215 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
216 SDValue visitTRUNCATE(SDNode *N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000217 SDValue visitBITCAST(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000218 SDValue visitBUILD_PAIR(SDNode *N);
219 SDValue visitFADD(SDNode *N);
220 SDValue visitFSUB(SDNode *N);
221 SDValue visitFMUL(SDNode *N);
Owen Anderson062c0a52012-05-02 22:17:40 +0000222 SDValue visitFMA(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000223 SDValue visitFDIV(SDNode *N);
224 SDValue visitFREM(SDNode *N);
225 SDValue visitFCOPYSIGN(SDNode *N);
226 SDValue visitSINT_TO_FP(SDNode *N);
227 SDValue visitUINT_TO_FP(SDNode *N);
228 SDValue visitFP_TO_SINT(SDNode *N);
229 SDValue visitFP_TO_UINT(SDNode *N);
230 SDValue visitFP_ROUND(SDNode *N);
231 SDValue visitFP_ROUND_INREG(SDNode *N);
232 SDValue visitFP_EXTEND(SDNode *N);
233 SDValue visitFNEG(SDNode *N);
234 SDValue visitFABS(SDNode *N);
Owen Anderson7c626d32012-08-13 23:32:49 +0000235 SDValue visitFCEIL(SDNode *N);
236 SDValue visitFTRUNC(SDNode *N);
237 SDValue visitFFLOOR(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000238 SDValue visitBRCOND(SDNode *N);
239 SDValue visitBR_CC(SDNode *N);
240 SDValue visitLOAD(SDNode *N);
241 SDValue visitSTORE(SDNode *N);
242 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
243 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
244 SDValue visitBUILD_VECTOR(SDNode *N);
245 SDValue visitCONCAT_VECTORS(SDNode *N);
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +0000246 SDValue visitEXTRACT_SUBVECTOR(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000247 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000248
Dan Gohman475871a2008-07-27 21:46:04 +0000249 SDValue XformToShuffleWithZero(SDNode *N);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000250 SDValue ReassociateOps(unsigned Opc, SDLoc DL, SDValue LHS, SDValue RHS);
Scott Michelfdc40a02009-02-17 22:15:04 +0000251
Dan Gohman475871a2008-07-27 21:46:04 +0000252 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattnere70da202007-12-06 07:33:36 +0000253
Dan Gohman475871a2008-07-27 21:46:04 +0000254 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
255 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000256 SDValue SimplifySelect(SDLoc DL, SDValue N0, SDValue N1, SDValue N2);
257 SDValue SimplifySelectCC(SDLoc DL, SDValue N0, SDValue N1, SDValue N2,
Scott Michelfdc40a02009-02-17 22:15:04 +0000258 SDValue N3, ISD::CondCode CC,
Bill Wendling836ca7d2009-01-30 23:59:18 +0000259 bool NotExtCompare = false);
Owen Andersone50ed302009-08-10 22:56:29 +0000260 SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000261 SDLoc DL, bool foldBooleans = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000262 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner5eee4272008-01-26 01:09:19 +0000263 unsigned HiOp);
Owen Andersone50ed302009-08-10 22:56:29 +0000264 SDValue CombineConsecutiveLoads(SDNode *N, EVT VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000265 SDValue ConstantFoldBITCASTofBUILD_VECTOR(SDNode *, EVT);
Dan Gohman475871a2008-07-27 21:46:04 +0000266 SDValue BuildSDIV(SDNode *N);
267 SDValue BuildUDIV(SDNode *N);
Evan Cheng9568e5c2011-06-21 06:01:08 +0000268 SDValue MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
269 bool DemandHighBits = true);
270 SDValue MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000271 SDNode *MatchRotate(SDValue LHS, SDValue RHS, SDLoc DL);
Dan Gohman475871a2008-07-27 21:46:04 +0000272 SDValue ReduceLoadWidth(SDNode *N);
Evan Cheng8b944d32009-05-28 00:35:15 +0000273 SDValue ReduceLoadOpStoreWidth(SDNode *N);
Evan Cheng31959b12011-02-02 01:06:55 +0000274 SDValue TransformFPLoadStorePair(SDNode *N);
Michael Liaofac14ab2012-10-23 23:06:52 +0000275 SDValue reduceBuildVecExtToExtBuildVec(SDNode *N);
Michael Liao1a5cc712012-10-24 04:14:18 +0000276 SDValue reduceBuildVecConvertToConvertBuildVec(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000277
Dan Gohman475871a2008-07-27 21:46:04 +0000278 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Scott Michelfdc40a02009-02-17 22:15:04 +0000279
Jim Laskey6ff23e52006-10-04 16:53:27 +0000280 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
281 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000282 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
Craig Toppera0ec3f92013-07-14 04:42:23 +0000283 SmallVectorImpl<SDValue> &Aliases);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000284
Jim Laskey096c22e2006-10-18 12:29:57 +0000285 /// isAlias - Return true if there is any possibility that the two addresses
286 /// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +0000287 bool isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +0000288 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000289 unsigned SrcValueAlign1,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000290 const MDNode *TBAAInfo1,
Dan Gohman475871a2008-07-27 21:46:04 +0000291 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000292 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000293 unsigned SrcValueAlign2,
294 const MDNode *TBAAInfo2) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000295
Nadav Rotem90e11dc2012-11-29 00:00:08 +0000296 /// isAlias - Return true if there is any possibility that the two addresses
297 /// overlap.
298 bool isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1);
299
Jim Laskey7ca56af2006-10-11 13:47:09 +0000300 /// FindAliasInfo - Extracts the relevant alias information from the memory
301 /// node. Returns true if the operand was a load.
302 bool FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000303 SDValue &Ptr, int64_t &Size,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000304 const Value *&SrcValue, int &SrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000305 unsigned &SrcValueAlignment,
306 const MDNode *&TBAAInfo) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000307
Jim Laskey279f0532006-09-25 16:29:54 +0000308 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000309 /// looking for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +0000310 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Duncan Sands92abc622009-01-31 15:50:11 +0000311
Nadav Rotemc653de62012-10-03 16:11:15 +0000312 /// Merge consecutive store operations into a wide store.
313 /// This optimization uses wide integers or vectors when possible.
314 /// \return True if some memory operations were changed.
315 bool MergeConsecutiveStores(StoreSDNode *N);
316
Chris Lattner2392ae72010-04-15 04:48:01 +0000317 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000318 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, CodeGenOpt::Level OL)
Eli Friedman50185242011-11-12 00:35:34 +0000319 : DAG(D), TLI(D.getTargetLoweringInfo()), Level(BeforeLegalizeTypes),
Chris Lattner2392ae72010-04-15 04:48:01 +0000320 OptLevel(OL), LegalOperations(false), LegalTypes(false), AA(A) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000321
Nate Begeman1d4d4142005-09-01 00:19:25 +0000322 /// Run - runs the dag combiner on all nodes in the work list
Duncan Sands25cf2272008-11-24 14:53:14 +0000323 void Run(CombineLevel AtLevel);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000324
Chris Lattner2392ae72010-04-15 04:48:01 +0000325 SelectionDAG &getDAG() const { return DAG; }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000326
Chris Lattner2392ae72010-04-15 04:48:01 +0000327 /// getShiftAmountTy - Returns a type large enough to hold any valid
328 /// shift amount - before type legalization these can be huge.
Owen Anderson95771af2011-02-25 21:41:48 +0000329 EVT getShiftAmountTy(EVT LHSTy) {
Elena Demikhovsky87070fe2013-06-26 10:55:03 +0000330 assert(LHSTy.isInteger() && "Shift amount is not an integer type!");
331 if (LHSTy.isVector())
332 return LHSTy;
333 return LegalTypes ? TLI.getScalarShiftAmountTy(LHSTy) : TLI.getPointerTy();
Chris Lattner2392ae72010-04-15 04:48:01 +0000334 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000335
Chris Lattner2392ae72010-04-15 04:48:01 +0000336 /// isTypeLegal - This method returns true if we are running before type
337 /// legalization or if the specified VT is legal.
338 bool isTypeLegal(const EVT &VT) {
339 if (!LegalTypes) return true;
340 return TLI.isTypeLegal(VT);
341 }
Matt Arsenault225ed702013-05-18 00:21:46 +0000342
343 /// getSetCCResultType - Convenience wrapper around
344 /// TargetLowering::getSetCCResultType
345 EVT getSetCCResultType(EVT VT) const {
346 return TLI.getSetCCResultType(*DAG.getContext(), VT);
347 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000348 };
349}
350
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000351
352namespace {
353/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
354/// nodes from the worklist.
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000355class WorkListRemover : public SelectionDAG::DAGUpdateListener {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000356 DAGCombiner &DC;
357public:
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000358 explicit WorkListRemover(DAGCombiner &dc)
359 : SelectionDAG::DAGUpdateListener(dc.getDAG()), DC(dc) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000360
Duncan Sandsedfcf592008-06-11 11:42:12 +0000361 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000362 DC.removeFromWorkList(N);
363 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000364};
365}
366
Chris Lattner24664722006-03-01 04:53:38 +0000367//===----------------------------------------------------------------------===//
368// TargetLowering::DAGCombinerInfo implementation
369//===----------------------------------------------------------------------===//
370
371void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
372 ((DAGCombiner*)DC)->AddToWorkList(N);
373}
374
Cameron Zwariched3caf92011-04-02 02:40:26 +0000375void TargetLowering::DAGCombinerInfo::RemoveFromWorklist(SDNode *N) {
376 ((DAGCombiner*)DC)->removeFromWorkList(N);
377}
378
Dan Gohman475871a2008-07-27 21:46:04 +0000379SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000380CombineTo(SDNode *N, const std::vector<SDValue> &To, bool AddTo) {
381 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000382}
383
Dan Gohman475871a2008-07-27 21:46:04 +0000384SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000385CombineTo(SDNode *N, SDValue Res, bool AddTo) {
386 return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000387}
388
389
Dan Gohman475871a2008-07-27 21:46:04 +0000390SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000391CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) {
392 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000393}
394
Dan Gohmane5af2d32009-01-29 01:59:02 +0000395void TargetLowering::DAGCombinerInfo::
396CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
397 return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO);
398}
Chris Lattner24664722006-03-01 04:53:38 +0000399
Chris Lattner24664722006-03-01 04:53:38 +0000400//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000401// Helper Functions
402//===----------------------------------------------------------------------===//
403
404/// isNegatibleForFree - Return 1 if we can compute the negated form of the
405/// specified expression for the same cost as the expression itself, or 2 if we
406/// can compute the negated form more cheaply than the expression itself.
Duncan Sands25cf2272008-11-24 14:53:14 +0000407static char isNegatibleForFree(SDValue Op, bool LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000408 const TargetLowering &TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000409 const TargetOptions *Options,
Chris Lattner0254e702008-02-26 07:04:54 +0000410 unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000411 // fneg is removable even if it has multiple uses.
412 if (Op.getOpcode() == ISD::FNEG) return 2;
Scott Michelfdc40a02009-02-17 22:15:04 +0000413
Chris Lattner29446522007-05-14 22:04:50 +0000414 // Don't allow anything with multiple uses.
415 if (!Op.hasOneUse()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000416
Chris Lattner3adf9512007-05-25 02:19:06 +0000417 // Don't recurse exponentially.
418 if (Depth > 6) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000419
Chris Lattner29446522007-05-14 22:04:50 +0000420 switch (Op.getOpcode()) {
421 default: return false;
422 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000423 // Don't invert constant FP values after legalize. The negated constant
424 // isn't necessarily legal.
Duncan Sands25cf2272008-11-24 14:53:14 +0000425 return LegalOperations ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000426 case ISD::FADD:
427 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000428 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000429
Owen Andersonafd3d562012-03-06 00:29:31 +0000430 // After operation legalization, it might not be legal to create new FSUBs.
431 if (LegalOperations &&
432 !TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType()))
433 return 0;
434
Craig Topper956342b2012-09-09 22:58:45 +0000435 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Owen Andersonafd3d562012-03-06 00:29:31 +0000436 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
437 Options, Depth + 1))
Chris Lattner29446522007-05-14 22:04:50 +0000438 return V;
Bill Wendlingd34470c2009-01-30 23:10:18 +0000439 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Owen Andersonafd3d562012-03-06 00:29:31 +0000440 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000441 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000442 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000443 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000444 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000445
Bill Wendlingd34470c2009-01-30 23:10:18 +0000446 // fold (fneg (fsub A, B)) -> (fsub B, A)
Chris Lattner29446522007-05-14 22:04:50 +0000447 return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000448
Chris Lattner29446522007-05-14 22:04:50 +0000449 case ISD::FMUL:
450 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000451 if (Options->HonorSignDependentRoundingFPMath()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000452
Bill Wendlingd34470c2009-01-30 23:10:18 +0000453 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y))
Owen Andersonafd3d562012-03-06 00:29:31 +0000454 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
455 Options, Depth + 1))
Chris Lattner29446522007-05-14 22:04:50 +0000456 return V;
Scott Michelfdc40a02009-02-17 22:15:04 +0000457
Owen Andersonafd3d562012-03-06 00:29:31 +0000458 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000459 Depth + 1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000460
Chris Lattner29446522007-05-14 22:04:50 +0000461 case ISD::FP_EXTEND:
462 case ISD::FP_ROUND:
463 case ISD::FSIN:
Owen Andersonafd3d562012-03-06 00:29:31 +0000464 return isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000465 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000466 }
467}
468
469/// GetNegatedExpression - If isNegatibleForFree returns true, this function
470/// returns the newly negated expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000471static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000472 bool LegalOperations, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000473 // fneg is removable even if it has multiple uses.
474 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000475
Chris Lattner29446522007-05-14 22:04:50 +0000476 // Don't allow anything with multiple uses.
477 assert(Op.hasOneUse() && "Unknown reuse!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000478
Chris Lattner3adf9512007-05-25 02:19:06 +0000479 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000480 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000481 default: llvm_unreachable("Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000482 case ISD::ConstantFP: {
483 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
484 V.changeSign();
485 return DAG.getConstantFP(V, Op.getValueType());
486 }
Chris Lattner29446522007-05-14 22:04:50 +0000487 case ISD::FADD:
488 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000489 assert(DAG.getTarget().Options.UnsafeFPMath);
Scott Michelfdc40a02009-02-17 22:15:04 +0000490
Bill Wendlingd34470c2009-01-30 23:10:18 +0000491 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000492 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000493 DAG.getTargetLoweringInfo(),
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000494 &DAG.getTarget().Options, Depth+1))
Andrew Trickac6d9be2013-05-25 02:42:55 +0000495 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000496 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000497 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000498 Op.getOperand(1));
Bill Wendlingd34470c2009-01-30 23:10:18 +0000499 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Andrew Trickac6d9be2013-05-25 02:42:55 +0000500 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000501 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000502 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000503 Op.getOperand(0));
504 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000505 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000506 assert(DAG.getTarget().Options.UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000507
Bill Wendlingd34470c2009-01-30 23:10:18 +0000508 // fold (fneg (fsub 0, B)) -> B
Dan Gohman23ff1822007-07-02 15:48:56 +0000509 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000510 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000511 return Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000512
Bill Wendlingd34470c2009-01-30 23:10:18 +0000513 // fold (fneg (fsub A, B)) -> (fsub B, A)
Andrew Trickac6d9be2013-05-25 02:42:55 +0000514 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Bill Wendling35247c32009-01-30 00:45:56 +0000515 Op.getOperand(1), Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +0000516
Chris Lattner29446522007-05-14 22:04:50 +0000517 case ISD::FMUL:
518 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000519 assert(!DAG.getTarget().Options.HonorSignDependentRoundingFPMath());
Scott Michelfdc40a02009-02-17 22:15:04 +0000520
Bill Wendlingd34470c2009-01-30 23:10:18 +0000521 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000522 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000523 DAG.getTargetLoweringInfo(),
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000524 &DAG.getTarget().Options, Depth+1))
Andrew Trickac6d9be2013-05-25 02:42:55 +0000525 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000526 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000527 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000528 Op.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000529
Bill Wendlingd34470c2009-01-30 23:10:18 +0000530 // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y))
Andrew Trickac6d9be2013-05-25 02:42:55 +0000531 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Chris Lattner29446522007-05-14 22:04:50 +0000532 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000533 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000534 LegalOperations, Depth+1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000535
Chris Lattner29446522007-05-14 22:04:50 +0000536 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000537 case ISD::FSIN:
Andrew Trickac6d9be2013-05-25 02:42:55 +0000538 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000539 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000540 LegalOperations, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000541 case ISD::FP_ROUND:
Andrew Trickac6d9be2013-05-25 02:42:55 +0000542 return DAG.getNode(ISD::FP_ROUND, SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000543 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000544 LegalOperations, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000545 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000546 }
547}
Chris Lattner24664722006-03-01 04:53:38 +0000548
549
Nate Begeman4ebd8052005-09-01 23:24:04 +0000550// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
551// that selects between the values 1 and 0, making it equivalent to a setcc.
Scott Michelfdc40a02009-02-17 22:15:04 +0000552// Also, set the incoming LHS, RHS, and CC references to the appropriate
Nate Begeman646d7e22005-09-02 21:18:40 +0000553// nodes based on the type of node we are checking. This simplifies life a
554// bit for the callers.
Dan Gohman475871a2008-07-27 21:46:04 +0000555static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
556 SDValue &CC) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000557 if (N.getOpcode() == ISD::SETCC) {
558 LHS = N.getOperand(0);
559 RHS = N.getOperand(1);
560 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000561 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000562 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000563 if (N.getOpcode() == ISD::SELECT_CC &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000564 N.getOperand(2).getOpcode() == ISD::Constant &&
565 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000566 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000567 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
568 LHS = N.getOperand(0);
569 RHS = N.getOperand(1);
570 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000571 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000572 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000573 return false;
574}
575
Nate Begeman99801192005-09-07 23:25:52 +0000576// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
577// one use. If this is true, it allows the users to invert the operation for
578// free when it is profitable to do so.
Dan Gohman475871a2008-07-27 21:46:04 +0000579static bool isOneUseSetCC(SDValue N) {
580 SDValue N0, N1, N2;
Gabor Greifba36cb52008-08-28 21:40:38 +0000581 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000582 return true;
583 return false;
584}
585
Andrew Trickac6d9be2013-05-25 02:42:55 +0000586SDValue DAGCombiner::ReassociateOps(unsigned Opc, SDLoc DL,
Bill Wendling35247c32009-01-30 00:45:56 +0000587 SDValue N0, SDValue N1) {
Owen Andersone50ed302009-08-10 22:56:29 +0000588 EVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000589 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
590 if (isa<ConstantSDNode>(N1)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000591 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000592 SDValue OpNode =
593 DAG.FoldConstantArithmetic(Opc, VT,
594 cast<ConstantSDNode>(N0.getOperand(1)),
595 cast<ConstantSDNode>(N1));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000596 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000597 }
598 if (N0.hasOneUse()) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000599 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
Andrew Trickac6d9be2013-05-25 02:42:55 +0000600 SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000601 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000602 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000603 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000604 }
605 }
Bill Wendling35247c32009-01-30 00:45:56 +0000606
Nate Begemancd4d58c2006-02-03 06:46:56 +0000607 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
608 if (isa<ConstantSDNode>(N0)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000609 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000610 SDValue OpNode =
611 DAG.FoldConstantArithmetic(Opc, VT,
612 cast<ConstantSDNode>(N1.getOperand(1)),
613 cast<ConstantSDNode>(N0));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000614 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000615 }
616 if (N1.hasOneUse()) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000617 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
Andrew Trickac6d9be2013-05-25 02:42:55 +0000618 SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000619 N1.getOperand(0), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000620 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000621 return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000622 }
623 }
Bill Wendling35247c32009-01-30 00:45:56 +0000624
Dan Gohman475871a2008-07-27 21:46:04 +0000625 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000626}
627
Dan Gohman475871a2008-07-27 21:46:04 +0000628SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
629 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000630 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
631 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +0000632 DEBUG(dbgs() << "\nReplacing.1 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000633 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000634 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000635 To[0].getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000636 dbgs() << " and " << NumTo-1 << " other values\n";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000637 for (unsigned i = 0, e = NumTo; i != e; ++i)
Jakob Stoklund Olesen9f0d4e62009-12-03 05:15:35 +0000638 assert((!To[i].getNode() ||
639 N->getValueType(i) == To[i].getValueType()) &&
Dan Gohman764fd0c2009-01-21 15:17:51 +0000640 "Cannot combine value to value of different type!"));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000641 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000642 DAG.ReplaceAllUsesWith(N, To);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000643 if (AddTo) {
644 // Push the new nodes and any users onto the worklist
645 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattnerd1980a52009-03-12 06:52:53 +0000646 if (To[i].getNode()) {
647 AddToWorkList(To[i].getNode());
648 AddUsersToWorkList(To[i].getNode());
649 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000650 }
651 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000652
Dan Gohmandbe664a2009-01-19 21:44:21 +0000653 // Finally, if the node is now dead, remove it from the graph. The node
654 // may not be dead if the replacement process recursively simplified to
655 // something else needing this node.
656 if (N->use_empty()) {
657 // Nodes can be reintroduced into the worklist. Make sure we do not
658 // process a node that has been replaced.
659 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000660
Dan Gohmandbe664a2009-01-19 21:44:21 +0000661 // Finally, since the node is now dead, remove it from the graph.
662 DAG.DeleteNode(N);
663 }
Dan Gohman475871a2008-07-27 21:46:04 +0000664 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000665}
666
Evan Chenge5b51ac2010-04-17 06:13:15 +0000667void DAGCombiner::
668CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000669 // Replace all uses. If any nodes become isomorphic to other nodes and
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000670 // are deleted, make sure to remove them from our worklist.
671 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000672 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000673
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000674 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000675 AddToWorkList(TLO.New.getNode());
676 AddUsersToWorkList(TLO.New.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000677
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000678 // Finally, if the node is now dead, remove it from the graph. The node
679 // may not be dead if the replacement process recursively simplified to
680 // something else needing this node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000681 if (TLO.Old.getNode()->use_empty()) {
682 removeFromWorkList(TLO.Old.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000683
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000684 // If the operands of this node are only used by the node, they will now
685 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greifba36cb52008-08-28 21:40:38 +0000686 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
687 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
688 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000689
Gabor Greifba36cb52008-08-28 21:40:38 +0000690 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000691 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000692}
693
694/// SimplifyDemandedBits - Check the specified integer node value to see if
695/// it can be simplified or if things it uses can be simplified by bit
696/// propagation. If so, return true.
697bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000698 TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000699 APInt KnownZero, KnownOne;
700 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
701 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000702
Dan Gohmane5af2d32009-01-29 01:59:02 +0000703 // Revisit the node.
704 AddToWorkList(Op.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000705
Dan Gohmane5af2d32009-01-29 01:59:02 +0000706 // Replace the old value with the new one.
707 ++NodesCombined;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000708 DEBUG(dbgs() << "\nReplacing.2 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000709 TLO.Old.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000710 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000711 TLO.New.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000712 dbgs() << '\n');
Scott Michelfdc40a02009-02-17 22:15:04 +0000713
Dan Gohmane5af2d32009-01-29 01:59:02 +0000714 CommitTargetLoweringOpt(TLO);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000715 return true;
716}
717
Evan Cheng95c57ea2010-04-24 04:43:44 +0000718void DAGCombiner::ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad) {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000719 SDLoc dl(Load);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000720 EVT VT = Load->getValueType(0);
721 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, VT, SDValue(ExtLoad, 0));
Evan Cheng4c26e932010-04-19 19:29:22 +0000722
Evan Cheng95c57ea2010-04-24 04:43:44 +0000723 DEBUG(dbgs() << "\nReplacing.9 ";
724 Load->dump(&DAG);
725 dbgs() << "\nWith: ";
726 Trunc.getNode()->dump(&DAG);
727 dbgs() << '\n');
728 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000729 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 0), Trunc);
730 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), SDValue(ExtLoad, 1));
Evan Cheng95c57ea2010-04-24 04:43:44 +0000731 removeFromWorkList(Load);
732 DAG.DeleteNode(Load);
Evan Chengac7eae52010-04-27 19:48:13 +0000733 AddToWorkList(Trunc.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000734}
735
736SDValue DAGCombiner::PromoteOperand(SDValue Op, EVT PVT, bool &Replace) {
737 Replace = false;
Andrew Trickac6d9be2013-05-25 02:42:55 +0000738 SDLoc dl(Op);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000739 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
Evan Chengac7eae52010-04-27 19:48:13 +0000740 EVT MemVT = LD->getMemoryVT();
741 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000742 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000743 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000744 : LD->getExtensionType();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000745 Replace = true;
Stuart Hastingsa9011292011-02-16 16:23:55 +0000746 return DAG.getExtLoad(ExtType, dl, PVT,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000747 LD->getChain(), LD->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000748 LD->getPointerInfo(),
Evan Chengac7eae52010-04-27 19:48:13 +0000749 MemVT, LD->isVolatile(),
Evan Chenge5b51ac2010-04-17 06:13:15 +0000750 LD->isNonTemporal(), LD->getAlignment());
751 }
752
Evan Cheng4c26e932010-04-19 19:29:22 +0000753 unsigned Opc = Op.getOpcode();
Evan Chengcaf77402010-04-23 19:10:30 +0000754 switch (Opc) {
755 default: break;
756 case ISD::AssertSext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000757 return DAG.getNode(ISD::AssertSext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000758 SExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000759 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000760 case ISD::AssertZext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000761 return DAG.getNode(ISD::AssertZext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000762 ZExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000763 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000764 case ISD::Constant: {
765 unsigned ExtOpc =
Evan Cheng4c26e932010-04-19 19:29:22 +0000766 Op.getValueType().isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Evan Chengcaf77402010-04-23 19:10:30 +0000767 return DAG.getNode(ExtOpc, dl, PVT, Op);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000768 }
Evan Chengcaf77402010-04-23 19:10:30 +0000769 }
770
771 if (!TLI.isOperationLegal(ISD::ANY_EXTEND, PVT))
Evan Chenge5b51ac2010-04-17 06:13:15 +0000772 return SDValue();
Evan Chengcaf77402010-04-23 19:10:30 +0000773 return DAG.getNode(ISD::ANY_EXTEND, dl, PVT, Op);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000774}
775
Evan Cheng95c57ea2010-04-24 04:43:44 +0000776SDValue DAGCombiner::SExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000777 if (!TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, PVT))
778 return SDValue();
779 EVT OldVT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +0000780 SDLoc dl(Op);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000781 bool Replace = false;
782 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
783 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000784 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000785 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000786
787 if (Replace)
788 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
789 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NewOp.getValueType(), NewOp,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000790 DAG.getValueType(OldVT));
791}
792
Evan Cheng95c57ea2010-04-24 04:43:44 +0000793SDValue DAGCombiner::ZExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000794 EVT OldVT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +0000795 SDLoc dl(Op);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000796 bool Replace = false;
797 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
798 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000799 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000800 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000801
802 if (Replace)
803 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
804 return DAG.getZeroExtendInReg(NewOp, dl, OldVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000805}
806
Evan Cheng64b7bf72010-04-16 06:14:10 +0000807/// PromoteIntBinOp - Promote the specified integer binary operation if the
808/// target indicates it is beneficial. e.g. On x86, it's usually better to
809/// promote i16 operations to i32 since i16 instructions are longer.
810SDValue DAGCombiner::PromoteIntBinOp(SDValue Op) {
811 if (!LegalOperations)
812 return SDValue();
813
814 EVT VT = Op.getValueType();
815 if (VT.isVector() || !VT.isInteger())
816 return SDValue();
817
Evan Chenge5b51ac2010-04-17 06:13:15 +0000818 // If operation type is 'undesirable', e.g. i16 on x86, consider
819 // promoting it.
820 unsigned Opc = Op.getOpcode();
821 if (TLI.isTypeDesirableForOp(Opc, VT))
822 return SDValue();
823
Evan Cheng64b7bf72010-04-16 06:14:10 +0000824 EVT PVT = VT;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000825 // Consult target whether it is a good idea to promote this operation and
826 // what's the right type to promote it to.
827 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
Evan Cheng64b7bf72010-04-16 06:14:10 +0000828 assert(PVT != VT && "Don't know what type to promote to!");
829
Evan Cheng95c57ea2010-04-24 04:43:44 +0000830 bool Replace0 = false;
831 SDValue N0 = Op.getOperand(0);
832 SDValue NN0 = PromoteOperand(N0, PVT, Replace0);
833 if (NN0.getNode() == 0)
Evan Cheng07c4e102010-04-22 20:19:46 +0000834 return SDValue();
835
Evan Cheng95c57ea2010-04-24 04:43:44 +0000836 bool Replace1 = false;
837 SDValue N1 = Op.getOperand(1);
Evan Chengaad753b2010-05-10 19:03:57 +0000838 SDValue NN1;
839 if (N0 == N1)
840 NN1 = NN0;
841 else {
842 NN1 = PromoteOperand(N1, PVT, Replace1);
843 if (NN1.getNode() == 0)
844 return SDValue();
845 }
Evan Cheng07c4e102010-04-22 20:19:46 +0000846
Evan Cheng95c57ea2010-04-24 04:43:44 +0000847 AddToWorkList(NN0.getNode());
Evan Chengaad753b2010-05-10 19:03:57 +0000848 if (NN1.getNode())
849 AddToWorkList(NN1.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000850
851 if (Replace0)
852 ReplaceLoadWithPromotedLoad(N0.getNode(), NN0.getNode());
853 if (Replace1)
854 ReplaceLoadWithPromotedLoad(N1.getNode(), NN1.getNode());
Evan Cheng07c4e102010-04-22 20:19:46 +0000855
Evan Chengac7eae52010-04-27 19:48:13 +0000856 DEBUG(dbgs() << "\nPromoting ";
857 Op.getNode()->dump(&DAG));
Andrew Trickac6d9be2013-05-25 02:42:55 +0000858 SDLoc dl(Op);
Evan Cheng07c4e102010-04-22 20:19:46 +0000859 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000860 DAG.getNode(Opc, dl, PVT, NN0, NN1));
Evan Cheng07c4e102010-04-22 20:19:46 +0000861 }
862 return SDValue();
863}
864
865/// PromoteIntShiftOp - Promote the specified integer shift operation if the
866/// target indicates it is beneficial. e.g. On x86, it's usually better to
867/// promote i16 operations to i32 since i16 instructions are longer.
868SDValue DAGCombiner::PromoteIntShiftOp(SDValue Op) {
869 if (!LegalOperations)
870 return SDValue();
871
872 EVT VT = Op.getValueType();
873 if (VT.isVector() || !VT.isInteger())
874 return SDValue();
875
876 // If operation type is 'undesirable', e.g. i16 on x86, consider
877 // promoting it.
878 unsigned Opc = Op.getOpcode();
879 if (TLI.isTypeDesirableForOp(Opc, VT))
880 return SDValue();
881
882 EVT PVT = VT;
883 // Consult target whether it is a good idea to promote this operation and
884 // what's the right type to promote it to.
885 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
886 assert(PVT != VT && "Don't know what type to promote to!");
887
Evan Cheng95c57ea2010-04-24 04:43:44 +0000888 bool Replace = false;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000889 SDValue N0 = Op.getOperand(0);
890 if (Opc == ISD::SRA)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000891 N0 = SExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000892 else if (Opc == ISD::SRL)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000893 N0 = ZExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000894 else
Evan Cheng95c57ea2010-04-24 04:43:44 +0000895 N0 = PromoteOperand(N0, PVT, Replace);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000896 if (N0.getNode() == 0)
897 return SDValue();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000898
Evan Chenge5b51ac2010-04-17 06:13:15 +0000899 AddToWorkList(N0.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000900 if (Replace)
901 ReplaceLoadWithPromotedLoad(Op.getOperand(0).getNode(), N0.getNode());
Evan Cheng64b7bf72010-04-16 06:14:10 +0000902
Evan Chengac7eae52010-04-27 19:48:13 +0000903 DEBUG(dbgs() << "\nPromoting ";
904 Op.getNode()->dump(&DAG));
Andrew Trickac6d9be2013-05-25 02:42:55 +0000905 SDLoc dl(Op);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000906 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng07c4e102010-04-22 20:19:46 +0000907 DAG.getNode(Opc, dl, PVT, N0, Op.getOperand(1)));
Evan Cheng64b7bf72010-04-16 06:14:10 +0000908 }
909 return SDValue();
910}
911
Evan Cheng4c26e932010-04-19 19:29:22 +0000912SDValue DAGCombiner::PromoteExtend(SDValue Op) {
913 if (!LegalOperations)
914 return SDValue();
915
916 EVT VT = Op.getValueType();
917 if (VT.isVector() || !VT.isInteger())
918 return SDValue();
919
920 // If operation type is 'undesirable', e.g. i16 on x86, consider
921 // promoting it.
922 unsigned Opc = Op.getOpcode();
923 if (TLI.isTypeDesirableForOp(Opc, VT))
924 return SDValue();
925
926 EVT PVT = VT;
927 // Consult target whether it is a good idea to promote this operation and
928 // what's the right type to promote it to.
929 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
930 assert(PVT != VT && "Don't know what type to promote to!");
931 // fold (aext (aext x)) -> (aext x)
932 // fold (aext (zext x)) -> (zext x)
933 // fold (aext (sext x)) -> (sext x)
Evan Chengac7eae52010-04-27 19:48:13 +0000934 DEBUG(dbgs() << "\nPromoting ";
935 Op.getNode()->dump(&DAG));
Andrew Trickac6d9be2013-05-25 02:42:55 +0000936 return DAG.getNode(Op.getOpcode(), SDLoc(Op), VT, Op.getOperand(0));
Evan Cheng4c26e932010-04-19 19:29:22 +0000937 }
938 return SDValue();
939}
940
941bool DAGCombiner::PromoteLoad(SDValue Op) {
942 if (!LegalOperations)
943 return false;
944
945 EVT VT = Op.getValueType();
946 if (VT.isVector() || !VT.isInteger())
947 return false;
948
949 // If operation type is 'undesirable', e.g. i16 on x86, consider
950 // promoting it.
951 unsigned Opc = Op.getOpcode();
952 if (TLI.isTypeDesirableForOp(Opc, VT))
953 return false;
954
955 EVT PVT = VT;
956 // Consult target whether it is a good idea to promote this operation and
957 // what's the right type to promote it to.
958 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
959 assert(PVT != VT && "Don't know what type to promote to!");
960
Andrew Trickac6d9be2013-05-25 02:42:55 +0000961 SDLoc dl(Op);
Evan Cheng4c26e932010-04-19 19:29:22 +0000962 SDNode *N = Op.getNode();
963 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chengac7eae52010-04-27 19:48:13 +0000964 EVT MemVT = LD->getMemoryVT();
965 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000966 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000967 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000968 : LD->getExtensionType();
Stuart Hastingsa9011292011-02-16 16:23:55 +0000969 SDValue NewLD = DAG.getExtLoad(ExtType, dl, PVT,
Evan Cheng4c26e932010-04-19 19:29:22 +0000970 LD->getChain(), LD->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000971 LD->getPointerInfo(),
Evan Chengac7eae52010-04-27 19:48:13 +0000972 MemVT, LD->isVolatile(),
Evan Cheng4c26e932010-04-19 19:29:22 +0000973 LD->isNonTemporal(), LD->getAlignment());
974 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, VT, NewLD);
975
Evan Cheng95c57ea2010-04-24 04:43:44 +0000976 DEBUG(dbgs() << "\nPromoting ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000977 N->dump(&DAG);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000978 dbgs() << "\nTo: ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000979 Result.getNode()->dump(&DAG);
980 dbgs() << '\n');
981 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000982 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
983 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), NewLD.getValue(1));
Evan Cheng4c26e932010-04-19 19:29:22 +0000984 removeFromWorkList(N);
985 DAG.DeleteNode(N);
Evan Chengac7eae52010-04-27 19:48:13 +0000986 AddToWorkList(Result.getNode());
Evan Cheng4c26e932010-04-19 19:29:22 +0000987 return true;
988 }
989 return false;
990}
991
Evan Chenge5b51ac2010-04-17 06:13:15 +0000992
Chris Lattner29446522007-05-14 22:04:50 +0000993//===----------------------------------------------------------------------===//
994// Main DAG Combiner implementation
995//===----------------------------------------------------------------------===//
996
Duncan Sands25cf2272008-11-24 14:53:14 +0000997void DAGCombiner::Run(CombineLevel AtLevel) {
998 // set the instance variables, so that the various visit routines may use it.
999 Level = AtLevel;
Eli Friedman50185242011-11-12 00:35:34 +00001000 LegalOperations = Level >= AfterLegalizeVectorOps;
1001 LegalTypes = Level >= AfterLegalizeTypes;
Nate Begeman4ebd8052005-09-01 23:24:04 +00001002
Evan Cheng17a568b2008-08-29 22:21:44 +00001003 // Add all the dag nodes to the worklist.
Evan Cheng17a568b2008-08-29 22:21:44 +00001004 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
1005 E = DAG.allnodes_end(); I != E; ++I)
James Molloy6660c052012-02-16 09:17:04 +00001006 AddToWorkList(I);
Duncan Sands25cf2272008-11-24 14:53:14 +00001007
Evan Cheng17a568b2008-08-29 22:21:44 +00001008 // Create a dummy node (which is not added to allnodes), that adds a reference
1009 // to the root node, preventing it from being deleted, and tracking any
1010 // changes of the root.
1011 HandleSDNode Dummy(DAG.getRoot());
Scott Michelfdc40a02009-02-17 22:15:04 +00001012
Jim Laskey26f7fa72006-10-17 19:33:52 +00001013 // The root of the dag may dangle to deleted nodes until the dag combiner is
1014 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +00001015 DAG.setRoot(SDValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00001016
James Molloy6660c052012-02-16 09:17:04 +00001017 // while the worklist isn't empty, find a node and
Evan Cheng17a568b2008-08-29 22:21:44 +00001018 // try and combine it.
James Molloy6660c052012-02-16 09:17:04 +00001019 while (!WorkListContents.empty()) {
1020 SDNode *N;
1021 // The WorkListOrder holds the SDNodes in order, but it may contain duplicates.
1022 // In order to avoid a linear scan, we use a set (O(log N)) to hold what the
1023 // worklist *should* contain, and check the node we want to visit is should
1024 // actually be visited.
1025 do {
Benjamin Kramerd5f76902012-03-10 00:23:58 +00001026 N = WorkListOrder.pop_back_val();
James Molloy6660c052012-02-16 09:17:04 +00001027 } while (!WorkListContents.erase(N));
Scott Michelfdc40a02009-02-17 22:15:04 +00001028
Evan Cheng17a568b2008-08-29 22:21:44 +00001029 // If N has no uses, it is dead. Make sure to revisit all N's operands once
1030 // N is deleted from the DAG, since they too may now be dead or may have a
1031 // reduced number of uses, allowing other xforms.
1032 if (N->use_empty() && N != &Dummy) {
1033 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1034 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001035
Evan Cheng17a568b2008-08-29 22:21:44 +00001036 DAG.DeleteNode(N);
1037 continue;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001038 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001039
Evan Cheng17a568b2008-08-29 22:21:44 +00001040 SDValue RV = combine(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001041
Evan Cheng17a568b2008-08-29 22:21:44 +00001042 if (RV.getNode() == 0)
1043 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001044
Evan Cheng17a568b2008-08-29 22:21:44 +00001045 ++NodesCombined;
Scott Michelfdc40a02009-02-17 22:15:04 +00001046
Evan Cheng17a568b2008-08-29 22:21:44 +00001047 // If we get back the same node we passed in, rather than a new node or
1048 // zero, we know that the node must have defined multiple values and
Scott Michelfdc40a02009-02-17 22:15:04 +00001049 // CombineTo was used. Since CombineTo takes care of the worklist
Evan Cheng17a568b2008-08-29 22:21:44 +00001050 // mechanics for us, we have no work to do in this case.
1051 if (RV.getNode() == N)
1052 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001053
Evan Cheng17a568b2008-08-29 22:21:44 +00001054 assert(N->getOpcode() != ISD::DELETED_NODE &&
1055 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
1056 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +00001057
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001058 DEBUG(dbgs() << "\nReplacing.3 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001059 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001060 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001061 RV.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001062 dbgs() << '\n');
Eric Christopher7332e6e2011-07-14 01:12:15 +00001063
Devang Patel9728ea22011-05-23 22:04:42 +00001064 // Transfer debug value.
1065 DAG.TransferDbgValues(SDValue(N, 0), RV);
Evan Cheng17a568b2008-08-29 22:21:44 +00001066 WorkListRemover DeadNodes(*this);
1067 if (N->getNumValues() == RV.getNode()->getNumValues())
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001068 DAG.ReplaceAllUsesWith(N, RV.getNode());
Evan Cheng17a568b2008-08-29 22:21:44 +00001069 else {
1070 assert(N->getValueType(0) == RV.getValueType() &&
1071 N->getNumValues() == 1 && "Type mismatch");
1072 SDValue OpV = RV;
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001073 DAG.ReplaceAllUsesWith(N, &OpV);
Evan Cheng17a568b2008-08-29 22:21:44 +00001074 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001075
Evan Cheng17a568b2008-08-29 22:21:44 +00001076 // Push the new node and any users onto the worklist
1077 AddToWorkList(RV.getNode());
1078 AddUsersToWorkList(RV.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001079
Evan Cheng17a568b2008-08-29 22:21:44 +00001080 // Add any uses of the old node to the worklist in case this node is the
1081 // last one that uses them. They may become dead after this node is
1082 // deleted.
1083 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1084 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001085
Dan Gohmandbe664a2009-01-19 21:44:21 +00001086 // Finally, if the node is now dead, remove it from the graph. The node
1087 // may not be dead if the replacement process recursively simplified to
1088 // something else needing this node.
1089 if (N->use_empty()) {
1090 // Nodes can be reintroduced into the worklist. Make sure we do not
1091 // process a node that has been replaced.
1092 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001093
Dan Gohmandbe664a2009-01-19 21:44:21 +00001094 // Finally, since the node is now dead, remove it from the graph.
1095 DAG.DeleteNode(N);
1096 }
Evan Cheng17a568b2008-08-29 22:21:44 +00001097 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001098
Chris Lattner95038592005-10-05 06:35:28 +00001099 // If the root changed (e.g. it was a dead load, update the root).
1100 DAG.setRoot(Dummy.getValue());
Hal Finkel31490ba2012-04-16 03:33:22 +00001101 DAG.RemoveDeadNodes();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001102}
1103
Dan Gohman475871a2008-07-27 21:46:04 +00001104SDValue DAGCombiner::visit(SDNode *N) {
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001105 switch (N->getOpcode()) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001106 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +00001107 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001108 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001109 case ISD::ADD: return visitADD(N);
1110 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +00001111 case ISD::ADDC: return visitADDC(N);
Craig Toppercc274522012-01-07 09:06:39 +00001112 case ISD::SUBC: return visitSUBC(N);
Chris Lattner91153682007-03-04 20:03:15 +00001113 case ISD::ADDE: return visitADDE(N);
Craig Toppercc274522012-01-07 09:06:39 +00001114 case ISD::SUBE: return visitSUBE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001115 case ISD::MUL: return visitMUL(N);
1116 case ISD::SDIV: return visitSDIV(N);
1117 case ISD::UDIV: return visitUDIV(N);
1118 case ISD::SREM: return visitSREM(N);
1119 case ISD::UREM: return visitUREM(N);
1120 case ISD::MULHU: return visitMULHU(N);
1121 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001122 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
1123 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00001124 case ISD::SMULO: return visitSMULO(N);
1125 case ISD::UMULO: return visitUMULO(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001126 case ISD::SDIVREM: return visitSDIVREM(N);
1127 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001128 case ISD::AND: return visitAND(N);
1129 case ISD::OR: return visitOR(N);
1130 case ISD::XOR: return visitXOR(N);
1131 case ISD::SHL: return visitSHL(N);
1132 case ISD::SRA: return visitSRA(N);
1133 case ISD::SRL: return visitSRL(N);
1134 case ISD::CTLZ: return visitCTLZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001135 case ISD::CTLZ_ZERO_UNDEF: return visitCTLZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001136 case ISD::CTTZ: return visitCTTZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001137 case ISD::CTTZ_ZERO_UNDEF: return visitCTTZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001138 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +00001139 case ISD::SELECT: return visitSELECT(N);
Benjamin Kramer6242fda2013-04-26 09:19:19 +00001140 case ISD::VSELECT: return visitVSELECT(N);
Nate Begeman452d7be2005-09-16 00:54:12 +00001141 case ISD::SELECT_CC: return visitSELECT_CC(N);
1142 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001143 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
1144 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001145 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001146 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
1147 case ISD::TRUNCATE: return visitTRUNCATE(N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001148 case ISD::BITCAST: return visitBITCAST(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00001149 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001150 case ISD::FADD: return visitFADD(N);
1151 case ISD::FSUB: return visitFSUB(N);
1152 case ISD::FMUL: return visitFMUL(N);
Owen Anderson062c0a52012-05-02 22:17:40 +00001153 case ISD::FMA: return visitFMA(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001154 case ISD::FDIV: return visitFDIV(N);
1155 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +00001156 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001157 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
1158 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
1159 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
1160 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
1161 case ISD::FP_ROUND: return visitFP_ROUND(N);
1162 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
1163 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
1164 case ISD::FNEG: return visitFNEG(N);
1165 case ISD::FABS: return visitFABS(N);
Owen Anderson7c626d32012-08-13 23:32:49 +00001166 case ISD::FFLOOR: return visitFFLOOR(N);
1167 case ISD::FCEIL: return visitFCEIL(N);
1168 case ISD::FTRUNC: return visitFTRUNC(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001169 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001170 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +00001171 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00001172 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +00001173 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +00001174 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +00001175 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
1176 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00001177 case ISD::EXTRACT_SUBVECTOR: return visitEXTRACT_SUBVECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +00001178 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001179 }
Dan Gohman475871a2008-07-27 21:46:04 +00001180 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001181}
1182
Dan Gohman475871a2008-07-27 21:46:04 +00001183SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00001184 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001185
1186 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +00001187 if (RV.getNode() == 0) {
Dan Gohman389079b2007-10-08 17:57:15 +00001188 assert(N->getOpcode() != ISD::DELETED_NODE &&
1189 "Node was deleted but visit returned NULL!");
1190
1191 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
1192 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
1193
1194 // Expose the DAG combiner to the target combiner impls.
Scott Michelfdc40a02009-02-17 22:15:04 +00001195 TargetLowering::DAGCombinerInfo
Nadav Rotem444b4bf2012-12-27 06:47:41 +00001196 DagCombineInfo(DAG, Level, false, this);
Dan Gohman389079b2007-10-08 17:57:15 +00001197
1198 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
1199 }
1200 }
1201
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001202 // If nothing happened still, try promoting the operation.
1203 if (RV.getNode() == 0) {
1204 switch (N->getOpcode()) {
1205 default: break;
1206 case ISD::ADD:
1207 case ISD::SUB:
1208 case ISD::MUL:
1209 case ISD::AND:
1210 case ISD::OR:
1211 case ISD::XOR:
1212 RV = PromoteIntBinOp(SDValue(N, 0));
1213 break;
1214 case ISD::SHL:
1215 case ISD::SRA:
1216 case ISD::SRL:
1217 RV = PromoteIntShiftOp(SDValue(N, 0));
1218 break;
1219 case ISD::SIGN_EXTEND:
1220 case ISD::ZERO_EXTEND:
1221 case ISD::ANY_EXTEND:
1222 RV = PromoteExtend(SDValue(N, 0));
1223 break;
1224 case ISD::LOAD:
1225 if (PromoteLoad(SDValue(N, 0)))
1226 RV = SDValue(N, 0);
1227 break;
1228 }
1229 }
1230
Scott Michelfdc40a02009-02-17 22:15:04 +00001231 // If N is a commutative binary node, try commuting it to enable more
Evan Cheng08b11732008-03-22 01:55:50 +00001232 // sdisel CSE.
Scott Michelfdc40a02009-02-17 22:15:04 +00001233 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +00001234 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
1235 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00001236 SDValue N0 = N->getOperand(0);
1237 SDValue N1 = N->getOperand(1);
Bill Wendling5c71acf2009-01-30 01:13:16 +00001238
Evan Cheng08b11732008-03-22 01:55:50 +00001239 // Constant operands are canonicalized to RHS.
1240 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001241 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +00001242 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
1243 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +00001244 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +00001245 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +00001246 }
1247 }
1248
Dan Gohman389079b2007-10-08 17:57:15 +00001249 return RV;
Scott Michelfdc40a02009-02-17 22:15:04 +00001250}
Dan Gohman389079b2007-10-08 17:57:15 +00001251
Chris Lattner6270f682006-10-08 22:57:01 +00001252/// getInputChainForNode - Given a node, return its input chain if it has one,
1253/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +00001254static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001255 if (unsigned NumOps = N->getNumOperands()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001256 if (N->getOperand(0).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001257 return N->getOperand(0);
Stephen Linb4940152013-07-09 00:44:49 +00001258 if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001259 return N->getOperand(NumOps-1);
1260 for (unsigned i = 1; i < NumOps-1; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +00001261 if (N->getOperand(i).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001262 return N->getOperand(i);
1263 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001264 return SDValue();
Chris Lattner6270f682006-10-08 22:57:01 +00001265}
1266
Dan Gohman475871a2008-07-27 21:46:04 +00001267SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001268 // If N has two operands, where one has an input chain equal to the other,
1269 // the 'other' chain is redundant.
1270 if (N->getNumOperands() == 2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001271 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +00001272 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001273 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +00001274 return N->getOperand(1);
1275 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001276
Chris Lattnerc76d4412007-05-16 06:37:59 +00001277 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +00001278 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001279 SmallPtrSet<SDNode*, 16> SeenOps;
Chris Lattnerc76d4412007-05-16 06:37:59 +00001280 bool Changed = false; // If we should replace this token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001281
Jim Laskey6ff23e52006-10-04 16:53:27 +00001282 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00001283 TFs.push_back(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001284
Jim Laskey71382342006-10-07 23:37:56 +00001285 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +00001286 // encountered.
1287 for (unsigned i = 0; i < TFs.size(); ++i) {
1288 SDNode *TF = TFs[i];
Scott Michelfdc40a02009-02-17 22:15:04 +00001289
Jim Laskey6ff23e52006-10-04 16:53:27 +00001290 // Check each of the operands.
1291 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001292 SDValue Op = TF->getOperand(i);
Scott Michelfdc40a02009-02-17 22:15:04 +00001293
Jim Laskey6ff23e52006-10-04 16:53:27 +00001294 switch (Op.getOpcode()) {
1295 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +00001296 // Entry tokens don't need to be added to the list. They are
1297 // rededundant.
1298 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001299 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001300
Jim Laskey6ff23e52006-10-04 16:53:27 +00001301 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +00001302 if (Op.hasOneUse() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001303 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001304 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +00001305 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001306 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +00001307 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001308 Changed = true;
1309 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001310 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001311 // Fall thru
Scott Michelfdc40a02009-02-17 22:15:04 +00001312
Jim Laskey6ff23e52006-10-04 16:53:27 +00001313 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +00001314 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +00001315 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +00001316 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +00001317 else
1318 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001319 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001320 }
1321 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001322 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001323
Dan Gohman475871a2008-07-27 21:46:04 +00001324 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001325
1326 // If we've change things around then replace token factor.
1327 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +00001328 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001329 // The entry token is the only possible outcome.
1330 Result = DAG.getEntryNode();
1331 } else {
1332 // New and improved token factor.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001333 Result = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson825b72b2009-08-11 20:47:22 +00001334 MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +00001335 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001336
Jim Laskey274062c2006-10-13 23:32:28 +00001337 // Don't add users to work list.
1338 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +00001339 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001340
Jim Laskey6ff23e52006-10-04 16:53:27 +00001341 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001342}
1343
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001344/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +00001345SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001346 WorkListRemover DeadNodes(*this);
Dan Gohman00edf392009-08-10 23:43:19 +00001347 // Replacing results may cause a different MERGE_VALUES to suddenly
1348 // be CSE'd with N, and carry its uses with it. Iterate until no
1349 // uses remain, to ensure that the node can be safely deleted.
Pete Cooper3affd9e2012-06-20 19:35:43 +00001350 // First add the users of this node to the work list so that they
1351 // can be tried again once they have new operands.
1352 AddUsersToWorkList(N);
Dan Gohman00edf392009-08-10 23:43:19 +00001353 do {
1354 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001355 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i));
Dan Gohman00edf392009-08-10 23:43:19 +00001356 } while (!N->use_empty());
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001357 removeFromWorkList(N);
1358 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001359 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001360}
1361
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001362static
Andrew Trickac6d9be2013-05-25 02:42:55 +00001363SDValue combineShlAddConstant(SDLoc DL, SDValue N0, SDValue N1,
Bill Wendlingd69c3142009-01-30 02:23:43 +00001364 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001365 EVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001366 SDValue N00 = N0.getOperand(0);
1367 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001368 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Bill Wendlingd69c3142009-01-30 02:23:43 +00001369
Gabor Greifba36cb52008-08-28 21:40:38 +00001370 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001371 isa<ConstantSDNode>(N00.getOperand(1))) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001372 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Andrew Trickac6d9be2013-05-25 02:42:55 +00001373 N0 = DAG.getNode(ISD::ADD, SDLoc(N0), VT,
1374 DAG.getNode(ISD::SHL, SDLoc(N00), VT,
Bill Wendlingd69c3142009-01-30 02:23:43 +00001375 N00.getOperand(0), N01),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001376 DAG.getNode(ISD::SHL, SDLoc(N01), VT,
Bill Wendlingd69c3142009-01-30 02:23:43 +00001377 N00.getOperand(1), N01));
1378 return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001379 }
Bill Wendlingd69c3142009-01-30 02:23:43 +00001380
Dan Gohman475871a2008-07-27 21:46:04 +00001381 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001382}
1383
Dan Gohman475871a2008-07-27 21:46:04 +00001384SDValue DAGCombiner::visitADD(SDNode *N) {
1385 SDValue N0 = N->getOperand(0);
1386 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001387 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1388 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001389 EVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00001390
1391 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001392 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001393 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001394 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper48b509c2012-12-10 08:12:29 +00001395
1396 // fold (add x, 0) -> x, vector edition
1397 if (ISD::isBuildVectorAllZeros(N1.getNode()))
1398 return N0;
1399 if (ISD::isBuildVectorAllZeros(N0.getNode()))
1400 return N1;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001401 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001402
Dan Gohman613e0d82007-07-03 14:03:57 +00001403 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001404 if (N0.getOpcode() == ISD::UNDEF)
1405 return N0;
1406 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001407 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001408 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001409 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001410 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001411 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001412 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001413 return DAG.getNode(ISD::ADD, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001414 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001415 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001416 return N0;
Dan Gohman6520e202008-10-18 02:06:02 +00001417 // fold (add Sym, c) -> Sym+c
1418 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001419 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman6520e202008-10-18 02:06:02 +00001420 GA->getOpcode() == ISD::GlobalAddress)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001421 return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001422 GA->getOffset() +
1423 (uint64_t)N1C->getSExtValue());
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001424 // fold ((c1-A)+c2) -> (c1+c2)-A
1425 if (N1C && N0.getOpcode() == ISD::SUB)
1426 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001427 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Dan Gohman002e5d02008-03-13 22:13:53 +00001428 DAG.getConstant(N1C->getAPIntValue()+
1429 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001430 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +00001431 // reassociate add
Andrew Trickac6d9be2013-05-25 02:42:55 +00001432 SDValue RADD = ReassociateOps(ISD::ADD, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001433 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001434 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001435 // fold ((0-A) + B) -> B-A
1436 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1437 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001438 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001439 // fold (A + (0-B)) -> A-B
1440 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1441 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001442 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001443 // fold (A+(B-A)) -> B
1444 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001445 return N1.getOperand(0);
Dale Johannesen56eca912008-11-27 00:43:21 +00001446 // fold ((B-A)+A) -> B
1447 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1448 return N0.getOperand(0);
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001449 // fold (A+(B-(A+C))) to (B-C)
1450 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001451 N0 == N1.getOperand(1).getOperand(0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001452 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001453 N1.getOperand(1).getOperand(1));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001454 // fold (A+(B-(C+A))) to (B-C)
1455 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001456 N0 == N1.getOperand(1).getOperand(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001457 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001458 N1.getOperand(1).getOperand(0));
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001459 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesen34d79852008-12-02 18:40:40 +00001460 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1461 N1.getOperand(0).getOpcode() == ISD::SUB &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001462 N0 == N1.getOperand(0).getOperand(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001463 return DAG.getNode(N1.getOpcode(), SDLoc(N), VT,
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001464 N1.getOperand(0).getOperand(0), N1.getOperand(1));
Dale Johannesen34d79852008-12-02 18:40:40 +00001465
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001466 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1467 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1468 SDValue N00 = N0.getOperand(0);
1469 SDValue N01 = N0.getOperand(1);
1470 SDValue N10 = N1.getOperand(0);
1471 SDValue N11 = N1.getOperand(1);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001472
1473 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001474 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
1475 DAG.getNode(ISD::ADD, SDLoc(N0), VT, N00, N10),
1476 DAG.getNode(ISD::ADD, SDLoc(N1), VT, N01, N11));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001477 }
Chris Lattner947c2892006-03-13 06:51:27 +00001478
Dan Gohman475871a2008-07-27 21:46:04 +00001479 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1480 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001481
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001482 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001483 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001484 APInt LHSZero, LHSOne;
1485 APInt RHSZero, RHSOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001486 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001487
Dan Gohman948d8ea2008-02-20 16:33:30 +00001488 if (LHSZero.getBoolValue()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001489 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001490
Chris Lattner947c2892006-03-13 06:51:27 +00001491 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1492 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001493 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001494 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N1);
Chris Lattner947c2892006-03-13 06:51:27 +00001495 }
1496 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001497
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001498 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001499 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001500 SDValue Result = combineShlAddConstant(SDLoc(N), N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001501 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001502 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001503 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001504 SDValue Result = combineShlAddConstant(SDLoc(N), N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001505 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001506 }
1507
Dan Gohmancd9e1552010-01-19 23:30:49 +00001508 // fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n))
1509 if (N1.getOpcode() == ISD::SHL &&
1510 N1.getOperand(0).getOpcode() == ISD::SUB)
1511 if (ConstantSDNode *C =
1512 dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(0)))
1513 if (C->getAPIntValue() == 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001514 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N0,
1515 DAG.getNode(ISD::SHL, SDLoc(N), VT,
Dan Gohmancd9e1552010-01-19 23:30:49 +00001516 N1.getOperand(0).getOperand(1),
1517 N1.getOperand(1)));
1518 if (N0.getOpcode() == ISD::SHL &&
1519 N0.getOperand(0).getOpcode() == ISD::SUB)
1520 if (ConstantSDNode *C =
1521 dyn_cast<ConstantSDNode>(N0.getOperand(0).getOperand(0)))
1522 if (C->getAPIntValue() == 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001523 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1,
1524 DAG.getNode(ISD::SHL, SDLoc(N), VT,
Dan Gohmancd9e1552010-01-19 23:30:49 +00001525 N0.getOperand(0).getOperand(1),
1526 N0.getOperand(1)));
1527
Owen Andersonbc146b02010-09-21 20:42:50 +00001528 if (N1.getOpcode() == ISD::AND) {
1529 SDValue AndOp0 = N1.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001530 ConstantSDNode *AndOp1 = dyn_cast<ConstantSDNode>(N1->getOperand(1));
Owen Andersonbc146b02010-09-21 20:42:50 +00001531 unsigned NumSignBits = DAG.ComputeNumSignBits(AndOp0);
1532 unsigned DestBits = VT.getScalarType().getSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001533
Owen Andersonbc146b02010-09-21 20:42:50 +00001534 // (add z, (and (sbbl x, x), 1)) -> (sub z, (sbbl x, x))
1535 // and similar xforms where the inner op is either ~0 or 0.
1536 if (NumSignBits == DestBits && AndOp1 && AndOp1->isOne()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001537 SDLoc DL(N);
Owen Andersonbc146b02010-09-21 20:42:50 +00001538 return DAG.getNode(ISD::SUB, DL, VT, N->getOperand(0), AndOp0);
1539 }
1540 }
1541
Benjamin Kramerf50125e2010-12-22 23:17:45 +00001542 // add (sext i1), X -> sub X, (zext i1)
1543 if (N0.getOpcode() == ISD::SIGN_EXTEND &&
1544 N0.getOperand(0).getValueType() == MVT::i1 &&
1545 !TLI.isOperationLegal(ISD::SIGN_EXTEND, MVT::i1)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001546 SDLoc DL(N);
Benjamin Kramerf50125e2010-12-22 23:17:45 +00001547 SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0));
1548 return DAG.getNode(ISD::SUB, DL, VT, N1, ZExt);
1549 }
1550
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001551 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001552}
1553
Dan Gohman475871a2008-07-27 21:46:04 +00001554SDValue DAGCombiner::visitADDC(SDNode *N) {
1555 SDValue N0 = N->getOperand(0);
1556 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001557 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1558 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001559 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001560
Chris Lattner91153682007-03-04 20:03:15 +00001561 // If the flag result is dead, turn this into an ADD.
Craig Topper704e1a02012-01-07 18:31:09 +00001562 if (!N->hasAnyUseOfValue(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001563 return CombineTo(N, DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001564 DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001565 SDLoc(N), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001566
Chris Lattner91153682007-03-04 20:03:15 +00001567 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001568 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001569 return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N1, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001570
Chris Lattnerb6541762007-03-04 20:40:38 +00001571 // fold (addc x, 0) -> x + no carry out
1572 if (N1C && N1C->isNullValue())
Dale Johannesen874ae252009-06-02 03:12:52 +00001573 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001574 SDLoc(N), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001575
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001576 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001577 APInt LHSZero, LHSOne;
1578 APInt RHSZero, RHSOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001579 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendling14036c02009-01-30 02:38:00 +00001580
Dan Gohman948d8ea2008-02-20 16:33:30 +00001581 if (LHSZero.getBoolValue()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001582 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001583
Chris Lattnerb6541762007-03-04 20:40:38 +00001584 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1585 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001586 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001587 return CombineTo(N, DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001588 DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001589 SDLoc(N), MVT::Glue));
Chris Lattnerb6541762007-03-04 20:40:38 +00001590 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001591
Dan Gohman475871a2008-07-27 21:46:04 +00001592 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001593}
1594
Dan Gohman475871a2008-07-27 21:46:04 +00001595SDValue DAGCombiner::visitADDE(SDNode *N) {
1596 SDValue N0 = N->getOperand(0);
1597 SDValue N1 = N->getOperand(1);
1598 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001599 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1600 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001601
Chris Lattner91153682007-03-04 20:03:15 +00001602 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001603 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001604 return DAG.getNode(ISD::ADDE, SDLoc(N), N->getVTList(),
Bill Wendling14036c02009-01-30 02:38:00 +00001605 N1, N0, CarryIn);
Scott Michelfdc40a02009-02-17 22:15:04 +00001606
Chris Lattnerb6541762007-03-04 20:40:38 +00001607 // fold (adde x, y, false) -> (addc x, y)
Dale Johannesen874ae252009-06-02 03:12:52 +00001608 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001609 return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001610
Dan Gohman475871a2008-07-27 21:46:04 +00001611 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001612}
1613
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001614// Since it may not be valid to emit a fold to zero for vector initializers
1615// check if we can before folding.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001616static SDValue tryFoldToZero(SDLoc DL, const TargetLowering &TLI, EVT VT,
Hal Finkelbd6f1f62013-07-09 17:02:45 +00001617 SelectionDAG &DAG,
1618 bool LegalOperations, bool LegalTypes) {
Stephen Linb4940152013-07-09 00:44:49 +00001619 if (!VT.isVector())
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001620 return DAG.getConstant(0, VT);
Dan Gohman71dc7c92011-05-17 22:20:36 +00001621 if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001622 // Produce a vector of zeros.
Hal Finkelbd6f1f62013-07-09 17:02:45 +00001623 EVT ElemTy = VT.getVectorElementType();
1624 if (LegalTypes && TLI.getTypeAction(*DAG.getContext(), ElemTy) ==
1625 TargetLowering::TypePromoteInteger)
1626 ElemTy = TLI.getTypeToTransformTo(*DAG.getContext(), ElemTy);
1627 assert((!LegalTypes || TLI.isTypeLegal(ElemTy)) &&
1628 "Type for zero vector elements is not legal");
1629 SDValue El = DAG.getConstant(0, ElemTy);
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001630 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
1631 return DAG.getNode(ISD::BUILD_VECTOR, DL, VT,
1632 &Ops[0], Ops.size());
1633 }
1634 return SDValue();
1635}
1636
Dan Gohman475871a2008-07-27 21:46:04 +00001637SDValue DAGCombiner::visitSUB(SDNode *N) {
1638 SDValue N0 = N->getOperand(0);
1639 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001640 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1641 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Eric Christopher7332e6e2011-07-14 01:12:15 +00001642 ConstantSDNode *N1C1 = N1.getOpcode() != ISD::ADD ? 0 :
1643 dyn_cast<ConstantSDNode>(N1.getOperand(1).getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001644 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001645
Dan Gohman7f321562007-06-25 16:23:39 +00001646 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001647 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001648 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001649 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper48b509c2012-12-10 08:12:29 +00001650
1651 // fold (sub x, 0) -> x, vector edition
1652 if (ISD::isBuildVectorAllZeros(N1.getNode()))
1653 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001654 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001655
Chris Lattner854077d2005-10-17 01:07:11 +00001656 // fold (sub x, x) -> 0
Eric Christopher169e1552011-02-16 01:10:03 +00001657 // FIXME: Refactor this and xor and other similar operations together.
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001658 if (N0 == N1)
Hal Finkelbd6f1f62013-07-09 17:02:45 +00001659 return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations, LegalTypes);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001660 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001661 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001662 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattner05b57432005-10-11 06:07:15 +00001663 // fold (sub x, c) -> (add x, -c)
1664 if (N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001665 return DAG.getNode(ISD::ADD, SDLoc(N), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001666 DAG.getConstant(-N1C->getAPIntValue(), VT));
Evan Cheng1ad0e8b2010-01-18 21:38:44 +00001667 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1)
1668 if (N0C && N0C->isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001669 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0);
Benjamin Kramer2c94b422011-01-29 12:34:05 +00001670 // fold A-(A-B) -> B
1671 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(0))
1672 return N1.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001673 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001674 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001675 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001676 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001677 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Scott Michelfdc40a02009-02-17 22:15:04 +00001678 return N0.getOperand(0);
Eric Christopher7332e6e2011-07-14 01:12:15 +00001679 // fold C2-(A+C1) -> (C2-C1)-A
1680 if (N1.getOpcode() == ISD::ADD && N0C && N1C1) {
Nadav Rotem6dfabb62012-09-20 08:53:31 +00001681 SDValue NewC = DAG.getConstant(N0C->getAPIntValue() - N1C1->getAPIntValue(),
1682 VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001683 return DAG.getNode(ISD::SUB, SDLoc(N), VT, NewC,
Bill Wendling96cb1122012-07-19 00:04:14 +00001684 N1.getOperand(0));
Eric Christopher7332e6e2011-07-14 01:12:15 +00001685 }
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001686 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001687 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001688 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1689 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001690 N0.getOperand(1).getOperand(0) == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001691 return DAG.getNode(N0.getOperand(1).getOpcode(), SDLoc(N), VT,
Bill Wendlingb0702e02009-01-30 02:42:10 +00001692 N0.getOperand(0), N0.getOperand(1).getOperand(1));
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001693 // fold ((A+(C+B))-B) -> A+C
1694 if (N0.getOpcode() == ISD::ADD &&
1695 N0.getOperand(1).getOpcode() == ISD::ADD &&
1696 N0.getOperand(1).getOperand(1) == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001697 return DAG.getNode(ISD::ADD, SDLoc(N), VT,
Bill Wendlingb0702e02009-01-30 02:42:10 +00001698 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dale Johannesen58e39b02008-12-23 01:59:54 +00001699 // fold ((A-(B-C))-C) -> A-B
1700 if (N0.getOpcode() == ISD::SUB &&
1701 N0.getOperand(1).getOpcode() == ISD::SUB &&
1702 N0.getOperand(1).getOperand(1) == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001703 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendlingb0702e02009-01-30 02:42:10 +00001704 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Bill Wendlingb0702e02009-01-30 02:42:10 +00001705
Dan Gohman613e0d82007-07-03 14:03:57 +00001706 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001707 if (N0.getOpcode() == ISD::UNDEF)
1708 return N0;
1709 if (N1.getOpcode() == ISD::UNDEF)
1710 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001711
Dan Gohman6520e202008-10-18 02:06:02 +00001712 // If the relocation model supports it, consider symbol offsets.
1713 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001714 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman6520e202008-10-18 02:06:02 +00001715 // fold (sub Sym, c) -> Sym-c
1716 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001717 return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001718 GA->getOffset() -
1719 (uint64_t)N1C->getSExtValue());
1720 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1721 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1722 if (GA->getGlobal() == GB->getGlobal())
1723 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1724 VT);
1725 }
1726
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001727 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001728}
1729
Craig Toppercc274522012-01-07 09:06:39 +00001730SDValue DAGCombiner::visitSUBC(SDNode *N) {
1731 SDValue N0 = N->getOperand(0);
1732 SDValue N1 = N->getOperand(1);
1733 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1734 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1735 EVT VT = N0.getValueType();
1736
1737 // If the flag result is dead, turn this into an SUB.
Craig Topper704e1a02012-01-07 18:31:09 +00001738 if (!N->hasAnyUseOfValue(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001739 return CombineTo(N, DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, N1),
1740 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001741 MVT::Glue));
1742
1743 // fold (subc x, x) -> 0 + no borrow
1744 if (N0 == N1)
1745 return CombineTo(N, DAG.getConstant(0, VT),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001746 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001747 MVT::Glue));
1748
1749 // fold (subc x, 0) -> x + no borrow
1750 if (N1C && N1C->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001751 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001752 MVT::Glue));
1753
1754 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) + no borrow
1755 if (N0C && N0C->isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001756 return CombineTo(N, DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0),
1757 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001758 MVT::Glue));
1759
1760 return SDValue();
1761}
1762
1763SDValue DAGCombiner::visitSUBE(SDNode *N) {
1764 SDValue N0 = N->getOperand(0);
1765 SDValue N1 = N->getOperand(1);
1766 SDValue CarryIn = N->getOperand(2);
1767
1768 // fold (sube x, y, false) -> (subc x, y)
1769 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001770 return DAG.getNode(ISD::SUBC, SDLoc(N), N->getVTList(), N0, N1);
Craig Toppercc274522012-01-07 09:06:39 +00001771
1772 return SDValue();
1773}
1774
Elena Demikhovskyd8026702013-06-26 12:15:53 +00001775/// isConstantSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
1776/// all the same constant or undefined.
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001777static bool isConstantSplatVector(SDNode *N, APInt& SplatValue) {
1778 BuildVectorSDNode *C = dyn_cast<BuildVectorSDNode>(N);
1779 if (!C)
1780 return false;
1781
1782 APInt SplatUndef;
1783 unsigned SplatBitSize;
1784 bool HasAnyUndefs;
1785 EVT EltVT = N->getValueType(0).getVectorElementType();
1786 return (C->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
1787 HasAnyUndefs) &&
1788 EltVT.getSizeInBits() >= SplatBitSize);
1789}
1790
Dan Gohman475871a2008-07-27 21:46:04 +00001791SDValue DAGCombiner::visitMUL(SDNode *N) {
1792 SDValue N0 = N->getOperand(0);
1793 SDValue N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00001794 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001795
Dan Gohman613e0d82007-07-03 14:03:57 +00001796 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001797 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001798 return DAG.getConstant(0, VT);
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001799
1800 bool N0IsConst = false;
1801 bool N1IsConst = false;
1802 APInt ConstValue0, ConstValue1;
1803 // fold vector ops
1804 if (VT.isVector()) {
1805 SDValue FoldedVOp = SimplifyVBinOp(N);
1806 if (FoldedVOp.getNode()) return FoldedVOp;
1807
1808 N0IsConst = isConstantSplatVector(N0.getNode(), ConstValue0);
1809 N1IsConst = isConstantSplatVector(N1.getNode(), ConstValue1);
1810 } else {
1811 N0IsConst = dyn_cast<ConstantSDNode>(N0) != 0;
1812 ConstValue0 = N0IsConst? (dyn_cast<ConstantSDNode>(N0))->getAPIntValue() : APInt();
1813 N1IsConst = dyn_cast<ConstantSDNode>(N1) != 0;
1814 ConstValue1 = N1IsConst? (dyn_cast<ConstantSDNode>(N1))->getAPIntValue() : APInt();
1815 }
1816
Nate Begeman1d4d4142005-09-01 00:19:25 +00001817 // fold (mul c1, c2) -> c1*c2
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001818 if (N0IsConst && N1IsConst)
1819 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0.getNode(), N1.getNode());
1820
Nate Begeman99801192005-09-07 23:25:52 +00001821 // canonicalize constant to RHS
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001822 if (N0IsConst && !N1IsConst)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001823 return DAG.getNode(ISD::MUL, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001824 // fold (mul x, 0) -> 0
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001825 if (N1IsConst && ConstValue1 == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001826 return N1;
Benjamin Kramer530d09a2013-09-19 13:28:20 +00001827 // We require a splat of the entire scalar bit width for non-contiguous
1828 // bit patterns.
1829 bool IsFullSplat =
1830 ConstValue1.getBitWidth() == VT.getScalarType().getSizeInBits();
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001831 // fold (mul x, 1) -> x
Benjamin Kramer530d09a2013-09-19 13:28:20 +00001832 if (N1IsConst && ConstValue1 == 1 && IsFullSplat)
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001833 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001834 // fold (mul x, -1) -> 0-x
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001835 if (N1IsConst && ConstValue1.isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001836 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001837 DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001838 // fold (mul x, (1 << c)) -> x << c
Benjamin Kramer530d09a2013-09-19 13:28:20 +00001839 if (N1IsConst && ConstValue1.isPowerOf2() && IsFullSplat)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001840 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001841 DAG.getConstant(ConstValue1.logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00001842 getShiftAmountTy(N0.getValueType())));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001843 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Benjamin Kramer530d09a2013-09-19 13:28:20 +00001844 if (N1IsConst && (-ConstValue1).isPowerOf2() && IsFullSplat) {
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001845 unsigned Log2Val = (-ConstValue1).logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00001846 // FIXME: If the input is something that is easily negated (e.g. a
Chris Lattner3e6099b2005-10-30 06:41:49 +00001847 // single-use add), we should put the negate there.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001848 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001849 DAG.getConstant(0, VT),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001850 DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
Owen Anderson95771af2011-02-25 21:41:48 +00001851 DAG.getConstant(Log2Val,
1852 getShiftAmountTy(N0.getValueType()))));
Chris Lattner66b8bc32009-03-09 20:22:18 +00001853 }
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001854
1855 APInt Val;
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001856 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
Stephen Lin155615d2013-07-08 00:37:03 +00001857 if (N1IsConst && N0.getOpcode() == ISD::SHL &&
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001858 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1859 isa<ConstantSDNode>(N0.getOperand(1)))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001860 SDValue C3 = DAG.getNode(ISD::SHL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001861 N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001862 AddToWorkList(C3.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00001863 return DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001864 N0.getOperand(0), C3);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001865 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001866
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001867 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1868 // use.
1869 {
Dan Gohman475871a2008-07-27 21:46:04 +00001870 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001871 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
Stephen Lin155615d2013-07-08 00:37:03 +00001872 if (N0.getOpcode() == ISD::SHL &&
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001873 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1874 isa<ConstantSDNode>(N0.getOperand(1))) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001875 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001876 Sh = N0; Y = N1;
Scott Michelfdc40a02009-02-17 22:15:04 +00001877 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001878 isa<ConstantSDNode>(N1.getOperand(1)) &&
1879 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001880 Sh = N1; Y = N0;
1881 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001882
Gabor Greifba36cb52008-08-28 21:40:38 +00001883 if (Sh.getNode()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001884 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001885 Sh.getOperand(0), Y);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001886 return DAG.getNode(ISD::SHL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001887 Mul, Sh.getOperand(1));
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001888 }
1889 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001890
Chris Lattnera1deca32006-03-04 23:33:26 +00001891 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001892 if (N1IsConst && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
1893 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1894 isa<ConstantSDNode>(N0.getOperand(1))))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001895 return DAG.getNode(ISD::ADD, SDLoc(N), VT,
1896 DAG.getNode(ISD::MUL, SDLoc(N0), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001897 N0.getOperand(0), N1),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001898 DAG.getNode(ISD::MUL, SDLoc(N1), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001899 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001900
Nate Begemancd4d58c2006-02-03 06:46:56 +00001901 // reassociate mul
Andrew Trickac6d9be2013-05-25 02:42:55 +00001902 SDValue RMUL = ReassociateOps(ISD::MUL, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001903 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001904 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001905
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001906 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001907}
1908
Dan Gohman475871a2008-07-27 21:46:04 +00001909SDValue DAGCombiner::visitSDIV(SDNode *N) {
1910 SDValue N0 = N->getOperand(0);
1911 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001912 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1913 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001914 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001915
Dan Gohman7f321562007-06-25 16:23:39 +00001916 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001917 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001918 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001919 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001920 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001921
Nate Begeman1d4d4142005-09-01 00:19:25 +00001922 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001923 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001924 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001925 // fold (sdiv X, 1) -> X
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001926 if (N1C && N1C->getAPIntValue() == 1LL)
Nate Begeman405e3ec2005-10-21 00:02:42 +00001927 return N0;
1928 // fold (sdiv X, -1) -> 0-X
1929 if (N1C && N1C->isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001930 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling944d34b2009-01-30 02:52:17 +00001931 DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001932 // If we know the sign bits of both operands are zero, strength reduce to a
1933 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001934 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001935 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001936 return DAG.getNode(ISD::UDIV, SDLoc(N), N1.getValueType(),
Bill Wendling944d34b2009-01-30 02:52:17 +00001937 N0, N1);
Chris Lattnerf32aac32008-01-27 23:32:17 +00001938 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001939 // fold (sdiv X, pow2) -> simple ops after legalize
Eli Friedman1c663fe2011-12-07 03:55:52 +00001940 if (N1C && !N1C->isNullValue() &&
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001941 (N1C->getAPIntValue().isPowerOf2() ||
1942 (-N1C->getAPIntValue()).isPowerOf2())) {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001943 // If dividing by powers of two is cheap, then don't perform the following
1944 // fold.
1945 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001946 return SDValue();
Bill Wendling944d34b2009-01-30 02:52:17 +00001947
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001948 unsigned lg2 = N1C->getAPIntValue().countTrailingZeros();
Bill Wendling944d34b2009-01-30 02:52:17 +00001949
Chris Lattner8f4880b2006-02-16 08:02:36 +00001950 // Splat the sign bit into the register
Andrew Trickac6d9be2013-05-25 02:42:55 +00001951 SDValue SGN = DAG.getNode(ISD::SRA, SDLoc(N), VT, N0,
Bill Wendling944d34b2009-01-30 02:52:17 +00001952 DAG.getConstant(VT.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00001953 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00001954 AddToWorkList(SGN.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001955
Chris Lattner8f4880b2006-02-16 08:02:36 +00001956 // Add (N0 < 0) ? abs2 - 1 : 0;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001957 SDValue SRL = DAG.getNode(ISD::SRL, SDLoc(N), VT, SGN,
Bill Wendling944d34b2009-01-30 02:52:17 +00001958 DAG.getConstant(VT.getSizeInBits() - lg2,
Owen Anderson95771af2011-02-25 21:41:48 +00001959 getShiftAmountTy(SGN.getValueType())));
Andrew Trickac6d9be2013-05-25 02:42:55 +00001960 SDValue ADD = DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001961 AddToWorkList(SRL.getNode());
1962 AddToWorkList(ADD.getNode()); // Divide by pow2
Andrew Trickac6d9be2013-05-25 02:42:55 +00001963 SDValue SRA = DAG.getNode(ISD::SRA, SDLoc(N), VT, ADD,
Owen Anderson95771af2011-02-25 21:41:48 +00001964 DAG.getConstant(lg2, getShiftAmountTy(ADD.getValueType())));
Bill Wendling944d34b2009-01-30 02:52:17 +00001965
Nate Begeman405e3ec2005-10-21 00:02:42 +00001966 // If we're dividing by a positive value, we're done. Otherwise, we must
1967 // negate the result.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001968 if (N1C->getAPIntValue().isNonNegative())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001969 return SRA;
Bill Wendling944d34b2009-01-30 02:52:17 +00001970
Gabor Greifba36cb52008-08-28 21:40:38 +00001971 AddToWorkList(SRA.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00001972 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling944d34b2009-01-30 02:52:17 +00001973 DAG.getConstant(0, VT), SRA);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001974 }
Bill Wendling944d34b2009-01-30 02:52:17 +00001975
Nate Begeman69575232005-10-20 02:15:44 +00001976 // if integer divide is expensive and we satisfy the requirements, emit an
1977 // alternate sequence.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001978 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001979 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001980 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001981 }
Dan Gohman7f321562007-06-25 16:23:39 +00001982
Dan Gohman613e0d82007-07-03 14:03:57 +00001983 // undef / X -> 0
1984 if (N0.getOpcode() == ISD::UNDEF)
1985 return DAG.getConstant(0, VT);
1986 // X / undef -> undef
1987 if (N1.getOpcode() == ISD::UNDEF)
1988 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001989
Dan Gohman475871a2008-07-27 21:46:04 +00001990 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001991}
1992
Dan Gohman475871a2008-07-27 21:46:04 +00001993SDValue DAGCombiner::visitUDIV(SDNode *N) {
1994 SDValue N0 = N->getOperand(0);
1995 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001996 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1997 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001998 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001999
Dan Gohman7f321562007-06-25 16:23:39 +00002000 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002001 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002002 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002003 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002004 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002005
Nate Begeman1d4d4142005-09-01 00:19:25 +00002006 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002007 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002008 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002009 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00002010 if (N1C && N1C->getAPIntValue().isPowerOf2())
Andrew Trickac6d9be2013-05-25 02:42:55 +00002011 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00002012 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00002013 getShiftAmountTy(N0.getValueType())));
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002014 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00002015 if (N1.getOpcode() == ISD::SHL) {
2016 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002017 if (SHC->getAPIntValue().isPowerOf2()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002018 EVT ADDVT = N1.getOperand(1).getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002019 SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N), ADDVT,
Bill Wendling07d85142009-01-30 02:55:25 +00002020 N1.getOperand(1),
2021 DAG.getConstant(SHC->getAPIntValue()
2022 .logBase2(),
2023 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002024 AddToWorkList(Add.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002025 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00002026 }
2027 }
2028 }
Nate Begeman69575232005-10-20 02:15:44 +00002029 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00002030 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002031 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002032 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00002033 }
Dan Gohman7f321562007-06-25 16:23:39 +00002034
Dan Gohman613e0d82007-07-03 14:03:57 +00002035 // undef / X -> 0
2036 if (N0.getOpcode() == ISD::UNDEF)
2037 return DAG.getConstant(0, VT);
2038 // X / undef -> undef
2039 if (N1.getOpcode() == ISD::UNDEF)
2040 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002041
Dan Gohman475871a2008-07-27 21:46:04 +00002042 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002043}
2044
Dan Gohman475871a2008-07-27 21:46:04 +00002045SDValue DAGCombiner::visitSREM(SDNode *N) {
2046 SDValue N0 = N->getOperand(0);
2047 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002048 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2049 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002050 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002051
Nate Begeman1d4d4142005-09-01 00:19:25 +00002052 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002053 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002054 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00002055 // If we know the sign bits of both operands are zero, strength reduce to a
2056 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00002057 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002058 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00002059 return DAG.getNode(ISD::UREM, SDLoc(N), VT, N0, N1);
Chris Lattneree339f42008-01-27 23:21:58 +00002060 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002061
Dan Gohman77003042007-11-26 23:46:11 +00002062 // If X/C can be simplified by the division-by-constant logic, lower
2063 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00002064 if (N1C && !N1C->isNullValue()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002065 SDValue Div = DAG.getNode(ISD::SDIV, SDLoc(N), VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002066 AddToWorkList(Div.getNode());
2067 SDValue OptimizedDiv = combine(Div.getNode());
2068 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002069 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002070 OptimizedDiv, N1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002071 SDValue Sub = DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00002072 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00002073 return Sub;
2074 }
Chris Lattner26d29902006-10-12 20:58:32 +00002075 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002076
Dan Gohman613e0d82007-07-03 14:03:57 +00002077 // undef % X -> 0
2078 if (N0.getOpcode() == ISD::UNDEF)
2079 return DAG.getConstant(0, VT);
2080 // X % undef -> undef
2081 if (N1.getOpcode() == ISD::UNDEF)
2082 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002083
Dan Gohman475871a2008-07-27 21:46:04 +00002084 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002085}
2086
Dan Gohman475871a2008-07-27 21:46:04 +00002087SDValue DAGCombiner::visitUREM(SDNode *N) {
2088 SDValue N0 = N->getOperand(0);
2089 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002090 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2091 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002092 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002093
Nate Begeman1d4d4142005-09-01 00:19:25 +00002094 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002095 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002096 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00002097 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002098 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
Andrew Trickac6d9be2013-05-25 02:42:55 +00002099 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00002100 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00002101 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
2102 if (N1.getOpcode() == ISD::SHL) {
2103 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002104 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002105 SDValue Add =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002106 DAG.getNode(ISD::ADD, SDLoc(N), VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002107 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00002108 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002109 AddToWorkList(Add.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002110 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, Add);
Nate Begemanc031e332006-02-05 07:36:48 +00002111 }
2112 }
2113 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002114
Dan Gohman77003042007-11-26 23:46:11 +00002115 // If X/C can be simplified by the division-by-constant logic, lower
2116 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00002117 if (N1C && !N1C->isNullValue()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002118 SDValue Div = DAG.getNode(ISD::UDIV, SDLoc(N), VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00002119 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00002120 SDValue OptimizedDiv = combine(Div.getNode());
2121 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002122 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002123 OptimizedDiv, N1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002124 SDValue Sub = DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00002125 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00002126 return Sub;
2127 }
Chris Lattner26d29902006-10-12 20:58:32 +00002128 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002129
Dan Gohman613e0d82007-07-03 14:03:57 +00002130 // undef % X -> 0
2131 if (N0.getOpcode() == ISD::UNDEF)
2132 return DAG.getConstant(0, VT);
2133 // X % undef -> undef
2134 if (N1.getOpcode() == ISD::UNDEF)
2135 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002136
Dan Gohman475871a2008-07-27 21:46:04 +00002137 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002138}
2139
Dan Gohman475871a2008-07-27 21:46:04 +00002140SDValue DAGCombiner::visitMULHS(SDNode *N) {
2141 SDValue N0 = N->getOperand(0);
2142 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002143 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002144 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002145 SDLoc DL(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00002146
Nate Begeman1d4d4142005-09-01 00:19:25 +00002147 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002148 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002149 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002150 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002151 if (N1C && N1C->getAPIntValue() == 1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002152 return DAG.getNode(ISD::SRA, SDLoc(N), N0.getValueType(), N0,
Bill Wendling326411d2009-01-30 03:00:18 +00002153 DAG.getConstant(N0.getValueType().getSizeInBits() - 1,
Owen Anderson95771af2011-02-25 21:41:48 +00002154 getShiftAmountTy(N0.getValueType())));
Dan Gohman613e0d82007-07-03 14:03:57 +00002155 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002156 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002157 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002158
Chris Lattnerde1c3602010-12-13 08:39:01 +00002159 // If the type twice as wide is legal, transform the mulhs to a wider multiply
2160 // plus a shift.
2161 if (VT.isSimple() && !VT.isVector()) {
2162 MVT Simple = VT.getSimpleVT();
2163 unsigned SimpleSize = Simple.getSizeInBits();
2164 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2165 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2166 N0 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N0);
2167 N1 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N1);
2168 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
Chris Lattner1a0fbe22010-12-15 05:51:39 +00002169 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002170 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002171 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2172 }
2173 }
Owen Anderson95771af2011-02-25 21:41:48 +00002174
Dan Gohman475871a2008-07-27 21:46:04 +00002175 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002176}
2177
Dan Gohman475871a2008-07-27 21:46:04 +00002178SDValue DAGCombiner::visitMULHU(SDNode *N) {
2179 SDValue N0 = N->getOperand(0);
2180 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002181 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002182 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002183 SDLoc DL(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00002184
Nate Begeman1d4d4142005-09-01 00:19:25 +00002185 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002186 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002187 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002188 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00002189 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002190 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00002191 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002192 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002193 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002194
Chris Lattnerde1c3602010-12-13 08:39:01 +00002195 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2196 // plus a shift.
2197 if (VT.isSimple() && !VT.isVector()) {
2198 MVT Simple = VT.getSimpleVT();
2199 unsigned SimpleSize = Simple.getSizeInBits();
2200 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2201 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2202 N0 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N0);
2203 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N1);
2204 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
2205 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002206 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002207 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2208 }
2209 }
Owen Anderson95771af2011-02-25 21:41:48 +00002210
Dan Gohman475871a2008-07-27 21:46:04 +00002211 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002212}
2213
Dan Gohman389079b2007-10-08 17:57:15 +00002214/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
2215/// compute two values. LoOp and HiOp give the opcodes for the two computations
2216/// that are being performed. Return true if a simplification was made.
2217///
Scott Michelfdc40a02009-02-17 22:15:04 +00002218SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Dan Gohman475871a2008-07-27 21:46:04 +00002219 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00002220 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00002221 bool HiExists = N->hasAnyUseOfValue(1);
2222 if (!HiExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002223 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002224 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002225 SDValue Res = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0),
Bill Wendling826d1142009-01-30 03:08:40 +00002226 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002227 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002228 }
2229
2230 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00002231 bool LoExists = N->hasAnyUseOfValue(0);
2232 if (!LoExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002233 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002234 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002235 SDValue Res = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1),
Bill Wendling826d1142009-01-30 03:08:40 +00002236 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002237 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002238 }
2239
Evan Cheng44711942007-11-08 09:25:29 +00002240 // If both halves are used, return as it is.
2241 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00002242 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00002243
2244 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00002245 if (LoExists) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002246 SDValue Lo = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0),
Bill Wendling826d1142009-01-30 03:08:40 +00002247 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002248 AddToWorkList(Lo.getNode());
2249 SDValue LoOpt = combine(Lo.getNode());
2250 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002251 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002252 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002253 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00002254 }
2255
Evan Cheng44711942007-11-08 09:25:29 +00002256 if (HiExists) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002257 SDValue Hi = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1),
Duncan Sands25cf2272008-11-24 14:53:14 +00002258 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002259 AddToWorkList(Hi.getNode());
2260 SDValue HiOpt = combine(Hi.getNode());
2261 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002262 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002263 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002264 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00002265 }
Bill Wendling826d1142009-01-30 03:08:40 +00002266
Dan Gohman475871a2008-07-27 21:46:04 +00002267 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002268}
2269
Dan Gohman475871a2008-07-27 21:46:04 +00002270SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
2271 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00002272 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002273
Chris Lattner33e77d32010-12-15 06:04:19 +00002274 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002275 SDLoc DL(N);
Chris Lattner33e77d32010-12-15 06:04:19 +00002276
2277 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2278 // plus a shift.
2279 if (VT.isSimple() && !VT.isVector()) {
2280 MVT Simple = VT.getSimpleVT();
2281 unsigned SimpleSize = Simple.getSizeInBits();
2282 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2283 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2284 SDValue Lo = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(0));
2285 SDValue Hi = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(1));
2286 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2287 // Compute the high part as N1.
2288 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002289 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002290 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2291 // Compute the low part as N0.
2292 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2293 return CombineTo(N, Lo, Hi);
2294 }
2295 }
Owen Anderson95771af2011-02-25 21:41:48 +00002296
Dan Gohman475871a2008-07-27 21:46:04 +00002297 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002298}
2299
Dan Gohman475871a2008-07-27 21:46:04 +00002300SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
2301 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00002302 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002303
Chris Lattner33e77d32010-12-15 06:04:19 +00002304 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002305 SDLoc DL(N);
Owen Anderson95771af2011-02-25 21:41:48 +00002306
Chris Lattner33e77d32010-12-15 06:04:19 +00002307 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2308 // plus a shift.
2309 if (VT.isSimple() && !VT.isVector()) {
2310 MVT Simple = VT.getSimpleVT();
2311 unsigned SimpleSize = Simple.getSizeInBits();
2312 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2313 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2314 SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(0));
2315 SDValue Hi = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(1));
2316 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2317 // Compute the high part as N1.
2318 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002319 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002320 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2321 // Compute the low part as N0.
2322 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2323 return CombineTo(N, Lo, Hi);
2324 }
2325 }
Owen Anderson95771af2011-02-25 21:41:48 +00002326
Dan Gohman475871a2008-07-27 21:46:04 +00002327 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002328}
2329
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002330SDValue DAGCombiner::visitSMULO(SDNode *N) {
2331 // (smulo x, 2) -> (saddo x, x)
2332 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2333 if (C2->getAPIntValue() == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002334 return DAG.getNode(ISD::SADDO, SDLoc(N), N->getVTList(),
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002335 N->getOperand(0), N->getOperand(0));
2336
2337 return SDValue();
2338}
2339
2340SDValue DAGCombiner::visitUMULO(SDNode *N) {
2341 // (umulo x, 2) -> (uaddo x, x)
2342 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2343 if (C2->getAPIntValue() == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002344 return DAG.getNode(ISD::UADDO, SDLoc(N), N->getVTList(),
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002345 N->getOperand(0), N->getOperand(0));
2346
2347 return SDValue();
2348}
2349
Dan Gohman475871a2008-07-27 21:46:04 +00002350SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
2351 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002352 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002353
Dan Gohman475871a2008-07-27 21:46:04 +00002354 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002355}
2356
Dan Gohman475871a2008-07-27 21:46:04 +00002357SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
2358 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002359 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002360
Dan Gohman475871a2008-07-27 21:46:04 +00002361 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002362}
2363
Chris Lattner35e5c142006-05-05 05:51:50 +00002364/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
2365/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00002366SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
2367 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00002368 EVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00002369 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
Scott Michelfdc40a02009-02-17 22:15:04 +00002370
Dan Gohmanff00a552010-01-14 03:08:49 +00002371 // Bail early if none of these transforms apply.
2372 if (N0.getNode()->getNumOperands() == 0) return SDValue();
2373
Chris Lattner540121f2006-05-05 06:31:05 +00002374 // For each of OP in AND/OR/XOR:
2375 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
2376 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
2377 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002378 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y)) (if trunc isn't free)
Nate Begeman93e0ed32009-12-03 07:11:29 +00002379 //
2380 // do not sink logical op inside of a vector extend, since it may combine
2381 // into a vsetcc.
Evan Chengd40d03e2010-01-06 19:38:29 +00002382 EVT Op0VT = N0.getOperand(0).getValueType();
2383 if ((N0.getOpcode() == ISD::ZERO_EXTEND ||
Dan Gohman97121ba2009-04-08 00:15:30 +00002384 N0.getOpcode() == ISD::SIGN_EXTEND ||
Evan Chenge5b51ac2010-04-17 06:13:15 +00002385 // Avoid infinite looping with PromoteIntBinOp.
2386 (N0.getOpcode() == ISD::ANY_EXTEND &&
2387 (!LegalTypes || TLI.isTypeDesirableForOp(N->getOpcode(), Op0VT))) ||
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002388 (N0.getOpcode() == ISD::TRUNCATE &&
2389 (!TLI.isZExtFree(VT, Op0VT) ||
2390 !TLI.isTruncateFree(Op0VT, VT)) &&
2391 TLI.isTypeLegal(Op0VT))) &&
Nate Begeman93e0ed32009-12-03 07:11:29 +00002392 !VT.isVector() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00002393 Op0VT == N1.getOperand(0).getValueType() &&
2394 (!LegalOperations || TLI.isOperationLegal(N->getOpcode(), Op0VT))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002395 SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0),
Bill Wendlingb74c8672009-01-30 19:25:47 +00002396 N0.getOperand(0).getValueType(),
2397 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002398 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002399 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00002400 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002401
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002402 // For each of OP in SHL/SRL/SRA/AND...
2403 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
2404 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
2405 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00002406 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002407 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00002408 N0.getOperand(1) == N1.getOperand(1)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002409 SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0),
Bill Wendlingb74c8672009-01-30 19:25:47 +00002410 N0.getOperand(0).getValueType(),
2411 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002412 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002413 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Bill Wendlingb74c8672009-01-30 19:25:47 +00002414 ORNode, N0.getOperand(1));
Chris Lattner35e5c142006-05-05 05:51:50 +00002415 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002416
Nadav Rotem4ac90812012-04-01 19:31:22 +00002417 // Simplify xor/and/or (bitcast(A), bitcast(B)) -> bitcast(op (A,B))
2418 // Only perform this optimization after type legalization and before
2419 // LegalizeVectorOprs. LegalizeVectorOprs promotes vector operations by
2420 // adding bitcasts. For example (xor v4i32) is promoted to (v2i64), and
2421 // we don't want to undo this promotion.
2422 // We also handle SCALAR_TO_VECTOR because xor/or/and operations are cheaper
2423 // on scalars.
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002424 if ((N0.getOpcode() == ISD::BITCAST ||
2425 N0.getOpcode() == ISD::SCALAR_TO_VECTOR) &&
2426 Level == AfterLegalizeTypes) {
Nadav Rotem4ac90812012-04-01 19:31:22 +00002427 SDValue In0 = N0.getOperand(0);
2428 SDValue In1 = N1.getOperand(0);
2429 EVT In0Ty = In0.getValueType();
2430 EVT In1Ty = In1.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002431 SDLoc DL(N);
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002432 // If both incoming values are integers, and the original types are the
2433 // same.
Nadav Rotem4ac90812012-04-01 19:31:22 +00002434 if (In0Ty.isInteger() && In1Ty.isInteger() && In0Ty == In1Ty) {
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002435 SDValue Op = DAG.getNode(N->getOpcode(), DL, In0Ty, In0, In1);
2436 SDValue BC = DAG.getNode(N0.getOpcode(), DL, VT, Op);
Nadav Rotem4ac90812012-04-01 19:31:22 +00002437 AddToWorkList(Op.getNode());
2438 return BC;
2439 }
2440 }
2441
2442 // Xor/and/or are indifferent to the swizzle operation (shuffle of one value).
2443 // Simplify xor/and/or (shuff(A), shuff(B)) -> shuff(op (A,B))
2444 // If both shuffles use the same mask, and both shuffle within a single
2445 // vector, then it is worthwhile to move the swizzle after the operation.
2446 // The type-legalizer generates this pattern when loading illegal
2447 // vector types from memory. In many cases this allows additional shuffle
2448 // optimizations.
Craig Topperf9204232012-04-09 07:19:09 +00002449 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
2450 N0.getOperand(1).getOpcode() == ISD::UNDEF &&
2451 N1.getOperand(1).getOpcode() == ISD::UNDEF) {
Nadav Rotem4ac90812012-04-01 19:31:22 +00002452 ShuffleVectorSDNode *SVN0 = cast<ShuffleVectorSDNode>(N0);
2453 ShuffleVectorSDNode *SVN1 = cast<ShuffleVectorSDNode>(N1);
Craig Topperf9204232012-04-09 07:19:09 +00002454
2455 assert(N0.getOperand(0).getValueType() == N1.getOperand(1).getValueType() &&
2456 "Inputs to shuffles are not the same type");
Nadav Rotem4ac90812012-04-01 19:31:22 +00002457
2458 unsigned NumElts = VT.getVectorNumElements();
Nadav Rotem4ac90812012-04-01 19:31:22 +00002459
2460 // Check that both shuffles use the same mask. The masks are known to be of
2461 // the same length because the result vector type is the same.
2462 bool SameMask = true;
2463 for (unsigned i = 0; i != NumElts; ++i) {
2464 int Idx0 = SVN0->getMaskElt(i);
2465 int Idx1 = SVN1->getMaskElt(i);
2466 if (Idx0 != Idx1) {
2467 SameMask = false;
2468 break;
2469 }
2470 }
2471
Craig Topperf9204232012-04-09 07:19:09 +00002472 if (SameMask) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002473 SDValue Op = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Craig Topperf9204232012-04-09 07:19:09 +00002474 N0.getOperand(0), N1.getOperand(0));
Nadav Rotem4ac90812012-04-01 19:31:22 +00002475 AddToWorkList(Op.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002476 return DAG.getVectorShuffle(VT, SDLoc(N), Op,
Craig Topperf9204232012-04-09 07:19:09 +00002477 DAG.getUNDEF(VT), &SVN0->getMask()[0]);
Nadav Rotem4ac90812012-04-01 19:31:22 +00002478 }
2479 }
Craig Topperf9204232012-04-09 07:19:09 +00002480
Dan Gohman475871a2008-07-27 21:46:04 +00002481 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00002482}
2483
Dan Gohman475871a2008-07-27 21:46:04 +00002484SDValue DAGCombiner::visitAND(SDNode *N) {
2485 SDValue N0 = N->getOperand(0);
2486 SDValue N1 = N->getOperand(1);
2487 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00002488 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2489 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002490 EVT VT = N1.getValueType();
Dan Gohman6900a392010-03-04 00:23:16 +00002491 unsigned BitWidth = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002492
Dan Gohman7f321562007-06-25 16:23:39 +00002493 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002494 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002495 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002496 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00002497
2498 // fold (and x, 0) -> 0, vector edition
2499 if (ISD::isBuildVectorAllZeros(N0.getNode()))
2500 return N0;
2501 if (ISD::isBuildVectorAllZeros(N1.getNode()))
2502 return N1;
2503
2504 // fold (and x, -1) -> x, vector edition
2505 if (ISD::isBuildVectorAllOnes(N0.getNode()))
2506 return N1;
2507 if (ISD::isBuildVectorAllOnes(N1.getNode()))
2508 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002509 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002510
Dan Gohman613e0d82007-07-03 14:03:57 +00002511 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002512 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002513 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002514 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002515 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002516 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002517 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002518 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002519 return DAG.getNode(ISD::AND, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002520 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002521 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002522 return N0;
2523 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002524 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002525 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002526 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00002527 // reassociate and
Andrew Trickac6d9be2013-05-25 02:42:55 +00002528 SDValue RAND = ReassociateOps(ISD::AND, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002529 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002530 return RAND;
Bill Wendling7d9f2b92010-03-03 00:35:56 +00002531 // fold (and (or x, C), D) -> D if (C & D) == D
Nate Begeman5dc7e862005-11-02 18:42:59 +00002532 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002533 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002534 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002535 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00002536 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
2537 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00002538 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002539 APInt Mask = ~N1C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00002540 Mask = Mask.trunc(N0Op0.getValueSizeInBits());
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002541 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002542 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N),
Bill Wendling2627a882009-01-30 20:43:18 +00002543 N0.getValueType(), N0Op0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002544
Chris Lattner1ec05d12006-03-01 21:47:21 +00002545 // Replace uses of the AND with uses of the Zero extend node.
2546 CombineTo(N, Zext);
Scott Michelfdc40a02009-02-17 22:15:04 +00002547
Chris Lattner3603cd62006-02-02 07:17:31 +00002548 // We actually want to replace all uses of the any_extend with the
2549 // zero_extend, to avoid duplicating things. This will later cause this
2550 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00002551 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00002552 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00002553 }
2554 }
Stephen Lin155615d2013-07-08 00:37:03 +00002555 // similarly fold (and (X (load ([non_ext|any_ext|zero_ext] V))), c) ->
James Molloy6259dcd2012-02-20 12:02:38 +00002556 // (X (load ([non_ext|zero_ext] V))) if 'and' only clears top bits which must
2557 // already be zero by virtue of the width of the base type of the load.
2558 //
2559 // the 'X' node here can either be nothing or an extract_vector_elt to catch
2560 // more cases.
2561 if ((N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
2562 N0.getOperand(0).getOpcode() == ISD::LOAD) ||
2563 N0.getOpcode() == ISD::LOAD) {
2564 LoadSDNode *Load = cast<LoadSDNode>( (N0.getOpcode() == ISD::LOAD) ?
2565 N0 : N0.getOperand(0) );
2566
2567 // Get the constant (if applicable) the zero'th operand is being ANDed with.
2568 // This can be a pure constant or a vector splat, in which case we treat the
2569 // vector as a scalar and use the splat value.
2570 APInt Constant = APInt::getNullValue(1);
2571 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2572 Constant = C->getAPIntValue();
2573 } else if (BuildVectorSDNode *Vector = dyn_cast<BuildVectorSDNode>(N1)) {
2574 APInt SplatValue, SplatUndef;
2575 unsigned SplatBitSize;
2576 bool HasAnyUndefs;
2577 bool IsSplat = Vector->isConstantSplat(SplatValue, SplatUndef,
2578 SplatBitSize, HasAnyUndefs);
2579 if (IsSplat) {
2580 // Undef bits can contribute to a possible optimisation if set, so
2581 // set them.
2582 SplatValue |= SplatUndef;
2583
2584 // The splat value may be something like "0x00FFFFFF", which means 0 for
2585 // the first vector value and FF for the rest, repeating. We need a mask
2586 // that will apply equally to all members of the vector, so AND all the
2587 // lanes of the constant together.
2588 EVT VT = Vector->getValueType(0);
2589 unsigned BitWidth = VT.getVectorElementType().getSizeInBits();
Silviu Baranga3d5e1612012-09-05 08:57:21 +00002590
2591 // If the splat value has been compressed to a bitlength lower
2592 // than the size of the vector lane, we need to re-expand it to
2593 // the lane size.
2594 if (BitWidth > SplatBitSize)
2595 for (SplatValue = SplatValue.zextOrTrunc(BitWidth);
2596 SplatBitSize < BitWidth;
2597 SplatBitSize = SplatBitSize * 2)
2598 SplatValue |= SplatValue.shl(SplatBitSize);
2599
James Molloy6259dcd2012-02-20 12:02:38 +00002600 Constant = APInt::getAllOnesValue(BitWidth);
Silviu Baranga3d5e1612012-09-05 08:57:21 +00002601 for (unsigned i = 0, n = SplatBitSize/BitWidth; i < n; ++i)
James Molloy6259dcd2012-02-20 12:02:38 +00002602 Constant &= SplatValue.lshr(i*BitWidth).zextOrTrunc(BitWidth);
2603 }
2604 }
2605
2606 // If we want to change an EXTLOAD to a ZEXTLOAD, ensure a ZEXTLOAD is
2607 // actually legal and isn't going to get expanded, else this is a false
2608 // optimisation.
2609 bool CanZextLoadProfitably = TLI.isLoadExtLegal(ISD::ZEXTLOAD,
2610 Load->getMemoryVT());
2611
2612 // Resize the constant to the same size as the original memory access before
2613 // extension. If it is still the AllOnesValue then this AND is completely
2614 // unneeded.
2615 Constant =
2616 Constant.zextOrTrunc(Load->getMemoryVT().getScalarType().getSizeInBits());
2617
2618 bool B;
2619 switch (Load->getExtensionType()) {
2620 default: B = false; break;
2621 case ISD::EXTLOAD: B = CanZextLoadProfitably; break;
2622 case ISD::ZEXTLOAD:
2623 case ISD::NON_EXTLOAD: B = true; break;
2624 }
2625
2626 if (B && Constant.isAllOnesValue()) {
2627 // If the load type was an EXTLOAD, convert to ZEXTLOAD in order to
2628 // preserve semantics once we get rid of the AND.
2629 SDValue NewLoad(Load, 0);
2630 if (Load->getExtensionType() == ISD::EXTLOAD) {
2631 NewLoad = DAG.getLoad(Load->getAddressingMode(), ISD::ZEXTLOAD,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002632 Load->getValueType(0), SDLoc(Load),
James Molloy6259dcd2012-02-20 12:02:38 +00002633 Load->getChain(), Load->getBasePtr(),
2634 Load->getOffset(), Load->getMemoryVT(),
2635 Load->getMemOperand());
2636 // Replace uses of the EXTLOAD with the new ZEXTLOAD.
Hal Finkeld65e4632012-06-20 15:42:48 +00002637 if (Load->getNumValues() == 3) {
2638 // PRE/POST_INC loads have 3 values.
2639 SDValue To[] = { NewLoad.getValue(0), NewLoad.getValue(1),
2640 NewLoad.getValue(2) };
2641 CombineTo(Load, To, 3, true);
2642 } else {
2643 CombineTo(Load, NewLoad.getValue(0), NewLoad.getValue(1));
2644 }
James Molloy6259dcd2012-02-20 12:02:38 +00002645 }
2646
2647 // Fold the AND away, taking care not to fold to the old load node if we
2648 // replaced it.
2649 CombineTo(N, (N0.getNode() == Load) ? NewLoad : N0);
2650
2651 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2652 }
2653 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002654 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
2655 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
2656 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
2657 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00002658
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002659 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002660 LL.getValueType().isInteger()) {
Bill Wendling2627a882009-01-30 20:43:18 +00002661 // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00002662 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002663 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0),
Bill Wendling2627a882009-01-30 20:43:18 +00002664 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002665 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002666 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002667 }
Bill Wendling2627a882009-01-30 20:43:18 +00002668 // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002669 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002670 SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(N0),
Bill Wendling2627a882009-01-30 20:43:18 +00002671 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002672 AddToWorkList(ANDNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002673 return DAG.getSetCC(SDLoc(N), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002674 }
Bill Wendling2627a882009-01-30 20:43:18 +00002675 // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002676 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002677 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0),
Bill Wendling2627a882009-01-30 20:43:18 +00002678 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002679 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002680 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002681 }
2682 }
Jim Grosbach51a02802013-08-13 21:30:58 +00002683 // Simplify (and (setne X, 0), (setne X, -1)) -> (setuge (add X, 1), 2)
2684 if (LL == RL && isa<ConstantSDNode>(LR) && isa<ConstantSDNode>(RR) &&
2685 Op0 == Op1 && LL.getValueType().isInteger() &&
2686 Op0 == ISD::SETNE && ((cast<ConstantSDNode>(LR)->isNullValue() &&
2687 cast<ConstantSDNode>(RR)->isAllOnesValue()) ||
2688 (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
2689 cast<ConstantSDNode>(RR)->isNullValue()))) {
2690 SDValue ADDNode = DAG.getNode(ISD::ADD, SDLoc(N0), LL.getValueType(),
2691 LL, DAG.getConstant(1, LL.getValueType()));
2692 AddToWorkList(ADDNode.getNode());
2693 return DAG.getSetCC(SDLoc(N), VT, ADDNode,
2694 DAG.getConstant(2, LL.getValueType()), ISD::SETUGE);
2695 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002696 // canonicalize equivalent to ll == rl
2697 if (LL == RR && LR == RL) {
2698 Op1 = ISD::getSetCCSwappedOperands(Op1);
2699 std::swap(RL, RR);
2700 }
2701 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002702 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002703 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00002704 if (Result != ISD::SETCC_INVALID &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00002705 (!LegalOperations ||
Owen Anderson39125d92013-02-14 09:07:33 +00002706 (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) &&
2707 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault225ed702013-05-18 00:21:46 +00002708 getSetCCResultType(N0.getSimpleValueType())))))
Andrew Trickac6d9be2013-05-25 02:42:55 +00002709 return DAG.getSetCC(SDLoc(N), N0.getValueType(),
Bill Wendling2627a882009-01-30 20:43:18 +00002710 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002711 }
2712 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002713
Bill Wendling2627a882009-01-30 20:43:18 +00002714 // Simplify: (and (op x...), (op y...)) -> (op (and x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00002715 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002716 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002717 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002718 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002719
Nate Begemande996292006-02-03 22:24:05 +00002720 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
2721 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002722 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002723 SimplifyDemandedBits(SDValue(N, 0)))
2724 return SDValue(N, 0);
Evan Chengd40d03e2010-01-06 19:38:29 +00002725
Nate Begemanded49632005-10-13 03:11:28 +00002726 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002727 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00002728 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002729 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002730 // If we zero all the possible extended bits, then we can turn this into
2731 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002732 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002733 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002734 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002735 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002736 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002737 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT,
Bill Wendling2627a882009-01-30 20:43:18 +00002738 LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002739 LN0->getPointerInfo(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00002740 LN0->isVolatile(), LN0->isNonTemporal(),
2741 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002742 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002743 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002744 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002745 }
2746 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002747 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00002748 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00002749 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002750 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002751 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002752 // If we zero all the possible extended bits, then we can turn this into
2753 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002754 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002755 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002756 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002757 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002758 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002759 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT,
Bill Wendling2627a882009-01-30 20:43:18 +00002760 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002761 LN0->getBasePtr(), LN0->getPointerInfo(),
2762 MemVT,
David Greene1e559442010-02-15 17:00:31 +00002763 LN0->isVolatile(), LN0->isNonTemporal(),
2764 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002765 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002766 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002767 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002768 }
2769 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002770
Chris Lattner35a9f5a2006-02-28 06:49:37 +00002771 // fold (and (load x), 255) -> (zextload x, i8)
2772 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Chengd40d03e2010-01-06 19:38:29 +00002773 // fold (and (any_ext (extload x, i16)), 255) -> (zextload x, i8)
2774 if (N1C && (N0.getOpcode() == ISD::LOAD ||
2775 (N0.getOpcode() == ISD::ANY_EXTEND &&
2776 N0.getOperand(0).getOpcode() == ISD::LOAD))) {
2777 bool HasAnyExt = N0.getOpcode() == ISD::ANY_EXTEND;
2778 LoadSDNode *LN0 = HasAnyExt
2779 ? cast<LoadSDNode>(N0.getOperand(0))
2780 : cast<LoadSDNode>(N0);
Evan Cheng466685d2006-10-09 20:57:25 +00002781 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Tim Northover5bce67a2013-07-02 09:58:53 +00002782 LN0->isUnindexed() && N0.hasOneUse() && SDValue(LN0, 0).hasOneUse()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00002783 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
Evan Chengd40d03e2010-01-06 19:38:29 +00002784 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue())){
2785 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits);
2786 EVT LoadedVT = LN0->getMemoryVT();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002787
Evan Chengd40d03e2010-01-06 19:38:29 +00002788 if (ExtVT == LoadedVT &&
2789 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
Chris Lattneref7634c2010-01-07 21:53:27 +00002790 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002791
2792 SDValue NewLoad =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002793 DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy,
Chris Lattneref7634c2010-01-07 21:53:27 +00002794 LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002795 LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002796 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2797 LN0->getAlignment());
Chris Lattneref7634c2010-01-07 21:53:27 +00002798 AddToWorkList(N);
2799 CombineTo(LN0, NewLoad, NewLoad.getValue(1));
2800 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2801 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002802
Chris Lattneref7634c2010-01-07 21:53:27 +00002803 // Do not change the width of a volatile load.
2804 // Do not generate loads of non-round integer types since these can
2805 // be expensive (and would be wrong if the type is not byte sized).
2806 if (!LN0->isVolatile() && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() &&
2807 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
2808 EVT PtrType = LN0->getOperand(1).getValueType();
Bill Wendling2627a882009-01-30 20:43:18 +00002809
Chris Lattneref7634c2010-01-07 21:53:27 +00002810 unsigned Alignment = LN0->getAlignment();
2811 SDValue NewPtr = LN0->getBasePtr();
2812
2813 // For big endian targets, we need to add an offset to the pointer
2814 // to load the correct bytes. For little endian systems, we merely
2815 // need to read fewer bytes from the same pointer.
2816 if (TLI.isBigEndian()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00002817 unsigned LVTStoreBytes = LoadedVT.getStoreSize();
2818 unsigned EVTStoreBytes = ExtVT.getStoreSize();
2819 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Andrew Trickac6d9be2013-05-25 02:42:55 +00002820 NewPtr = DAG.getNode(ISD::ADD, SDLoc(LN0), PtrType,
Chris Lattneref7634c2010-01-07 21:53:27 +00002821 NewPtr, DAG.getConstant(PtrOff, PtrType));
2822 Alignment = MinAlign(Alignment, PtrOff);
Evan Chengd40d03e2010-01-06 19:38:29 +00002823 }
Chris Lattneref7634c2010-01-07 21:53:27 +00002824
2825 AddToWorkList(NewPtr.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002826
Chris Lattneref7634c2010-01-07 21:53:27 +00002827 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
2828 SDValue Load =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002829 DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy,
Chris Lattneref7634c2010-01-07 21:53:27 +00002830 LN0->getChain(), NewPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002831 LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002832 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2833 Alignment);
Chris Lattneref7634c2010-01-07 21:53:27 +00002834 AddToWorkList(N);
2835 CombineTo(LN0, Load, Load.getValue(1));
2836 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsdc846502007-10-28 12:59:45 +00002837 }
Evan Cheng466685d2006-10-09 20:57:25 +00002838 }
Chris Lattner15045b62006-02-28 06:35:35 +00002839 }
2840 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002841
Evan Chenga9e13ba2012-07-17 18:54:11 +00002842 if (N0.getOpcode() == ISD::ADD && N1.getOpcode() == ISD::SRL &&
2843 VT.getSizeInBits() <= 64) {
2844 if (ConstantSDNode *ADDI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2845 APInt ADDC = ADDI->getAPIntValue();
2846 if (!TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2847 // Look for (and (add x, c1), (lshr y, c2)). If C1 wasn't a legal
2848 // immediate for an add, but it is legal if its top c2 bits are set,
2849 // transform the ADD so the immediate doesn't need to be materialized
2850 // in a register.
2851 if (ConstantSDNode *SRLI = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
2852 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
2853 SRLI->getZExtValue());
2854 if (DAG.MaskedValueIsZero(N0.getOperand(1), Mask)) {
2855 ADDC |= Mask;
2856 if (TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2857 SDValue NewAdd =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002858 DAG.getNode(ISD::ADD, SDLoc(N0), VT,
Evan Chenga9e13ba2012-07-17 18:54:11 +00002859 N0.getOperand(0), DAG.getConstant(ADDC, VT));
2860 CombineTo(N0.getNode(), NewAdd);
2861 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2862 }
2863 }
2864 }
2865 }
2866 }
2867 }
Evan Chenga9e13ba2012-07-17 18:54:11 +00002868
Tim Northover5d8c2e42013-08-27 13:46:45 +00002869 // fold (and (or (srl N, 8), (shl N, 8)), 0xffff) -> (srl (bswap N), const)
2870 if (N1C && N1C->getAPIntValue() == 0xffff && N0.getOpcode() == ISD::OR) {
2871 SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0),
2872 N0.getOperand(1), false);
2873 if (BSwap.getNode())
2874 return BSwap;
2875 }
2876
Evan Chengb3a3d5e2010-04-28 07:10:39 +00002877 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002878}
2879
Evan Cheng9568e5c2011-06-21 06:01:08 +00002880/// MatchBSwapHWord - Match (a >> 8) | (a << 8) as (bswap a) >> 16
2881///
2882SDValue DAGCombiner::MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
2883 bool DemandHighBits) {
2884 if (!LegalOperations)
2885 return SDValue();
2886
2887 EVT VT = N->getValueType(0);
2888 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16)
2889 return SDValue();
2890 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
2891 return SDValue();
2892
2893 // Recognize (and (shl a, 8), 0xff), (and (srl a, 8), 0xff00)
2894 bool LookPassAnd0 = false;
2895 bool LookPassAnd1 = false;
2896 if (N0.getOpcode() == ISD::AND && N0.getOperand(0).getOpcode() == ISD::SRL)
2897 std::swap(N0, N1);
2898 if (N1.getOpcode() == ISD::AND && N1.getOperand(0).getOpcode() == ISD::SHL)
2899 std::swap(N0, N1);
2900 if (N0.getOpcode() == ISD::AND) {
2901 if (!N0.getNode()->hasOneUse())
2902 return SDValue();
2903 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2904 if (!N01C || N01C->getZExtValue() != 0xFF00)
2905 return SDValue();
2906 N0 = N0.getOperand(0);
2907 LookPassAnd0 = true;
2908 }
2909
2910 if (N1.getOpcode() == ISD::AND) {
2911 if (!N1.getNode()->hasOneUse())
2912 return SDValue();
2913 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2914 if (!N11C || N11C->getZExtValue() != 0xFF)
2915 return SDValue();
2916 N1 = N1.getOperand(0);
2917 LookPassAnd1 = true;
2918 }
2919
2920 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
2921 std::swap(N0, N1);
2922 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
2923 return SDValue();
2924 if (!N0.getNode()->hasOneUse() ||
2925 !N1.getNode()->hasOneUse())
2926 return SDValue();
2927
2928 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2929 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2930 if (!N01C || !N11C)
2931 return SDValue();
2932 if (N01C->getZExtValue() != 8 || N11C->getZExtValue() != 8)
2933 return SDValue();
2934
2935 // Look for (shl (and a, 0xff), 8), (srl (and a, 0xff00), 8)
2936 SDValue N00 = N0->getOperand(0);
2937 if (!LookPassAnd0 && N00.getOpcode() == ISD::AND) {
2938 if (!N00.getNode()->hasOneUse())
2939 return SDValue();
2940 ConstantSDNode *N001C = dyn_cast<ConstantSDNode>(N00.getOperand(1));
2941 if (!N001C || N001C->getZExtValue() != 0xFF)
2942 return SDValue();
2943 N00 = N00.getOperand(0);
2944 LookPassAnd0 = true;
2945 }
2946
2947 SDValue N10 = N1->getOperand(0);
2948 if (!LookPassAnd1 && N10.getOpcode() == ISD::AND) {
2949 if (!N10.getNode()->hasOneUse())
2950 return SDValue();
2951 ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N10.getOperand(1));
2952 if (!N101C || N101C->getZExtValue() != 0xFF00)
2953 return SDValue();
2954 N10 = N10.getOperand(0);
2955 LookPassAnd1 = true;
2956 }
2957
2958 if (N00 != N10)
2959 return SDValue();
2960
Tim Northover5d8c2e42013-08-27 13:46:45 +00002961 // Make sure everything beyond the low halfword gets set to zero since the SRL
2962 // 16 will clear the top bits.
Evan Cheng9568e5c2011-06-21 06:01:08 +00002963 unsigned OpSizeInBits = VT.getSizeInBits();
Tim Northover5d8c2e42013-08-27 13:46:45 +00002964 if (DemandHighBits && OpSizeInBits > 16) {
2965 // If the left-shift isn't masked out then the only way this is a bswap is
2966 // if all bits beyond the low 8 are 0. In that case the entire pattern
2967 // reduces to a left shift anyway: leave it for other parts of the combiner.
2968 if (!LookPassAnd0)
2969 return SDValue();
2970
2971 // However, if the right shift isn't masked out then it might be because
2972 // it's not needed. See if we can spot that too.
2973 if (!LookPassAnd1 &&
2974 !DAG.MaskedValueIsZero(
2975 N10, APInt::getHighBitsSet(OpSizeInBits, OpSizeInBits - 16)))
2976 return SDValue();
2977 }
Eric Christopher7332e6e2011-07-14 01:12:15 +00002978
Andrew Trickac6d9be2013-05-25 02:42:55 +00002979 SDValue Res = DAG.getNode(ISD::BSWAP, SDLoc(N), VT, N00);
Evan Cheng9568e5c2011-06-21 06:01:08 +00002980 if (OpSizeInBits > 16)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002981 Res = DAG.getNode(ISD::SRL, SDLoc(N), VT, Res,
Evan Cheng9568e5c2011-06-21 06:01:08 +00002982 DAG.getConstant(OpSizeInBits-16, getShiftAmountTy(VT)));
2983 return Res;
2984}
2985
2986/// isBSwapHWordElement - Return true if the specified node is an element
2987/// that makes up a 32-bit packed halfword byteswap. i.e.
2988/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
Craig Toppera0ec3f92013-07-14 04:42:23 +00002989static bool isBSwapHWordElement(SDValue N, SmallVectorImpl<SDNode *> &Parts) {
Evan Cheng9568e5c2011-06-21 06:01:08 +00002990 if (!N.getNode()->hasOneUse())
2991 return false;
2992
2993 unsigned Opc = N.getOpcode();
2994 if (Opc != ISD::AND && Opc != ISD::SHL && Opc != ISD::SRL)
2995 return false;
2996
2997 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2998 if (!N1C)
2999 return false;
3000
3001 unsigned Num;
3002 switch (N1C->getZExtValue()) {
3003 default:
3004 return false;
3005 case 0xFF: Num = 0; break;
3006 case 0xFF00: Num = 1; break;
3007 case 0xFF0000: Num = 2; break;
3008 case 0xFF000000: Num = 3; break;
3009 }
3010
3011 // Look for (x & 0xff) << 8 as well as ((x << 8) & 0xff00).
3012 SDValue N0 = N.getOperand(0);
3013 if (Opc == ISD::AND) {
3014 if (Num == 0 || Num == 2) {
3015 // (x >> 8) & 0xff
3016 // (x >> 8) & 0xff0000
3017 if (N0.getOpcode() != ISD::SRL)
3018 return false;
3019 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3020 if (!C || C->getZExtValue() != 8)
3021 return false;
3022 } else {
3023 // (x << 8) & 0xff00
3024 // (x << 8) & 0xff000000
3025 if (N0.getOpcode() != ISD::SHL)
3026 return false;
3027 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3028 if (!C || C->getZExtValue() != 8)
3029 return false;
3030 }
3031 } else if (Opc == ISD::SHL) {
3032 // (x & 0xff) << 8
3033 // (x & 0xff0000) << 8
3034 if (Num != 0 && Num != 2)
3035 return false;
3036 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
3037 if (!C || C->getZExtValue() != 8)
3038 return false;
3039 } else { // Opc == ISD::SRL
3040 // (x & 0xff00) >> 8
3041 // (x & 0xff000000) >> 8
3042 if (Num != 1 && Num != 3)
3043 return false;
3044 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
3045 if (!C || C->getZExtValue() != 8)
3046 return false;
3047 }
3048
3049 if (Parts[Num])
3050 return false;
3051
3052 Parts[Num] = N0.getOperand(0).getNode();
3053 return true;
3054}
3055
3056/// MatchBSwapHWord - Match a 32-bit packed halfword bswap. That is
3057/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
3058/// => (rotl (bswap x), 16)
3059SDValue DAGCombiner::MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1) {
3060 if (!LegalOperations)
3061 return SDValue();
3062
3063 EVT VT = N->getValueType(0);
3064 if (VT != MVT::i32)
3065 return SDValue();
3066 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
3067 return SDValue();
3068
3069 SmallVector<SDNode*,4> Parts(4, (SDNode*)0);
3070 // Look for either
3071 // (or (or (and), (and)), (or (and), (and)))
3072 // (or (or (or (and), (and)), (and)), (and))
3073 if (N0.getOpcode() != ISD::OR)
3074 return SDValue();
3075 SDValue N00 = N0.getOperand(0);
3076 SDValue N01 = N0.getOperand(1);
3077
Evan Cheng9a65a012012-12-13 01:34:32 +00003078 if (N1.getOpcode() == ISD::OR &&
3079 N00.getNumOperands() == 2 && N01.getNumOperands() == 2) {
Evan Cheng9568e5c2011-06-21 06:01:08 +00003080 // (or (or (and), (and)), (or (and), (and)))
3081 SDValue N000 = N00.getOperand(0);
3082 if (!isBSwapHWordElement(N000, Parts))
3083 return SDValue();
3084
3085 SDValue N001 = N00.getOperand(1);
3086 if (!isBSwapHWordElement(N001, Parts))
3087 return SDValue();
3088 SDValue N010 = N01.getOperand(0);
3089 if (!isBSwapHWordElement(N010, Parts))
3090 return SDValue();
3091 SDValue N011 = N01.getOperand(1);
3092 if (!isBSwapHWordElement(N011, Parts))
3093 return SDValue();
3094 } else {
3095 // (or (or (or (and), (and)), (and)), (and))
3096 if (!isBSwapHWordElement(N1, Parts))
3097 return SDValue();
3098 if (!isBSwapHWordElement(N01, Parts))
3099 return SDValue();
3100 if (N00.getOpcode() != ISD::OR)
3101 return SDValue();
3102 SDValue N000 = N00.getOperand(0);
3103 if (!isBSwapHWordElement(N000, Parts))
3104 return SDValue();
3105 SDValue N001 = N00.getOperand(1);
3106 if (!isBSwapHWordElement(N001, Parts))
3107 return SDValue();
3108 }
3109
3110 // Make sure the parts are all coming from the same node.
3111 if (Parts[0] != Parts[1] || Parts[0] != Parts[2] || Parts[0] != Parts[3])
3112 return SDValue();
3113
Andrew Trickac6d9be2013-05-25 02:42:55 +00003114 SDValue BSwap = DAG.getNode(ISD::BSWAP, SDLoc(N), VT,
Evan Cheng9568e5c2011-06-21 06:01:08 +00003115 SDValue(Parts[0],0));
3116
3117 // Result of the bswap should be rotated by 16. If it's not legal, than
3118 // do (x << 16) | (x >> 16).
3119 SDValue ShAmt = DAG.getConstant(16, getShiftAmountTy(VT));
3120 if (TLI.isOperationLegalOrCustom(ISD::ROTL, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003121 return DAG.getNode(ISD::ROTL, SDLoc(N), VT, BSwap, ShAmt);
Craig Topper0eb5dad2012-09-29 07:18:53 +00003122 if (TLI.isOperationLegalOrCustom(ISD::ROTR, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003123 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, BSwap, ShAmt);
3124 return DAG.getNode(ISD::OR, SDLoc(N), VT,
3125 DAG.getNode(ISD::SHL, SDLoc(N), VT, BSwap, ShAmt),
3126 DAG.getNode(ISD::SRL, SDLoc(N), VT, BSwap, ShAmt));
Evan Cheng9568e5c2011-06-21 06:01:08 +00003127}
3128
Dan Gohman475871a2008-07-27 21:46:04 +00003129SDValue DAGCombiner::visitOR(SDNode *N) {
3130 SDValue N0 = N->getOperand(0);
3131 SDValue N1 = N->getOperand(1);
3132 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00003133 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3134 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003135 EVT VT = N1.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003136
Dan Gohman7f321562007-06-25 16:23:39 +00003137 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003138 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003139 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003140 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00003141
3142 // fold (or x, 0) -> x, vector edition
3143 if (ISD::isBuildVectorAllZeros(N0.getNode()))
3144 return N1;
3145 if (ISD::isBuildVectorAllZeros(N1.getNode()))
3146 return N0;
3147
3148 // fold (or x, -1) -> -1, vector edition
3149 if (ISD::isBuildVectorAllOnes(N0.getNode()))
3150 return N0;
3151 if (ISD::isBuildVectorAllOnes(N1.getNode()))
3152 return N1;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003153 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003154
Dan Gohman613e0d82007-07-03 14:03:57 +00003155 // fold (or x, undef) -> -1
Bob Wilson86749492010-06-28 23:40:25 +00003156 if (!LegalOperations &&
3157 (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)) {
Nate Begeman93e0ed32009-12-03 07:11:29 +00003158 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
3159 return DAG.getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
3160 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003161 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003162 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003163 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00003164 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003165 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003166 return DAG.getNode(ISD::OR, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003167 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003168 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003169 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003170 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00003171 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003172 return N1;
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003173 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003174 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003175 return N1;
Evan Cheng9568e5c2011-06-21 06:01:08 +00003176
3177 // Recognize halfword bswaps as (bswap + rotl 16) or (bswap + shl 16)
3178 SDValue BSwap = MatchBSwapHWord(N, N0, N1);
3179 if (BSwap.getNode() != 0)
3180 return BSwap;
3181 BSwap = MatchBSwapHWordLow(N, N0, N1);
3182 if (BSwap.getNode() != 0)
3183 return BSwap;
3184
Nate Begemancd4d58c2006-02-03 06:46:56 +00003185 // reassociate or
Andrew Trickac6d9be2013-05-25 02:42:55 +00003186 SDValue ROR = ReassociateOps(ISD::OR, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003187 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00003188 return ROR;
3189 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003190 // iff (c1 & c2) == 0.
Gabor Greifba36cb52008-08-28 21:40:38 +00003191 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00003192 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00003193 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
Bill Wendling32f9eb22010-03-03 01:58:01 +00003194 if ((C1->getAPIntValue() & N1C->getAPIntValue()) != 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003195 return DAG.getNode(ISD::AND, SDLoc(N), VT,
3196 DAG.getNode(ISD::OR, SDLoc(N0), VT,
Bill Wendling7d9f2b92010-03-03 00:35:56 +00003197 N0.getOperand(0), N1),
3198 DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1));
Nate Begeman223df222005-09-08 20:18:10 +00003199 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003200 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
3201 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
3202 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
3203 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00003204
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003205 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003206 LL.getValueType().isInteger()) {
Bill Wendling09025642009-01-30 20:59:34 +00003207 // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0)
3208 // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00003209 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003210 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003211 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(LR),
Bill Wendling09025642009-01-30 20:59:34 +00003212 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003213 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003214 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003215 }
Bill Wendling09025642009-01-30 20:59:34 +00003216 // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1)
3217 // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1)
Scott Michelfdc40a02009-02-17 22:15:04 +00003218 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003219 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003220 SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(LR),
Bill Wendling09025642009-01-30 20:59:34 +00003221 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003222 AddToWorkList(ANDNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003223 return DAG.getSetCC(SDLoc(N), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003224 }
3225 }
3226 // canonicalize equivalent to ll == rl
3227 if (LL == RR && LR == RL) {
3228 Op1 = ISD::getSetCCSwappedOperands(Op1);
3229 std::swap(RL, RR);
3230 }
3231 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003232 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003233 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00003234 if (Result != ISD::SETCC_INVALID &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00003235 (!LegalOperations ||
Owen Anderson39125d92013-02-14 09:07:33 +00003236 (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) &&
3237 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault225ed702013-05-18 00:21:46 +00003238 getSetCCResultType(N0.getValueType())))))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003239 return DAG.getSetCC(SDLoc(N), N0.getValueType(),
Bill Wendling09025642009-01-30 20:59:34 +00003240 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003241 }
3242 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003243
Bill Wendling09025642009-01-30 20:59:34 +00003244 // Simplify: (or (op x...), (op y...)) -> (op (or x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00003245 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003246 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003247 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003248 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003249
Bill Wendling09025642009-01-30 20:59:34 +00003250 // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible.
Chris Lattner1ec72732006-09-14 21:11:37 +00003251 if (N0.getOpcode() == ISD::AND &&
3252 N1.getOpcode() == ISD::AND &&
3253 N0.getOperand(1).getOpcode() == ISD::Constant &&
3254 N1.getOperand(1).getOpcode() == ISD::Constant &&
3255 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00003256 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00003257 // We can only do this xform if we know that bits from X that are set in C2
3258 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003259 const APInt &LHSMask =
3260 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
3261 const APInt &RHSMask =
3262 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003263
Dan Gohmanea859be2007-06-22 14:59:07 +00003264 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
3265 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003266 SDValue X = DAG.getNode(ISD::OR, SDLoc(N0), VT,
Bill Wendling09025642009-01-30 20:59:34 +00003267 N0.getOperand(0), N1.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003268 return DAG.getNode(ISD::AND, SDLoc(N), VT, X,
Bill Wendling09025642009-01-30 20:59:34 +00003269 DAG.getConstant(LHSMask | RHSMask, VT));
Chris Lattner1ec72732006-09-14 21:11:37 +00003270 }
3271 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003272
Chris Lattner516b9622006-09-14 20:50:57 +00003273 // See if this is some rotate idiom.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003274 if (SDNode *Rot = MatchRotate(N0, N1, SDLoc(N)))
Dan Gohman475871a2008-07-27 21:46:04 +00003275 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00003276
Dan Gohman4e39e9d2010-06-24 14:30:44 +00003277 // Simplify the operands using demanded-bits information.
3278 if (!VT.isVector() &&
3279 SimplifyDemandedBits(SDValue(N, 0)))
3280 return SDValue(N, 0);
3281
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003282 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003283}
3284
Chris Lattner516b9622006-09-14 20:50:57 +00003285/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00003286static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00003287 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00003288 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00003289 Mask = Op.getOperand(1);
3290 Op = Op.getOperand(0);
3291 } else {
3292 return false;
3293 }
3294 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003295
Chris Lattner516b9622006-09-14 20:50:57 +00003296 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
3297 Shift = Op;
3298 return true;
3299 }
Bill Wendling09025642009-01-30 20:59:34 +00003300
Scott Michelfdc40a02009-02-17 22:15:04 +00003301 return false;
Chris Lattner516b9622006-09-14 20:50:57 +00003302}
3303
Chris Lattner516b9622006-09-14 20:50:57 +00003304// MatchRotate - Handle an 'or' of two operands. If this is one of the many
3305// idioms for rotate, and if the target supports rotation instructions, generate
3306// a rot[lr].
Andrew Trickac6d9be2013-05-25 02:42:55 +00003307SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, SDLoc DL) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003308 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Owen Andersone50ed302009-08-10 22:56:29 +00003309 EVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00003310 if (!TLI.isTypeLegal(VT)) return 0;
3311
3312 // The target must have at least one rotate flavor.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003313 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
3314 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Chris Lattner516b9622006-09-14 20:50:57 +00003315 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003316
Chris Lattner516b9622006-09-14 20:50:57 +00003317 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00003318 SDValue LHSShift; // The shift.
3319 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00003320 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
3321 return 0; // Not part of a rotate.
3322
Dan Gohman475871a2008-07-27 21:46:04 +00003323 SDValue RHSShift; // The shift.
3324 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00003325 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
3326 return 0; // Not part of a rotate.
Scott Michelfdc40a02009-02-17 22:15:04 +00003327
Chris Lattner516b9622006-09-14 20:50:57 +00003328 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
3329 return 0; // Not shifting the same value.
3330
3331 if (LHSShift.getOpcode() == RHSShift.getOpcode())
3332 return 0; // Shifts must disagree.
Scott Michelfdc40a02009-02-17 22:15:04 +00003333
Chris Lattner516b9622006-09-14 20:50:57 +00003334 // Canonicalize shl to left side in a shl/srl pair.
3335 if (RHSShift.getOpcode() == ISD::SHL) {
3336 std::swap(LHS, RHS);
3337 std::swap(LHSShift, RHSShift);
3338 std::swap(LHSMask , RHSMask );
3339 }
3340
Duncan Sands83ec4b62008-06-06 12:08:01 +00003341 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00003342 SDValue LHSShiftArg = LHSShift.getOperand(0);
3343 SDValue LHSShiftAmt = LHSShift.getOperand(1);
Kai Nackeceb3b462013-09-19 23:00:28 +00003344 SDValue RHSShiftArg = RHSShift.getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00003345 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00003346
3347 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
3348 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00003349 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
3350 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003351 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
3352 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00003353 if ((LShVal + RShVal) != OpSizeInBits)
3354 return 0;
3355
Craig Topper32b73432012-09-29 06:54:22 +00003356 SDValue Rot = DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
3357 LHSShiftArg, HasROTL ? LHSShiftAmt : RHSShiftAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00003358
Chris Lattner516b9622006-09-14 20:50:57 +00003359 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00003360 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003361 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00003362
Gabor Greifba36cb52008-08-28 21:40:38 +00003363 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003364 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
3365 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003366 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003367 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003368 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
3369 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003370 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003371
Bill Wendling317bd702009-01-30 21:14:50 +00003372 Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT));
Chris Lattner516b9622006-09-14 20:50:57 +00003373 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003374
Gabor Greifba36cb52008-08-28 21:40:38 +00003375 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00003376 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003377
Chris Lattner516b9622006-09-14 20:50:57 +00003378 // If there is a mask here, and we have a variable shift, we can't be sure
3379 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00003380 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00003381 return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00003382
Chris Lattner516b9622006-09-14 20:50:57 +00003383 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
3384 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00003385 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
3386 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003387 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00003388 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Stephen Linb4940152013-07-09 00:44:49 +00003389 if (SUBC->getAPIntValue() == OpSizeInBits)
Craig Topper32b73432012-09-29 06:54:22 +00003390 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT, LHSShiftArg,
3391 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00003392 }
3393 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003394
Chris Lattner516b9622006-09-14 20:50:57 +00003395 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
3396 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00003397 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
Stephen Linb4940152013-07-09 00:44:49 +00003398 RHSShiftAmt == LHSShiftAmt.getOperand(1))
Scott Michelfdc40a02009-02-17 22:15:04 +00003399 if (ConstantSDNode *SUBC =
Stephen Linb4940152013-07-09 00:44:49 +00003400 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0)))
3401 if (SUBC->getAPIntValue() == OpSizeInBits)
Craig Topper32b73432012-09-29 06:54:22 +00003402 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT, LHSShiftArg,
3403 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00003404
Dan Gohman74feef22008-10-17 01:23:35 +00003405 // Look for sign/zext/any-extended or truncate cases:
Craig Topper0eb5dad2012-09-29 07:18:53 +00003406 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND ||
3407 LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND ||
3408 LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND ||
3409 LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
3410 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND ||
3411 RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND ||
3412 RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND ||
3413 RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003414 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
3415 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +00003416 if (RExtOp0.getOpcode() == ISD::SUB &&
3417 RExtOp0.getOperand(1) == LExtOp0) {
3418 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003419 // (rotl x, y)
Scott Michelc9dc1142007-04-02 21:36:32 +00003420 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003421 // (rotr x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00003422 if (ConstantSDNode *SUBC =
David Blaikiec2286722013-09-20 00:33:11 +00003423 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Kai Nackeceb3b462013-09-19 23:00:28 +00003424 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00003425 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
3426 LHSShiftArg,
Gabor Greif12632d22008-08-30 19:29:20 +00003427 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Kai Nackeceb3b462013-09-19 23:00:28 +00003428 } else if (LHSShiftArg.getOpcode() == ISD::ZERO_EXTEND ||
3429 LHSShiftArg.getOpcode() == ISD::ANY_EXTEND) {
3430 // fold (or (shl (*ext x), (*ext y)),
3431 // (srl (*ext x), (*ext (sub 32, y)))) ->
3432 // (*ext (rotl x, y))
3433 // fold (or (shl (*ext x), (*ext y)),
3434 // (srl (*ext x), (*ext (sub 32, y)))) ->
3435 // (*ext (rotr x, (sub 32, y)))
3436 SDValue LArgExtOp0 = LHSShiftArg.getOperand(0);
3437 EVT LArgVT = LArgExtOp0.getValueType();
3438 if (LArgVT.getSizeInBits() == SUBC->getAPIntValue()) {
3439 SDValue V = DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, LArgVT,
3440 LArgExtOp0,
3441 HasROTL ? LHSShiftAmt : RHSShiftAmt);
3442 return DAG.getNode(LHSShiftArg.getOpcode(), DL, VT, V).getNode();
3443 }
3444 }
David Blaikiec2286722013-09-20 00:33:11 +00003445 }
Scott Michelc9dc1142007-04-02 21:36:32 +00003446 } else if (LExtOp0.getOpcode() == ISD::SUB &&
3447 RExtOp0 == LExtOp0.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003448 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003449 // (rotr x, y)
Bill Wendling353dea22008-08-31 01:04:56 +00003450 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003451 // (rotl x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00003452 if (ConstantSDNode *SUBC =
David Blaikiec2286722013-09-20 00:33:11 +00003453 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Kai Nackeceb3b462013-09-19 23:00:28 +00003454 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00003455 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT,
3456 LHSShiftArg,
Bill Wendling353dea22008-08-31 01:04:56 +00003457 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Kai Nackeceb3b462013-09-19 23:00:28 +00003458 } else if (RHSShiftArg.getOpcode() == ISD::ZERO_EXTEND ||
3459 RHSShiftArg.getOpcode() == ISD::ANY_EXTEND) {
3460 // fold (or (shl (*ext x), (*ext (sub 32, y))),
3461 // (srl (*ext x), (*ext y))) ->
3462 // (*ext (rotl x, y))
3463 // fold (or (shl (*ext x), (*ext (sub 32, y))),
3464 // (srl (*ext x), (*ext y))) ->
3465 // (*ext (rotr x, (sub 32, y)))
3466 SDValue RArgExtOp0 = RHSShiftArg.getOperand(0);
3467 EVT RArgVT = RArgExtOp0.getValueType();
3468 if (RArgVT.getSizeInBits() == SUBC->getAPIntValue()) {
3469 SDValue V = DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, RArgVT,
3470 RArgExtOp0,
3471 HasROTR ? RHSShiftAmt : LHSShiftAmt);
3472 return DAG.getNode(RHSShiftArg.getOpcode(), DL, VT, V).getNode();
3473 }
3474 }
David Blaikiec2286722013-09-20 00:33:11 +00003475 }
Chris Lattner516b9622006-09-14 20:50:57 +00003476 }
3477 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003478
Chris Lattner516b9622006-09-14 20:50:57 +00003479 return 0;
3480}
3481
Dan Gohman475871a2008-07-27 21:46:04 +00003482SDValue DAGCombiner::visitXOR(SDNode *N) {
3483 SDValue N0 = N->getOperand(0);
3484 SDValue N1 = N->getOperand(1);
3485 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00003486 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3487 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003488 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003489
Dan Gohman7f321562007-06-25 16:23:39 +00003490 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003491 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003492 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003493 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00003494
3495 // fold (xor x, 0) -> x, vector edition
3496 if (ISD::isBuildVectorAllZeros(N0.getNode()))
3497 return N1;
3498 if (ISD::isBuildVectorAllZeros(N1.getNode()))
3499 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003500 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003501
Evan Cheng26471c42008-03-25 20:08:07 +00003502 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
3503 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
3504 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00003505 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00003506 if (N0.getOpcode() == ISD::UNDEF)
3507 return N0;
3508 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00003509 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003510 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003511 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003512 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00003513 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003514 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003515 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003516 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003517 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003518 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00003519 // reassociate xor
Andrew Trickac6d9be2013-05-25 02:42:55 +00003520 SDValue RXOR = ReassociateOps(ISD::XOR, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003521 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00003522 return RXOR;
Bill Wendlingae89bb12008-11-11 08:25:46 +00003523
Nate Begeman1d4d4142005-09-01 00:19:25 +00003524 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00003525 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003526 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00003527 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
3528 isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003529
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00003530 if (!LegalOperations ||
3531 TLI.isCondCodeLegal(NotCC, LHS.getSimpleValueType())) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00003532 switch (N0.getOpcode()) {
3533 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003534 llvm_unreachable("Unhandled SetCC Equivalent!");
Bill Wendlingae89bb12008-11-11 08:25:46 +00003535 case ISD::SETCC:
Andrew Trickac6d9be2013-05-25 02:42:55 +00003536 return DAG.getSetCC(SDLoc(N), VT, LHS, RHS, NotCC);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003537 case ISD::SELECT_CC:
Andrew Trickac6d9be2013-05-25 02:42:55 +00003538 return DAG.getSelectCC(SDLoc(N), LHS, RHS, N0.getOperand(2),
Bill Wendlingae89bb12008-11-11 08:25:46 +00003539 N0.getOperand(3), NotCC);
3540 }
3541 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003542 }
Bill Wendlingae89bb12008-11-11 08:25:46 +00003543
Chris Lattner61c5ff42007-09-10 21:39:07 +00003544 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00003545 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00003546 N0.getNode()->hasOneUse() &&
3547 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00003548 SDValue V = N0.getOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003549 V = DAG.getNode(ISD::XOR, SDLoc(N0), V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00003550 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003551 AddToWorkList(V.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003552 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, V);
Chris Lattner61c5ff42007-09-10 21:39:07 +00003553 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003554
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003555 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc
Owen Anderson825b72b2009-08-11 20:47:22 +00003556 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00003557 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003558 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003559 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
3560 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Andrew Trickac6d9be2013-05-25 02:42:55 +00003561 LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS
3562 RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003563 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003564 return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003565 }
3566 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003567 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants
Scott Michelfdc40a02009-02-17 22:15:04 +00003568 if (N1C && N1C->isAllOnesValue() &&
Nate Begeman99801192005-09-07 23:25:52 +00003569 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003570 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003571 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
3572 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Andrew Trickac6d9be2013-05-25 02:42:55 +00003573 LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS
3574 RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003575 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003576 return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003577 }
3578 }
David Majnemer363160a2013-05-08 06:44:42 +00003579 // fold (xor (and x, y), y) -> (and (not x), y)
3580 if (N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
3581 N0->getOperand(1) == N1) {
3582 SDValue X = N0->getOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003583 SDValue NotX = DAG.getNOT(SDLoc(X), X, VT);
David Majnemer363160a2013-05-08 06:44:42 +00003584 AddToWorkList(NotX.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003585 return DAG.getNode(ISD::AND, SDLoc(N), VT, NotX, N1);
David Majnemer363160a2013-05-08 06:44:42 +00003586 }
Bill Wendling317bd702009-01-30 21:14:50 +00003587 // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2))
Nate Begeman223df222005-09-08 20:18:10 +00003588 if (N1C && N0.getOpcode() == ISD::XOR) {
3589 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
3590 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3591 if (N00C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003592 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N0.getOperand(1),
Bill Wendling317bd702009-01-30 21:14:50 +00003593 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003594 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003595 if (N01C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003596 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N0.getOperand(0),
Bill Wendling317bd702009-01-30 21:14:50 +00003597 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003598 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003599 }
3600 // fold (xor x, x) -> 0
Eric Christopher7bccf6a2011-02-16 04:50:12 +00003601 if (N0 == N1)
Hal Finkelbd6f1f62013-07-09 17:02:45 +00003602 return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations, LegalTypes);
Scott Michelfdc40a02009-02-17 22:15:04 +00003603
Chris Lattner35e5c142006-05-05 05:51:50 +00003604 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
3605 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003606 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003607 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003608 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003609
Chris Lattner3e104b12006-04-08 04:15:24 +00003610 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003611 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00003612 SimplifyDemandedBits(SDValue(N, 0)))
3613 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003614
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003615 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003616}
3617
Chris Lattnere70da202007-12-06 07:33:36 +00003618/// visitShiftByConstant - Handle transforms common to the three shifts, when
3619/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00003620SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003621 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00003622 if (!LHS->hasOneUse()) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003623
Chris Lattnere70da202007-12-06 07:33:36 +00003624 // We want to pull some binops through shifts, so that we have (and (shift))
3625 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
3626 // thing happens with address calculations, so it's important to canonicalize
3627 // it.
3628 bool HighBitSet = false; // Can we transform this if the high bit is set?
Scott Michelfdc40a02009-02-17 22:15:04 +00003629
Chris Lattnere70da202007-12-06 07:33:36 +00003630 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003631 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003632 case ISD::OR:
3633 case ISD::XOR:
3634 HighBitSet = false; // We can only transform sra if the high bit is clear.
3635 break;
3636 case ISD::AND:
3637 HighBitSet = true; // We can only transform sra if the high bit is set.
3638 break;
3639 case ISD::ADD:
Scott Michelfdc40a02009-02-17 22:15:04 +00003640 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00003641 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00003642 HighBitSet = false; // We can only transform sra if the high bit is clear.
3643 break;
3644 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003645
Chris Lattnere70da202007-12-06 07:33:36 +00003646 // We require the RHS of the binop to be a constant as well.
3647 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003648 if (!BinOpCst) return SDValue();
Bill Wendling88103372009-01-30 21:37:17 +00003649
3650 // FIXME: disable this unless the input to the binop is a shift by a constant.
3651 // If it is not a shift, it pessimizes some common cases like:
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003652 //
Bill Wendling88103372009-01-30 21:37:17 +00003653 // void foo(int *X, int i) { X[i & 1235] = 1; }
3654 // int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00003655 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00003656 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003657 BinOpLHSVal->getOpcode() != ISD::SRA &&
3658 BinOpLHSVal->getOpcode() != ISD::SRL) ||
3659 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00003660 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003661
Owen Andersone50ed302009-08-10 22:56:29 +00003662 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003663
Bill Wendling88103372009-01-30 21:37:17 +00003664 // If this is a signed shift right, and the high bit is modified by the
3665 // logical operation, do not perform the transformation. The highBitSet
3666 // boolean indicates the value of the high bit of the constant which would
3667 // cause it to be modified for this operation.
Chris Lattnere70da202007-12-06 07:33:36 +00003668 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00003669 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
3670 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00003671 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003672 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003673
Chris Lattnere70da202007-12-06 07:33:36 +00003674 // Fold the constants, shifting the binop RHS by the shift amount.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003675 SDValue NewRHS = DAG.getNode(N->getOpcode(), SDLoc(LHS->getOperand(1)),
Bill Wendling88103372009-01-30 21:37:17 +00003676 N->getValueType(0),
3677 LHS->getOperand(1), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003678
3679 // Create the new shift.
Eric Christopher503a64d2010-12-09 04:48:06 +00003680 SDValue NewShift = DAG.getNode(N->getOpcode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00003681 SDLoc(LHS->getOperand(0)),
Bill Wendling88103372009-01-30 21:37:17 +00003682 VT, LHS->getOperand(0), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003683
3684 // Create the new binop.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003685 return DAG.getNode(LHS->getOpcode(), SDLoc(N), VT, NewShift, NewRHS);
Chris Lattnere70da202007-12-06 07:33:36 +00003686}
3687
Dan Gohman475871a2008-07-27 21:46:04 +00003688SDValue DAGCombiner::visitSHL(SDNode *N) {
3689 SDValue N0 = N->getOperand(0);
3690 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003691 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3692 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003693 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003694 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003695
Nate Begeman1d4d4142005-09-01 00:19:25 +00003696 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003697 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003698 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003699 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003700 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003701 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003702 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003703 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003704 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003705 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003706 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003707 return N0;
Chad Rosier92bcd962011-06-14 22:29:10 +00003708 // fold (shl undef, x) -> 0
3709 if (N0.getOpcode() == ISD::UNDEF)
3710 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003711 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00003712 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman87862e72009-12-11 21:31:27 +00003713 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003714 return DAG.getConstant(0, VT);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003715 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003716 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003717 N1.getOperand(0).getOpcode() == ISD::AND &&
3718 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003719 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003720 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003721 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003722 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003723 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003724 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003725 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
3726 DAG.getNode(ISD::AND, SDLoc(N), TruncVT,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003727 DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003728 SDLoc(N),
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003729 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003730 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003731 }
3732 }
3733
Dan Gohman475871a2008-07-27 21:46:04 +00003734 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3735 return SDValue(N, 0);
Bill Wendling88103372009-01-30 21:37:17 +00003736
3737 // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00003738 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003739 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003740 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3741 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003742 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003743 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003744 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00003745 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003746 }
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003747
3748 // fold (shl (ext (shl x, c1)), c2) -> (ext (shl x, (add c1, c2)))
3749 // For this to be valid, the second form must not preserve any of the bits
3750 // that are shifted out by the inner shift in the first form. This means
3751 // the outer shift size must be >= the number of bits added by the ext.
3752 // As a corollary, we don't care what kind of ext it is.
3753 if (N1C && (N0.getOpcode() == ISD::ZERO_EXTEND ||
3754 N0.getOpcode() == ISD::ANY_EXTEND ||
3755 N0.getOpcode() == ISD::SIGN_EXTEND) &&
3756 N0.getOperand(0).getOpcode() == ISD::SHL &&
3757 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00003758 uint64_t c1 =
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003759 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3760 uint64_t c2 = N1C->getZExtValue();
3761 EVT InnerShiftVT = N0.getOperand(0).getValueType();
3762 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
3763 if (c2 >= OpSizeInBits - InnerShiftSize) {
3764 if (c1 + c2 >= OpSizeInBits)
3765 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003766 return DAG.getNode(ISD::SHL, SDLoc(N0), VT,
3767 DAG.getNode(N0.getOpcode(), SDLoc(N0), VT,
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003768 N0.getOperand(0)->getOperand(0)),
3769 DAG.getConstant(c1 + c2, N1.getValueType()));
3770 }
3771 }
3772
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003773 // fold (shl (srl x, c1), c2) -> (and (shl x, (sub c2, c1), MASK) or
3774 // (and (srl x, (sub c1, c2), MASK)
Chandler Carruth62dfc512012-01-05 11:05:55 +00003775 // Only fold this if the inner shift has no other uses -- if it does, folding
3776 // this will increase the total number of instructions.
3777 if (N1C && N0.getOpcode() == ISD::SRL && N0.hasOneUse() &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003778 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003779 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
Evan Chengd101a722009-07-21 05:40:15 +00003780 if (c1 < VT.getSizeInBits()) {
3781 uint64_t c2 = N1C->getZExtValue();
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003782 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
3783 VT.getSizeInBits() - c1);
3784 SDValue Shift;
3785 if (c2 > c1) {
3786 Mask = Mask.shl(c2-c1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003787 Shift = DAG.getNode(ISD::SHL, SDLoc(N), VT, N0.getOperand(0),
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003788 DAG.getConstant(c2-c1, N1.getValueType()));
3789 } else {
3790 Mask = Mask.lshr(c1-c2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003791 Shift = DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0),
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003792 DAG.getConstant(c1-c2, N1.getValueType()));
3793 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00003794 return DAG.getNode(ISD::AND, SDLoc(N0), VT, Shift,
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003795 DAG.getConstant(Mask, VT));
Evan Chengd101a722009-07-21 05:40:15 +00003796 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003797 }
Bill Wendling88103372009-01-30 21:37:17 +00003798 // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1))
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003799 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) {
3800 SDValue HiBitsMask =
3801 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
3802 VT.getSizeInBits() -
3803 N1C->getZExtValue()),
3804 VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003805 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0.getOperand(0),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003806 HiBitsMask);
3807 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003808
Evan Chenge5b51ac2010-04-17 06:13:15 +00003809 if (N1C) {
3810 SDValue NewSHL = visitShiftByConstant(N, N1C->getZExtValue());
3811 if (NewSHL.getNode())
3812 return NewSHL;
3813 }
3814
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003815 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003816}
3817
Dan Gohman475871a2008-07-27 21:46:04 +00003818SDValue DAGCombiner::visitSRA(SDNode *N) {
3819 SDValue N0 = N->getOperand(0);
3820 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003821 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3822 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003823 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003824 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003825
Bill Wendling88103372009-01-30 21:37:17 +00003826 // fold (sra c1, c2) -> (sra c1, c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00003827 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003828 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003829 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003830 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003831 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003832 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00003833 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003834 return N0;
Bill Wendling88103372009-01-30 21:37:17 +00003835 // fold (sra x, (setge c, size(x))) -> undef
Dan Gohman87862e72009-12-11 21:31:27 +00003836 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003837 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003838 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003839 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003840 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00003841 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
3842 // sext_inreg.
3843 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohman87862e72009-12-11 21:31:27 +00003844 unsigned LowBits = OpSizeInBits - (unsigned)N1C->getZExtValue();
Dan Gohmand1996362010-01-09 02:13:55 +00003845 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), LowBits);
3846 if (VT.isVector())
3847 ExtVT = EVT::getVectorVT(*DAG.getContext(),
3848 ExtVT, VT.getVectorNumElements());
3849 if ((!LegalOperations ||
3850 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, ExtVT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003851 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Dan Gohmand1996362010-01-09 02:13:55 +00003852 N0.getOperand(0), DAG.getValueType(ExtVT));
Nate Begemanfb7217b2006-02-17 19:54:08 +00003853 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003854
Bill Wendling88103372009-01-30 21:37:17 +00003855 // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2))
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003856 if (N1C && N0.getOpcode() == ISD::SRA) {
3857 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003858 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Dan Gohman87862e72009-12-11 21:31:27 +00003859 if (Sum >= OpSizeInBits) Sum = OpSizeInBits-1;
Andrew Trickac6d9be2013-05-25 02:42:55 +00003860 return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0.getOperand(0),
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003861 DAG.getConstant(Sum, N1C->getValueType(0)));
3862 }
3863 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00003864
Bill Wendling88103372009-01-30 21:37:17 +00003865 // fold (sra (shl X, m), (sub result_size, n))
3866 // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for
Scott Michelfdc40a02009-02-17 22:15:04 +00003867 // result_size - n != m.
3868 // If truncate is free for the target sext(shl) is likely to result in better
Christopher Lambb9b04282008-03-20 04:31:39 +00003869 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00003870 if (N0.getOpcode() == ISD::SHL) {
3871 // Get the two constanst of the shifts, CN0 = m, CN = n.
3872 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3873 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003874 // Determine what the truncate's result bitsize and type would be.
Owen Andersone50ed302009-08-10 22:56:29 +00003875 EVT TruncVT =
Eric Christopher503a64d2010-12-09 04:48:06 +00003876 EVT::getIntegerVT(*DAG.getContext(),
3877 OpSizeInBits - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00003878 // Determine the residual right-shift amount.
Torok Edwin6bb49582009-05-23 17:29:48 +00003879 signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003880
Scott Michelfdc40a02009-02-17 22:15:04 +00003881 // If the shift is not a no-op (in which case this should be just a sign
3882 // extend already), the truncated to type is legal, sign_extend is legal
Dan Gohmanf451cb82010-02-10 16:03:48 +00003883 // on that type, and the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00003884 // perform the transform.
Torok Edwin6bb49582009-05-23 17:29:48 +00003885 if ((ShiftAmt > 0) &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003886 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
3887 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00003888 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003889
Owen Anderson95771af2011-02-25 21:41:48 +00003890 SDValue Amt = DAG.getConstant(ShiftAmt,
3891 getShiftAmountTy(N0.getOperand(0).getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003892 SDValue Shift = DAG.getNode(ISD::SRL, SDLoc(N0), VT,
Bill Wendling88103372009-01-30 21:37:17 +00003893 N0.getOperand(0), Amt);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003894 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), TruncVT,
Bill Wendling88103372009-01-30 21:37:17 +00003895 Shift);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003896 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N),
Bill Wendling88103372009-01-30 21:37:17 +00003897 N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00003898 }
3899 }
3900 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003901
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003902 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003903 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003904 N1.getOperand(0).getOpcode() == ISD::AND &&
3905 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003906 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003907 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003908 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003909 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003910 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003911 TruncC = TruncC.trunc(TruncVT.getScalarType().getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003912 return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0,
3913 DAG.getNode(ISD::AND, SDLoc(N),
Bill Wendling88103372009-01-30 21:37:17 +00003914 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003915 DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003916 SDLoc(N),
Bill Wendling9729c5a2009-01-31 03:12:48 +00003917 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003918 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003919 }
3920 }
3921
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003922 // fold (sra (trunc (sr x, c1)), c2) -> (trunc (sra x, c1+c2))
3923 // if c1 is equal to the number of bits the trunc removes
3924 if (N0.getOpcode() == ISD::TRUNCATE &&
3925 (N0.getOperand(0).getOpcode() == ISD::SRL ||
3926 N0.getOperand(0).getOpcode() == ISD::SRA) &&
3927 N0.getOperand(0).hasOneUse() &&
3928 N0.getOperand(0).getOperand(1).hasOneUse() &&
3929 N1C && isa<ConstantSDNode>(N0.getOperand(0).getOperand(1))) {
3930 EVT LargeVT = N0.getOperand(0).getValueType();
3931 ConstantSDNode *LargeShiftAmt =
3932 cast<ConstantSDNode>(N0.getOperand(0).getOperand(1));
3933
3934 if (LargeVT.getScalarType().getSizeInBits() - OpSizeInBits ==
3935 LargeShiftAmt->getZExtValue()) {
3936 SDValue Amt =
3937 DAG.getConstant(LargeShiftAmt->getZExtValue() + N1C->getZExtValue(),
Owen Anderson95771af2011-02-25 21:41:48 +00003938 getShiftAmountTy(N0.getOperand(0).getOperand(0).getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003939 SDValue SRA = DAG.getNode(ISD::SRA, SDLoc(N), LargeVT,
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003940 N0.getOperand(0).getOperand(0), Amt);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003941 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, SRA);
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003942 }
3943 }
3944
Scott Michelfdc40a02009-02-17 22:15:04 +00003945 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00003946 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3947 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003948
3949
Nate Begeman1d4d4142005-09-01 00:19:25 +00003950 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003951 if (DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003952 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00003953
Evan Chenge5b51ac2010-04-17 06:13:15 +00003954 if (N1C) {
3955 SDValue NewSRA = visitShiftByConstant(N, N1C->getZExtValue());
3956 if (NewSRA.getNode())
3957 return NewSRA;
3958 }
3959
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003960 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003961}
3962
Dan Gohman475871a2008-07-27 21:46:04 +00003963SDValue DAGCombiner::visitSRL(SDNode *N) {
3964 SDValue N0 = N->getOperand(0);
3965 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003966 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3967 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003968 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003969 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003970
Nate Begeman1d4d4142005-09-01 00:19:25 +00003971 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003972 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003973 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003974 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003975 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003976 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003977 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003978 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003979 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003980 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003981 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003982 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003983 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00003984 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003985 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003986 return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003987
Bill Wendling88103372009-01-30 21:37:17 +00003988 // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00003989 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003990 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003991 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3992 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003993 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003994 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003995 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00003996 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003997 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003998
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003999 // fold (srl (trunc (srl x, c1)), c2) -> 0 or (trunc (srl x, (add c1, c2)))
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00004000 if (N1C && N0.getOpcode() == ISD::TRUNCATE &&
4001 N0.getOperand(0).getOpcode() == ISD::SRL &&
Dale Johannesen025cc6e2010-12-20 20:10:50 +00004002 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00004003 uint64_t c1 =
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00004004 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
4005 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00004006 EVT InnerShiftVT = N0.getOperand(0).getValueType();
4007 EVT ShiftCountVT = N0.getOperand(0)->getOperand(1).getValueType();
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00004008 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
Dale Johannesen025cc6e2010-12-20 20:10:50 +00004009 // This is only valid if the OpSizeInBits + c1 = size of inner shift.
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00004010 if (c1 + OpSizeInBits == InnerShiftSize) {
4011 if (c1 + c2 >= InnerShiftSize)
4012 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004013 return DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT,
4014 DAG.getNode(ISD::SRL, SDLoc(N0), InnerShiftVT,
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00004015 N0.getOperand(0)->getOperand(0),
Dale Johannesenc72b18c2010-12-21 21:55:50 +00004016 DAG.getConstant(c1 + c2, ShiftCountVT)));
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00004017 }
4018 }
4019
Chris Lattnerefcddc32010-04-15 05:28:43 +00004020 // fold (srl (shl x, c), c) -> (and x, cst2)
4021 if (N1C && N0.getOpcode() == ISD::SHL && N0.getOperand(1) == N1 &&
4022 N0.getValueSizeInBits() <= 64) {
4023 uint64_t ShAmt = N1C->getZExtValue()+64-N0.getValueSizeInBits();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004024 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0.getOperand(0),
Chris Lattnerefcddc32010-04-15 05:28:43 +00004025 DAG.getConstant(~0ULL >> ShAmt, VT));
4026 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004027
Michael Liao2da86392013-06-21 18:45:27 +00004028 // fold (srl (anyextend x), c) -> (and (anyextend (srl x, c)), mask)
Chris Lattner06afe072006-05-05 22:53:17 +00004029 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
4030 // Shifting in all undef bits?
Owen Andersone50ed302009-08-10 22:56:29 +00004031 EVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004032 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Dale Johannesene8d72302009-02-06 23:05:02 +00004033 return DAG.getUNDEF(VT);
Chris Lattner06afe072006-05-05 22:53:17 +00004034
Evan Chenge5b51ac2010-04-17 06:13:15 +00004035 if (!LegalTypes || TLI.isTypeDesirableForOp(ISD::SRL, SmallVT)) {
Owen Andersona34d9362011-04-14 17:30:49 +00004036 uint64_t ShiftAmt = N1C->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004037 SDValue SmallShift = DAG.getNode(ISD::SRL, SDLoc(N0), SmallVT,
Owen Andersona34d9362011-04-14 17:30:49 +00004038 N0.getOperand(0),
4039 DAG.getConstant(ShiftAmt, getShiftAmountTy(SmallVT)));
Evan Chenge5b51ac2010-04-17 06:13:15 +00004040 AddToWorkList(SmallShift.getNode());
Michael Liao2da86392013-06-21 18:45:27 +00004041 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits()).lshr(ShiftAmt);
4042 return DAG.getNode(ISD::AND, SDLoc(N), VT,
4043 DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, SmallShift),
4044 DAG.getConstant(Mask, VT));
Evan Chenge5b51ac2010-04-17 06:13:15 +00004045 }
Chris Lattner06afe072006-05-05 22:53:17 +00004046 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004047
Chris Lattner3657ffe2006-10-12 20:23:19 +00004048 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
4049 // bit, which is unmodified by sra.
Bill Wendling88103372009-01-30 21:37:17 +00004050 if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00004051 if (N0.getOpcode() == ISD::SRA)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004052 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0), N1);
Chris Lattner3657ffe2006-10-12 20:23:19 +00004053 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004054
Sylvestre Ledru94c22712012-09-27 10:14:43 +00004055 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
Scott Michelfdc40a02009-02-17 22:15:04 +00004056 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004057 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00004058 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00004059 DAG.ComputeMaskedBits(N0.getOperand(0), KnownZero, KnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00004060
Chris Lattner350bec02006-04-02 06:11:11 +00004061 // If any of the input bits are KnownOne, then the input couldn't be all
4062 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00004063 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004064
Chris Lattner350bec02006-04-02 06:11:11 +00004065 // If all of the bits input the to ctlz node are known to be zero, then
4066 // the result of the ctlz is "32" and the result of the shift is one.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00004067 APInt UnknownBits = ~KnownZero;
Chris Lattner350bec02006-04-02 06:11:11 +00004068 if (UnknownBits == 0) return DAG.getConstant(1, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004069
Chris Lattner350bec02006-04-02 06:11:11 +00004070 // Otherwise, check to see if there is exactly one bit input to the ctlz.
Bill Wendling88103372009-01-30 21:37:17 +00004071 if ((UnknownBits & (UnknownBits - 1)) == 0) {
Chris Lattner350bec02006-04-02 06:11:11 +00004072 // Okay, we know that only that the single bit specified by UnknownBits
Bill Wendling88103372009-01-30 21:37:17 +00004073 // could be set on input to the CTLZ node. If this bit is set, the SRL
4074 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
4075 // to an SRL/XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00004076 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00004077 SDValue Op = N0.getOperand(0);
Bill Wendling88103372009-01-30 21:37:17 +00004078
Chris Lattner350bec02006-04-02 06:11:11 +00004079 if (ShAmt) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004080 Op = DAG.getNode(ISD::SRL, SDLoc(N0), VT, Op,
Owen Anderson95771af2011-02-25 21:41:48 +00004081 DAG.getConstant(ShAmt, getShiftAmountTy(Op.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00004082 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00004083 }
Bill Wendling88103372009-01-30 21:37:17 +00004084
Andrew Trickac6d9be2013-05-25 02:42:55 +00004085 return DAG.getNode(ISD::XOR, SDLoc(N), VT,
Bill Wendling88103372009-01-30 21:37:17 +00004086 Op, DAG.getConstant(1, VT));
Chris Lattner350bec02006-04-02 06:11:11 +00004087 }
4088 }
Evan Chengeb9f8922008-08-30 02:03:58 +00004089
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00004090 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00004091 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00004092 N1.getOperand(0).getOpcode() == ISD::AND &&
4093 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00004094 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00004095 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00004096 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00004097 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00004098 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004099 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004100 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0,
4101 DAG.getNode(ISD::AND, SDLoc(N),
Bill Wendling88103372009-01-30 21:37:17 +00004102 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00004103 DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004104 SDLoc(N),
Bill Wendling9729c5a2009-01-31 03:12:48 +00004105 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00004106 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00004107 }
4108 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004109
Chris Lattner61a4c072007-04-18 03:06:49 +00004110 // fold operands of srl based on knowledge that the low bits are not
4111 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00004112 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
4113 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004114
Evan Cheng9ab2b982009-12-18 21:31:31 +00004115 if (N1C) {
4116 SDValue NewSRL = visitShiftByConstant(N, N1C->getZExtValue());
4117 if (NewSRL.getNode())
4118 return NewSRL;
4119 }
4120
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004121 // Attempt to convert a srl of a load into a narrower zero-extending load.
4122 SDValue NarrowLoad = ReduceLoadWidth(N);
4123 if (NarrowLoad.getNode())
4124 return NarrowLoad;
4125
Evan Cheng9ab2b982009-12-18 21:31:31 +00004126 // Here is a common situation. We want to optimize:
4127 //
4128 // %a = ...
4129 // %b = and i32 %a, 2
4130 // %c = srl i32 %b, 1
4131 // brcond i32 %c ...
4132 //
4133 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004134 //
Evan Cheng9ab2b982009-12-18 21:31:31 +00004135 // %a = ...
4136 // %b = and %a, 2
4137 // %c = setcc eq %b, 0
4138 // brcond %c ...
4139 //
4140 // However when after the source operand of SRL is optimized into AND, the SRL
4141 // itself may not be optimized further. Look for it and add the BRCOND into
4142 // the worklist.
Evan Chengd40d03e2010-01-06 19:38:29 +00004143 if (N->hasOneUse()) {
4144 SDNode *Use = *N->use_begin();
4145 if (Use->getOpcode() == ISD::BRCOND)
4146 AddToWorkList(Use);
4147 else if (Use->getOpcode() == ISD::TRUNCATE && Use->hasOneUse()) {
4148 // Also look pass the truncate.
4149 Use = *Use->use_begin();
4150 if (Use->getOpcode() == ISD::BRCOND)
4151 AddToWorkList(Use);
4152 }
4153 }
Evan Cheng9ab2b982009-12-18 21:31:31 +00004154
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004155 return SDValue();
Evan Cheng4c26e932010-04-19 19:29:22 +00004156}
4157
Dan Gohman475871a2008-07-27 21:46:04 +00004158SDValue DAGCombiner::visitCTLZ(SDNode *N) {
4159 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004160 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004161
4162 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004163 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004164 return DAG.getNode(ISD::CTLZ, SDLoc(N), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004165 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004166}
4167
Chandler Carruth63974b22011-12-13 01:56:10 +00004168SDValue DAGCombiner::visitCTLZ_ZERO_UNDEF(SDNode *N) {
4169 SDValue N0 = N->getOperand(0);
4170 EVT VT = N->getValueType(0);
4171
4172 // fold (ctlz_zero_undef c1) -> c2
4173 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004174 return DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SDLoc(N), VT, N0);
Chandler Carruth63974b22011-12-13 01:56:10 +00004175 return SDValue();
4176}
4177
Dan Gohman475871a2008-07-27 21:46:04 +00004178SDValue DAGCombiner::visitCTTZ(SDNode *N) {
4179 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004180 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004181
Nate Begeman1d4d4142005-09-01 00:19:25 +00004182 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004183 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004184 return DAG.getNode(ISD::CTTZ, SDLoc(N), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004185 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004186}
4187
Chandler Carruth63974b22011-12-13 01:56:10 +00004188SDValue DAGCombiner::visitCTTZ_ZERO_UNDEF(SDNode *N) {
4189 SDValue N0 = N->getOperand(0);
4190 EVT VT = N->getValueType(0);
4191
4192 // fold (cttz_zero_undef c1) -> c2
4193 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004194 return DAG.getNode(ISD::CTTZ_ZERO_UNDEF, SDLoc(N), VT, N0);
Chandler Carruth63974b22011-12-13 01:56:10 +00004195 return SDValue();
4196}
4197
Dan Gohman475871a2008-07-27 21:46:04 +00004198SDValue DAGCombiner::visitCTPOP(SDNode *N) {
4199 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004200 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004201
Nate Begeman1d4d4142005-09-01 00:19:25 +00004202 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004203 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004204 return DAG.getNode(ISD::CTPOP, SDLoc(N), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004205 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004206}
4207
Dan Gohman475871a2008-07-27 21:46:04 +00004208SDValue DAGCombiner::visitSELECT(SDNode *N) {
4209 SDValue N0 = N->getOperand(0);
4210 SDValue N1 = N->getOperand(1);
4211 SDValue N2 = N->getOperand(2);
Nate Begeman452d7be2005-09-16 00:54:12 +00004212 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
4213 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4214 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Owen Andersone50ed302009-08-10 22:56:29 +00004215 EVT VT = N->getValueType(0);
4216 EVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00004217
Bill Wendling34584e62009-01-30 22:02:18 +00004218 // fold (select C, X, X) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00004219 if (N1 == N2)
4220 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00004221 // fold (select true, X, Y) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00004222 if (N0C && !N0C->isNullValue())
4223 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00004224 // fold (select false, X, Y) -> Y
Nate Begeman452d7be2005-09-16 00:54:12 +00004225 if (N0C && N0C->isNullValue())
4226 return N2;
Bill Wendling34584e62009-01-30 22:02:18 +00004227 // fold (select C, 1, X) -> (or C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004228 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004229 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2);
Bill Wendling34584e62009-01-30 22:02:18 +00004230 // fold (select C, 0, 1) -> (xor C, 1)
Bob Wilson67ba2232009-01-22 22:05:48 +00004231 if (VT.isInteger() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004232 (VT0 == MVT::i1 ||
Bob Wilson67ba2232009-01-22 22:05:48 +00004233 (VT0.isInteger() &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00004234 TLI.getBooleanContents(false) ==
4235 TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004236 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00004237 SDValue XORNode;
Evan Cheng571c4782007-08-18 05:57:05 +00004238 if (VT == VT0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004239 return DAG.getNode(ISD::XOR, SDLoc(N), VT0,
Bill Wendling34584e62009-01-30 22:02:18 +00004240 N0, DAG.getConstant(1, VT0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004241 XORNode = DAG.getNode(ISD::XOR, SDLoc(N0), VT0,
Bill Wendling34584e62009-01-30 22:02:18 +00004242 N0, DAG.getConstant(1, VT0));
Gabor Greifba36cb52008-08-28 21:40:38 +00004243 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00004244 if (VT.bitsGT(VT0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004245 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, XORNode);
4246 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, XORNode);
Evan Cheng571c4782007-08-18 05:57:05 +00004247 }
Bill Wendling34584e62009-01-30 22:02:18 +00004248 // fold (select C, 0, X) -> (and (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004249 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004250 SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00004251 AddToWorkList(NOTNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004252 return DAG.getNode(ISD::AND, SDLoc(N), VT, NOTNode, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00004253 }
Bill Wendling34584e62009-01-30 22:02:18 +00004254 // fold (select C, X, 1) -> (or (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004255 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004256 SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00004257 AddToWorkList(NOTNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004258 return DAG.getNode(ISD::OR, SDLoc(N), VT, NOTNode, N1);
Nate Begeman452d7be2005-09-16 00:54:12 +00004259 }
Bill Wendling34584e62009-01-30 22:02:18 +00004260 // fold (select C, X, 0) -> (and C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004261 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00004262 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, N1);
Bill Wendling34584e62009-01-30 22:02:18 +00004263 // fold (select X, X, Y) -> (or X, Y)
4264 // fold (select X, 1, Y) -> (or X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00004265 if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004266 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2);
Bill Wendling34584e62009-01-30 22:02:18 +00004267 // fold (select X, Y, X) -> (and X, Y)
4268 // fold (select X, Y, 0) -> (and X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00004269 if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004270 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004271
Chris Lattner40c62d52005-10-18 06:04:22 +00004272 // If we can fold this based on the true/false value, do so.
4273 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00004274 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004275
Nate Begeman44728a72005-09-19 22:34:01 +00004276 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004277 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004278 // FIXME:
Owen Anderson825b72b2009-08-11 20:47:22 +00004279 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
Nate Begeman750ac1b2006-02-01 07:19:44 +00004280 // having to say they don't support SELECT_CC on every type the DAG knows
4281 // about, since there is no way to mark an opcode illegal at all value types
Owen Anderson825b72b2009-08-11 20:47:22 +00004282 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) &&
Dan Gohman4ea48042009-08-02 16:19:38 +00004283 TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004284 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT,
Bill Wendling34584e62009-01-30 22:02:18 +00004285 N0.getOperand(0), N0.getOperand(1),
Nate Begeman750ac1b2006-02-01 07:19:44 +00004286 N1, N2, N0.getOperand(2));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004287 return SimplifySelect(SDLoc(N), N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004288 }
Bill Wendling34584e62009-01-30 22:02:18 +00004289
Dan Gohman475871a2008-07-27 21:46:04 +00004290 return SDValue();
Nate Begeman452d7be2005-09-16 00:54:12 +00004291}
4292
Benjamin Kramer6242fda2013-04-26 09:19:19 +00004293SDValue DAGCombiner::visitVSELECT(SDNode *N) {
4294 SDValue N0 = N->getOperand(0);
4295 SDValue N1 = N->getOperand(1);
4296 SDValue N2 = N->getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004297 SDLoc DL(N);
Benjamin Kramer6242fda2013-04-26 09:19:19 +00004298
4299 // Canonicalize integer abs.
4300 // vselect (setg[te] X, 0), X, -X ->
4301 // vselect (setgt X, -1), X, -X ->
4302 // vselect (setl[te] X, 0), -X, X ->
4303 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4304 if (N0.getOpcode() == ISD::SETCC) {
4305 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
4306 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
4307 bool isAbs = false;
4308 bool RHSIsAllZeros = ISD::isBuildVectorAllZeros(RHS.getNode());
4309
4310 if (((RHSIsAllZeros && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
4311 (ISD::isBuildVectorAllOnes(RHS.getNode()) && CC == ISD::SETGT)) &&
4312 N1 == LHS && N2.getOpcode() == ISD::SUB && N1 == N2.getOperand(1))
4313 isAbs = ISD::isBuildVectorAllZeros(N2.getOperand(0).getNode());
4314 else if ((RHSIsAllZeros && (CC == ISD::SETLT || CC == ISD::SETLE)) &&
4315 N2 == LHS && N1.getOpcode() == ISD::SUB && N2 == N1.getOperand(1))
4316 isAbs = ISD::isBuildVectorAllZeros(N1.getOperand(0).getNode());
4317
4318 if (isAbs) {
4319 EVT VT = LHS.getValueType();
4320 SDValue Shift = DAG.getNode(
4321 ISD::SRA, DL, VT, LHS,
4322 DAG.getConstant(VT.getScalarType().getSizeInBits() - 1, VT));
4323 SDValue Add = DAG.getNode(ISD::ADD, DL, VT, LHS, Shift);
4324 AddToWorkList(Shift.getNode());
4325 AddToWorkList(Add.getNode());
4326 return DAG.getNode(ISD::XOR, DL, VT, Add, Shift);
4327 }
4328 }
4329
4330 return SDValue();
4331}
4332
Dan Gohman475871a2008-07-27 21:46:04 +00004333SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
4334 SDValue N0 = N->getOperand(0);
4335 SDValue N1 = N->getOperand(1);
4336 SDValue N2 = N->getOperand(2);
4337 SDValue N3 = N->getOperand(3);
4338 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00004339 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00004340
Nate Begeman44728a72005-09-19 22:34:01 +00004341 // fold select_cc lhs, rhs, x, x, cc -> x
4342 if (N2 == N3)
4343 return N2;
Scott Michelfdc40a02009-02-17 22:15:04 +00004344
Chris Lattner5f42a242006-09-20 06:19:26 +00004345 // Determine if the condition we're dealing with is constant
Matt Arsenault225ed702013-05-18 00:21:46 +00004346 SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004347 N0, N1, CC, SDLoc(N), false);
Stephen Lin7e6d6202013-06-15 04:03:33 +00004348 if (SCC.getNode()) {
4349 AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00004350
Stephen Lin7e6d6202013-06-15 04:03:33 +00004351 if (ConstantSDNode *SCCC = dyn_cast<ConstantSDNode>(SCC.getNode())) {
4352 if (!SCCC->isNullValue())
4353 return N2; // cond always true -> true val
4354 else
4355 return N3; // cond always false -> false val
4356 }
4357
4358 // Fold to a simpler select_cc
4359 if (SCC.getOpcode() == ISD::SETCC)
4360 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N2.getValueType(),
4361 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
4362 SCC.getOperand(2));
Chris Lattner5f42a242006-09-20 06:19:26 +00004363 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004364
Chris Lattner40c62d52005-10-18 06:04:22 +00004365 // If we can fold this based on the true/false value, do so.
4366 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00004367 return SDValue(N, 0); // Don't revisit N.
Scott Michelfdc40a02009-02-17 22:15:04 +00004368
Nate Begeman44728a72005-09-19 22:34:01 +00004369 // fold select_cc into other things, such as min/max/abs
Andrew Trickac6d9be2013-05-25 02:42:55 +00004370 return SimplifySelectCC(SDLoc(N), N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00004371}
4372
Dan Gohman475871a2008-07-27 21:46:04 +00004373SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7be2005-09-16 00:54:12 +00004374 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00004375 cast<CondCodeSDNode>(N->getOperand(2))->get(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004376 SDLoc(N));
Nate Begeman452d7be2005-09-16 00:54:12 +00004377}
4378
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004379// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
Dan Gohman57fc82d2009-04-09 03:51:29 +00004380// "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))"
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004381// transformation. Returns true if extension are possible and the above
Scott Michelfdc40a02009-02-17 22:15:04 +00004382// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00004383static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004384 unsigned ExtOpc,
Craig Toppera0ec3f92013-07-14 04:42:23 +00004385 SmallVectorImpl<SDNode *> &ExtendNodes,
Dan Gohman79ce2762009-01-15 19:20:50 +00004386 const TargetLowering &TLI) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004387 bool HasCopyToRegUses = false;
4388 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00004389 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
4390 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004391 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00004392 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004393 if (User == N)
4394 continue;
Dan Gohman57fc82d2009-04-09 03:51:29 +00004395 if (UI.getUse().getResNo() != N0.getResNo())
4396 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004397 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
Dan Gohman57fc82d2009-04-09 03:51:29 +00004398 if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004399 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
4400 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
4401 // Sign bits will be lost after a zext.
4402 return false;
4403 bool Add = false;
4404 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004405 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004406 if (UseOp == N0)
4407 continue;
4408 if (!isa<ConstantSDNode>(UseOp))
4409 return false;
4410 Add = true;
4411 }
4412 if (Add)
4413 ExtendNodes.push_back(User);
Dan Gohman57fc82d2009-04-09 03:51:29 +00004414 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004415 }
Dan Gohman57fc82d2009-04-09 03:51:29 +00004416 // If truncates aren't free and there are users we can't
4417 // extend, it isn't worthwhile.
4418 if (!isTruncFree)
4419 return false;
4420 // Remember if this value is live-out.
4421 if (User->getOpcode() == ISD::CopyToReg)
4422 HasCopyToRegUses = true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004423 }
4424
4425 if (HasCopyToRegUses) {
4426 bool BothLiveOut = false;
4427 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
4428 UI != UE; ++UI) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00004429 SDUse &Use = UI.getUse();
4430 if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) {
4431 BothLiveOut = true;
4432 break;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004433 }
4434 }
4435 if (BothLiveOut)
4436 // Both unextended and extended values are live out. There had better be
Bob Wilsonbebfbc52010-11-28 06:51:19 +00004437 // a good reason for the transformation.
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004438 return ExtendNodes.size();
4439 }
4440 return true;
4441}
4442
Craig Topper6c64fba2013-07-13 07:43:40 +00004443void DAGCombiner::ExtendSetCCUses(const SmallVectorImpl<SDNode *> &SetCCs,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004444 SDValue Trunc, SDValue ExtLoad, SDLoc DL,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004445 ISD::NodeType ExtType) {
4446 // Extend SetCC uses if necessary.
4447 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
4448 SDNode *SetCC = SetCCs[i];
4449 SmallVector<SDValue, 4> Ops;
4450
4451 for (unsigned j = 0; j != 2; ++j) {
4452 SDValue SOp = SetCC->getOperand(j);
4453 if (SOp == Trunc)
4454 Ops.push_back(ExtLoad);
4455 else
4456 Ops.push_back(DAG.getNode(ExtType, DL, ExtLoad->getValueType(0), SOp));
4457 }
4458
4459 Ops.push_back(SetCC->getOperand(2));
4460 CombineTo(SetCC, DAG.getNode(ISD::SETCC, DL, SetCC->getValueType(0),
4461 &Ops[0], Ops.size()));
4462 }
4463}
4464
Dan Gohman475871a2008-07-27 21:46:04 +00004465SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
4466 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004467 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004468
Nate Begeman1d4d4142005-09-01 00:19:25 +00004469 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004470 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004471 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004472
Nadav Rotem0c8607b2013-01-20 08:35:56 +00004473 // fold (sext (sext x)) -> (sext x)
4474 // fold (sext (aext x)) -> (sext x)
4475 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004476 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT,
Nadav Rotem0c8607b2013-01-20 08:35:56 +00004477 N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004478
Chris Lattner22558872007-02-26 03:13:59 +00004479 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004480 // fold (sext (truncate (load x))) -> (sext (smaller load x))
4481 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004482 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4483 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004484 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4485 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004486 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004487 // CombineTo deleted the truncate, if needed, but not what's under it.
4488 AddToWorkList(oye);
4489 }
Dan Gohmanc7b34442009-04-27 02:00:55 +00004490 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004491 }
Evan Chengc88138f2007-03-22 01:54:19 +00004492
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004493 // See if the value being truncated is already sign extended. If so, just
4494 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00004495 SDValue Op = N0.getOperand(0);
Dan Gohmand1996362010-01-09 02:13:55 +00004496 unsigned OpBits = Op.getValueType().getScalarType().getSizeInBits();
4497 unsigned MidBits = N0.getValueType().getScalarType().getSizeInBits();
4498 unsigned DestBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00004499 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004500
Chris Lattner22558872007-02-26 03:13:59 +00004501 if (OpBits == DestBits) {
4502 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
4503 // bits, it is already ready.
4504 if (NumSignBits > DestBits-MidBits)
4505 return Op;
4506 } else if (OpBits < DestBits) {
4507 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
4508 // bits, just sext from i32.
4509 if (NumSignBits > OpBits-MidBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004510 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, Op);
Chris Lattner22558872007-02-26 03:13:59 +00004511 } else {
4512 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
4513 // bits, just truncate to i32.
4514 if (NumSignBits > OpBits-MidBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004515 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00004516 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004517
Chris Lattner22558872007-02-26 03:13:59 +00004518 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sands25cf2272008-11-24 14:53:14 +00004519 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
4520 N0.getValueType())) {
Dan Gohmand1996362010-01-09 02:13:55 +00004521 if (OpBits < DestBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004522 Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N0), VT, Op);
Dan Gohmand1996362010-01-09 02:13:55 +00004523 else if (OpBits > DestBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004524 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT, Op);
4525 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, Op,
Dan Gohmand1996362010-01-09 02:13:55 +00004526 DAG.getValueType(N0.getValueType()));
Chris Lattner22558872007-02-26 03:13:59 +00004527 }
Chris Lattner6007b842006-09-21 06:00:20 +00004528 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004529
Evan Cheng110dec22005-12-14 02:19:23 +00004530 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004531 // None of the supported targets knows how to perform load and sign extend
Nadav Rotemfcd96192011-02-27 07:40:43 +00004532 // on vectors in one instruction. We only perform this transformation on
4533 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004534 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004535 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004536 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004537 bool DoXform = true;
4538 SmallVector<SDNode*, 4> SetCCs;
4539 if (!N0.hasOneUse())
4540 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
4541 if (DoXform) {
4542 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004543 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00004544 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004545 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004546 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004547 LN0->isVolatile(), LN0->isNonTemporal(),
4548 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004549 CombineTo(N, ExtLoad);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004550 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004551 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004552 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004553 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004554 ISD::SIGN_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004555 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004556 }
Nate Begeman3df4d522005-10-12 20:40:40 +00004557 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004558
4559 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
4560 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004561 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4562 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004563 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004564 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004565 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004566 TLI.isLoadExtLegal(ISD::SEXTLOAD, MemVT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004567 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004568 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004569 LN0->getBasePtr(), LN0->getPointerInfo(),
4570 MemVT,
David Greene1e559442010-02-15 17:00:31 +00004571 LN0->isVolatile(), LN0->isNonTemporal(),
4572 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004573 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004574 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004575 DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004576 N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004577 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004578 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004579 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004580 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004581
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004582 // fold (sext (and/or/xor (load x), cst)) ->
4583 // (and/or/xor (sextload x), (sext cst))
4584 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4585 N0.getOpcode() == ISD::XOR) &&
4586 isa<LoadSDNode>(N0.getOperand(0)) &&
4587 N0.getOperand(1).getOpcode() == ISD::Constant &&
4588 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()) &&
4589 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4590 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4591 if (LN0->getExtensionType() != ISD::ZEXTLOAD) {
4592 bool DoXform = true;
4593 SmallVector<SDNode*, 4> SetCCs;
4594 if (!N0.hasOneUse())
4595 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::SIGN_EXTEND,
4596 SetCCs, TLI);
4597 if (DoXform) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004598 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(LN0), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004599 LN0->getChain(), LN0->getBasePtr(),
4600 LN0->getPointerInfo(),
4601 LN0->getMemoryVT(),
4602 LN0->isVolatile(),
4603 LN0->isNonTemporal(),
4604 LN0->getAlignment());
4605 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4606 Mask = Mask.sext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004607 SDValue And = DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004608 ExtLoad, DAG.getConstant(Mask, VT));
4609 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004610 SDLoc(N0.getOperand(0)),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004611 N0.getOperand(0).getValueType(), ExtLoad);
4612 CombineTo(N, And);
4613 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004614 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004615 ISD::SIGN_EXTEND);
4616 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4617 }
4618 }
4619 }
4620
Chris Lattner20a35c32007-04-11 05:32:27 +00004621 if (N0.getOpcode() == ISD::SETCC) {
Chris Lattner2b7a2712009-07-08 00:31:33 +00004622 // sext(setcc) -> sext_in_reg(vsetcc) for vectors.
Dan Gohman3ce89f42010-04-30 17:19:19 +00004623 // Only do this before legalize for now.
Owen Andersoned5707b2013-04-23 18:09:28 +00004624 if (VT.isVector() && !LegalOperations &&
Stephen Lin155615d2013-07-08 00:37:03 +00004625 TLI.getBooleanContents(true) ==
Owen Andersoned5707b2013-04-23 18:09:28 +00004626 TargetLowering::ZeroOrNegativeOneBooleanContent) {
Dan Gohman3ce89f42010-04-30 17:19:19 +00004627 EVT N0VT = N0.getOperand(0).getValueType();
Nadav Rotem2e506192012-04-11 08:26:11 +00004628 // On some architectures (such as SSE/NEON/etc) the SETCC result type is
4629 // of the same size as the compared operands. Only optimize sext(setcc())
4630 // if this is the case.
Matt Arsenault225ed702013-05-18 00:21:46 +00004631 EVT SVT = getSetCCResultType(N0VT);
Nadav Rotem2e506192012-04-11 08:26:11 +00004632
4633 // We know that the # elements of the results is the same as the
4634 // # elements of the compare (and the # elements of the compare result
4635 // for that matter). Check to see that they are the same size. If so,
4636 // we know that the element size of the sext'd result matches the
4637 // element size of the compare operands.
4638 if (VT.getSizeInBits() == SVT.getSizeInBits())
Andrew Trickac6d9be2013-05-25 02:42:55 +00004639 return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00004640 N0.getOperand(1),
4641 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Matt Arsenault9aa8fdf2013-05-17 21:43:43 +00004642
Dan Gohman3ce89f42010-04-30 17:19:19 +00004643 // If the desired elements are smaller or larger than the source
4644 // elements we can use a matching integer vector type and then
4645 // truncate/sign extend
Matt Arsenault9aa8fdf2013-05-17 21:43:43 +00004646 EVT MatchingVectorType = N0VT.changeVectorElementTypeToInteger();
Craig Topper0eb5dad2012-09-29 07:18:53 +00004647 if (SVT == MatchingVectorType) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004648 SDValue VsetCC = DAG.getSetCC(SDLoc(N), MatchingVectorType,
Craig Topper0eb5dad2012-09-29 07:18:53 +00004649 N0.getOperand(0), N0.getOperand(1),
4650 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004651 return DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT);
Dan Gohman3ce89f42010-04-30 17:19:19 +00004652 }
Chris Lattner2b7a2712009-07-08 00:31:33 +00004653 }
Dan Gohman3ce89f42010-04-30 17:19:19 +00004654
Chris Lattner2b7a2712009-07-08 00:31:33 +00004655 // sext(setcc x, y, cc) -> (select_cc x, y, -1, 0, cc)
Dan Gohmana7bcef12010-04-24 01:17:30 +00004656 unsigned ElementWidth = VT.getScalarType().getSizeInBits();
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004657 SDValue NegOne =
Dan Gohmana7bcef12010-04-24 01:17:30 +00004658 DAG.getConstant(APInt::getAllOnesValue(ElementWidth), VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004659 SDValue SCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00004660 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004661 NegOne, DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004662 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004663 if (SCC.getNode()) return SCC;
Matt Arsenaultb05e4772013-06-14 22:04:37 +00004664 if (!VT.isVector() &&
4665 (!LegalOperations ||
4666 TLI.isOperationLegal(ISD::SETCC, getSetCCResultType(VT)))) {
4667 return DAG.getSelect(SDLoc(N), VT,
4668 DAG.getSetCC(SDLoc(N),
4669 getSetCCResultType(VT),
4670 N0.getOperand(0), N0.getOperand(1),
4671 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
4672 NegOne, DAG.getConstant(0, VT));
4673 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004674 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004675
Dan Gohman8f0ad582008-04-28 16:58:24 +00004676 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sands25cf2272008-11-24 14:53:14 +00004677 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman187db7b2008-04-28 18:47:17 +00004678 DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004679 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004680
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004681 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004682}
4683
Rafael Espindoladecbc432012-04-09 16:06:03 +00004684// isTruncateOf - If N is a truncate of some other value, return true, record
4685// the value being truncated in Op and which of Op's bits are zero in KnownZero.
4686// This function computes KnownZero to avoid a duplicated call to
4687// ComputeMaskedBits in the caller.
4688static bool isTruncateOf(SelectionDAG &DAG, SDValue N, SDValue &Op,
4689 APInt &KnownZero) {
4690 APInt KnownOne;
4691 if (N->getOpcode() == ISD::TRUNCATE) {
4692 Op = N->getOperand(0);
4693 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4694 return true;
4695 }
4696
4697 if (N->getOpcode() != ISD::SETCC || N->getValueType(0) != MVT::i1 ||
4698 cast<CondCodeSDNode>(N->getOperand(2))->get() != ISD::SETNE)
4699 return false;
4700
4701 SDValue Op0 = N->getOperand(0);
4702 SDValue Op1 = N->getOperand(1);
4703 assert(Op0.getValueType() == Op1.getValueType());
4704
4705 ConstantSDNode *COp0 = dyn_cast<ConstantSDNode>(Op0);
4706 ConstantSDNode *COp1 = dyn_cast<ConstantSDNode>(Op1);
Rafael Espindolafdb230a2012-04-10 00:16:22 +00004707 if (COp0 && COp0->isNullValue())
Rafael Espindoladecbc432012-04-09 16:06:03 +00004708 Op = Op1;
Rafael Espindolafdb230a2012-04-10 00:16:22 +00004709 else if (COp1 && COp1->isNullValue())
Rafael Espindoladecbc432012-04-09 16:06:03 +00004710 Op = Op0;
4711 else
4712 return false;
4713
4714 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4715
4716 if (!(KnownZero | APInt(Op.getValueSizeInBits(), 1)).isAllOnesValue())
4717 return false;
4718
4719 return true;
4720}
4721
Dan Gohman475871a2008-07-27 21:46:04 +00004722SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
4723 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004724 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004725
Nate Begeman1d4d4142005-09-01 00:19:25 +00004726 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004727 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004728 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004729 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00004730 // fold (zext (aext x)) -> (zext x)
4731 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004732 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004733 N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00004734
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004735 // fold (zext (truncate x)) -> (zext x) or
4736 // (zext (truncate x)) -> (truncate x)
4737 // This is valid when the truncated bits of x are already zero.
4738 // FIXME: We should extend this to work for vectors too.
Rafael Espindoladecbc432012-04-09 16:06:03 +00004739 SDValue Op;
4740 APInt KnownZero;
4741 if (!VT.isVector() && isTruncateOf(DAG, N0, Op, KnownZero)) {
4742 APInt TruncatedBits =
4743 (Op.getValueSizeInBits() == N0.getValueSizeInBits()) ?
4744 APInt(Op.getValueSizeInBits(), 0) :
4745 APInt::getBitsSet(Op.getValueSizeInBits(),
4746 N0.getValueSizeInBits(),
4747 std::min(Op.getValueSizeInBits(),
4748 VT.getSizeInBits()));
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00004749 if (TruncatedBits == (KnownZero & TruncatedBits)) {
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004750 if (VT.bitsGT(Op.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004751 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, Op);
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004752 if (VT.bitsLT(Op.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004753 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004754
4755 return Op;
4756 }
4757 }
4758
Evan Chengc88138f2007-03-22 01:54:19 +00004759 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4760 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00004761 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004762 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4763 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004764 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4765 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004766 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004767 // CombineTo deleted the truncate, if needed, but not what's under it.
4768 AddToWorkList(oye);
4769 }
Eli Friedmane545d382011-04-16 23:25:34 +00004770 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004771 }
Evan Chengc88138f2007-03-22 01:54:19 +00004772 }
4773
Chris Lattner6007b842006-09-21 06:00:20 +00004774 // fold (zext (truncate x)) -> (and x, mask)
4775 if (N0.getOpcode() == ISD::TRUNCATE &&
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004776 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman394d6292010-11-03 01:47:46 +00004777
4778 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4779 // fold (zext (truncate (srl (load x), c))) -> (zext (smaller load (x+c/n)))
4780 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4781 if (NarrowLoad.getNode()) {
4782 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4783 if (NarrowLoad.getNode() != N0.getNode()) {
4784 CombineTo(N0.getNode(), NarrowLoad);
4785 // CombineTo deleted the truncate, if needed, but not what's under it.
4786 AddToWorkList(oye);
4787 }
4788 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4789 }
4790
Dan Gohman475871a2008-07-27 21:46:04 +00004791 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004792 if (Op.getValueType().bitsLT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004793 Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00004794 AddToWorkList(Op.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00004795 } else if (Op.getValueType().bitsGT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004796 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00004797 AddToWorkList(Op.getNode());
Chris Lattner6007b842006-09-21 06:00:20 +00004798 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00004799 return DAG.getZeroExtendInReg(Op, SDLoc(N),
Dan Gohman87862e72009-12-11 21:31:27 +00004800 N0.getValueType().getScalarType());
Chris Lattner6007b842006-09-21 06:00:20 +00004801 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004802
Dan Gohman97121ba2009-04-08 00:15:30 +00004803 // Fold (zext (and (trunc x), cst)) -> (and x, cst),
4804 // if either of the casts is not free.
Chris Lattner111c2282006-09-21 06:14:31 +00004805 if (N0.getOpcode() == ISD::AND &&
4806 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00004807 N0.getOperand(1).getOpcode() == ISD::Constant &&
4808 (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
4809 N0.getValueType()) ||
4810 !TLI.isZExtFree(N0.getValueType(), VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00004811 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004812 if (X.getValueType().bitsLT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004813 X = DAG.getNode(ISD::ANY_EXTEND, SDLoc(X), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004814 } else if (X.getValueType().bitsGT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004815 X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X);
Chris Lattner111c2282006-09-21 06:14:31 +00004816 }
Dan Gohman220a8232008-03-03 23:51:38 +00004817 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004818 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004819 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004820 X, DAG.getConstant(Mask, VT));
Chris Lattner111c2282006-09-21 06:14:31 +00004821 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004822
Evan Cheng110dec22005-12-14 02:19:23 +00004823 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Nadav Rotemed9b9342011-02-20 12:37:50 +00004824 // None of the supported targets knows how to perform load and vector_zext
Nadav Rotemfcd96192011-02-27 07:40:43 +00004825 // on vectors in one instruction. We only perform this transformation on
4826 // scalars.
Nadav Rotemed9b9342011-02-20 12:37:50 +00004827 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004828 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004829 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004830 bool DoXform = true;
4831 SmallVector<SDNode*, 4> SetCCs;
4832 if (!N0.hasOneUse())
4833 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
4834 if (DoXform) {
4835 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004836 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004837 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004838 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004839 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004840 LN0->isVolatile(), LN0->isNonTemporal(),
4841 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004842 CombineTo(N, ExtLoad);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004843 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004844 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004845 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling6ce610f2009-01-30 22:23:15 +00004846
Andrew Trickac6d9be2013-05-25 02:42:55 +00004847 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004848 ISD::ZERO_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004849 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004850 }
Evan Cheng110dec22005-12-14 02:19:23 +00004851 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004852
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004853 // fold (zext (and/or/xor (load x), cst)) ->
4854 // (and/or/xor (zextload x), (zext cst))
4855 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4856 N0.getOpcode() == ISD::XOR) &&
4857 isa<LoadSDNode>(N0.getOperand(0)) &&
4858 N0.getOperand(1).getOpcode() == ISD::Constant &&
4859 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()) &&
4860 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4861 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4862 if (LN0->getExtensionType() != ISD::SEXTLOAD) {
4863 bool DoXform = true;
4864 SmallVector<SDNode*, 4> SetCCs;
4865 if (!N0.hasOneUse())
4866 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::ZERO_EXTEND,
4867 SetCCs, TLI);
4868 if (DoXform) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004869 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004870 LN0->getChain(), LN0->getBasePtr(),
4871 LN0->getPointerInfo(),
4872 LN0->getMemoryVT(),
4873 LN0->isVolatile(),
4874 LN0->isNonTemporal(),
4875 LN0->getAlignment());
4876 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4877 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004878 SDValue And = DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004879 ExtLoad, DAG.getConstant(Mask, VT));
4880 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004881 SDLoc(N0.getOperand(0)),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004882 N0.getOperand(0).getValueType(), ExtLoad);
4883 CombineTo(N, And);
4884 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004885 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004886 ISD::ZERO_EXTEND);
4887 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4888 }
4889 }
4890 }
4891
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004892 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
4893 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004894 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4895 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004896 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004897 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004898 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004899 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004900 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004901 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004902 LN0->getBasePtr(), LN0->getPointerInfo(),
4903 MemVT,
David Greene1e559442010-02-15 17:00:31 +00004904 LN0->isVolatile(), LN0->isNonTemporal(),
4905 LN0->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004906 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004907 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004908 DAG.getNode(ISD::TRUNCATE, SDLoc(N0), N0.getValueType(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004909 ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004910 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004911 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004912 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004913 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004914
Chris Lattner20a35c32007-04-11 05:32:27 +00004915 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00004916 if (!LegalOperations && VT.isVector()) {
4917 // zext(setcc) -> (and (vsetcc), (1, 1, ...) for vectors.
4918 // Only do this before legalize for now.
4919 EVT N0VT = N0.getOperand(0).getValueType();
4920 EVT EltVT = VT.getVectorElementType();
4921 SmallVector<SDValue,8> OneOps(VT.getVectorNumElements(),
4922 DAG.getConstant(1, EltVT));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004923 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Evan Cheng0a942db2010-05-19 01:08:17 +00004924 // We know that the # elements of the results is the same as the
4925 // # elements of the compare (and the # elements of the compare result
4926 // for that matter). Check to see that they are the same size. If so,
4927 // we know that the element size of the sext'd result matches the
4928 // element size of the compare operands.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004929 return DAG.getNode(ISD::AND, SDLoc(N), VT,
4930 DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Evan Cheng0a942db2010-05-19 01:08:17 +00004931 N0.getOperand(1),
4932 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004933 DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT,
Evan Cheng0a942db2010-05-19 01:08:17 +00004934 &OneOps[0], OneOps.size()));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004935
4936 // If the desired elements are smaller or larger than the source
4937 // elements we can use a matching integer vector type and then
4938 // truncate/sign extend
4939 EVT MatchingElementType =
4940 EVT::getIntegerVT(*DAG.getContext(),
4941 N0VT.getScalarType().getSizeInBits());
4942 EVT MatchingVectorType =
4943 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
4944 N0VT.getVectorNumElements());
4945 SDValue VsetCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00004946 DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0),
Dan Gohman71dc7c92011-05-17 22:20:36 +00004947 N0.getOperand(1),
4948 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004949 return DAG.getNode(ISD::AND, SDLoc(N), VT,
4950 DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT),
4951 DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT,
Dan Gohman71dc7c92011-05-17 22:20:36 +00004952 &OneOps[0], OneOps.size()));
Evan Cheng0a942db2010-05-19 01:08:17 +00004953 }
4954
4955 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00004956 SDValue SCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00004957 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Chris Lattner20a35c32007-04-11 05:32:27 +00004958 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004959 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004960 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00004961 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004962
Evan Cheng9818c042009-12-15 03:00:32 +00004963 // (zext (shl (zext x), cst)) -> (shl (zext x), cst)
Evan Cheng99b653c2009-12-15 00:41:36 +00004964 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL) &&
Evan Cheng9818c042009-12-15 03:00:32 +00004965 isa<ConstantSDNode>(N0.getOperand(1)) &&
Evan Cheng99b653c2009-12-15 00:41:36 +00004966 N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND &&
4967 N0.hasOneUse()) {
Chris Lattnere0751182011-02-13 19:09:16 +00004968 SDValue ShAmt = N0.getOperand(1);
4969 unsigned ShAmtVal = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Evan Cheng9818c042009-12-15 03:00:32 +00004970 if (N0.getOpcode() == ISD::SHL) {
Chris Lattnere0751182011-02-13 19:09:16 +00004971 SDValue InnerZExt = N0.getOperand(0);
Evan Cheng9818c042009-12-15 03:00:32 +00004972 // If the original shl may be shifting out bits, do not perform this
4973 // transformation.
Chris Lattnere0751182011-02-13 19:09:16 +00004974 unsigned KnownZeroBits = InnerZExt.getValueType().getSizeInBits() -
4975 InnerZExt.getOperand(0).getValueType().getSizeInBits();
4976 if (ShAmtVal > KnownZeroBits)
Evan Cheng9818c042009-12-15 03:00:32 +00004977 return SDValue();
4978 }
Chris Lattnere0751182011-02-13 19:09:16 +00004979
Andrew Trickac6d9be2013-05-25 02:42:55 +00004980 SDLoc DL(N);
Owen Anderson95771af2011-02-25 21:41:48 +00004981
4982 // Ensure that the shift amount is wide enough for the shifted value.
Chris Lattnere0751182011-02-13 19:09:16 +00004983 if (VT.getSizeInBits() >= 256)
4984 ShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, ShAmt);
Owen Anderson95771af2011-02-25 21:41:48 +00004985
Chris Lattnere0751182011-02-13 19:09:16 +00004986 return DAG.getNode(N0.getOpcode(), DL, VT,
4987 DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)),
4988 ShAmt);
Evan Cheng99b653c2009-12-15 00:41:36 +00004989 }
4990
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004991 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004992}
4993
Dan Gohman475871a2008-07-27 21:46:04 +00004994SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
4995 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004996 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004997
Chris Lattner5ffc0662006-05-05 05:58:59 +00004998 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00004999 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005000 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, N0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00005001 // fold (aext (aext x)) -> (aext x)
5002 // fold (aext (zext x)) -> (zext x)
5003 // fold (aext (sext x)) -> (sext x)
5004 if (N0.getOpcode() == ISD::ANY_EXTEND ||
5005 N0.getOpcode() == ISD::ZERO_EXTEND ||
5006 N0.getOpcode() == ISD::SIGN_EXTEND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005007 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00005008
Evan Chengc88138f2007-03-22 01:54:19 +00005009 // fold (aext (truncate (load x))) -> (aext (smaller load x))
5010 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
5011 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005012 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
5013 if (NarrowLoad.getNode()) {
Dale Johannesen86234c32010-05-25 18:47:23 +00005014 SDNode* oye = N0.getNode()->getOperand(0).getNode();
5015 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005016 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen86234c32010-05-25 18:47:23 +00005017 // CombineTo deleted the truncate, if needed, but not what's under it.
5018 AddToWorkList(oye);
5019 }
Eli Friedmane545d382011-04-16 23:25:34 +00005020 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00005021 }
Evan Chengc88138f2007-03-22 01:54:19 +00005022 }
5023
Chris Lattner84750582006-09-20 06:29:17 +00005024 // fold (aext (truncate x))
5025 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00005026 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00005027 if (TruncOp.getValueType() == VT)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005028 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005029 if (TruncOp.getValueType().bitsGT(VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005030 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, TruncOp);
5031 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, TruncOp);
Chris Lattner84750582006-09-20 06:29:17 +00005032 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005033
Dan Gohman97121ba2009-04-08 00:15:30 +00005034 // Fold (aext (and (trunc x), cst)) -> (and x, cst)
5035 // if the trunc is not free.
Chris Lattner0e4b9222006-09-21 06:40:43 +00005036 if (N0.getOpcode() == ISD::AND &&
5037 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00005038 N0.getOperand(1).getOpcode() == ISD::Constant &&
5039 !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
5040 N0.getValueType())) {
Dan Gohman475871a2008-07-27 21:46:04 +00005041 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005042 if (X.getValueType().bitsLT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005043 X = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005044 } else if (X.getValueType().bitsGT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005045 X = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, X);
Chris Lattner0e4b9222006-09-21 06:40:43 +00005046 }
Dan Gohman220a8232008-03-03 23:51:38 +00005047 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00005048 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005049 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling683c9572009-01-30 22:27:33 +00005050 X, DAG.getConstant(Mask, VT));
Chris Lattner0e4b9222006-09-21 06:40:43 +00005051 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005052
Chris Lattner5ffc0662006-05-05 05:58:59 +00005053 // fold (aext (load x)) -> (aext (truncate (extload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005054 // None of the supported targets knows how to perform load and any_ext
Nadav Rotemfcd96192011-02-27 07:40:43 +00005055 // on vectors in one instruction. We only perform this transformation on
5056 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005057 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005058 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005059 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00005060 bool DoXform = true;
5061 SmallVector<SDNode*, 4> SetCCs;
5062 if (!N0.hasOneUse())
5063 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI);
5064 if (DoXform) {
5065 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005066 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00005067 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005068 LN0->getBasePtr(), LN0->getPointerInfo(),
Dan Gohman57fc82d2009-04-09 03:51:29 +00005069 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00005070 LN0->isVolatile(), LN0->isNonTemporal(),
5071 LN0->getAlignment());
Dan Gohman57fc82d2009-04-09 03:51:29 +00005072 CombineTo(N, ExtLoad);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005073 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Dan Gohman57fc82d2009-04-09 03:51:29 +00005074 N0.getValueType(), ExtLoad);
5075 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005076 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00005077 ISD::ANY_EXTEND);
Dan Gohman57fc82d2009-04-09 03:51:29 +00005078 return SDValue(N, 0); // Return N so it doesn't get rechecked!
5079 }
Chris Lattner5ffc0662006-05-05 05:58:59 +00005080 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005081
Chris Lattner5ffc0662006-05-05 05:58:59 +00005082 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
5083 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
5084 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00005085 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005086 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00005087 N0.hasOneUse()) {
5088 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005089 EVT MemVT = LN0->getMemoryVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005090 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), SDLoc(N),
Stuart Hastingsa9011292011-02-16 16:23:55 +00005091 VT, LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005092 LN0->getPointerInfo(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00005093 LN0->isVolatile(), LN0->isNonTemporal(),
5094 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00005095 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00005096 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005097 DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling683c9572009-01-30 22:27:33 +00005098 N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00005099 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005100 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00005101 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005102
Chris Lattner20a35c32007-04-11 05:32:27 +00005103 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00005104 // aext(setcc) -> sext_in_reg(vsetcc) for vectors.
5105 // Only do this before legalize for now.
5106 if (VT.isVector() && !LegalOperations) {
5107 EVT N0VT = N0.getOperand(0).getValueType();
5108 // We know that the # elements of the results is the same as the
5109 // # elements of the compare (and the # elements of the compare result
5110 // for that matter). Check to see that they are the same size. If so,
5111 // we know that the element size of the sext'd result matches the
5112 // element size of the compare operands.
5113 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Andrew Trickac6d9be2013-05-25 02:42:55 +00005114 return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00005115 N0.getOperand(1),
5116 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Evan Cheng0a942db2010-05-19 01:08:17 +00005117 // If the desired elements are smaller or larger than the source
5118 // elements we can use a matching integer vector type and then
5119 // truncate/sign extend
5120 else {
Duncan Sands34727662010-07-12 08:16:59 +00005121 EVT MatchingElementType =
5122 EVT::getIntegerVT(*DAG.getContext(),
5123 N0VT.getScalarType().getSizeInBits());
5124 EVT MatchingVectorType =
5125 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
5126 N0VT.getVectorNumElements());
5127 SDValue VsetCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00005128 DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00005129 N0.getOperand(1),
5130 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005131 return DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT);
Evan Cheng0a942db2010-05-19 01:08:17 +00005132 }
5133 }
5134
5135 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00005136 SDValue SCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00005137 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Chris Lattner1eba01e2007-04-11 06:50:51 +00005138 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00005139 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00005140 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005141 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00005142 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005143
Evan Chengb3a3d5e2010-04-28 07:10:39 +00005144 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00005145}
5146
Chris Lattner2b4c2792007-10-13 06:35:54 +00005147/// GetDemandedBits - See if the specified operand can be simplified with the
5148/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00005149/// simpler operand, otherwise return a null SDValue.
5150SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00005151 switch (V.getOpcode()) {
5152 default: break;
Lang Hames5207bf22011-11-08 18:56:23 +00005153 case ISD::Constant: {
5154 const ConstantSDNode *CV = cast<ConstantSDNode>(V.getNode());
5155 assert(CV != 0 && "Const value should be ConstSDNode.");
5156 const APInt &CVal = CV->getAPIntValue();
5157 APInt NewVal = CVal & Mask;
Stephen Linb4940152013-07-09 00:44:49 +00005158 if (NewVal != CVal)
Lang Hames5207bf22011-11-08 18:56:23 +00005159 return DAG.getConstant(NewVal, V.getValueType());
Lang Hames5207bf22011-11-08 18:56:23 +00005160 break;
5161 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00005162 case ISD::OR:
5163 case ISD::XOR:
5164 // If the LHS or RHS don't contribute bits to the or, drop them.
5165 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
5166 return V.getOperand(1);
5167 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
5168 return V.getOperand(0);
5169 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00005170 case ISD::SRL:
5171 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00005172 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00005173 break;
5174 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
5175 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005176 unsigned Amt = RHSC->getZExtValue();
Bill Wendling8509c902009-01-30 22:33:24 +00005177
Dan Gohmancc91d632009-01-03 19:22:06 +00005178 // Watch out for shift count overflow though.
5179 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005180 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00005181 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Bill Wendling8509c902009-01-30 22:33:24 +00005182 if (SimplifyLHS.getNode())
Andrew Trickac6d9be2013-05-25 02:42:55 +00005183 return DAG.getNode(ISD::SRL, SDLoc(V), V.getValueType(),
Chris Lattnere33544c2007-10-13 06:58:48 +00005184 SimplifyLHS, V.getOperand(1));
Chris Lattnere33544c2007-10-13 06:58:48 +00005185 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00005186 }
Dan Gohman475871a2008-07-27 21:46:04 +00005187 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00005188}
5189
Evan Chengc88138f2007-03-22 01:54:19 +00005190/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
5191/// bits and then truncated to a narrower type and where N is a multiple
5192/// of number of bits of the narrower type, transform it to a narrower load
5193/// from address + N / num of bits of new type. If the result is to be
5194/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00005195SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00005196 unsigned Opc = N->getOpcode();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005197
Evan Chengc88138f2007-03-22 01:54:19 +00005198 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00005199 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005200 EVT VT = N->getValueType(0);
5201 EVT ExtVT = VT;
Evan Chengc88138f2007-03-22 01:54:19 +00005202
Dan Gohman7f8613e2008-08-14 20:04:46 +00005203 // This transformation isn't valid for vector loads.
5204 if (VT.isVector())
5205 return SDValue();
5206
Dan Gohmand1996362010-01-09 02:13:55 +00005207 // Special case: SIGN_EXTEND_INREG is basically truncating to ExtVT then
Evan Chenge177e302007-03-23 22:13:36 +00005208 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00005209 if (Opc == ISD::SIGN_EXTEND_INREG) {
5210 ExtType = ISD::SEXTLOAD;
Owen Andersone50ed302009-08-10 22:56:29 +00005211 ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005212 } else if (Opc == ISD::SRL) {
Chris Lattner90b03642010-12-21 18:05:22 +00005213 // Another special-case: SRL is basically zero-extending a narrower value.
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005214 ExtType = ISD::ZEXTLOAD;
5215 N0 = SDValue(N, 0);
5216 ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1));
5217 if (!N01) return SDValue();
5218 ExtVT = EVT::getIntegerVT(*DAG.getContext(),
5219 VT.getSizeInBits() - N01->getZExtValue());
Evan Chengc88138f2007-03-22 01:54:19 +00005220 }
Richard Osborne4e3740e2011-01-31 17:41:44 +00005221 if (LegalOperations && !TLI.isLoadExtLegal(ExtType, ExtVT))
5222 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005223
Owen Andersone50ed302009-08-10 22:56:29 +00005224 unsigned EVTBits = ExtVT.getSizeInBits();
Owen Anderson95771af2011-02-25 21:41:48 +00005225
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005226 // Do not generate loads of non-round integer types since these can
5227 // be expensive (and would be wrong if the type is not byte sized).
5228 if (!ExtVT.isRound())
5229 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005230
Evan Chengc88138f2007-03-22 01:54:19 +00005231 unsigned ShAmt = 0;
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005232 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
Evan Chengc88138f2007-03-22 01:54:19 +00005233 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005234 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005235 // Is the shift amount a multiple of size of VT?
5236 if ((ShAmt & (EVTBits-1)) == 0) {
5237 N0 = N0.getOperand(0);
Eli Friedmand68eea22009-08-19 08:46:10 +00005238 // Is the load width a multiple of size of VT?
5239 if ((N0.getValueType().getSizeInBits() & (EVTBits-1)) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00005240 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005241 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005242
Chris Lattnercbf68df2010-12-22 08:02:57 +00005243 // At this point, we must have a load or else we can't do the transform.
5244 if (!isa<LoadSDNode>(N0)) return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005245
Chandler Carruth1c49fda2012-12-11 00:36:57 +00005246 // Because a SRL must be assumed to *need* to zero-extend the high bits
5247 // (as opposed to anyext the high bits), we can't combine the zextload
5248 // lowering of SRL and an sextload.
5249 if (cast<LoadSDNode>(N0)->getExtensionType() == ISD::SEXTLOAD)
5250 return SDValue();
5251
Chris Lattner2831a192010-10-01 05:36:09 +00005252 // If the shift amount is larger than the input type then we're not
5253 // accessing any of the loaded bytes. If the load was a zextload/extload
5254 // then the result of the shift+trunc is zero/undef (handled elsewhere).
Chris Lattnercbf68df2010-12-22 08:02:57 +00005255 if (ShAmt >= cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits())
Chris Lattner2831a192010-10-01 05:36:09 +00005256 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005257 }
5258 }
5259
Dan Gohman394d6292010-11-03 01:47:46 +00005260 // If the load is shifted left (and the result isn't shifted back right),
5261 // we can fold the truncate through the shift.
5262 unsigned ShLeftAmt = 0;
5263 if (ShAmt == 0 && N0.getOpcode() == ISD::SHL && N0.hasOneUse() &&
Chris Lattner4c32bc22010-12-22 07:36:50 +00005264 ExtVT == VT && TLI.isNarrowingProfitable(N0.getValueType(), VT)) {
Dan Gohman394d6292010-11-03 01:47:46 +00005265 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
5266 ShLeftAmt = N01->getZExtValue();
5267 N0 = N0.getOperand(0);
5268 }
5269 }
Owen Anderson95771af2011-02-25 21:41:48 +00005270
Chris Lattner4c32bc22010-12-22 07:36:50 +00005271 // If we haven't found a load, we can't narrow it. Don't transform one with
5272 // multiple uses, this would require adding a new load.
Bill Schmidt89e88e32013-01-14 22:04:38 +00005273 if (!isa<LoadSDNode>(N0) || !N0.hasOneUse())
5274 return SDValue();
5275
5276 // Don't change the width of a volatile load.
5277 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
5278 if (LN0->isVolatile())
Chris Lattner4c32bc22010-12-22 07:36:50 +00005279 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005280
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005281 // Verify that we are actually reducing a load width here.
Bill Schmidt89e88e32013-01-14 22:04:38 +00005282 if (LN0->getMemoryVT().getSizeInBits() < EVTBits)
Chris Lattner4c32bc22010-12-22 07:36:50 +00005283 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005284
Bill Schmidt89e88e32013-01-14 22:04:38 +00005285 // For the transform to be legal, the load must produce only two values
5286 // (the value loaded and the chain). Don't transform a pre-increment
Stephen Lin155615d2013-07-08 00:37:03 +00005287 // load, for example, which produces an extra value. Otherwise the
Bill Schmidt89e88e32013-01-14 22:04:38 +00005288 // transformation is not equivalent, and the downstream logic to replace
5289 // uses gets things wrong.
5290 if (LN0->getNumValues() > 2)
5291 return SDValue();
5292
Benjamin Kramerf4eeab42013-07-06 14:05:09 +00005293 // If the load that we're shrinking is an extload and we're not just
5294 // discarding the extension we can't simply shrink the load. Bail.
5295 // TODO: It would be possible to merge the extensions in some cases.
5296 if (LN0->getExtensionType() != ISD::NON_EXTLOAD &&
5297 LN0->getMemoryVT().getSizeInBits() < ExtVT.getSizeInBits() + ShAmt)
5298 return SDValue();
5299
Chris Lattner4c32bc22010-12-22 07:36:50 +00005300 EVT PtrType = N0.getOperand(1).getValueType();
Bill Wendling8509c902009-01-30 22:33:24 +00005301
Evan Cheng16436df2012-06-26 01:19:33 +00005302 if (PtrType == MVT::Untyped || PtrType.isExtended())
5303 // It's not possible to generate a constant of extended or untyped type.
5304 return SDValue();
5305
Chris Lattner4c32bc22010-12-22 07:36:50 +00005306 // For big endian targets, we need to adjust the offset to the pointer to
5307 // load the correct bytes.
5308 if (TLI.isBigEndian()) {
5309 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
5310 unsigned EVTStoreBits = ExtVT.getStoreSizeInBits();
5311 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
Evan Chengc88138f2007-03-22 01:54:19 +00005312 }
5313
Chris Lattner4c32bc22010-12-22 07:36:50 +00005314 uint64_t PtrOff = ShAmt / 8;
5315 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005316 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(LN0),
Chris Lattner4c32bc22010-12-22 07:36:50 +00005317 PtrType, LN0->getBasePtr(),
5318 DAG.getConstant(PtrOff, PtrType));
5319 AddToWorkList(NewPtr.getNode());
5320
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005321 SDValue Load;
5322 if (ExtType == ISD::NON_EXTLOAD)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005323 Load = DAG.getLoad(VT, SDLoc(N0), LN0->getChain(), NewPtr,
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005324 LN0->getPointerInfo().getWithOffset(PtrOff),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005325 LN0->isVolatile(), LN0->isNonTemporal(),
5326 LN0->isInvariant(), NewAlign);
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005327 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00005328 Load = DAG.getExtLoad(ExtType, SDLoc(N0), VT, LN0->getChain(),NewPtr,
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005329 LN0->getPointerInfo().getWithOffset(PtrOff),
5330 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
5331 NewAlign);
Chris Lattner4c32bc22010-12-22 07:36:50 +00005332
5333 // Replace the old load's chain with the new load's chain.
5334 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005335 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1));
Chris Lattner4c32bc22010-12-22 07:36:50 +00005336
5337 // Shift the result left, if we've swallowed a left shift.
5338 SDValue Result = Load;
5339 if (ShLeftAmt != 0) {
Owen Anderson95771af2011-02-25 21:41:48 +00005340 EVT ShImmTy = getShiftAmountTy(Result.getValueType());
Chris Lattner4c32bc22010-12-22 07:36:50 +00005341 if (!isUIntN(ShImmTy.getSizeInBits(), ShLeftAmt))
5342 ShImmTy = VT;
Paul Redmond5c974502013-02-12 15:21:21 +00005343 // If the shift amount is as large as the result size (but, presumably,
5344 // no larger than the source) then the useful bits of the result are
5345 // zero; we can't simply return the shortened shift, because the result
5346 // of that operation is undefined.
5347 if (ShLeftAmt >= VT.getSizeInBits())
5348 Result = DAG.getConstant(0, VT);
5349 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00005350 Result = DAG.getNode(ISD::SHL, SDLoc(N0), VT,
Paul Redmond5c974502013-02-12 15:21:21 +00005351 Result, DAG.getConstant(ShLeftAmt, ShImmTy));
Chris Lattner4c32bc22010-12-22 07:36:50 +00005352 }
5353
5354 // Return the new loaded value.
5355 return Result;
Evan Chengc88138f2007-03-22 01:54:19 +00005356}
5357
Dan Gohman475871a2008-07-27 21:46:04 +00005358SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
5359 SDValue N0 = N->getOperand(0);
5360 SDValue N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005361 EVT VT = N->getValueType(0);
5362 EVT EVT = cast<VTSDNode>(N1)->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00005363 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmand1996362010-01-09 02:13:55 +00005364 unsigned EVTBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00005365
Nate Begeman1d4d4142005-09-01 00:19:25 +00005366 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00005367 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005368 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005369
Chris Lattner541a24f2006-05-06 22:43:44 +00005370 // If the input is already sign extended, just drop the extension.
Dan Gohman87862e72009-12-11 21:31:27 +00005371 if (DAG.ComputeNumSignBits(N0) >= VTBits-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00005372 return N0;
Scott Michelfdc40a02009-02-17 22:15:04 +00005373
Nate Begeman646d7e22005-09-02 21:18:40 +00005374 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
5375 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Stephen Linb4940152013-07-09 00:44:49 +00005376 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005377 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005378 N0.getOperand(0), N1);
Chris Lattner4b37e872006-05-08 21:18:59 +00005379
Dan Gohman75dcf082008-07-31 00:50:31 +00005380 // fold (sext_in_reg (sext x)) -> (sext x)
5381 // fold (sext_in_reg (aext x)) -> (sext x)
5382 // if x is small enough.
5383 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
5384 SDValue N00 = N0.getOperand(0);
Evan Cheng003d7c42010-04-16 22:26:19 +00005385 if (N00.getValueType().getScalarType().getSizeInBits() <= EVTBits &&
5386 (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005387 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N00, N1);
Dan Gohman75dcf082008-07-31 00:50:31 +00005388 }
5389
Chris Lattner95a5e052007-04-17 19:03:21 +00005390 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005391 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005392 return DAG.getZeroExtendInReg(N0, SDLoc(N), EVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00005393
Chris Lattner95a5e052007-04-17 19:03:21 +00005394 // fold operands of sext_in_reg based on knowledge that the top bits are not
5395 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00005396 if (SimplifyDemandedBits(SDValue(N, 0)))
5397 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005398
Evan Chengc88138f2007-03-22 01:54:19 +00005399 // fold (sext_in_reg (load x)) -> (smaller sextload x)
5400 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00005401 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005402 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00005403 return NarrowLoad;
5404
Bill Wendling8509c902009-01-30 22:33:24 +00005405 // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005406 // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible.
Chris Lattner4b37e872006-05-08 21:18:59 +00005407 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
5408 if (N0.getOpcode() == ISD::SRL) {
5409 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman87862e72009-12-11 21:31:27 +00005410 if (ShAmt->getZExtValue()+EVTBits <= VTBits) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005411 // We can turn this into an SRA iff the input to the SRL is already sign
Chris Lattner4b37e872006-05-08 21:18:59 +00005412 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00005413 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohman87862e72009-12-11 21:31:27 +00005414 if (VTBits-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005415 return DAG.getNode(ISD::SRA, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005416 N0.getOperand(0), N0.getOperand(1));
Chris Lattner4b37e872006-05-08 21:18:59 +00005417 }
5418 }
Evan Chengc88138f2007-03-22 01:54:19 +00005419
Nate Begemanded49632005-10-13 03:11:28 +00005420 // fold (sext_inreg (extload x)) -> (sextload x)
Scott Michelfdc40a02009-02-17 22:15:04 +00005421 if (ISD::isEXTLoad(N0.getNode()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005422 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005423 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005424 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005425 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005426 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005427 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005428 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005429 LN0->getBasePtr(), LN0->getPointerInfo(),
5430 EVT,
David Greene1e559442010-02-15 17:00:31 +00005431 LN0->isVolatile(), LN0->isNonTemporal(),
5432 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00005433 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00005434 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Elena Demikhovsky4b977312012-12-19 07:50:20 +00005435 AddToWorkList(ExtLoad.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00005436 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00005437 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005438 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00005439 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00005440 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005441 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005442 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005443 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005444 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005445 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005446 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005447 LN0->getBasePtr(), LN0->getPointerInfo(),
5448 EVT,
David Greene1e559442010-02-15 17:00:31 +00005449 LN0->isVolatile(), LN0->isNonTemporal(),
5450 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00005451 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00005452 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005453 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00005454 }
Evan Cheng9568e5c2011-06-21 06:01:08 +00005455
5456 // Form (sext_inreg (bswap >> 16)) or (sext_inreg (rotl (bswap) 16))
5457 if (EVTBits <= 16 && N0.getOpcode() == ISD::OR) {
5458 SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0),
5459 N0.getOperand(1), false);
5460 if (BSwap.getNode() != 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005461 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Evan Cheng9568e5c2011-06-21 06:01:08 +00005462 BSwap, N1);
5463 }
5464
Dan Gohman475871a2008-07-27 21:46:04 +00005465 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005466}
5467
Dan Gohman475871a2008-07-27 21:46:04 +00005468SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
5469 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005470 EVT VT = N->getValueType(0);
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005471 bool isLE = TLI.isLittleEndian();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005472
5473 // noop truncate
5474 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00005475 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00005476 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00005477 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005478 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005479 // fold (truncate (truncate x)) -> (truncate x)
5480 if (N0.getOpcode() == ISD::TRUNCATE)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005481 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00005482 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattner7f893c02010-04-07 18:13:33 +00005483 if (N0.getOpcode() == ISD::ZERO_EXTEND ||
5484 N0.getOpcode() == ISD::SIGN_EXTEND ||
Chris Lattnerb72773b2006-05-05 22:56:26 +00005485 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00005486 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00005487 // if the source is smaller than the dest, we still need an extend
Andrew Trickac6d9be2013-05-25 02:42:55 +00005488 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005489 N0.getOperand(0));
Craig Topper0eb5dad2012-09-29 07:18:53 +00005490 if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00005491 // if the source is larger than the dest, than we just need the truncate
Andrew Trickac6d9be2013-05-25 02:42:55 +00005492 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0));
Craig Topper0eb5dad2012-09-29 07:18:53 +00005493 // if the source and dest are the same type, we can drop both the extend
5494 // and the truncate.
5495 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005496 }
Evan Cheng007b69e2007-03-21 20:14:05 +00005497
Nadav Rotemcc870a82012-02-05 11:39:23 +00005498 // Fold extract-and-trunc into a narrow extract. For example:
5499 // i64 x = EXTRACT_VECTOR_ELT(v2i64 val, i32 1)
5500 // i32 y = TRUNCATE(i64 x)
5501 // -- becomes --
5502 // v16i8 b = BITCAST (v2i64 val)
5503 // i8 x = EXTRACT_VECTOR_ELT(v16i8 b, i32 8)
5504 //
5505 // Note: We only run this optimization after type legalization (which often
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005506 // creates this pattern) and before operation legalization after which
5507 // we need to be more careful about the vector instructions that we generate.
5508 if (N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5509 LegalTypes && !LegalOperations && N0->hasOneUse()) {
5510
5511 EVT VecTy = N0.getOperand(0).getValueType();
5512 EVT ExTy = N0.getValueType();
5513 EVT TrTy = N->getValueType(0);
5514
5515 unsigned NumElem = VecTy.getVectorNumElements();
5516 unsigned SizeRatio = ExTy.getSizeInBits()/TrTy.getSizeInBits();
5517
5518 EVT NVT = EVT::getVectorVT(*DAG.getContext(), TrTy, SizeRatio * NumElem);
5519 assert(NVT.getSizeInBits() == VecTy.getSizeInBits() && "Invalid Size");
5520
5521 SDValue EltNo = N0->getOperand(1);
5522 if (isa<ConstantSDNode>(EltNo) && isTypeLegal(NVT)) {
5523 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Tom Stellard425b76c2013-08-05 22:22:01 +00005524 EVT IndexTy = TLI.getVectorIdxTy();
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005525 int Index = isLE ? (Elt*SizeRatio) : (Elt*SizeRatio + (SizeRatio-1));
5526
Andrew Trickac6d9be2013-05-25 02:42:55 +00005527 SDValue V = DAG.getNode(ISD::BITCAST, SDLoc(N),
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005528 NVT, N0.getOperand(0));
5529
5530 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005531 SDLoc(N), TrTy, V,
Jim Grosbacha249f7d2012-05-08 20:56:07 +00005532 DAG.getConstant(Index, IndexTy));
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005533 }
5534 }
5535
Arnold Schwaighoferc46e2df2013-02-20 21:33:32 +00005536 // Fold a series of buildvector, bitcast, and truncate if possible.
5537 // For example fold
5538 // (2xi32 trunc (bitcast ((4xi32)buildvector x, x, y, y) 2xi64)) to
5539 // (2xi32 (buildvector x, y)).
5540 if (Level == AfterLegalizeVectorOps && VT.isVector() &&
5541 N0.getOpcode() == ISD::BITCAST && N0.hasOneUse() &&
5542 N0.getOperand(0).getOpcode() == ISD::BUILD_VECTOR &&
5543 N0.getOperand(0).hasOneUse()) {
5544
5545 SDValue BuildVect = N0.getOperand(0);
5546 EVT BuildVectEltTy = BuildVect.getValueType().getVectorElementType();
5547 EVT TruncVecEltTy = VT.getVectorElementType();
5548
5549 // Check that the element types match.
5550 if (BuildVectEltTy == TruncVecEltTy) {
5551 // Now we only need to compute the offset of the truncated elements.
5552 unsigned BuildVecNumElts = BuildVect.getNumOperands();
5553 unsigned TruncVecNumElts = VT.getVectorNumElements();
5554 unsigned TruncEltOffset = BuildVecNumElts / TruncVecNumElts;
5555
5556 assert((BuildVecNumElts % TruncVecNumElts) == 0 &&
5557 "Invalid number of elements");
5558
5559 SmallVector<SDValue, 8> Opnds;
5560 for (unsigned i = 0, e = BuildVecNumElts; i != e; i += TruncEltOffset)
5561 Opnds.push_back(BuildVect.getOperand(i));
5562
Andrew Trickac6d9be2013-05-25 02:42:55 +00005563 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT, &Opnds[0],
Arnold Schwaighoferc46e2df2013-02-20 21:33:32 +00005564 Opnds.size());
5565 }
5566 }
5567
Chris Lattner2b4c2792007-10-13 06:35:54 +00005568 // See if we can simplify the input to this truncate through knowledge that
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005569 // only the low bits are being used.
5570 // For example "trunc (or (shl x, 8), y)" // -> trunc y
Nadav Rotemfcd96192011-02-27 07:40:43 +00005571 // Currently we only perform this optimization on scalars because vectors
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005572 // may have different active low bits.
5573 if (!VT.isVector()) {
5574 SDValue Shorter =
5575 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
5576 VT.getSizeInBits()));
5577 if (Shorter.getNode())
Andrew Trickac6d9be2013-05-25 02:42:55 +00005578 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Shorter);
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005579 }
Nate Begeman3df4d522005-10-12 20:40:40 +00005580 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00005581 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005582 if (!LegalTypes || TLI.isTypeDesirableForOp(N0.getOpcode(), VT)) {
5583 SDValue Reduced = ReduceLoadWidth(N);
5584 if (Reduced.getNode())
5585 return Reduced;
5586 }
Michael Liao07edaf32012-10-17 23:45:54 +00005587 // fold (trunc (concat ... x ...)) -> (concat ..., (trunc x), ...)),
5588 // where ... are all 'undef'.
5589 if (N0.getOpcode() == ISD::CONCAT_VECTORS && !LegalTypes) {
5590 SmallVector<EVT, 8> VTs;
5591 SDValue V;
5592 unsigned Idx = 0;
5593 unsigned NumDefs = 0;
5594
5595 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) {
5596 SDValue X = N0.getOperand(i);
5597 if (X.getOpcode() != ISD::UNDEF) {
5598 V = X;
5599 Idx = i;
5600 NumDefs++;
5601 }
5602 // Stop if more than one members are non-undef.
5603 if (NumDefs > 1)
5604 break;
5605 VTs.push_back(EVT::getVectorVT(*DAG.getContext(),
5606 VT.getVectorElementType(),
5607 X.getValueType().getVectorNumElements()));
5608 }
5609
5610 if (NumDefs == 0)
5611 return DAG.getUNDEF(VT);
5612
5613 if (NumDefs == 1) {
5614 assert(V.getNode() && "The single defined operand is empty!");
5615 SmallVector<SDValue, 8> Opnds;
5616 for (unsigned i = 0, e = VTs.size(); i != e; ++i) {
5617 if (i != Idx) {
5618 Opnds.push_back(DAG.getUNDEF(VTs[i]));
5619 continue;
5620 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005621 SDValue NV = DAG.getNode(ISD::TRUNCATE, SDLoc(V), VTs[i], V);
Michael Liao07edaf32012-10-17 23:45:54 +00005622 AddToWorkList(NV.getNode());
5623 Opnds.push_back(NV);
5624 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005625 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Michael Liao07edaf32012-10-17 23:45:54 +00005626 &Opnds[0], Opnds.size());
5627 }
5628 }
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005629
5630 // Simplify the operands using demanded-bits information.
5631 if (!VT.isVector() &&
5632 SimplifyDemandedBits(SDValue(N, 0)))
5633 return SDValue(N, 0);
5634
Evan Chenge5b51ac2010-04-17 06:13:15 +00005635 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005636}
5637
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005638static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005639 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005640 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00005641 return Elt.getNode();
5642 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005643}
5644
5645/// CombineConsecutiveLoads - build_pair (load, load) -> load
Scott Michelfdc40a02009-02-17 22:15:04 +00005646/// if load locations are consecutive.
Owen Andersone50ed302009-08-10 22:56:29 +00005647SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005648 assert(N->getOpcode() == ISD::BUILD_PAIR);
5649
Nate Begemanabc01992009-06-05 21:37:30 +00005650 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0));
5651 LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1));
Chris Lattnerfa459012010-09-21 16:08:50 +00005652 if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse() ||
5653 LD1->getPointerInfo().getAddrSpace() !=
5654 LD2->getPointerInfo().getAddrSpace())
Dan Gohman475871a2008-07-27 21:46:04 +00005655 return SDValue();
Owen Andersone50ed302009-08-10 22:56:29 +00005656 EVT LD1VT = LD1->getValueType(0);
Bill Wendling67a67682009-01-30 22:44:24 +00005657
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005658 if (ISD::isNON_EXTLoad(LD2) &&
5659 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005660 // If both are volatile this would reduce the number of volatile loads.
5661 // If one is volatile it might be ok, but play conservative and bail out.
Nate Begemanabc01992009-06-05 21:37:30 +00005662 !LD1->isVolatile() &&
5663 !LD2->isVolatile() &&
Evan Cheng64fa4a92009-12-09 01:36:00 +00005664 DAG.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1)) {
Nate Begemanabc01992009-06-05 21:37:30 +00005665 unsigned Align = LD1->getAlignment();
Micah Villmow3574eca2012-10-08 16:38:25 +00005666 unsigned NewAlign = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005667 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Bill Wendling67a67682009-01-30 22:44:24 +00005668
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005669 if (NewAlign <= Align &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005670 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005671 return DAG.getLoad(VT, SDLoc(N), LD1->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005672 LD1->getBasePtr(), LD1->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005673 false, false, false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005674 }
Bill Wendling67a67682009-01-30 22:44:24 +00005675
Dan Gohman475871a2008-07-27 21:46:04 +00005676 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005677}
5678
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005679SDValue DAGCombiner::visitBITCAST(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00005680 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005681 EVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00005682
Dan Gohman7f321562007-06-25 16:23:39 +00005683 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
5684 // Only do this before legalize, since afterward the target may be depending
5685 // on the bitconvert.
5686 // First check to see if this is all constant.
Duncan Sands25cf2272008-11-24 14:53:14 +00005687 if (!LegalTypes &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005688 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005689 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00005690 bool isSimple = true;
5691 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
5692 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
5693 N0.getOperand(i).getOpcode() != ISD::Constant &&
5694 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005695 isSimple = false;
Dan Gohman7f321562007-06-25 16:23:39 +00005696 break;
5697 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005698
Owen Andersone50ed302009-08-10 22:56:29 +00005699 EVT DestEltVT = N->getValueType(0).getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005700 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00005701 "Element type of vector ValueType must not be vector!");
Bill Wendling67a67682009-01-30 22:44:24 +00005702 if (isSimple)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005703 return ConstantFoldBITCASTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00005704 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005705
Dan Gohman3dd168d2008-09-05 01:58:21 +00005706 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00005707 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005708 SDValue Res = DAG.getNode(ISD::BITCAST, SDLoc(N), VT, N0);
Dan Gohmana407ca12009-08-10 23:15:10 +00005709 if (Res.getNode() != N) {
5710 if (!LegalOperations ||
5711 TLI.isOperationLegal(Res.getNode()->getOpcode(), VT))
5712 return Res;
5713
5714 // Folding it resulted in an illegal node, and it's too late to
5715 // do that. Clean up the old node and forego the transformation.
5716 // Ideally this won't happen very often, because instcombine
5717 // and the earlier dagcombine runs (where illegal nodes are
5718 // permitted) should have folded most of them already.
5719 DAG.DeleteNode(Res.getNode());
5720 }
Chris Lattner94683772005-12-23 05:30:37 +00005721 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005722
Bill Wendling67a67682009-01-30 22:44:24 +00005723 // (conv (conv x, t1), t2) -> (conv x, t2)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005724 if (N0.getOpcode() == ISD::BITCAST)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005725 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005726 N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00005727
Chris Lattner57104102005-12-23 05:44:41 +00005728 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00005729 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00005730 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005731 // Do not change the width of a volatile load.
5732 !cast<LoadSDNode>(N0)->isVolatile() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005733 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005734 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Micah Villmow3574eca2012-10-08 16:38:25 +00005735 unsigned Align = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005736 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Evan Cheng59d5b682007-05-07 21:27:48 +00005737 unsigned OrigAlign = LN0->getAlignment();
Bill Wendling67a67682009-01-30 22:44:24 +00005738
Evan Cheng59d5b682007-05-07 21:27:48 +00005739 if (Align <= OrigAlign) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005740 SDValue Load = DAG.getLoad(VT, SDLoc(N), LN0->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005741 LN0->getBasePtr(), LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00005742 LN0->isVolatile(), LN0->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005743 LN0->isInvariant(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00005744 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00005745 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005746 DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling67a67682009-01-30 22:44:24 +00005747 N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00005748 Load.getValue(1));
5749 return Load;
5750 }
Chris Lattner57104102005-12-23 05:44:41 +00005751 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005752
Bill Wendling67a67682009-01-30 22:44:24 +00005753 // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
5754 // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
Chris Lattner3bd39d42008-01-27 17:42:27 +00005755 // This often reduces constant pool loads.
Tom Stellard1f67c632013-07-23 23:55:03 +00005756 if (((N0.getOpcode() == ISD::FNEG && !TLI.isFNegFree(N0.getValueType())) ||
5757 (N0.getOpcode() == ISD::FABS && !TLI.isFAbsFree(N0.getValueType()))) &&
Nadav Rotem91a7e012012-09-13 14:54:28 +00005758 N0.getNode()->hasOneUse() && VT.isInteger() &&
5759 !VT.isVector() && !N0.getValueType().isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005760 SDValue NewConv = DAG.getNode(ISD::BITCAST, SDLoc(N0), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005761 N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00005762 AddToWorkList(NewConv.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005763
Duncan Sands83ec4b62008-06-06 12:08:01 +00005764 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005765 if (N0.getOpcode() == ISD::FNEG)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005766 return DAG.getNode(ISD::XOR, SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005767 NewConv, DAG.getConstant(SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005768 assert(N0.getOpcode() == ISD::FABS);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005769 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005770 NewConv, DAG.getConstant(~SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005771 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005772
Bill Wendling67a67682009-01-30 22:44:24 +00005773 // fold (bitconvert (fcopysign cst, x)) ->
5774 // (or (and (bitconvert x), sign), (and cst, (not sign)))
5775 // Note that we don't handle (copysign x, cst) because this can always be
5776 // folded to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00005777 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00005778 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005779 VT.isInteger() && !VT.isVector()) {
5780 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Owen Anderson23b9b192009-08-12 00:36:31 +00005781 EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth);
Chris Lattner2392ae72010-04-15 04:48:01 +00005782 if (isTypeLegal(IntXVT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005783 SDValue X = DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling67a67682009-01-30 22:44:24 +00005784 IntXVT, N0.getOperand(1));
Duncan Sands25cf2272008-11-24 14:53:14 +00005785 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005786
Duncan Sands25cf2272008-11-24 14:53:14 +00005787 // If X has a different width than the result/lhs, sext it or truncate it.
5788 unsigned VTWidth = VT.getSizeInBits();
5789 if (OrigXWidth < VTWidth) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005790 X = DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005791 AddToWorkList(X.getNode());
5792 } else if (OrigXWidth > VTWidth) {
5793 // To get the sign bit in the right place, we have to shift it right
5794 // before truncating.
Andrew Trickac6d9be2013-05-25 02:42:55 +00005795 X = DAG.getNode(ISD::SRL, SDLoc(X),
Bill Wendling67a67682009-01-30 22:44:24 +00005796 X.getValueType(), X,
Duncan Sands25cf2272008-11-24 14:53:14 +00005797 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
5798 AddToWorkList(X.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005799 X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005800 AddToWorkList(X.getNode());
5801 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005802
Duncan Sands25cf2272008-11-24 14:53:14 +00005803 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005804 X = DAG.getNode(ISD::AND, SDLoc(X), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005805 X, DAG.getConstant(SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005806 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005807
Andrew Trickac6d9be2013-05-25 02:42:55 +00005808 SDValue Cst = DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling67a67682009-01-30 22:44:24 +00005809 VT, N0.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005810 Cst = DAG.getNode(ISD::AND, SDLoc(Cst), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005811 Cst, DAG.getConstant(~SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005812 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005813
Andrew Trickac6d9be2013-05-25 02:42:55 +00005814 return DAG.getNode(ISD::OR, SDLoc(N), VT, X, Cst);
Duncan Sands25cf2272008-11-24 14:53:14 +00005815 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00005816 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005817
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005818 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005819 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005820 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
5821 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005822 return CombineLD;
5823 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005824
Dan Gohman475871a2008-07-27 21:46:04 +00005825 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00005826}
5827
Dan Gohman475871a2008-07-27 21:46:04 +00005828SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00005829 EVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005830 return CombineConsecutiveLoads(N, VT);
5831}
5832
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005833/// ConstantFoldBITCASTofBUILD_VECTOR - We know that BV is a build_vector
Scott Michelfdc40a02009-02-17 22:15:04 +00005834/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
Chris Lattner6258fb22006-04-02 02:53:43 +00005835/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00005836SDValue DAGCombiner::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005837ConstantFoldBITCASTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) {
Owen Andersone50ed302009-08-10 22:56:29 +00005838 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005839
Chris Lattner6258fb22006-04-02 02:53:43 +00005840 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00005841 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005842
Duncan Sands83ec4b62008-06-06 12:08:01 +00005843 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
5844 unsigned DstBitSize = DstEltVT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00005845
Chris Lattner6258fb22006-04-02 02:53:43 +00005846 // If this is a conversion of N elements of one type to N elements of another
5847 // type, convert each element. This handles FP<->INT cases.
5848 if (SrcBitSize == DstBitSize) {
Nate Begemane0efc212010-07-27 18:02:18 +00005849 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5850 BV->getValueType(0).getVectorNumElements());
5851
5852 // Due to the FP element handling below calling this routine recursively,
5853 // we can end up with a scalar-to-vector node here.
5854 if (BV->getOpcode() == ISD::SCALAR_TO_VECTOR)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005855 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(BV), VT,
5856 DAG.getNode(ISD::BITCAST, SDLoc(BV),
Nate Begemane0efc212010-07-27 18:02:18 +00005857 DstEltVT, BV->getOperand(0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005858
Dan Gohman475871a2008-07-27 21:46:04 +00005859 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005860 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Bob Wilsonb1303d02009-04-13 22:05:19 +00005861 SDValue Op = BV->getOperand(i);
5862 // If the vector element type is not legal, the BUILD_VECTOR operands
5863 // are promoted and implicitly truncated. Make that explicit here.
Bob Wilsonc8851652009-04-20 17:27:09 +00005864 if (Op.getValueType() != SrcEltVT)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005865 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(BV), SrcEltVT, Op);
5866 Ops.push_back(DAG.getNode(ISD::BITCAST, SDLoc(BV),
Bob Wilsonb1303d02009-04-13 22:05:19 +00005867 DstEltVT, Op));
Gabor Greifba36cb52008-08-28 21:40:38 +00005868 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00005869 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005870 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga87008d2009-02-25 22:49:59 +00005871 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005872 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005873
Chris Lattner6258fb22006-04-02 02:53:43 +00005874 // Otherwise, we're growing or shrinking the elements. To avoid having to
5875 // handle annoying details of growing/shrinking FP values, we convert them to
5876 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005877 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005878 // Convert the input float vector to a int vector where the elements are the
5879 // same sizes.
Owen Anderson825b72b2009-08-11 20:47:22 +00005880 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005881 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005882 BV = ConstantFoldBITCASTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00005883 SrcEltVT = IntVT;
5884 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005885
Chris Lattner6258fb22006-04-02 02:53:43 +00005886 // Now we know the input is an integer vector. If the output is a FP type,
5887 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005888 if (DstEltVT.isFloatingPoint()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005889 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005890 EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005891 SDNode *Tmp = ConstantFoldBITCASTofBUILD_VECTOR(BV, TmpVT).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005892
Chris Lattner6258fb22006-04-02 02:53:43 +00005893 // Next, convert to FP elements of the same size.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005894 return ConstantFoldBITCASTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00005895 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005896
Chris Lattner6258fb22006-04-02 02:53:43 +00005897 // Okay, we know the src/dst types are both integers of differing types.
5898 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005899 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00005900 if (SrcBitSize < DstBitSize) {
5901 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
Scott Michelfdc40a02009-02-17 22:15:04 +00005902
Dan Gohman475871a2008-07-27 21:46:04 +00005903 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005904 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00005905 i += NumInputsPerOutput) {
5906 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00005907 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00005908 bool EltIsUndef = true;
5909 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
5910 // Shift the previously computed bits over.
5911 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00005912 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00005913 if (Op.getOpcode() == ISD::UNDEF) continue;
5914 EltIsUndef = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005915
Jay Foad40f8f622010-12-07 08:25:19 +00005916 NewBits |= cast<ConstantSDNode>(Op)->getAPIntValue().
Dan Gohman58c25872010-04-12 02:24:01 +00005917 zextOrTrunc(SrcBitSize).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005918 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005919
Chris Lattner6258fb22006-04-02 02:53:43 +00005920 if (EltIsUndef)
Dale Johannesene8d72302009-02-06 23:05:02 +00005921 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005922 else
5923 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
5924 }
5925
Owen Anderson23b9b192009-08-12 00:36:31 +00005926 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005927 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga87008d2009-02-25 22:49:59 +00005928 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005929 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005930
Chris Lattner6258fb22006-04-02 02:53:43 +00005931 // Finally, this must be the case where we are shrinking elements: each input
5932 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00005933 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00005934 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Owen Anderson23b9b192009-08-12 00:36:31 +00005935 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5936 NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00005937 SmallVector<SDValue, 8> Ops;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005938
Dan Gohman7f321562007-06-25 16:23:39 +00005939 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005940 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
5941 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
Dale Johannesene8d72302009-02-06 23:05:02 +00005942 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005943 continue;
5944 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005945
Jay Foad40f8f622010-12-07 08:25:19 +00005946 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->
5947 getAPIntValue().zextOrTrunc(SrcBitSize);
Bill Wendlingb0162f52009-01-30 22:53:48 +00005948
Chris Lattner6258fb22006-04-02 02:53:43 +00005949 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Jay Foad40f8f622010-12-07 08:25:19 +00005950 APInt ThisVal = OpVal.trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005951 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Jay Foad40f8f622010-12-07 08:25:19 +00005952 if (isS2V && i == 0 && j == 0 && ThisVal.zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00005953 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
Andrew Trickac6d9be2013-05-25 02:42:55 +00005954 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(BV), VT,
Bill Wendlingb0162f52009-01-30 22:53:48 +00005955 Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00005956 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005957 }
5958
5959 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00005960 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00005961 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
5962 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005963
Andrew Trickac6d9be2013-05-25 02:42:55 +00005964 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga87008d2009-02-25 22:49:59 +00005965 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005966}
5967
Dan Gohman475871a2008-07-27 21:46:04 +00005968SDValue DAGCombiner::visitFADD(SDNode *N) {
5969 SDValue N0 = N->getOperand(0);
5970 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005971 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
5972 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00005973 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005974
Dan Gohman7f321562007-06-25 16:23:39 +00005975 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00005976 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005977 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005978 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00005979 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005980
Lang Hames01806942012-06-14 20:37:15 +00005981 // fold (fadd c1, c2) -> c1 + c2
Ulrich Weigande669c932012-10-29 18:35:49 +00005982 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005983 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005984 // canonicalize constant to RHS
5985 if (N0CFP && !N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005986 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N0);
Bill Wendlingb0162f52009-01-30 22:53:48 +00005987 // fold (fadd A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005988 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
5989 N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00005990 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005991 // fold (fadd A, (fneg B)) -> (fsub A, B)
Owen Andersonafd3d562012-03-06 00:29:31 +00005992 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00005993 isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005994 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00005995 GetNegatedExpression(N1, DAG, LegalOperations));
Bill Wendlingb0162f52009-01-30 22:53:48 +00005996 // fold (fadd (fneg A), B) -> (fsub B, A)
Owen Andersonafd3d562012-03-06 00:29:31 +00005997 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00005998 isNegatibleForFree(N0, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005999 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N1,
Duncan Sands25cf2272008-11-24 14:53:14 +00006000 GetNegatedExpression(N0, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00006001
Chris Lattnerddae4bd2007-01-08 23:04:05 +00006002 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006003 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6004 N0.getOpcode() == ISD::FADD && N0.getNode()->hasOneUse() &&
6005 isa<ConstantFPSDNode>(N0.getOperand(1)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006006 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0.getOperand(0),
6007 DAG.getNode(ISD::FADD, SDLoc(N), VT,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00006008 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006009
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006010 // No FP constant should be created after legalization as Instruction
6011 // Selection pass has hard time in dealing with FP constant.
6012 //
6013 // We don't need test this condition for transformation like following, as
6014 // the DAG being transformed implies it is legal to take FP constant as
6015 // operand.
Stephen Lin155615d2013-07-08 00:37:03 +00006016 //
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006017 // (fadd (fmul c, x), x) -> (fmul c+1, x)
Stephen Lin155615d2013-07-08 00:37:03 +00006018 //
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006019 bool AllowNewFpConst = (Level < AfterLegalizeDAG);
6020
Owen Anderson607ebde2012-11-01 02:00:53 +00006021 // If allow, fold (fadd (fneg x), x) -> 0.0
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006022 if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath &&
Stephen Linb4940152013-07-09 00:44:49 +00006023 N0.getOpcode() == ISD::FNEG && N0.getOperand(0) == N1)
Owen Anderson607ebde2012-11-01 02:00:53 +00006024 return DAG.getConstantFP(0.0, VT);
Owen Anderson607ebde2012-11-01 02:00:53 +00006025
6026 // If allow, fold (fadd x, (fneg x)) -> 0.0
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006027 if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath &&
Stephen Linb4940152013-07-09 00:44:49 +00006028 N1.getOpcode() == ISD::FNEG && N1.getOperand(0) == N0)
Owen Anderson607ebde2012-11-01 02:00:53 +00006029 return DAG.getConstantFP(0.0, VT);
Owen Anderson607ebde2012-11-01 02:00:53 +00006030
Owen Anderson43da6c72012-08-30 23:35:16 +00006031 // In unsafe math mode, we can fold chains of FADD's of the same value
6032 // into multiplications. This transform is not safe in general because
6033 // we are reducing the number of rounding steps.
6034 if (DAG.getTarget().Options.UnsafeFPMath &&
6035 TLI.isOperationLegalOrCustom(ISD::FMUL, VT) &&
6036 !N0CFP && !N1CFP) {
6037 if (N0.getOpcode() == ISD::FMUL) {
6038 ConstantFPSDNode *CFP00 = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
6039 ConstantFPSDNode *CFP01 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
6040
Stephen Lin38103d12013-06-14 18:17:35 +00006041 // (fadd (fmul c, x), x) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00006042 if (CFP00 && !CFP01 && N0.getOperand(1) == N1) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006043 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006044 SDValue(CFP00, 0),
6045 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006046 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006047 N1, NewCFP);
6048 }
6049
Stephen Lin38103d12013-06-14 18:17:35 +00006050 // (fadd (fmul x, c), x) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00006051 if (CFP01 && !CFP00 && N0.getOperand(0) == N1) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006052 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006053 SDValue(CFP01, 0),
6054 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006055 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006056 N1, NewCFP);
6057 }
6058
Stephen Lin38103d12013-06-14 18:17:35 +00006059 // (fadd (fmul c, x), (fadd x, x)) -> (fmul x, c+2)
Owen Anderson43da6c72012-08-30 23:35:16 +00006060 if (CFP00 && !CFP01 && N1.getOpcode() == ISD::FADD &&
6061 N1.getOperand(0) == N1.getOperand(1) &&
6062 N0.getOperand(1) == N1.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006063 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006064 SDValue(CFP00, 0),
6065 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006066 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006067 N0.getOperand(1), NewCFP);
6068 }
6069
Stephen Lin38103d12013-06-14 18:17:35 +00006070 // (fadd (fmul x, c), (fadd x, x)) -> (fmul x, c+2)
Owen Anderson43da6c72012-08-30 23:35:16 +00006071 if (CFP01 && !CFP00 && N1.getOpcode() == ISD::FADD &&
6072 N1.getOperand(0) == N1.getOperand(1) &&
6073 N0.getOperand(0) == N1.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006074 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006075 SDValue(CFP01, 0),
6076 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006077 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006078 N0.getOperand(0), NewCFP);
6079 }
6080 }
6081
6082 if (N1.getOpcode() == ISD::FMUL) {
6083 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
6084 ConstantFPSDNode *CFP11 = dyn_cast<ConstantFPSDNode>(N1.getOperand(1));
6085
Stephen Lin38103d12013-06-14 18:17:35 +00006086 // (fadd x, (fmul c, x)) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00006087 if (CFP10 && !CFP11 && N1.getOperand(1) == N0) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006088 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006089 SDValue(CFP10, 0),
6090 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006091 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006092 N0, NewCFP);
6093 }
6094
Stephen Lin38103d12013-06-14 18:17:35 +00006095 // (fadd x, (fmul x, c)) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00006096 if (CFP11 && !CFP10 && N1.getOperand(0) == N0) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006097 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006098 SDValue(CFP11, 0),
6099 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006100 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006101 N0, NewCFP);
6102 }
6103
Owen Anderson43da6c72012-08-30 23:35:16 +00006104
Stephen Lin38103d12013-06-14 18:17:35 +00006105 // (fadd (fadd x, x), (fmul c, x)) -> (fmul x, c+2)
6106 if (CFP10 && !CFP11 && N0.getOpcode() == ISD::FADD &&
6107 N0.getOperand(0) == N0.getOperand(1) &&
6108 N1.getOperand(1) == N0.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006109 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006110 SDValue(CFP10, 0),
6111 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006112 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Stephen Lin38103d12013-06-14 18:17:35 +00006113 N1.getOperand(1), NewCFP);
Owen Anderson43da6c72012-08-30 23:35:16 +00006114 }
6115
Stephen Lin38103d12013-06-14 18:17:35 +00006116 // (fadd (fadd x, x), (fmul x, c)) -> (fmul x, c+2)
6117 if (CFP11 && !CFP10 && N0.getOpcode() == ISD::FADD &&
6118 N0.getOperand(0) == N0.getOperand(1) &&
6119 N1.getOperand(0) == N0.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006120 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006121 SDValue(CFP11, 0),
6122 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006123 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Stephen Lin38103d12013-06-14 18:17:35 +00006124 N1.getOperand(0), NewCFP);
Owen Anderson43da6c72012-08-30 23:35:16 +00006125 }
6126 }
6127
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006128 if (N0.getOpcode() == ISD::FADD && AllowNewFpConst) {
Shuxin Yang98b93e52013-02-02 00:22:03 +00006129 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
Stephen Lina553bed2013-06-14 21:33:58 +00006130 // (fadd (fadd x, x), x) -> (fmul x, 3.0)
Shuxin Yang98b93e52013-02-02 00:22:03 +00006131 if (!CFP && N0.getOperand(0) == N0.getOperand(1) &&
Stephen Linb4940152013-07-09 00:44:49 +00006132 (N0.getOperand(0) == N1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006133 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Shuxin Yang98b93e52013-02-02 00:22:03 +00006134 N1, DAG.getConstantFP(3.0, VT));
Shuxin Yang98b93e52013-02-02 00:22:03 +00006135 }
6136
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006137 if (N1.getOpcode() == ISD::FADD && AllowNewFpConst) {
Shuxin Yang98b93e52013-02-02 00:22:03 +00006138 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
Stephen Lina553bed2013-06-14 21:33:58 +00006139 // (fadd x, (fadd x, x)) -> (fmul x, 3.0)
Shuxin Yang98b93e52013-02-02 00:22:03 +00006140 if (!CFP10 && N1.getOperand(0) == N1.getOperand(1) &&
Stephen Linb4940152013-07-09 00:44:49 +00006141 N1.getOperand(0) == N0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006142 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Shuxin Yang98b93e52013-02-02 00:22:03 +00006143 N0, DAG.getConstantFP(3.0, VT));
Shuxin Yang98b93e52013-02-02 00:22:03 +00006144 }
6145
Stephen Lina553bed2013-06-14 21:33:58 +00006146 // (fadd (fadd x, x), (fadd x, x)) -> (fmul x, 4.0)
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006147 if (AllowNewFpConst &&
6148 N0.getOpcode() == ISD::FADD && N1.getOpcode() == ISD::FADD &&
Owen Anderson43da6c72012-08-30 23:35:16 +00006149 N0.getOperand(0) == N0.getOperand(1) &&
6150 N1.getOperand(0) == N1.getOperand(1) &&
Stephen Linb4940152013-07-09 00:44:49 +00006151 N0.getOperand(0) == N1.getOperand(0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006152 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006153 N0.getOperand(0),
6154 DAG.getConstantFP(4.0, VT));
Owen Anderson43da6c72012-08-30 23:35:16 +00006155 }
6156
Lang Hamesd693caf2012-06-19 22:51:23 +00006157 // FADD -> FMA combines:
Lang Hamese0231412012-06-22 01:09:09 +00006158 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hamesd693caf2012-06-19 22:51:23 +00006159 DAG.getTarget().Options.UnsafeFPMath) &&
Stephen Line54885a2013-07-09 18:16:56 +00006160 DAG.getTarget().getTargetLowering()->isFMAFasterThanFMulAndFAdd(VT) &&
6161 (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FMA, VT))) {
Lang Hamesd693caf2012-06-19 22:51:23 +00006162
6163 // fold (fadd (fmul x, y), z) -> (fma x, y, z)
Stephen Linb4940152013-07-09 00:44:49 +00006164 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse())
Andrew Trickac6d9be2013-05-25 02:42:55 +00006165 return DAG.getNode(ISD::FMA, SDLoc(N), VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006166 N0.getOperand(0), N0.getOperand(1), N1);
Owen Anderson43da6c72012-08-30 23:35:16 +00006167
Michael Liaob79bff52012-09-01 04:09:16 +00006168 // fold (fadd x, (fmul y, z)) -> (fma y, z, x)
Lang Hamesd693caf2012-06-19 22:51:23 +00006169 // Note: Commutes FADD operands.
Stephen Linb4940152013-07-09 00:44:49 +00006170 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse())
Andrew Trickac6d9be2013-05-25 02:42:55 +00006171 return DAG.getNode(ISD::FMA, SDLoc(N), VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006172 N1.getOperand(0), N1.getOperand(1), N0);
Lang Hamesd693caf2012-06-19 22:51:23 +00006173 }
6174
Dan Gohman475871a2008-07-27 21:46:04 +00006175 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006176}
6177
Dan Gohman475871a2008-07-27 21:46:04 +00006178SDValue DAGCombiner::visitFSUB(SDNode *N) {
6179 SDValue N0 = N->getOperand(0);
6180 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00006181 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6182 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006183 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006184 SDLoc dl(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00006185
Dan Gohman7f321562007-06-25 16:23:39 +00006186 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006187 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006188 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006189 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006190 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006191
Nate Begemana0e221d2005-10-18 00:28:13 +00006192 // fold (fsub c1, c2) -> c1-c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006193 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006194 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N0, N1);
Bill Wendlingb0162f52009-01-30 22:53:48 +00006195 // fold (fsub A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006196 if (DAG.getTarget().Options.UnsafeFPMath &&
6197 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohmana90c8e62009-01-23 19:10:37 +00006198 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00006199 // fold (fsub 0, B) -> -B
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006200 if (DAG.getTarget().Options.UnsafeFPMath &&
6201 N0CFP && N0CFP->getValueAPF().isZero()) {
Owen Andersonafd3d562012-03-06 00:29:31 +00006202 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00006203 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman760f86f2009-01-22 21:58:43 +00006204 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006205 return DAG.getNode(ISD::FNEG, dl, VT, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00006206 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00006207 // fold (fsub A, (fneg B)) -> (fadd A, B)
Owen Andersonafd3d562012-03-06 00:29:31 +00006208 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006209 return DAG.getNode(ISD::FADD, dl, VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00006210 GetNegatedExpression(N1, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00006211
Bill Wendling5a894342012-03-15 05:12:00 +00006212 // If 'unsafe math' is enabled, fold
Owen Anderson713e9532012-05-07 20:51:25 +00006213 // (fsub x, x) -> 0.0 &
Bill Wendling5a894342012-03-15 05:12:00 +00006214 // (fsub x, (fadd x, y)) -> (fneg y) &
6215 // (fsub x, (fadd y, x)) -> (fneg y)
6216 if (DAG.getTarget().Options.UnsafeFPMath) {
Owen Anderson713e9532012-05-07 20:51:25 +00006217 if (N0 == N1)
6218 return DAG.getConstantFP(0.0f, VT);
6219
Bill Wendling5a894342012-03-15 05:12:00 +00006220 if (N1.getOpcode() == ISD::FADD) {
6221 SDValue N10 = N1->getOperand(0);
6222 SDValue N11 = N1->getOperand(1);
6223
6224 if (N10 == N0 && isNegatibleForFree(N11, LegalOperations, TLI,
6225 &DAG.getTarget().Options))
6226 return GetNegatedExpression(N11, DAG, LegalOperations);
Stephen Lin75d13062013-07-10 20:47:39 +00006227
Stephen Linb4940152013-07-09 00:44:49 +00006228 if (N11 == N0 && isNegatibleForFree(N10, LegalOperations, TLI,
6229 &DAG.getTarget().Options))
Bill Wendling5a894342012-03-15 05:12:00 +00006230 return GetNegatedExpression(N10, DAG, LegalOperations);
6231 }
6232 }
6233
Lang Hamesd693caf2012-06-19 22:51:23 +00006234 // FSUB -> FMA combines:
Lang Hamese0231412012-06-22 01:09:09 +00006235 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hamesd693caf2012-06-19 22:51:23 +00006236 DAG.getTarget().Options.UnsafeFPMath) &&
Stephen Line54885a2013-07-09 18:16:56 +00006237 DAG.getTarget().getTargetLowering()->isFMAFasterThanFMulAndFAdd(VT) &&
6238 (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FMA, VT))) {
Lang Hamesd693caf2012-06-19 22:51:23 +00006239
6240 // fold (fsub (fmul x, y), z) -> (fma x, y, (fneg z))
Stephen Linb4940152013-07-09 00:44:49 +00006241 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse())
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006242 return DAG.getNode(ISD::FMA, dl, VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006243 N0.getOperand(0), N0.getOperand(1),
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006244 DAG.getNode(ISD::FNEG, dl, VT, N1));
Lang Hamesd693caf2012-06-19 22:51:23 +00006245
6246 // fold (fsub x, (fmul y, z)) -> (fma (fneg y), z, x)
6247 // Note: Commutes FSUB operands.
Stephen Lin75d13062013-07-10 20:47:39 +00006248 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse())
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006249 return DAG.getNode(ISD::FMA, dl, VT,
6250 DAG.getNode(ISD::FNEG, dl, VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006251 N1.getOperand(0)),
6252 N1.getOperand(1), N0);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006253
Stephen Linb4940152013-07-09 00:44:49 +00006254 // fold (fsub (fneg (fmul, x, y)), z) -> (fma (fneg x), y, (fneg z))
Stephen Lin155615d2013-07-08 00:37:03 +00006255 if (N0.getOpcode() == ISD::FNEG &&
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006256 N0.getOperand(0).getOpcode() == ISD::FMUL &&
6257 N0->hasOneUse() && N0.getOperand(0).hasOneUse()) {
6258 SDValue N00 = N0.getOperand(0).getOperand(0);
6259 SDValue N01 = N0.getOperand(0).getOperand(1);
6260 return DAG.getNode(ISD::FMA, dl, VT,
6261 DAG.getNode(ISD::FNEG, dl, VT, N00), N01,
6262 DAG.getNode(ISD::FNEG, dl, VT, N1));
6263 }
Lang Hamesd693caf2012-06-19 22:51:23 +00006264 }
6265
Dan Gohman475871a2008-07-27 21:46:04 +00006266 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006267}
6268
Dan Gohman475871a2008-07-27 21:46:04 +00006269SDValue DAGCombiner::visitFMUL(SDNode *N) {
6270 SDValue N0 = N->getOperand(0);
6271 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00006272 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6273 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006274 EVT VT = N->getValueType(0);
Owen Andersonafd3d562012-03-06 00:29:31 +00006275 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner01b3d732005-09-28 22:28:18 +00006276
Dan Gohman7f321562007-06-25 16:23:39 +00006277 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006278 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006279 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006280 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006281 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006282
Nate Begeman11af4ea2005-10-17 20:40:11 +00006283 // fold (fmul c1, c2) -> c1*c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006284 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006285 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00006286 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00006287 if (N0CFP && !N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006288 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N1, N0);
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006289 // fold (fmul A, 0) -> 0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006290 if (DAG.getTarget().Options.UnsafeFPMath &&
6291 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00006292 return N1;
Dan Gohman77b81fe2009-06-04 17:12:12 +00006293 // fold (fmul A, 0) -> 0, vector edition.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006294 if (DAG.getTarget().Options.UnsafeFPMath &&
6295 ISD::isBuildVectorAllZeros(N1.getNode()))
Dan Gohman77b81fe2009-06-04 17:12:12 +00006296 return N1;
Owen Anderson363e4b92012-05-02 21:32:35 +00006297 // fold (fmul A, 1.0) -> A
6298 if (N1CFP && N1CFP->isExactlyValue(1.0))
6299 return N0;
Nate Begeman11af4ea2005-10-17 20:40:11 +00006300 // fold (fmul X, 2.0) -> (fadd X, X)
6301 if (N1CFP && N1CFP->isExactlyValue(+2.0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006302 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N0);
Dan Gohmaneb1fedc2009-08-10 16:50:32 +00006303 // fold (fmul X, -1.0) -> (fneg X)
Chris Lattner29446522007-05-14 22:04:50 +00006304 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman760f86f2009-01-22 21:58:43 +00006305 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006306 return DAG.getNode(ISD::FNEG, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006307
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006308 // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y)
Owen Andersonafd3d562012-03-06 00:29:31 +00006309 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006310 &DAG.getTarget().Options)) {
Stephen Lin155615d2013-07-08 00:37:03 +00006311 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006312 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00006313 // Both can be negated for free, check to see if at least one is cheaper
6314 // negated.
6315 if (LHSNeg == 2 || RHSNeg == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006316 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00006317 GetNegatedExpression(N0, DAG, LegalOperations),
6318 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00006319 }
6320 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006321
Chris Lattnerddae4bd2007-01-08 23:04:05 +00006322 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006323 if (DAG.getTarget().Options.UnsafeFPMath &&
6324 N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006325 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006326 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0.getOperand(0),
6327 DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Dale Johannesende064702009-02-06 21:50:26 +00006328 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006329
Dan Gohman475871a2008-07-27 21:46:04 +00006330 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006331}
6332
Owen Anderson062c0a52012-05-02 22:17:40 +00006333SDValue DAGCombiner::visitFMA(SDNode *N) {
6334 SDValue N0 = N->getOperand(0);
6335 SDValue N1 = N->getOperand(1);
6336 SDValue N2 = N->getOperand(2);
6337 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6338 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
6339 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006340 SDLoc dl(N);
Owen Anderson062c0a52012-05-02 22:17:40 +00006341
Owen Anderson607ebde2012-11-01 02:00:53 +00006342 if (DAG.getTarget().Options.UnsafeFPMath) {
6343 if (N0CFP && N0CFP->isZero())
6344 return N2;
6345 if (N1CFP && N1CFP->isZero())
6346 return N2;
6347 }
Owen Anderson062c0a52012-05-02 22:17:40 +00006348 if (N0CFP && N0CFP->isExactlyValue(1.0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006349 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N2);
Owen Anderson062c0a52012-05-02 22:17:40 +00006350 if (N1CFP && N1CFP->isExactlyValue(1.0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006351 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N2);
Owen Anderson062c0a52012-05-02 22:17:40 +00006352
Owen Anderson85ef6f42012-05-30 18:50:39 +00006353 // Canonicalize (fma c, x, y) -> (fma x, c, y)
Owen Andersonf917d202012-05-30 18:54:50 +00006354 if (N0CFP && !N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006355 return DAG.getNode(ISD::FMA, SDLoc(N), VT, N1, N0, N2);
Owen Anderson85ef6f42012-05-30 18:50:39 +00006356
Owen Anderson58d57292012-09-01 06:04:27 +00006357 // (fma x, c1, (fmul x, c2)) -> (fmul x, c1+c2)
6358 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6359 N2.getOpcode() == ISD::FMUL &&
6360 N0 == N2.getOperand(0) &&
6361 N2.getOperand(1).getOpcode() == ISD::ConstantFP) {
6362 return DAG.getNode(ISD::FMUL, dl, VT, N0,
6363 DAG.getNode(ISD::FADD, dl, VT, N1, N2.getOperand(1)));
6364 }
6365
6366
6367 // (fma (fmul x, c1), c2, y) -> (fma x, c1*c2, y)
6368 if (DAG.getTarget().Options.UnsafeFPMath &&
6369 N0.getOpcode() == ISD::FMUL && N1CFP &&
6370 N0.getOperand(1).getOpcode() == ISD::ConstantFP) {
6371 return DAG.getNode(ISD::FMA, dl, VT,
6372 N0.getOperand(0),
6373 DAG.getNode(ISD::FMUL, dl, VT, N1, N0.getOperand(1)),
6374 N2);
6375 }
6376
6377 // (fma x, 1, y) -> (fadd x, y)
6378 // (fma x, -1, y) -> (fadd (fneg x), y)
6379 if (N1CFP) {
6380 if (N1CFP->isExactlyValue(1.0))
6381 return DAG.getNode(ISD::FADD, dl, VT, N0, N2);
6382
6383 if (N1CFP->isExactlyValue(-1.0) &&
6384 (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))) {
6385 SDValue RHSNeg = DAG.getNode(ISD::FNEG, dl, VT, N0);
6386 AddToWorkList(RHSNeg.getNode());
6387 return DAG.getNode(ISD::FADD, dl, VT, N2, RHSNeg);
6388 }
6389 }
6390
6391 // (fma x, c, x) -> (fmul x, (c+1))
Stephen Linb4940152013-07-09 00:44:49 +00006392 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && N0 == N2)
6393 return DAG.getNode(ISD::FMUL, dl, VT, N0,
Owen Anderson58d57292012-09-01 06:04:27 +00006394 DAG.getNode(ISD::FADD, dl, VT,
6395 N1, DAG.getConstantFP(1.0, VT)));
Owen Anderson58d57292012-09-01 06:04:27 +00006396
6397 // (fma x, c, (fneg x)) -> (fmul x, (c-1))
6398 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
Stephen Linb4940152013-07-09 00:44:49 +00006399 N2.getOpcode() == ISD::FNEG && N2.getOperand(0) == N0)
6400 return DAG.getNode(ISD::FMUL, dl, VT, N0,
Owen Anderson58d57292012-09-01 06:04:27 +00006401 DAG.getNode(ISD::FADD, dl, VT,
6402 N1, DAG.getConstantFP(-1.0, VT)));
Owen Anderson58d57292012-09-01 06:04:27 +00006403
6404
Owen Anderson062c0a52012-05-02 22:17:40 +00006405 return SDValue();
6406}
6407
Dan Gohman475871a2008-07-27 21:46:04 +00006408SDValue DAGCombiner::visitFDIV(SDNode *N) {
6409 SDValue N0 = N->getOperand(0);
6410 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006411 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6412 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006413 EVT VT = N->getValueType(0);
Owen Andersonafd3d562012-03-06 00:29:31 +00006414 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner01b3d732005-09-28 22:28:18 +00006415
Dan Gohman7f321562007-06-25 16:23:39 +00006416 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006417 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006418 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006419 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006420 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006421
Nate Begemana148d982006-01-18 22:35:16 +00006422 // fold (fdiv c1, c2) -> c1/c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006423 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006424 return DAG.getNode(ISD::FDIV, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006425
Duncan Sands3ef3fcf2012-04-08 18:08:12 +00006426 // fold (fdiv X, c2) -> fmul X, 1/c2 if losing precision is acceptable.
Ulrich Weigande669c932012-10-29 18:35:49 +00006427 if (N1CFP && DAG.getTarget().Options.UnsafeFPMath) {
Duncan Sands961d6662012-04-07 20:04:00 +00006428 // Compute the reciprocal 1.0 / c2.
6429 APFloat N1APF = N1CFP->getValueAPF();
6430 APFloat Recip(N1APF.getSemantics(), 1); // 1.0
6431 APFloat::opStatus st = Recip.divide(N1APF, APFloat::rmNearestTiesToEven);
Duncan Sands507bb7a2012-04-10 20:35:27 +00006432 // Only do the transform if the reciprocal is a legal fp immediate that
6433 // isn't too nasty (eg NaN, denormal, ...).
6434 if ((st == APFloat::opOK || st == APFloat::opInexact) && // Not too nasty
Anton Korobeynikov999821c2012-04-10 13:22:49 +00006435 (!LegalOperations ||
6436 // FIXME: custom lowering of ConstantFP might fail (see e.g. ARM
6437 // backend)... we should handle this gracefully after Legalize.
6438 // TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT) ||
6439 TLI.isOperationLegal(llvm::ISD::ConstantFP, VT) ||
6440 TLI.isFPImmLegal(Recip, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006441 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0,
Duncan Sands961d6662012-04-07 20:04:00 +00006442 DAG.getConstantFP(Recip, VT));
6443 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006444
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006445 // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y)
Owen Andersonafd3d562012-03-06 00:29:31 +00006446 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006447 &DAG.getTarget().Options)) {
Owen Andersonafd3d562012-03-06 00:29:31 +00006448 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006449 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00006450 // Both can be negated for free, check to see if at least one is cheaper
6451 // negated.
6452 if (LHSNeg == 2 || RHSNeg == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006453 return DAG.getNode(ISD::FDIV, SDLoc(N), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00006454 GetNegatedExpression(N0, DAG, LegalOperations),
6455 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00006456 }
6457 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006458
Dan Gohman475871a2008-07-27 21:46:04 +00006459 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006460}
6461
Dan Gohman475871a2008-07-27 21:46:04 +00006462SDValue DAGCombiner::visitFREM(SDNode *N) {
6463 SDValue N0 = N->getOperand(0);
6464 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006465 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6466 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006467 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00006468
Nate Begemana148d982006-01-18 22:35:16 +00006469 // fold (frem c1, c2) -> fmod(c1,c2)
Ulrich Weigande669c932012-10-29 18:35:49 +00006470 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006471 return DAG.getNode(ISD::FREM, SDLoc(N), VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00006472
Dan Gohman475871a2008-07-27 21:46:04 +00006473 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006474}
6475
Dan Gohman475871a2008-07-27 21:46:04 +00006476SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
6477 SDValue N0 = N->getOperand(0);
6478 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00006479 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6480 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006481 EVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00006482
Ulrich Weigande669c932012-10-29 18:35:49 +00006483 if (N0CFP && N1CFP) // Constant fold
Andrew Trickac6d9be2013-05-25 02:42:55 +00006484 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006485
Chris Lattner12d83032006-03-05 05:30:57 +00006486 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00006487 const APFloat& V = N1CFP->getValueAPF();
Sylvestre Ledru94c22712012-09-27 10:14:43 +00006488 // copysign(x, c1) -> fabs(x) iff ispos(c1)
6489 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman760f86f2009-01-22 21:58:43 +00006490 if (!V.isNegative()) {
6491 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006492 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Dan Gohman760f86f2009-01-22 21:58:43 +00006493 } else {
6494 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006495 return DAG.getNode(ISD::FNEG, SDLoc(N), VT,
6496 DAG.getNode(ISD::FABS, SDLoc(N0), VT, N0));
Dan Gohman760f86f2009-01-22 21:58:43 +00006497 }
Chris Lattner12d83032006-03-05 05:30:57 +00006498 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006499
Chris Lattner12d83032006-03-05 05:30:57 +00006500 // copysign(fabs(x), y) -> copysign(x, y)
6501 // copysign(fneg(x), y) -> copysign(x, y)
6502 // copysign(copysign(x,z), y) -> copysign(x, y)
6503 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
6504 N0.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006505 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006506 N0.getOperand(0), N1);
Chris Lattner12d83032006-03-05 05:30:57 +00006507
6508 // copysign(x, abs(y)) -> abs(x)
6509 if (N1.getOpcode() == ISD::FABS)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006510 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006511
Chris Lattner12d83032006-03-05 05:30:57 +00006512 // copysign(x, copysign(y,z)) -> copysign(x, z)
6513 if (N1.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006514 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006515 N0, N1.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006516
Chris Lattner12d83032006-03-05 05:30:57 +00006517 // copysign(x, fp_extend(y)) -> copysign(x, y)
6518 // copysign(x, fp_round(y)) -> copysign(x, y)
6519 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006520 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006521 N0, N1.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00006522
Dan Gohman475871a2008-07-27 21:46:04 +00006523 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00006524}
6525
Dan Gohman475871a2008-07-27 21:46:04 +00006526SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
6527 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00006528 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006529 EVT VT = N->getValueType(0);
6530 EVT OpVT = N0.getValueType();
Chris Lattnercda88752008-06-26 00:16:49 +00006531
Nate Begeman1d4d4142005-09-01 00:19:25 +00006532 // fold (sint_to_fp c1) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006533 if (N0C &&
Stuart Hastings7e334182011-03-02 19:36:30 +00006534 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00006535 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00006536 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006537 return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006538
Chris Lattnercda88752008-06-26 00:16:49 +00006539 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
6540 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006541 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
6542 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006543 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00006544 if (DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006545 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00006546 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00006547
Nadav Rotemed1a3352012-07-23 07:59:50 +00006548 // The next optimizations are desireable only if SELECT_CC can be lowered.
6549 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6550 // having to say they don't support SELECT_CC on every type the DAG knows
6551 // about, since there is no way to mark an opcode illegal at all value types
6552 // (See also visitSELECT)
6553 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6554 // fold (sint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
6555 if (N0.getOpcode() == ISD::SETCC && N0.getValueType() == MVT::i1 &&
6556 !VT.isVector() &&
6557 (!LegalOperations ||
6558 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6559 SDValue Ops[] =
6560 { N0.getOperand(0), N0.getOperand(1),
6561 DAG.getConstantFP(-1.0, VT) , DAG.getConstantFP(0.0, VT),
6562 N0.getOperand(2) };
Andrew Trickac6d9be2013-05-25 02:42:55 +00006563 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotemed1a3352012-07-23 07:59:50 +00006564 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006565
Nadav Rotemed1a3352012-07-23 07:59:50 +00006566 // fold (sint_to_fp (zext (setcc x, y, cc))) ->
6567 // (select_cc x, y, 1.0, 0.0,, cc)
6568 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
6569 N0.getOperand(0).getOpcode() == ISD::SETCC &&!VT.isVector() &&
6570 (!LegalOperations ||
6571 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6572 SDValue Ops[] =
6573 { N0.getOperand(0).getOperand(0), N0.getOperand(0).getOperand(1),
6574 DAG.getConstantFP(1.0, VT) , DAG.getConstantFP(0.0, VT),
6575 N0.getOperand(0).getOperand(2) };
Andrew Trickac6d9be2013-05-25 02:42:55 +00006576 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotemed1a3352012-07-23 07:59:50 +00006577 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006578 }
6579
Dan Gohman475871a2008-07-27 21:46:04 +00006580 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006581}
6582
Dan Gohman475871a2008-07-27 21:46:04 +00006583SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
6584 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00006585 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006586 EVT VT = N->getValueType(0);
6587 EVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00006588
Nate Begeman1d4d4142005-09-01 00:19:25 +00006589 // fold (uint_to_fp c1) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006590 if (N0C &&
Stuart Hastings7e334182011-03-02 19:36:30 +00006591 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00006592 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00006593 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006594 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006595
Chris Lattnercda88752008-06-26 00:16:49 +00006596 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
6597 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006598 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
6599 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006600 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00006601 if (DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006602 return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00006603 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006604
Nadav Rotemed1a3352012-07-23 07:59:50 +00006605 // The next optimizations are desireable only if SELECT_CC can be lowered.
6606 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6607 // having to say they don't support SELECT_CC on every type the DAG knows
6608 // about, since there is no way to mark an opcode illegal at all value types
6609 // (See also visitSELECT)
6610 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6611 // fold (uint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
Owen Andersond9bf71f2012-07-09 20:31:12 +00006612
Nadav Rotemed1a3352012-07-23 07:59:50 +00006613 if (N0.getOpcode() == ISD::SETCC && !VT.isVector() &&
6614 (!LegalOperations ||
6615 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6616 SDValue Ops[] =
6617 { N0.getOperand(0), N0.getOperand(1),
6618 DAG.getConstantFP(1.0, VT), DAG.getConstantFP(0.0, VT),
6619 N0.getOperand(2) };
Andrew Trickac6d9be2013-05-25 02:42:55 +00006620 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotemed1a3352012-07-23 07:59:50 +00006621 }
6622 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006623
Dan Gohman475871a2008-07-27 21:46:04 +00006624 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006625}
6626
Dan Gohman475871a2008-07-27 21:46:04 +00006627SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
6628 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006629 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006630 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006631
Nate Begeman1d4d4142005-09-01 00:19:25 +00006632 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00006633 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006634 return DAG.getNode(ISD::FP_TO_SINT, SDLoc(N), VT, N0);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006635
Dan Gohman475871a2008-07-27 21:46:04 +00006636 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006637}
6638
Dan Gohman475871a2008-07-27 21:46:04 +00006639SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
6640 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006641 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006642 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006643
Nate Begeman1d4d4142005-09-01 00:19:25 +00006644 // fold (fp_to_uint c1fp) -> c1
Ulrich Weigande669c932012-10-29 18:35:49 +00006645 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006646 return DAG.getNode(ISD::FP_TO_UINT, SDLoc(N), VT, N0);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006647
Dan Gohman475871a2008-07-27 21:46:04 +00006648 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006649}
6650
Dan Gohman475871a2008-07-27 21:46:04 +00006651SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
6652 SDValue N0 = N->getOperand(0);
6653 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006654 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006655 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006656
Nate Begeman1d4d4142005-09-01 00:19:25 +00006657 // fold (fp_round c1fp) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006658 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006659 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006660
Chris Lattner79dbea52006-03-13 06:26:26 +00006661 // fold (fp_round (fp_extend x)) -> x
6662 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
6663 return N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006664
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00006665 // fold (fp_round (fp_round x)) -> (fp_round x)
6666 if (N0.getOpcode() == ISD::FP_ROUND) {
6667 // This is a value preserving truncation if both round's are.
6668 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006669 N0.getNode()->getConstantOperandVal(1) == 1;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006670 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0.getOperand(0),
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00006671 DAG.getIntPtrConstant(IsTrunc));
6672 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006673
Chris Lattner79dbea52006-03-13 06:26:26 +00006674 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00006675 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006676 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, SDLoc(N0), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006677 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00006678 AddToWorkList(Tmp.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006679 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006680 Tmp, N0.getOperand(1));
Chris Lattner79dbea52006-03-13 06:26:26 +00006681 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006682
Dan Gohman475871a2008-07-27 21:46:04 +00006683 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006684}
6685
Dan Gohman475871a2008-07-27 21:46:04 +00006686SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
6687 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006688 EVT VT = N->getValueType(0);
6689 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00006690 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006691
Nate Begeman1d4d4142005-09-01 00:19:25 +00006692 // fold (fp_round_inreg c1fp) -> c1fp
Chris Lattner2392ae72010-04-15 04:48:01 +00006693 if (N0CFP && isTypeLegal(EVT)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00006694 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006695 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006696 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00006697
Dan Gohman475871a2008-07-27 21:46:04 +00006698 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006699}
6700
Dan Gohman475871a2008-07-27 21:46:04 +00006701SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
6702 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006703 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006704 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006705
Chris Lattner5938bef2007-12-29 06:55:23 +00006706 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Scott Michelfdc40a02009-02-17 22:15:04 +00006707 if (N->hasOneUse() &&
Dan Gohmane7852d02009-01-26 04:35:06 +00006708 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman475871a2008-07-27 21:46:04 +00006709 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00006710
Nate Begeman1d4d4142005-09-01 00:19:25 +00006711 // fold (fp_extend c1fp) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006712 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006713 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00006714
6715 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
6716 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00006717 if (N0.getOpcode() == ISD::FP_ROUND
6718 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00006719 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00006720 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00006721 if (VT.bitsLT(In.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006722 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006723 In, N0.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006724 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, In);
Chris Lattner0bd48932008-01-17 07:00:52 +00006725 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006726
Chris Lattner0bd48932008-01-17 07:00:52 +00006727 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00006728 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00006729 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00006730 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00006731 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006732 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006733 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00006734 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00006735 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00006736 LN0->isVolatile(), LN0->isNonTemporal(),
6737 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00006738 CombineTo(N, ExtLoad);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006739 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00006740 DAG.getNode(ISD::FP_ROUND, SDLoc(N0),
Bill Wendling0225a1d2009-01-30 23:15:49 +00006741 N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00006742 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00006743 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00006744 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00006745
Dan Gohman475871a2008-07-27 21:46:04 +00006746 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006747}
6748
Dan Gohman475871a2008-07-27 21:46:04 +00006749SDValue DAGCombiner::visitFNEG(SDNode *N) {
6750 SDValue N0 = N->getOperand(0);
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006751 EVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00006752
Craig Topperdd201ff2012-09-11 01:45:21 +00006753 if (VT.isVector()) {
6754 SDValue FoldedVOp = SimplifyVUnaryOp(N);
6755 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper956342b2012-09-09 22:58:45 +00006756 }
6757
Owen Andersonafd3d562012-03-06 00:29:31 +00006758 if (isNegatibleForFree(N0, LegalOperations, DAG.getTargetLoweringInfo(),
6759 &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00006760 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohman23ff1822007-07-02 15:48:56 +00006761
Chris Lattner3bd39d42008-01-27 17:42:27 +00006762 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
6763 // constant pool values.
Owen Anderson29f60f32012-04-02 22:10:29 +00006764 if (!TLI.isFNegFree(VT) && N0.getOpcode() == ISD::BITCAST &&
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006765 !VT.isVector() &&
6766 N0.getNode()->hasOneUse() &&
6767 N0.getOperand(0).getValueType().isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006768 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006769 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006770 if (IntVT.isInteger() && !IntVT.isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006771 Int = DAG.getNode(ISD::XOR, SDLoc(N0), IntVT, Int,
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006772 DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00006773 AddToWorkList(Int.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006774 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006775 VT, Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00006776 }
6777 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006778
Owen Anderson58d57292012-09-01 06:04:27 +00006779 // (fneg (fmul c, x)) -> (fmul -c, x)
6780 if (N0.getOpcode() == ISD::FMUL) {
6781 ConstantFPSDNode *CFP1 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
Stephen Linb4940152013-07-09 00:44:49 +00006782 if (CFP1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006783 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson58d57292012-09-01 06:04:27 +00006784 N0.getOperand(0),
Andrew Trickac6d9be2013-05-25 02:42:55 +00006785 DAG.getNode(ISD::FNEG, SDLoc(N), VT,
Owen Anderson58d57292012-09-01 06:04:27 +00006786 N0.getOperand(1)));
Owen Anderson58d57292012-09-01 06:04:27 +00006787 }
6788
Dan Gohman475871a2008-07-27 21:46:04 +00006789 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006790}
6791
Owen Anderson7c626d32012-08-13 23:32:49 +00006792SDValue DAGCombiner::visitFCEIL(SDNode *N) {
6793 SDValue N0 = N->getOperand(0);
6794 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6795 EVT VT = N->getValueType(0);
6796
6797 // fold (fceil c1) -> fceil(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006798 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006799 return DAG.getNode(ISD::FCEIL, SDLoc(N), VT, N0);
Owen Anderson7c626d32012-08-13 23:32:49 +00006800
6801 return SDValue();
6802}
6803
6804SDValue DAGCombiner::visitFTRUNC(SDNode *N) {
6805 SDValue N0 = N->getOperand(0);
6806 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6807 EVT VT = N->getValueType(0);
6808
6809 // fold (ftrunc c1) -> ftrunc(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006810 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006811 return DAG.getNode(ISD::FTRUNC, SDLoc(N), VT, N0);
Owen Anderson7c626d32012-08-13 23:32:49 +00006812
6813 return SDValue();
6814}
6815
6816SDValue DAGCombiner::visitFFLOOR(SDNode *N) {
6817 SDValue N0 = N->getOperand(0);
6818 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6819 EVT VT = N->getValueType(0);
6820
6821 // fold (ffloor c1) -> ffloor(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006822 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006823 return DAG.getNode(ISD::FFLOOR, SDLoc(N), VT, N0);
Owen Anderson7c626d32012-08-13 23:32:49 +00006824
6825 return SDValue();
6826}
6827
Dan Gohman475871a2008-07-27 21:46:04 +00006828SDValue DAGCombiner::visitFABS(SDNode *N) {
6829 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006830 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006831 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006832
Craig Topperdd201ff2012-09-11 01:45:21 +00006833 if (VT.isVector()) {
6834 SDValue FoldedVOp = SimplifyVUnaryOp(N);
6835 if (FoldedVOp.getNode()) return FoldedVOp;
6836 }
6837
Nate Begeman1d4d4142005-09-01 00:19:25 +00006838 // fold (fabs c1) -> fabs(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006839 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006840 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006841 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00006842 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00006843 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006844 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00006845 // fold (fabs (fcopysign x, y)) -> (fabs x)
6846 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006847 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00006848
Chris Lattner3bd39d42008-01-27 17:42:27 +00006849 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
6850 // constant pool values.
Stephen Lin155615d2013-07-08 00:37:03 +00006851 if (!TLI.isFAbsFree(VT) &&
Owen Anderson29f60f32012-04-02 22:10:29 +00006852 N0.getOpcode() == ISD::BITCAST && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00006853 N0.getOperand(0).getValueType().isInteger() &&
6854 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006855 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006856 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006857 if (IntVT.isInteger() && !IntVT.isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006858 Int = DAG.getNode(ISD::AND, SDLoc(N0), IntVT, Int,
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006859 DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00006860 AddToWorkList(Int.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006861 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00006862 N->getValueType(0), Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00006863 }
6864 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006865
Dan Gohman475871a2008-07-27 21:46:04 +00006866 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006867}
6868
Dan Gohman475871a2008-07-27 21:46:04 +00006869SDValue DAGCombiner::visitBRCOND(SDNode *N) {
6870 SDValue Chain = N->getOperand(0);
6871 SDValue N1 = N->getOperand(1);
6872 SDValue N2 = N->getOperand(2);
Scott Michelfdc40a02009-02-17 22:15:04 +00006873
Dan Gohmane0f06c72009-11-17 00:47:23 +00006874 // If N is a constant we could fold this into a fallthrough or unconditional
6875 // branch. However that doesn't happen very often in normal code, because
6876 // Instcombine/SimplifyCFG should have handled the available opportunities.
6877 // If we did this folding here, it would be necessary to update the
6878 // MachineBasicBlock CFG, which is awkward.
6879
Nate Begeman750ac1b2006-02-01 07:19:44 +00006880 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
6881 // on the target.
Scott Michelfdc40a02009-02-17 22:15:04 +00006882 if (N1.getOpcode() == ISD::SETCC &&
Tom Stellard3ef53832013-03-08 15:36:57 +00006883 TLI.isOperationLegalOrCustom(ISD::BR_CC,
6884 N1.getOperand(0).getValueType())) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006885 return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00006886 Chain, N1.getOperand(2),
Nate Begeman750ac1b2006-02-01 07:19:44 +00006887 N1.getOperand(0), N1.getOperand(1), N2);
6888 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00006889
Evan Cheng2a135ae2010-10-04 22:41:01 +00006890 if ((N1.hasOneUse() && N1.getOpcode() == ISD::SRL) ||
6891 ((N1.getOpcode() == ISD::TRUNCATE && N1.hasOneUse()) &&
6892 (N1.getOperand(0).hasOneUse() &&
6893 N1.getOperand(0).getOpcode() == ISD::SRL))) {
6894 SDNode *Trunc = 0;
6895 if (N1.getOpcode() == ISD::TRUNCATE) {
6896 // Look pass the truncate.
6897 Trunc = N1.getNode();
6898 N1 = N1.getOperand(0);
6899 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006900
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006901 // Match this pattern so that we can generate simpler code:
6902 //
6903 // %a = ...
6904 // %b = and i32 %a, 2
6905 // %c = srl i32 %b, 1
6906 // brcond i32 %c ...
6907 //
6908 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006909 //
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006910 // %a = ...
Evan Chengd40d03e2010-01-06 19:38:29 +00006911 // %b = and i32 %a, 2
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006912 // %c = setcc eq %b, 0
6913 // brcond %c ...
6914 //
6915 // This applies only when the AND constant value has one bit set and the
6916 // SRL constant is equal to the log2 of the AND constant. The back-end is
6917 // smart enough to convert the result into a TEST/JMP sequence.
6918 SDValue Op0 = N1.getOperand(0);
6919 SDValue Op1 = N1.getOperand(1);
6920
6921 if (Op0.getOpcode() == ISD::AND &&
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006922 Op1.getOpcode() == ISD::Constant) {
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006923 SDValue AndOp1 = Op0.getOperand(1);
6924
6925 if (AndOp1.getOpcode() == ISD::Constant) {
6926 const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue();
6927
6928 if (AndConst.isPowerOf2() &&
6929 cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) {
6930 SDValue SetCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00006931 DAG.getSetCC(SDLoc(N),
Matt Arsenault225ed702013-05-18 00:21:46 +00006932 getSetCCResultType(Op0.getValueType()),
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006933 Op0, DAG.getConstant(0, Op0.getValueType()),
6934 ISD::SETNE);
6935
Andrew Trickac6d9be2013-05-25 02:42:55 +00006936 SDValue NewBRCond = DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Chengd40d03e2010-01-06 19:38:29 +00006937 MVT::Other, Chain, SetCC, N2);
6938 // Don't add the new BRCond into the worklist or else SimplifySelectCC
6939 // will convert it back to (X & C1) >> C2.
6940 CombineTo(N, NewBRCond, false);
6941 // Truncate is dead.
6942 if (Trunc) {
6943 removeFromWorkList(Trunc);
6944 DAG.DeleteNode(Trunc);
6945 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006946 // Replace the uses of SRL with SETCC
Evan Cheng2c755ba2010-02-27 07:36:59 +00006947 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006948 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006949 removeFromWorkList(N1.getNode());
6950 DAG.DeleteNode(N1.getNode());
Evan Chengd40d03e2010-01-06 19:38:29 +00006951 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006952 }
6953 }
6954 }
Evan Cheng2a135ae2010-10-04 22:41:01 +00006955
6956 if (Trunc)
6957 // Restore N1 if the above transformation doesn't match.
6958 N1 = N->getOperand(1);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006959 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006960
Evan Cheng2c755ba2010-02-27 07:36:59 +00006961 // Transform br(xor(x, y)) -> br(x != y)
6962 // Transform br(xor(xor(x,y), 1)) -> br (x == y)
6963 if (N1.hasOneUse() && N1.getOpcode() == ISD::XOR) {
6964 SDNode *TheXor = N1.getNode();
6965 SDValue Op0 = TheXor->getOperand(0);
6966 SDValue Op1 = TheXor->getOperand(1);
6967 if (Op0.getOpcode() == Op1.getOpcode()) {
6968 // Avoid missing important xor optimizations.
6969 SDValue Tmp = visitXOR(TheXor);
Evan Cheng78ec0252013-01-09 20:56:40 +00006970 if (Tmp.getNode()) {
6971 if (Tmp.getNode() != TheXor) {
6972 DEBUG(dbgs() << "\nReplacing.8 ";
6973 TheXor->dump(&DAG);
6974 dbgs() << "\nWith: ";
6975 Tmp.getNode()->dump(&DAG);
6976 dbgs() << '\n');
6977 WorkListRemover DeadNodes(*this);
6978 DAG.ReplaceAllUsesOfValueWith(N1, Tmp);
6979 removeFromWorkList(TheXor);
6980 DAG.DeleteNode(TheXor);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006981 return DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Cheng78ec0252013-01-09 20:56:40 +00006982 MVT::Other, Chain, Tmp, N2);
6983 }
6984
Benjamin Kramer0b68b752013-03-30 21:28:18 +00006985 // visitXOR has changed XOR's operands or replaced the XOR completely,
6986 // bail out.
6987 return SDValue(N, 0);
Evan Cheng2c755ba2010-02-27 07:36:59 +00006988 }
6989 }
6990
6991 if (Op0.getOpcode() != ISD::SETCC && Op1.getOpcode() != ISD::SETCC) {
6992 bool Equal = false;
6993 if (ConstantSDNode *RHSCI = dyn_cast<ConstantSDNode>(Op0))
6994 if (RHSCI->getAPIntValue() == 1 && Op0.hasOneUse() &&
6995 Op0.getOpcode() == ISD::XOR) {
6996 TheXor = Op0.getNode();
6997 Equal = true;
6998 }
6999
Evan Cheng2a135ae2010-10-04 22:41:01 +00007000 EVT SetCCVT = N1.getValueType();
Evan Cheng2c755ba2010-02-27 07:36:59 +00007001 if (LegalTypes)
Matt Arsenault225ed702013-05-18 00:21:46 +00007002 SetCCVT = getSetCCResultType(SetCCVT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00007003 SDValue SetCC = DAG.getSetCC(SDLoc(TheXor),
Evan Cheng2c755ba2010-02-27 07:36:59 +00007004 SetCCVT,
7005 Op0, Op1,
7006 Equal ? ISD::SETEQ : ISD::SETNE);
7007 // Replace the uses of XOR with SETCC
7008 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007009 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Evan Cheng2a135ae2010-10-04 22:41:01 +00007010 removeFromWorkList(N1.getNode());
7011 DAG.DeleteNode(N1.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00007012 return DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Cheng2c755ba2010-02-27 07:36:59 +00007013 MVT::Other, Chain, SetCC, N2);
7014 }
7015 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00007016
Dan Gohman475871a2008-07-27 21:46:04 +00007017 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00007018}
7019
Chris Lattner3ea0b472005-10-05 06:47:48 +00007020// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
7021//
Dan Gohman475871a2008-07-27 21:46:04 +00007022SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00007023 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00007024 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Scott Michelfdc40a02009-02-17 22:15:04 +00007025
Dan Gohmane0f06c72009-11-17 00:47:23 +00007026 // If N is a constant we could fold this into a fallthrough or unconditional
7027 // branch. However that doesn't happen very often in normal code, because
7028 // Instcombine/SimplifyCFG should have handled the available opportunities.
7029 // If we did this folding here, it would be necessary to update the
7030 // MachineBasicBlock CFG, which is awkward.
7031
Duncan Sands8eab8a22008-06-09 11:32:28 +00007032 // Use SimplifySetCC to simplify SETCC's.
Matt Arsenault225ed702013-05-18 00:21:46 +00007033 SDValue Simp = SimplifySetCC(getSetCCResultType(CondLHS.getValueType()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00007034 CondLHS, CondRHS, CC->get(), SDLoc(N),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00007035 false);
Gabor Greifba36cb52008-08-28 21:40:38 +00007036 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00007037
Nate Begemane17daeb2005-10-05 21:43:42 +00007038 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00007039 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Andrew Trickac6d9be2013-05-25 02:42:55 +00007040 return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00007041 N->getOperand(0), Simp.getOperand(2),
7042 Simp.getOperand(0), Simp.getOperand(1),
7043 N->getOperand(4));
7044
Dan Gohman475871a2008-07-27 21:46:04 +00007045 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00007046}
7047
Evan Chengc4b527a2012-01-13 01:37:24 +00007048/// canFoldInAddressingMode - Return true if 'Use' is a load or a store that
7049/// uses N as its base pointer and that N may be folded in the load / store
Evan Cheng03be3622012-03-06 23:33:32 +00007050/// addressing mode.
Evan Chengc4b527a2012-01-13 01:37:24 +00007051static bool canFoldInAddressingMode(SDNode *N, SDNode *Use,
7052 SelectionDAG &DAG,
7053 const TargetLowering &TLI) {
7054 EVT VT;
7055 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Use)) {
7056 if (LD->isIndexed() || LD->getBasePtr().getNode() != N)
7057 return false;
7058 VT = Use->getValueType(0);
7059 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(Use)) {
7060 if (ST->isIndexed() || ST->getBasePtr().getNode() != N)
7061 return false;
7062 VT = ST->getValue().getValueType();
7063 } else
7064 return false;
7065
Chandler Carruth56d433d2013-01-07 15:14:13 +00007066 TargetLowering::AddrMode AM;
Evan Chengc4b527a2012-01-13 01:37:24 +00007067 if (N->getOpcode() == ISD::ADD) {
7068 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
7069 if (Offset)
Evan Cheng03be3622012-03-06 23:33:32 +00007070 // [reg +/- imm]
Evan Chengc4b527a2012-01-13 01:37:24 +00007071 AM.BaseOffs = Offset->getSExtValue();
7072 else
Evan Cheng03be3622012-03-06 23:33:32 +00007073 // [reg +/- reg]
7074 AM.Scale = 1;
Evan Chengc4b527a2012-01-13 01:37:24 +00007075 } else if (N->getOpcode() == ISD::SUB) {
7076 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
7077 if (Offset)
Evan Cheng03be3622012-03-06 23:33:32 +00007078 // [reg +/- imm]
Evan Chengc4b527a2012-01-13 01:37:24 +00007079 AM.BaseOffs = -Offset->getSExtValue();
7080 else
Evan Cheng03be3622012-03-06 23:33:32 +00007081 // [reg +/- reg]
7082 AM.Scale = 1;
Evan Chengc4b527a2012-01-13 01:37:24 +00007083 } else
7084 return false;
7085
7086 return TLI.isLegalAddressingMode(AM, VT.getTypeForEVT(*DAG.getContext()));
7087}
7088
Duncan Sandsec87aa82008-06-15 20:12:31 +00007089/// CombineToPreIndexedLoadStore - Try turning a load / store into a
7090/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00007091/// and it has other uses besides the load / store. After the
7092/// transformation, the new indexed load / store has effectively folded
7093/// the add / subtract in and all of its other uses are redirected to the
7094/// new load / store.
7095bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00007096 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00007097 return false;
7098
7099 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007100 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00007101 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00007102 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007103 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007104 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007105 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00007106 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00007107 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
7108 return false;
7109 Ptr = LD->getBasePtr();
7110 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007111 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007112 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007113 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007114 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
7115 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
7116 return false;
7117 Ptr = ST->getBasePtr();
7118 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007119 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00007120 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007121 }
Chris Lattner448f2192006-11-11 00:39:41 +00007122
Chris Lattner9f1794e2006-11-11 00:56:29 +00007123 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
7124 // out. There is no reason to make this a preinc/predec.
7125 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00007126 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00007127 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00007128
Chris Lattner9f1794e2006-11-11 00:56:29 +00007129 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00007130 SDValue BasePtr;
7131 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007132 ISD::MemIndexedMode AM = ISD::UNINDEXED;
7133 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
7134 return false;
Hal Finkel089a5f82013-02-08 21:35:47 +00007135
7136 // Backends without true r+i pre-indexed forms may need to pass a
7137 // constant base with a variable offset so that constant coercion
7138 // will work with the patterns in canonical form.
7139 bool Swapped = false;
7140 if (isa<ConstantSDNode>(BasePtr)) {
7141 std::swap(BasePtr, Offset);
7142 Swapped = true;
7143 }
7144
Evan Chenga7d4a042007-05-03 23:52:19 +00007145 // Don't create a indexed load / store with zero offset.
7146 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00007147 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00007148 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007149
Chris Lattner41e53fd2006-11-11 01:00:15 +00007150 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00007151 // 1) The new base ptr is a frame index.
7152 // 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 +00007153 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00007154 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00007155 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00007156 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00007157
Chris Lattner41e53fd2006-11-11 01:00:15 +00007158 // Check #1. Preinc'ing a frame index would require copying the stack pointer
7159 // (plus the implicit offset) to a register to preinc anyway.
Evan Chengcaab1292009-05-06 18:25:01 +00007160 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
Chris Lattner41e53fd2006-11-11 01:00:15 +00007161 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007162
Chris Lattner41e53fd2006-11-11 01:00:15 +00007163 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00007164 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00007165 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007166 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007167 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00007168 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007169
Hal Finkel089a5f82013-02-08 21:35:47 +00007170 // If the offset is a constant, there may be other adds of constants that
7171 // can be folded with this one. We should do this to avoid having to keep
7172 // a copy of the original base pointer.
7173 SmallVector<SDNode *, 16> OtherUses;
7174 if (isa<ConstantSDNode>(Offset))
7175 for (SDNode::use_iterator I = BasePtr.getNode()->use_begin(),
7176 E = BasePtr.getNode()->use_end(); I != E; ++I) {
7177 SDNode *Use = *I;
7178 if (Use == Ptr.getNode())
7179 continue;
7180
7181 if (Use->isPredecessorOf(N))
7182 continue;
7183
7184 if (Use->getOpcode() != ISD::ADD && Use->getOpcode() != ISD::SUB) {
7185 OtherUses.clear();
7186 break;
7187 }
7188
7189 SDValue Op0 = Use->getOperand(0), Op1 = Use->getOperand(1);
7190 if (Op1.getNode() == BasePtr.getNode())
7191 std::swap(Op0, Op1);
7192 assert(Op0.getNode() == BasePtr.getNode() &&
7193 "Use of ADD/SUB but not an operand");
7194
7195 if (!isa<ConstantSDNode>(Op1)) {
7196 OtherUses.clear();
7197 break;
7198 }
7199
7200 // FIXME: In some cases, we can be smarter about this.
7201 if (Op1.getValueType() != Offset.getValueType()) {
7202 OtherUses.clear();
7203 break;
7204 }
7205
7206 OtherUses.push_back(Use);
7207 }
7208
7209 if (Swapped)
7210 std::swap(BasePtr, Offset);
7211
Evan Chengc843abe2007-05-24 02:35:39 +00007212 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00007213 bool RealUse = false;
Lang Hames944520f2011-07-07 04:31:51 +00007214
7215 // Caches for hasPredecessorHelper
7216 SmallPtrSet<const SDNode *, 32> Visited;
7217 SmallVector<const SDNode *, 16> Worklist;
7218
Gabor Greifba36cb52008-08-28 21:40:38 +00007219 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
7220 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00007221 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007222 if (Use == N)
7223 continue;
Lang Hames944520f2011-07-07 04:31:51 +00007224 if (N->hasPredecessorHelper(Use, Visited, Worklist))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007225 return false;
7226
Evan Chengc4b527a2012-01-13 01:37:24 +00007227 // If Ptr may be folded in addressing mode of other use, then it's
7228 // not profitable to do this transformation.
7229 if (!canFoldInAddressingMode(Ptr.getNode(), Use, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007230 RealUse = true;
7231 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007232
Chris Lattner9f1794e2006-11-11 00:56:29 +00007233 if (!RealUse)
7234 return false;
7235
Dan Gohman475871a2008-07-27 21:46:04 +00007236 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007237 if (isLoad)
Andrew Trickac6d9be2013-05-25 02:42:55 +00007238 Result = DAG.getIndexedLoad(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007239 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007240 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00007241 Result = DAG.getIndexedStore(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007242 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007243 ++PreIndexedNodes;
7244 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00007245 DEBUG(dbgs() << "\nReplacing.4 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007246 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007247 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007248 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007249 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007250 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007251 if (isLoad) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007252 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
7253 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007254 } else {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007255 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007256 }
7257
Chris Lattner9f1794e2006-11-11 00:56:29 +00007258 // Finally, since the node is now dead, remove it from the graph.
7259 DAG.DeleteNode(N);
7260
Hal Finkel089a5f82013-02-08 21:35:47 +00007261 if (Swapped)
7262 std::swap(BasePtr, Offset);
7263
7264 // Replace other uses of BasePtr that can be updated to use Ptr
7265 for (unsigned i = 0, e = OtherUses.size(); i != e; ++i) {
7266 unsigned OffsetIdx = 1;
7267 if (OtherUses[i]->getOperand(OffsetIdx).getNode() == BasePtr.getNode())
7268 OffsetIdx = 0;
7269 assert(OtherUses[i]->getOperand(!OffsetIdx).getNode() ==
7270 BasePtr.getNode() && "Expected BasePtr operand");
7271
Silviu Baranga730a5702013-04-26 15:52:24 +00007272 // We need to replace ptr0 in the following expression:
7273 // x0 * offset0 + y0 * ptr0 = t0
7274 // knowing that
7275 // x1 * offset1 + y1 * ptr0 = t1 (the indexed load/store)
Stephen Lin155615d2013-07-08 00:37:03 +00007276 //
Silviu Baranga730a5702013-04-26 15:52:24 +00007277 // where x0, x1, y0 and y1 in {-1, 1} are given by the types of the
7278 // indexed load/store and the expresion that needs to be re-written.
7279 //
7280 // Therefore, we have:
7281 // t0 = (x0 * offset0 - x1 * y0 * y1 *offset1) + (y0 * y1) * t1
Hal Finkel089a5f82013-02-08 21:35:47 +00007282
7283 ConstantSDNode *CN =
7284 cast<ConstantSDNode>(OtherUses[i]->getOperand(OffsetIdx));
Silviu Baranga730a5702013-04-26 15:52:24 +00007285 int X0, X1, Y0, Y1;
7286 APInt Offset0 = CN->getAPIntValue();
7287 APInt Offset1 = cast<ConstantSDNode>(Offset)->getAPIntValue();
Hal Finkel089a5f82013-02-08 21:35:47 +00007288
Silviu Baranga730a5702013-04-26 15:52:24 +00007289 X0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 1) ? -1 : 1;
7290 Y0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 0) ? -1 : 1;
7291 X1 = (AM == ISD::PRE_DEC && !Swapped) ? -1 : 1;
7292 Y1 = (AM == ISD::PRE_DEC && Swapped) ? -1 : 1;
Hal Finkel089a5f82013-02-08 21:35:47 +00007293
Silviu Baranga730a5702013-04-26 15:52:24 +00007294 unsigned Opcode = (Y0 * Y1 < 0) ? ISD::SUB : ISD::ADD;
7295
7296 APInt CNV = Offset0;
7297 if (X0 < 0) CNV = -CNV;
7298 if (X1 * Y0 * Y1 < 0) CNV = CNV + Offset1;
7299 else CNV = CNV - Offset1;
7300
7301 // We can now generate the new expression.
7302 SDValue NewOp1 = DAG.getConstant(CNV, CN->getValueType(0));
7303 SDValue NewOp2 = Result.getValue(isLoad ? 1 : 0);
7304
7305 SDValue NewUse = DAG.getNode(Opcode,
Andrew Trickac6d9be2013-05-25 02:42:55 +00007306 SDLoc(OtherUses[i]),
Hal Finkel089a5f82013-02-08 21:35:47 +00007307 OtherUses[i]->getValueType(0), NewOp1, NewOp2);
7308 DAG.ReplaceAllUsesOfValueWith(SDValue(OtherUses[i], 0), NewUse);
7309 removeFromWorkList(OtherUses[i]);
7310 DAG.DeleteNode(OtherUses[i]);
7311 }
7312
Chris Lattner9f1794e2006-11-11 00:56:29 +00007313 // Replace the uses of Ptr with uses of the updated base value.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007314 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0));
Gabor Greifba36cb52008-08-28 21:40:38 +00007315 removeFromWorkList(Ptr.getNode());
7316 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00007317
7318 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00007319}
7320
Duncan Sandsec87aa82008-06-15 20:12:31 +00007321/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00007322/// add / sub of the base pointer node into a post-indexed load / store.
7323/// The transformation folded the add / subtract into the new indexed
7324/// load / store effectively and all of its uses are redirected to the
7325/// new load / store.
7326bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00007327 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00007328 return false;
7329
7330 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007331 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00007332 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00007333 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007334 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007335 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007336 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007337 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
7338 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
7339 return false;
7340 Ptr = LD->getBasePtr();
7341 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007342 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007343 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007344 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007345 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
7346 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
7347 return false;
7348 Ptr = ST->getBasePtr();
7349 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007350 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00007351 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007352 }
Chris Lattner448f2192006-11-11 00:39:41 +00007353
Gabor Greifba36cb52008-08-28 21:40:38 +00007354 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00007355 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007356
Gabor Greifba36cb52008-08-28 21:40:38 +00007357 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
7358 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00007359 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007360 if (Op == N ||
7361 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
7362 continue;
7363
Dan Gohman475871a2008-07-27 21:46:04 +00007364 SDValue BasePtr;
7365 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007366 ISD::MemIndexedMode AM = ISD::UNINDEXED;
7367 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
Evan Chenga7d4a042007-05-03 23:52:19 +00007368 // Don't create a indexed load / store with zero offset.
7369 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00007370 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00007371 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00007372
Chris Lattner9f1794e2006-11-11 00:56:29 +00007373 // Try turning it into a post-indexed load / store except when
Evan Chengc4b527a2012-01-13 01:37:24 +00007374 // 1) All uses are load / store ops that use it as base ptr (and
7375 // it may be folded as addressing mmode).
Chris Lattner9f1794e2006-11-11 00:56:29 +00007376 // 2) Op must be independent of N, i.e. Op is neither a predecessor
7377 // nor a successor of N. Otherwise, if Op is folded that would
7378 // create a cycle.
7379
Evan Chengcaab1292009-05-06 18:25:01 +00007380 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
7381 continue;
7382
Chris Lattner9f1794e2006-11-11 00:56:29 +00007383 // Check for #1.
7384 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00007385 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
7386 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00007387 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00007388 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00007389 continue;
7390
Chris Lattner9f1794e2006-11-11 00:56:29 +00007391 // If all the uses are load / store addresses, then don't do the
7392 // transformation.
7393 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
7394 bool RealUse = false;
7395 for (SDNode::use_iterator III = Use->use_begin(),
7396 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00007397 SDNode *UseUse = *III;
Stephen Lin155615d2013-07-08 00:37:03 +00007398 if (!canFoldInAddressingMode(Use, UseUse, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007399 RealUse = true;
7400 }
Chris Lattner448f2192006-11-11 00:39:41 +00007401
Chris Lattner9f1794e2006-11-11 00:56:29 +00007402 if (!RealUse) {
7403 TryNext = true;
7404 break;
Chris Lattner448f2192006-11-11 00:39:41 +00007405 }
7406 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007407 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007408
Chris Lattner9f1794e2006-11-11 00:56:29 +00007409 if (TryNext)
7410 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00007411
Chris Lattner9f1794e2006-11-11 00:56:29 +00007412 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00007413 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00007414 SDValue Result = isLoad
Andrew Trickac6d9be2013-05-25 02:42:55 +00007415 ? DAG.getIndexedLoad(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007416 BasePtr, Offset, AM)
Andrew Trickac6d9be2013-05-25 02:42:55 +00007417 : DAG.getIndexedStore(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007418 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007419 ++PostIndexedNodes;
7420 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00007421 DEBUG(dbgs() << "\nReplacing.5 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007422 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007423 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007424 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007425 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007426 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007427 if (isLoad) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007428 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
7429 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007430 } else {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007431 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattner448f2192006-11-11 00:39:41 +00007432 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007433
Chris Lattner9f1794e2006-11-11 00:56:29 +00007434 // Finally, since the node is now dead, remove it from the graph.
7435 DAG.DeleteNode(N);
7436
7437 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00007438 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007439 Result.getValue(isLoad ? 1 : 0));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007440 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007441 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007442 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00007443 }
7444 }
7445 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007446
Chris Lattner448f2192006-11-11 00:39:41 +00007447 return false;
7448}
7449
Dan Gohman475871a2008-07-27 21:46:04 +00007450SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00007451 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007452 SDValue Chain = LD->getChain();
7453 SDValue Ptr = LD->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00007454
Evan Cheng45a7ca92007-05-01 00:38:21 +00007455 // If load is not volatile and there are no uses of the loaded value (and
7456 // the updated indexed value in case of indexed loads), change uses of the
7457 // chain value into uses of the chain input (i.e. delete the dead load).
7458 if (!LD->isVolatile()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007459 if (N->getValueType(1) == MVT::Other) {
Evan Cheng498f5592007-05-01 08:53:39 +00007460 // Unindexed loads.
Craig Topper704e1a02012-01-07 18:31:09 +00007461 if (!N->hasAnyUseOfValue(0)) {
Evan Cheng02c42852008-01-16 23:11:54 +00007462 // It's not safe to use the two value CombineTo variant here. e.g.
7463 // v1, chain2 = load chain1, loc
7464 // v2, chain3 = load chain2, loc
7465 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00007466 // Now we replace use of chain2 with chain1. This makes the second load
7467 // isomorphic to the one we are deleting, and thus makes this load live.
David Greenef1090292010-01-05 01:25:00 +00007468 DEBUG(dbgs() << "\nReplacing.6 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007469 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007470 dbgs() << "\nWith chain: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007471 Chain.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007472 dbgs() << "\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007473 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007474 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain);
Bill Wendlingc0debad2009-01-30 23:27:35 +00007475
Chris Lattner125991a2008-01-24 07:57:06 +00007476 if (N->use_empty()) {
7477 removeFromWorkList(N);
7478 DAG.DeleteNode(N);
7479 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007480
Dan Gohman475871a2008-07-27 21:46:04 +00007481 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00007482 }
Evan Cheng498f5592007-05-01 08:53:39 +00007483 } else {
7484 // Indexed loads.
Owen Anderson825b72b2009-08-11 20:47:22 +00007485 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
Craig Topper704e1a02012-01-07 18:31:09 +00007486 if (!N->hasAnyUseOfValue(0) && !N->hasAnyUseOfValue(1)) {
Dale Johannesene8d72302009-02-06 23:05:02 +00007487 SDValue Undef = DAG.getUNDEF(N->getValueType(0));
Evan Cheng2c755ba2010-02-27 07:36:59 +00007488 DEBUG(dbgs() << "\nReplacing.7 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007489 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007490 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007491 Undef.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007492 dbgs() << " and 2 other values\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007493 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007494 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef);
Dan Gohman475871a2008-07-27 21:46:04 +00007495 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007496 DAG.getUNDEF(N->getValueType(1)));
7497 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain);
Evan Cheng02c42852008-01-16 23:11:54 +00007498 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00007499 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007500 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00007501 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00007502 }
7503 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007504
Chris Lattner01a22022005-10-10 22:04:48 +00007505 // If this load is directly stored, replace the load value with the stored
7506 // value.
7507 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007508 // TODO: Handle TRUNCSTORE/LOADEXT
Evan Cheng9ef82ce2011-03-11 00:48:56 +00007509 if (ISD::isNormalLoad(N) && !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007510 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00007511 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
7512 if (PrevST->getBasePtr() == Ptr &&
7513 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007514 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00007515 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007516 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007517
Evan Cheng255f20f2010-04-01 06:04:33 +00007518 // Try to infer better alignment information than the load already has.
7519 if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00007520 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
Owen Andersonb48783b2013-02-05 19:24:39 +00007521 if (Align > LD->getMemOperand()->getBaseAlignment()) {
7522 SDValue NewLoad =
Andrew Trickac6d9be2013-05-25 02:42:55 +00007523 DAG.getExtLoad(LD->getExtensionType(), SDLoc(N),
Evan Chenged1c0c72011-11-28 22:37:34 +00007524 LD->getValueType(0),
7525 Chain, Ptr, LD->getPointerInfo(),
7526 LD->getMemoryVT(),
7527 LD->isVolatile(), LD->isNonTemporal(), Align);
Owen Andersonb48783b2013-02-05 19:24:39 +00007528 return CombineTo(N, NewLoad, SDValue(NewLoad.getNode(), 1), true);
7529 }
Evan Cheng255f20f2010-04-01 06:04:33 +00007530 }
7531 }
7532
Hal Finkel253acef2013-08-29 03:29:55 +00007533 bool UseAA = CombinerAA.getNumOccurrences() > 0 ? CombinerAA :
7534 TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA();
7535 if (UseAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00007536 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007537 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00007538
Jim Laskey6ff23e52006-10-04 16:53:27 +00007539 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00007540 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00007541 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007542
Jim Laskey279f0532006-09-25 16:29:54 +00007543 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007544 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007545 ReplLoad = DAG.getLoad(N->getValueType(0), SDLoc(LD),
Chris Lattnerfa459012010-09-21 16:08:50 +00007546 BetterChain, Ptr, LD->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00007547 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007548 LD->isInvariant(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007549 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007550 ReplLoad = DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD),
Stuart Hastingsa9011292011-02-16 16:23:55 +00007551 LD->getValueType(0),
Chris Lattnerfa459012010-09-21 16:08:50 +00007552 BetterChain, Ptr, LD->getPointerInfo(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007553 LD->getMemoryVT(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007554 LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00007555 LD->isNonTemporal(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00007556 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007557 }
Jim Laskey279f0532006-09-25 16:29:54 +00007558
Jim Laskey6ff23e52006-10-04 16:53:27 +00007559 // Create token factor to keep old chain connected.
Andrew Trickac6d9be2013-05-25 02:42:55 +00007560 SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson825b72b2009-08-11 20:47:22 +00007561 MVT::Other, Chain, ReplLoad.getValue(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007562
Nate Begemanb6aef5c2009-09-15 00:18:30 +00007563 // Make sure the new and old chains are cleaned up.
7564 AddToWorkList(Token.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007565
Jim Laskey274062c2006-10-13 23:32:28 +00007566 // Replace uses with load result and token factor. Don't add users
7567 // to work list.
7568 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00007569 }
7570 }
7571
Evan Cheng7fc033a2006-11-03 03:06:21 +00007572 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00007573 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00007574 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00007575
Dan Gohman475871a2008-07-27 21:46:04 +00007576 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00007577}
7578
Chris Lattner2392ae72010-04-15 04:48:01 +00007579/// CheckForMaskedLoad - Check to see if V is (and load (ptr), imm), where the
7580/// load is having specific bytes cleared out. If so, return the byte size
7581/// being masked out and the shift amount.
7582static std::pair<unsigned, unsigned>
7583CheckForMaskedLoad(SDValue V, SDValue Ptr, SDValue Chain) {
7584 std::pair<unsigned, unsigned> Result(0, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007585
Chris Lattner2392ae72010-04-15 04:48:01 +00007586 // Check for the structure we're looking for.
7587 if (V->getOpcode() != ISD::AND ||
7588 !isa<ConstantSDNode>(V->getOperand(1)) ||
7589 !ISD::isNormalLoad(V->getOperand(0).getNode()))
7590 return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007591
Chris Lattnere6987582010-04-15 06:10:49 +00007592 // Check the chain and pointer.
Chris Lattner2392ae72010-04-15 04:48:01 +00007593 LoadSDNode *LD = cast<LoadSDNode>(V->getOperand(0));
Chris Lattnere6987582010-04-15 06:10:49 +00007594 if (LD->getBasePtr() != Ptr) return Result; // Not from same pointer.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007595
Chris Lattnere6987582010-04-15 06:10:49 +00007596 // The store should be chained directly to the load or be an operand of a
7597 // tokenfactor.
7598 if (LD == Chain.getNode())
7599 ; // ok.
7600 else if (Chain->getOpcode() != ISD::TokenFactor)
7601 return Result; // Fail.
7602 else {
7603 bool isOk = false;
7604 for (unsigned i = 0, e = Chain->getNumOperands(); i != e; ++i)
7605 if (Chain->getOperand(i).getNode() == LD) {
7606 isOk = true;
7607 break;
7608 }
7609 if (!isOk) return Result;
7610 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007611
Chris Lattner2392ae72010-04-15 04:48:01 +00007612 // This only handles simple types.
7613 if (V.getValueType() != MVT::i16 &&
7614 V.getValueType() != MVT::i32 &&
7615 V.getValueType() != MVT::i64)
7616 return Result;
7617
7618 // Check the constant mask. Invert it so that the bits being masked out are
7619 // 0 and the bits being kept are 1. Use getSExtValue so that leading bits
7620 // follow the sign bit for uniformity.
7621 uint64_t NotMask = ~cast<ConstantSDNode>(V->getOperand(1))->getSExtValue();
Michael J. Spencerc6af2432013-05-24 22:23:49 +00007622 unsigned NotMaskLZ = countLeadingZeros(NotMask);
Chris Lattner2392ae72010-04-15 04:48:01 +00007623 if (NotMaskLZ & 7) return Result; // Must be multiple of a byte.
Michael J. Spencerc6af2432013-05-24 22:23:49 +00007624 unsigned NotMaskTZ = countTrailingZeros(NotMask);
Chris Lattner2392ae72010-04-15 04:48:01 +00007625 if (NotMaskTZ & 7) return Result; // Must be multiple of a byte.
7626 if (NotMaskLZ == 64) return Result; // All zero mask.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007627
Chris Lattner2392ae72010-04-15 04:48:01 +00007628 // See if we have a continuous run of bits. If so, we have 0*1+0*
7629 if (CountTrailingOnes_64(NotMask >> NotMaskTZ)+NotMaskTZ+NotMaskLZ != 64)
7630 return Result;
7631
7632 // Adjust NotMaskLZ down to be from the actual size of the int instead of i64.
7633 if (V.getValueType() != MVT::i64 && NotMaskLZ)
7634 NotMaskLZ -= 64-V.getValueSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007635
Chris Lattner2392ae72010-04-15 04:48:01 +00007636 unsigned MaskedBytes = (V.getValueSizeInBits()-NotMaskLZ-NotMaskTZ)/8;
7637 switch (MaskedBytes) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007638 case 1:
7639 case 2:
Chris Lattner2392ae72010-04-15 04:48:01 +00007640 case 4: break;
7641 default: return Result; // All one mask, or 5-byte mask.
7642 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007643
Chris Lattner2392ae72010-04-15 04:48:01 +00007644 // Verify that the first bit starts at a multiple of mask so that the access
7645 // is aligned the same as the access width.
7646 if (NotMaskTZ && NotMaskTZ/8 % MaskedBytes) return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007647
Chris Lattner2392ae72010-04-15 04:48:01 +00007648 Result.first = MaskedBytes;
7649 Result.second = NotMaskTZ/8;
7650 return Result;
7651}
7652
7653
7654/// ShrinkLoadReplaceStoreWithStore - Check to see if IVal is something that
7655/// provides a value as specified by MaskInfo. If so, replace the specified
7656/// store with a narrower store of truncated IVal.
7657static SDNode *
7658ShrinkLoadReplaceStoreWithStore(const std::pair<unsigned, unsigned> &MaskInfo,
7659 SDValue IVal, StoreSDNode *St,
7660 DAGCombiner *DC) {
7661 unsigned NumBytes = MaskInfo.first;
7662 unsigned ByteShift = MaskInfo.second;
7663 SelectionDAG &DAG = DC->getDAG();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007664
Chris Lattner2392ae72010-04-15 04:48:01 +00007665 // Check to see if IVal is all zeros in the part being masked in by the 'or'
7666 // that uses this. If not, this is not a replacement.
7667 APInt Mask = ~APInt::getBitsSet(IVal.getValueSizeInBits(),
7668 ByteShift*8, (ByteShift+NumBytes)*8);
7669 if (!DAG.MaskedValueIsZero(IVal, Mask)) return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007670
Chris Lattner2392ae72010-04-15 04:48:01 +00007671 // Check that it is legal on the target to do this. It is legal if the new
7672 // VT we're shrinking to (i8/i16/i32) is legal or we're still before type
7673 // legalization.
7674 MVT VT = MVT::getIntegerVT(NumBytes*8);
7675 if (!DC->isTypeLegal(VT))
7676 return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007677
Chris Lattner2392ae72010-04-15 04:48:01 +00007678 // Okay, we can do this! Replace the 'St' store with a store of IVal that is
7679 // shifted by ByteShift and truncated down to NumBytes.
7680 if (ByteShift)
Andrew Trickac6d9be2013-05-25 02:42:55 +00007681 IVal = DAG.getNode(ISD::SRL, SDLoc(IVal), IVal.getValueType(), IVal,
Owen Anderson95771af2011-02-25 21:41:48 +00007682 DAG.getConstant(ByteShift*8,
7683 DC->getShiftAmountTy(IVal.getValueType())));
Chris Lattner2392ae72010-04-15 04:48:01 +00007684
7685 // Figure out the offset for the store and the alignment of the access.
7686 unsigned StOffset;
7687 unsigned NewAlign = St->getAlignment();
7688
7689 if (DAG.getTargetLoweringInfo().isLittleEndian())
7690 StOffset = ByteShift;
7691 else
7692 StOffset = IVal.getValueType().getStoreSize() - ByteShift - NumBytes;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007693
Chris Lattner2392ae72010-04-15 04:48:01 +00007694 SDValue Ptr = St->getBasePtr();
7695 if (StOffset) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007696 Ptr = DAG.getNode(ISD::ADD, SDLoc(IVal), Ptr.getValueType(),
Chris Lattner2392ae72010-04-15 04:48:01 +00007697 Ptr, DAG.getConstant(StOffset, Ptr.getValueType()));
7698 NewAlign = MinAlign(NewAlign, StOffset);
7699 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007700
Chris Lattner2392ae72010-04-15 04:48:01 +00007701 // Truncate down to the new size.
Andrew Trickac6d9be2013-05-25 02:42:55 +00007702 IVal = DAG.getNode(ISD::TRUNCATE, SDLoc(IVal), VT, IVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007703
Chris Lattner2392ae72010-04-15 04:48:01 +00007704 ++OpsNarrowed;
Andrew Trickac6d9be2013-05-25 02:42:55 +00007705 return DAG.getStore(St->getChain(), SDLoc(St), IVal, Ptr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007706 St->getPointerInfo().getWithOffset(StOffset),
Chris Lattner2392ae72010-04-15 04:48:01 +00007707 false, false, NewAlign).getNode();
7708}
7709
Evan Cheng8b944d32009-05-28 00:35:15 +00007710
7711/// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is
7712/// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some
7713/// of the loaded bits, try narrowing the load and store if it would end up
7714/// being a win for performance or code size.
7715SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) {
7716 StoreSDNode *ST = cast<StoreSDNode>(N);
Evan Chengcdcecc02009-05-28 18:41:02 +00007717 if (ST->isVolatile())
7718 return SDValue();
7719
Evan Cheng8b944d32009-05-28 00:35:15 +00007720 SDValue Chain = ST->getChain();
7721 SDValue Value = ST->getValue();
7722 SDValue Ptr = ST->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +00007723 EVT VT = Value.getValueType();
Evan Cheng8b944d32009-05-28 00:35:15 +00007724
7725 if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse())
Evan Chengcdcecc02009-05-28 18:41:02 +00007726 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007727
7728 unsigned Opc = Value.getOpcode();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007729
Chris Lattner2392ae72010-04-15 04:48:01 +00007730 // If this is "store (or X, Y), P" and X is "(and (load P), cst)", where cst
7731 // is a byte mask indicating a consecutive number of bytes, check to see if
7732 // Y is known to provide just those bytes. If so, we try to replace the
7733 // load + replace + store sequence with a single (narrower) store, which makes
7734 // the load dead.
7735 if (Opc == ISD::OR) {
7736 std::pair<unsigned, unsigned> MaskedLoad;
7737 MaskedLoad = CheckForMaskedLoad(Value.getOperand(0), Ptr, Chain);
7738 if (MaskedLoad.first)
7739 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
7740 Value.getOperand(1), ST,this))
7741 return SDValue(NewST, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007742
Chris Lattner2392ae72010-04-15 04:48:01 +00007743 // Or is commutative, so try swapping X and Y.
7744 MaskedLoad = CheckForMaskedLoad(Value.getOperand(1), Ptr, Chain);
7745 if (MaskedLoad.first)
7746 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
7747 Value.getOperand(0), ST,this))
7748 return SDValue(NewST, 0);
7749 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007750
Evan Cheng8b944d32009-05-28 00:35:15 +00007751 if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) ||
7752 Value.getOperand(1).getOpcode() != ISD::Constant)
Evan Chengcdcecc02009-05-28 18:41:02 +00007753 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007754
7755 SDValue N0 = Value.getOperand(0);
Dan Gohman24bde5b2010-09-02 21:18:42 +00007756 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
7757 Chain == SDValue(N0.getNode(), 1)) {
Evan Cheng8b944d32009-05-28 00:35:15 +00007758 LoadSDNode *LD = cast<LoadSDNode>(N0);
Chris Lattnerfa459012010-09-21 16:08:50 +00007759 if (LD->getBasePtr() != Ptr ||
7760 LD->getPointerInfo().getAddrSpace() !=
7761 ST->getPointerInfo().getAddrSpace())
Evan Chengcdcecc02009-05-28 18:41:02 +00007762 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007763
7764 // Find the type to narrow it the load / op / store to.
7765 SDValue N1 = Value.getOperand(1);
7766 unsigned BitWidth = N1.getValueSizeInBits();
7767 APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue();
7768 if (Opc == ISD::AND)
7769 Imm ^= APInt::getAllOnesValue(BitWidth);
Evan Chengd3c76bb2009-05-28 23:52:18 +00007770 if (Imm == 0 || Imm.isAllOnesValue())
7771 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007772 unsigned ShAmt = Imm.countTrailingZeros();
7773 unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1;
7774 unsigned NewBW = NextPowerOf2(MSB - ShAmt);
Owen Anderson23b9b192009-08-12 00:36:31 +00007775 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00007776 while (NewBW < BitWidth &&
Evan Chengcdcecc02009-05-28 18:41:02 +00007777 !(TLI.isOperationLegalOrCustom(Opc, NewVT) &&
Evan Cheng8b944d32009-05-28 00:35:15 +00007778 TLI.isNarrowingProfitable(VT, NewVT))) {
7779 NewBW = NextPowerOf2(NewBW);
Owen Anderson23b9b192009-08-12 00:36:31 +00007780 NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00007781 }
Evan Chengcdcecc02009-05-28 18:41:02 +00007782 if (NewBW >= BitWidth)
7783 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007784
7785 // If the lsb changed does not start at the type bitwidth boundary,
7786 // start at the previous one.
7787 if (ShAmt % NewBW)
7788 ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW;
Manman Ren981b9632012-12-12 01:13:50 +00007789 APInt Mask = APInt::getBitsSet(BitWidth, ShAmt,
7790 std::min(BitWidth, ShAmt + NewBW));
Evan Cheng8b944d32009-05-28 00:35:15 +00007791 if ((Imm & Mask) == Imm) {
7792 APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW);
7793 if (Opc == ISD::AND)
7794 NewImm ^= APInt::getAllOnesValue(NewBW);
7795 uint64_t PtrOff = ShAmt / 8;
7796 // For big endian targets, we need to adjust the offset to the pointer to
7797 // load the correct bytes.
7798 if (TLI.isBigEndian())
Evan Chengcdcecc02009-05-28 18:41:02 +00007799 PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff;
Evan Cheng8b944d32009-05-28 00:35:15 +00007800
7801 unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007802 Type *NewVTTy = NewVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00007803 if (NewAlign < TLI.getDataLayout()->getABITypeAlignment(NewVTTy))
Evan Chengcdcecc02009-05-28 18:41:02 +00007804 return SDValue();
7805
Andrew Trickac6d9be2013-05-25 02:42:55 +00007806 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(LD),
Evan Cheng8b944d32009-05-28 00:35:15 +00007807 Ptr.getValueType(), Ptr,
7808 DAG.getConstant(PtrOff, Ptr.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00007809 SDValue NewLD = DAG.getLoad(NewVT, SDLoc(N0),
Evan Cheng8b944d32009-05-28 00:35:15 +00007810 LD->getChain(), NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00007811 LD->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00007812 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007813 LD->isInvariant(), NewAlign);
Andrew Trickac6d9be2013-05-25 02:42:55 +00007814 SDValue NewVal = DAG.getNode(Opc, SDLoc(Value), NewVT, NewLD,
Evan Cheng8b944d32009-05-28 00:35:15 +00007815 DAG.getConstant(NewImm, NewVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00007816 SDValue NewST = DAG.getStore(Chain, SDLoc(N),
Evan Cheng8b944d32009-05-28 00:35:15 +00007817 NewVal, NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00007818 ST->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00007819 false, false, NewAlign);
Evan Cheng8b944d32009-05-28 00:35:15 +00007820
7821 AddToWorkList(NewPtr.getNode());
7822 AddToWorkList(NewLD.getNode());
7823 AddToWorkList(NewVal.getNode());
7824 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007825 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1));
Evan Cheng8b944d32009-05-28 00:35:15 +00007826 ++OpsNarrowed;
7827 return NewST;
7828 }
7829 }
7830
Evan Chengcdcecc02009-05-28 18:41:02 +00007831 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007832}
7833
Evan Cheng31959b12011-02-02 01:06:55 +00007834/// TransformFPLoadStorePair - For a given floating point load / store pair,
7835/// if the load value isn't used by any other operations, then consider
7836/// transforming the pair to integer load / store operations if the target
7837/// deems the transformation profitable.
7838SDValue DAGCombiner::TransformFPLoadStorePair(SDNode *N) {
7839 StoreSDNode *ST = cast<StoreSDNode>(N);
7840 SDValue Chain = ST->getChain();
7841 SDValue Value = ST->getValue();
7842 if (ISD::isNormalStore(ST) && ISD::isNormalLoad(Value.getNode()) &&
7843 Value.hasOneUse() &&
7844 Chain == SDValue(Value.getNode(), 1)) {
7845 LoadSDNode *LD = cast<LoadSDNode>(Value);
7846 EVT VT = LD->getMemoryVT();
7847 if (!VT.isFloatingPoint() ||
7848 VT != ST->getMemoryVT() ||
7849 LD->isNonTemporal() ||
7850 ST->isNonTemporal() ||
7851 LD->getPointerInfo().getAddrSpace() != 0 ||
7852 ST->getPointerInfo().getAddrSpace() != 0)
7853 return SDValue();
7854
7855 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
7856 if (!TLI.isOperationLegal(ISD::LOAD, IntVT) ||
7857 !TLI.isOperationLegal(ISD::STORE, IntVT) ||
7858 !TLI.isDesirableToTransformToIntegerOp(ISD::LOAD, VT) ||
7859 !TLI.isDesirableToTransformToIntegerOp(ISD::STORE, VT))
7860 return SDValue();
7861
7862 unsigned LDAlign = LD->getAlignment();
7863 unsigned STAlign = ST->getAlignment();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007864 Type *IntVTTy = IntVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00007865 unsigned ABIAlign = TLI.getDataLayout()->getABITypeAlignment(IntVTTy);
Evan Cheng31959b12011-02-02 01:06:55 +00007866 if (LDAlign < ABIAlign || STAlign < ABIAlign)
7867 return SDValue();
7868
Andrew Trickac6d9be2013-05-25 02:42:55 +00007869 SDValue NewLD = DAG.getLoad(IntVT, SDLoc(Value),
Evan Cheng31959b12011-02-02 01:06:55 +00007870 LD->getChain(), LD->getBasePtr(),
7871 LD->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007872 false, false, false, LDAlign);
Evan Cheng31959b12011-02-02 01:06:55 +00007873
Andrew Trickac6d9be2013-05-25 02:42:55 +00007874 SDValue NewST = DAG.getStore(NewLD.getValue(1), SDLoc(N),
Evan Cheng31959b12011-02-02 01:06:55 +00007875 NewLD, ST->getBasePtr(),
7876 ST->getPointerInfo(),
7877 false, false, STAlign);
7878
7879 AddToWorkList(NewLD.getNode());
7880 AddToWorkList(NewST.getNode());
7881 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007882 DAG.ReplaceAllUsesOfValueWith(Value.getValue(1), NewLD.getValue(1));
Evan Cheng31959b12011-02-02 01:06:55 +00007883 ++LdStFP2Int;
7884 return NewST;
7885 }
7886
7887 return SDValue();
7888}
7889
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007890/// Helper struct to parse and store a memory address as base + index + offset.
7891/// We ignore sign extensions when it is safe to do so.
7892/// The following two expressions are not equivalent. To differentiate we need
7893/// to store whether there was a sign extension involved in the index
7894/// computation.
7895/// (load (i64 add (i64 copyfromreg %c)
7896/// (i64 signextend (add (i8 load %index)
7897/// (i8 1))))
7898/// vs
7899///
7900/// (load (i64 add (i64 copyfromreg %c)
7901/// (i64 signextend (i32 add (i32 signextend (i8 load %index))
7902/// (i32 1)))))
7903struct BaseIndexOffset {
7904 SDValue Base;
7905 SDValue Index;
7906 int64_t Offset;
7907 bool IsIndexSignExt;
7908
7909 BaseIndexOffset() : Offset(0), IsIndexSignExt(false) {}
7910
7911 BaseIndexOffset(SDValue Base, SDValue Index, int64_t Offset,
7912 bool IsIndexSignExt) :
7913 Base(Base), Index(Index), Offset(Offset), IsIndexSignExt(IsIndexSignExt) {}
7914
7915 bool equalBaseIndex(const BaseIndexOffset &Other) {
7916 return Other.Base == Base && Other.Index == Index &&
7917 Other.IsIndexSignExt == IsIndexSignExt;
Nadav Rotemc653de62012-10-03 16:11:15 +00007918 }
7919
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007920 /// Parses tree in Ptr for base, index, offset addresses.
7921 static BaseIndexOffset match(SDValue Ptr) {
7922 bool IsIndexSignExt = false;
7923
Juergen Ributzka915e9362013-08-21 21:53:38 +00007924 // We only can pattern match BASE + INDEX + OFFSET. If Ptr is not an ADD
7925 // instruction, then it could be just the BASE or everything else we don't
7926 // know how to handle. Just use Ptr as BASE and give up.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007927 if (Ptr->getOpcode() != ISD::ADD)
7928 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
7929
Juergen Ributzka915e9362013-08-21 21:53:38 +00007930 // We know that we have at least an ADD instruction. Try to pattern match
7931 // the simple case of BASE + OFFSET.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007932 if (isa<ConstantSDNode>(Ptr->getOperand(1))) {
7933 int64_t Offset = cast<ConstantSDNode>(Ptr->getOperand(1))->getSExtValue();
7934 return BaseIndexOffset(Ptr->getOperand(0), SDValue(), Offset,
7935 IsIndexSignExt);
7936 }
7937
Juergen Ributzka915e9362013-08-21 21:53:38 +00007938 // Inside a loop the current BASE pointer is calculated using an ADD and a
Juergen Ributzka2b884bc2013-08-28 22:33:58 +00007939 // MUL instruction. In this case Ptr is the actual BASE pointer.
Juergen Ributzka915e9362013-08-21 21:53:38 +00007940 // (i64 add (i64 %array_ptr)
7941 // (i64 mul (i64 %induction_var)
7942 // (i64 %element_size)))
Juergen Ributzka2b884bc2013-08-28 22:33:58 +00007943 if (Ptr->getOperand(1)->getOpcode() == ISD::MUL)
Juergen Ributzka915e9362013-08-21 21:53:38 +00007944 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
Juergen Ributzka915e9362013-08-21 21:53:38 +00007945
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007946 // Look at Base + Index + Offset cases.
7947 SDValue Base = Ptr->getOperand(0);
7948 SDValue IndexOffset = Ptr->getOperand(1);
7949
7950 // Skip signextends.
7951 if (IndexOffset->getOpcode() == ISD::SIGN_EXTEND) {
7952 IndexOffset = IndexOffset->getOperand(0);
7953 IsIndexSignExt = true;
7954 }
7955
7956 // Either the case of Base + Index (no offset) or something else.
7957 if (IndexOffset->getOpcode() != ISD::ADD)
7958 return BaseIndexOffset(Base, IndexOffset, 0, IsIndexSignExt);
7959
7960 // Now we have the case of Base + Index + offset.
7961 SDValue Index = IndexOffset->getOperand(0);
7962 SDValue Offset = IndexOffset->getOperand(1);
7963
7964 if (!isa<ConstantSDNode>(Offset))
7965 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
7966
7967 // Ignore signextends.
7968 if (Index->getOpcode() == ISD::SIGN_EXTEND) {
7969 Index = Index->getOperand(0);
7970 IsIndexSignExt = true;
7971 } else IsIndexSignExt = false;
7972
7973 int64_t Off = cast<ConstantSDNode>(Offset)->getSExtValue();
7974 return BaseIndexOffset(Base, Index, Off, IsIndexSignExt);
7975 }
7976};
Nadav Rotemc653de62012-10-03 16:11:15 +00007977
7978/// Holds a pointer to an LSBaseSDNode as well as information on where it
7979/// is located in a sequence of memory operations connected by a chain.
7980struct MemOpLink {
7981 MemOpLink (LSBaseSDNode *N, int64_t Offset, unsigned Seq):
7982 MemNode(N), OffsetFromBase(Offset), SequenceNum(Seq) { }
7983 // Ptr to the mem node.
7984 LSBaseSDNode *MemNode;
7985 // Offset from the base ptr.
7986 int64_t OffsetFromBase;
7987 // What is the sequence number of this mem node.
7988 // Lowest mem operand in the DAG starts at zero.
7989 unsigned SequenceNum;
7990};
7991
7992/// Sorts store nodes in a link according to their offset from a shared
7993// base ptr.
7994struct ConsecutiveMemoryChainSorter {
7995 bool operator()(MemOpLink LHS, MemOpLink RHS) {
7996 return LHS.OffsetFromBase < RHS.OffsetFromBase;
7997 }
7998};
7999
8000bool DAGCombiner::MergeConsecutiveStores(StoreSDNode* St) {
8001 EVT MemVT = St->getMemoryVT();
8002 int64_t ElementSizeBytes = MemVT.getSizeInBits()/8;
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008003 bool NoVectors = DAG.getMachineFunction().getFunction()->getAttributes().
8004 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Nadav Rotemc653de62012-10-03 16:11:15 +00008005
8006 // Don't merge vectors into wider inputs.
8007 if (MemVT.isVector() || !MemVT.isSimple())
8008 return false;
8009
8010 // Perform an early exit check. Do not bother looking at stored values that
8011 // are not constants or loads.
8012 SDValue StoredVal = St->getValue();
8013 bool IsLoadSrc = isa<LoadSDNode>(StoredVal);
8014 if (!isa<ConstantSDNode>(StoredVal) && !isa<ConstantFPSDNode>(StoredVal) &&
8015 !IsLoadSrc)
8016 return false;
8017
8018 // Only look at ends of store sequences.
8019 SDValue Chain = SDValue(St, 1);
8020 if (Chain->hasOneUse() && Chain->use_begin()->getOpcode() == ISD::STORE)
8021 return false;
8022
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008023 // This holds the base pointer, index, and the offset in bytes from the base
8024 // pointer.
8025 BaseIndexOffset BasePtr = BaseIndexOffset::match(St->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00008026
8027 // We must have a base and an offset.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008028 if (!BasePtr.Base.getNode())
Nadav Rotemc653de62012-10-03 16:11:15 +00008029 return false;
8030
8031 // Do not handle stores to undef base pointers.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008032 if (BasePtr.Base.getOpcode() == ISD::UNDEF)
Nadav Rotemc653de62012-10-03 16:11:15 +00008033 return false;
8034
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008035 // Save the LoadSDNodes that we find in the chain.
8036 // We need to make sure that these nodes do not interfere with
8037 // any of the store nodes.
8038 SmallVector<LSBaseSDNode*, 8> AliasLoadNodes;
8039
8040 // Save the StoreSDNodes that we find in the chain.
Nadav Rotemc653de62012-10-03 16:11:15 +00008041 SmallVector<MemOpLink, 8> StoreNodes;
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008042
Nadav Rotemc653de62012-10-03 16:11:15 +00008043 // Walk up the chain and look for nodes with offsets from the same
8044 // base pointer. Stop when reaching an instruction with a different kind
8045 // or instruction which has a different base pointer.
8046 unsigned Seq = 0;
8047 StoreSDNode *Index = St;
8048 while (Index) {
8049 // If the chain has more than one use, then we can't reorder the mem ops.
8050 if (Index != St && !SDValue(Index, 1)->hasOneUse())
8051 break;
8052
8053 // Find the base pointer and offset for this memory node.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008054 BaseIndexOffset Ptr = BaseIndexOffset::match(Index->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00008055
8056 // Check that the base pointer is the same as the original one.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008057 if (!Ptr.equalBaseIndex(BasePtr))
Nadav Rotemc653de62012-10-03 16:11:15 +00008058 break;
8059
8060 // Check that the alignment is the same.
8061 if (Index->getAlignment() != St->getAlignment())
8062 break;
8063
8064 // The memory operands must not be volatile.
8065 if (Index->isVolatile() || Index->isIndexed())
8066 break;
8067
8068 // No truncation.
8069 if (StoreSDNode *St = dyn_cast<StoreSDNode>(Index))
8070 if (St->isTruncatingStore())
8071 break;
8072
8073 // The stored memory type must be the same.
8074 if (Index->getMemoryVT() != MemVT)
8075 break;
8076
8077 // We do not allow unaligned stores because we want to prevent overriding
8078 // stores.
8079 if (Index->getAlignment()*8 != MemVT.getSizeInBits())
8080 break;
8081
8082 // We found a potential memory operand to merge.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008083 StoreNodes.push_back(MemOpLink(Index, Ptr.Offset, Seq++));
Nadav Rotemc653de62012-10-03 16:11:15 +00008084
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008085 // Find the next memory operand in the chain. If the next operand in the
8086 // chain is a store then move up and continue the scan with the next
8087 // memory operand. If the next operand is a load save it and use alias
8088 // information to check if it interferes with anything.
8089 SDNode *NextInChain = Index->getChain().getNode();
8090 while (1) {
Nadav Rotemdde785c2012-12-06 17:34:13 +00008091 if (StoreSDNode *STn = dyn_cast<StoreSDNode>(NextInChain)) {
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008092 // We found a store node. Use it for the next iteration.
Nadav Rotemdde785c2012-12-06 17:34:13 +00008093 Index = STn;
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008094 break;
8095 } else if (LoadSDNode *Ldn = dyn_cast<LoadSDNode>(NextInChain)) {
8096 // Save the load node for later. Continue the scan.
8097 AliasLoadNodes.push_back(Ldn);
8098 NextInChain = Ldn->getChain().getNode();
8099 continue;
8100 } else {
8101 Index = NULL;
8102 break;
8103 }
8104 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008105 }
8106
8107 // Check if there is anything to merge.
8108 if (StoreNodes.size() < 2)
8109 return false;
8110
8111 // Sort the memory operands according to their distance from the base pointer.
8112 std::sort(StoreNodes.begin(), StoreNodes.end(),
8113 ConsecutiveMemoryChainSorter());
8114
8115 // Scan the memory operations on the chain and find the first non-consecutive
8116 // store memory address.
8117 unsigned LastConsecutiveStore = 0;
8118 int64_t StartAddress = StoreNodes[0].OffsetFromBase;
Nadav Rotemdde785c2012-12-06 17:34:13 +00008119 for (unsigned i = 0, e = StoreNodes.size(); i < e; ++i) {
8120
8121 // Check that the addresses are consecutive starting from the second
8122 // element in the list of stores.
8123 if (i > 0) {
8124 int64_t CurrAddress = StoreNodes[i].OffsetFromBase;
8125 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
8126 break;
8127 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008128
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008129 bool Alias = false;
8130 // Check if this store interferes with any of the loads that we found.
8131 for (unsigned ld = 0, lde = AliasLoadNodes.size(); ld < lde; ++ld)
8132 if (isAlias(AliasLoadNodes[ld], StoreNodes[i].MemNode)) {
8133 Alias = true;
8134 break;
8135 }
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008136 // We found a load that alias with this store. Stop the sequence.
8137 if (Alias)
8138 break;
8139
Nadav Rotemc653de62012-10-03 16:11:15 +00008140 // Mark this node as useful.
8141 LastConsecutiveStore = i;
8142 }
8143
8144 // The node with the lowest store address.
8145 LSBaseSDNode *FirstInChain = StoreNodes[0].MemNode;
8146
8147 // Store the constants into memory as one consecutive store.
8148 if (!IsLoadSrc) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008149 unsigned LastLegalType = 0;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008150 unsigned LastLegalVectorType = 0;
8151 bool NonZero = false;
Nadav Rotemc653de62012-10-03 16:11:15 +00008152 for (unsigned i=0; i<LastConsecutiveStore+1; ++i) {
8153 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8154 SDValue StoredVal = St->getValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008155
8156 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(StoredVal)) {
Benjamin Kramerebd7eab2012-10-05 18:19:44 +00008157 NonZero |= !C->isNullValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008158 } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(StoredVal)) {
Benjamin Kramerebd7eab2012-10-05 18:19:44 +00008159 NonZero |= !C->getConstantFPValue()->isNullValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008160 } else {
8161 // Non constant.
Nadav Rotemc653de62012-10-03 16:11:15 +00008162 break;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008163 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008164
Nadav Rotemc653de62012-10-03 16:11:15 +00008165 // Find a legal type for the constant store.
8166 unsigned StoreBW = (i+1) * ElementSizeBytes * 8;
8167 EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8168 if (TLI.isTypeLegal(StoreTy))
8169 LastLegalType = i+1;
Arnold Schwaighofere7370182013-04-02 15:58:51 +00008170 // Or check whether a truncstore is legal.
8171 else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) ==
8172 TargetLowering::TypePromoteInteger) {
8173 EVT LegalizedStoredValueTy =
8174 TLI.getTypeToTransformTo(*DAG.getContext(), StoredVal.getValueType());
8175 if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy))
8176 LastLegalType = i+1;
8177 }
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008178
8179 // Find a legal type for the vector store.
8180 EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1);
8181 if (TLI.isTypeLegal(Ty))
8182 LastLegalVectorType = i + 1;
Nadav Rotemc653de62012-10-03 16:11:15 +00008183 }
8184
Bob Wilson99d8e762012-12-20 01:36:20 +00008185 // We only use vectors if the constant is known to be zero and the
8186 // function is not marked with the noimplicitfloat attribute.
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008187 if (NonZero || NoVectors)
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008188 LastLegalVectorType = 0;
8189
Nadav Rotemc653de62012-10-03 16:11:15 +00008190 // Check if we found a legal integer type to store.
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008191 if (LastLegalType == 0 && LastLegalVectorType == 0)
Nadav Rotemc653de62012-10-03 16:11:15 +00008192 return false;
8193
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008194 bool UseVector = (LastLegalVectorType > LastLegalType) && !NoVectors;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008195 unsigned NumElem = UseVector ? LastLegalVectorType : LastLegalType;
8196
8197 // Make sure we have something to merge.
8198 if (NumElem < 2)
8199 return false;
Nadav Rotemc653de62012-10-03 16:11:15 +00008200
8201 unsigned EarliestNodeUsed = 0;
8202 for (unsigned i=0; i < NumElem; ++i) {
8203 // Find a chain for the new wide-store operand. Notice that some
8204 // of the store nodes that we found may not be selected for inclusion
8205 // in the wide store. The chain we use needs to be the chain of the
8206 // earliest store node which is *used* and replaced by the wide store.
8207 if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum)
8208 EarliestNodeUsed = i;
8209 }
8210
8211 // The earliest Node in the DAG.
8212 LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode;
Andrew Trickac6d9be2013-05-25 02:42:55 +00008213 SDLoc DL(StoreNodes[0].MemNode);
Nadav Rotemc653de62012-10-03 16:11:15 +00008214
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008215 SDValue StoredVal;
8216 if (UseVector) {
8217 // Find a legal type for the vector store.
8218 EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem);
8219 assert(TLI.isTypeLegal(Ty) && "Illegal vector store");
8220 StoredVal = DAG.getConstant(0, Ty);
8221 } else {
8222 unsigned StoreBW = NumElem * ElementSizeBytes * 8;
8223 APInt StoreInt(StoreBW, 0);
8224
8225 // Construct a single integer constant which is made of the smaller
8226 // constant inputs.
8227 bool IsLE = TLI.isLittleEndian();
8228 for (unsigned i = 0; i < NumElem ; ++i) {
8229 unsigned Idx = IsLE ?(NumElem - 1 - i) : i;
8230 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[Idx].MemNode);
8231 SDValue Val = St->getValue();
8232 StoreInt<<=ElementSizeBytes*8;
8233 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val)) {
8234 StoreInt|=C->getAPIntValue().zext(StoreBW);
8235 } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Val)) {
8236 StoreInt|= C->getValueAPF().bitcastToAPInt().zext(StoreBW);
8237 } else {
8238 assert(false && "Invalid constant element type");
8239 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008240 }
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008241
8242 // Create the new Load and Store operations.
8243 EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8244 StoredVal = DAG.getConstant(StoreInt, StoreTy);
Nadav Rotemc653de62012-10-03 16:11:15 +00008245 }
8246
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008247 SDValue NewStore = DAG.getStore(EarliestOp->getChain(), DL, StoredVal,
Nadav Rotemc653de62012-10-03 16:11:15 +00008248 FirstInChain->getBasePtr(),
8249 FirstInChain->getPointerInfo(),
8250 false, false,
8251 FirstInChain->getAlignment());
8252
8253 // Replace the first store with the new store
8254 CombineTo(EarliestOp, NewStore);
8255 // Erase all other stores.
8256 for (unsigned i = 0; i < NumElem ; ++i) {
8257 if (StoreNodes[i].MemNode == EarliestOp)
8258 continue;
8259 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
Rafael Espindola8e2b8ae2012-11-14 05:08:56 +00008260 // ReplaceAllUsesWith will replace all uses that existed when it was
8261 // called, but graph optimizations may cause new ones to appear. For
8262 // example, the case in pr14333 looks like
8263 //
8264 // St's chain -> St -> another store -> X
8265 //
8266 // And the only difference from St to the other store is the chain.
8267 // When we change it's chain to be St's chain they become identical,
8268 // get CSEed and the net result is that X is now a use of St.
8269 // Since we know that St is redundant, just iterate.
8270 while (!St->use_empty())
8271 DAG.ReplaceAllUsesWith(SDValue(St, 0), St->getChain());
Nadav Rotemc653de62012-10-03 16:11:15 +00008272 removeFromWorkList(St);
8273 DAG.DeleteNode(St);
8274 }
8275
8276 return true;
8277 }
8278
8279 // Below we handle the case of multiple consecutive stores that
8280 // come from multiple consecutive loads. We merge them into a single
8281 // wide load and a single wide store.
8282
8283 // Look for load nodes which are used by the stored values.
8284 SmallVector<MemOpLink, 8> LoadNodes;
8285
8286 // Find acceptable loads. Loads need to have the same chain (token factor),
8287 // must not be zext, volatile, indexed, and they must be consecutive.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008288 BaseIndexOffset LdBasePtr;
Nadav Rotemc653de62012-10-03 16:11:15 +00008289 for (unsigned i=0; i<LastConsecutiveStore+1; ++i) {
8290 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8291 LoadSDNode *Ld = dyn_cast<LoadSDNode>(St->getValue());
8292 if (!Ld) break;
8293
8294 // Loads must only have one use.
8295 if (!Ld->hasNUsesOfValue(1, 0))
8296 break;
8297
8298 // Check that the alignment is the same as the stores.
8299 if (Ld->getAlignment() != St->getAlignment())
8300 break;
8301
8302 // The memory operands must not be volatile.
8303 if (Ld->isVolatile() || Ld->isIndexed())
8304 break;
8305
8306 // We do not accept ext loads.
8307 if (Ld->getExtensionType() != ISD::NON_EXTLOAD)
8308 break;
8309
8310 // The stored memory type must be the same.
8311 if (Ld->getMemoryVT() != MemVT)
8312 break;
8313
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008314 BaseIndexOffset LdPtr = BaseIndexOffset::match(Ld->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00008315 // If this is not the first ptr that we check.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008316 if (LdBasePtr.Base.getNode()) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008317 // The base ptr must be the same.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008318 if (!LdPtr.equalBaseIndex(LdBasePtr))
Nadav Rotemc653de62012-10-03 16:11:15 +00008319 break;
8320 } else {
8321 // Check that all other base pointers are the same as this one.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008322 LdBasePtr = LdPtr;
Nadav Rotemc653de62012-10-03 16:11:15 +00008323 }
8324
8325 // We found a potential memory operand to merge.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008326 LoadNodes.push_back(MemOpLink(Ld, LdPtr.Offset, 0));
Nadav Rotemc653de62012-10-03 16:11:15 +00008327 }
8328
8329 if (LoadNodes.size() < 2)
8330 return false;
8331
8332 // Scan the memory operations on the chain and find the first non-consecutive
8333 // load memory address. These variables hold the index in the store node
8334 // array.
8335 unsigned LastConsecutiveLoad = 0;
8336 // This variable refers to the size and not index in the array.
8337 unsigned LastLegalVectorType = 0;
8338 unsigned LastLegalIntegerType = 0;
8339 StartAddress = LoadNodes[0].OffsetFromBase;
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008340 SDValue FirstChain = LoadNodes[0].MemNode->getChain();
8341 for (unsigned i = 1; i < LoadNodes.size(); ++i) {
8342 // All loads much share the same chain.
8343 if (LoadNodes[i].MemNode->getChain() != FirstChain)
8344 break;
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008345
Nadav Rotemc653de62012-10-03 16:11:15 +00008346 int64_t CurrAddress = LoadNodes[i].OffsetFromBase;
8347 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
8348 break;
8349 LastConsecutiveLoad = i;
8350
8351 // Find a legal type for the vector store.
8352 EVT StoreTy = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1);
8353 if (TLI.isTypeLegal(StoreTy))
8354 LastLegalVectorType = i + 1;
8355
8356 // Find a legal type for the integer store.
8357 unsigned StoreBW = (i+1) * ElementSizeBytes * 8;
8358 StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8359 if (TLI.isTypeLegal(StoreTy))
8360 LastLegalIntegerType = i + 1;
Arnold Schwaighofere7370182013-04-02 15:58:51 +00008361 // Or check whether a truncstore and extload is legal.
8362 else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) ==
8363 TargetLowering::TypePromoteInteger) {
8364 EVT LegalizedStoredValueTy =
8365 TLI.getTypeToTransformTo(*DAG.getContext(), StoreTy);
8366 if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy) &&
8367 TLI.isLoadExtLegal(ISD::ZEXTLOAD, StoreTy) &&
8368 TLI.isLoadExtLegal(ISD::SEXTLOAD, StoreTy) &&
8369 TLI.isLoadExtLegal(ISD::EXTLOAD, StoreTy))
8370 LastLegalIntegerType = i+1;
8371 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008372 }
8373
8374 // Only use vector types if the vector type is larger than the integer type.
8375 // If they are the same, use integers.
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008376 bool UseVectorTy = LastLegalVectorType > LastLegalIntegerType && !NoVectors;
Nadav Rotemc653de62012-10-03 16:11:15 +00008377 unsigned LastLegalType = std::max(LastLegalVectorType, LastLegalIntegerType);
8378
8379 // We add +1 here because the LastXXX variables refer to location while
8380 // the NumElem refers to array/index size.
8381 unsigned NumElem = std::min(LastConsecutiveStore, LastConsecutiveLoad) + 1;
8382 NumElem = std::min(LastLegalType, NumElem);
8383
8384 if (NumElem < 2)
8385 return false;
8386
8387 // The earliest Node in the DAG.
8388 unsigned EarliestNodeUsed = 0;
8389 LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode;
8390 for (unsigned i=1; i<NumElem; ++i) {
8391 // Find a chain for the new wide-store operand. Notice that some
8392 // of the store nodes that we found may not be selected for inclusion
8393 // in the wide store. The chain we use needs to be the chain of the
8394 // earliest store node which is *used* and replaced by the wide store.
8395 if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum)
8396 EarliestNodeUsed = i;
8397 }
8398
8399 // Find if it is better to use vectors or integers to load and store
8400 // to memory.
8401 EVT JointMemOpVT;
8402 if (UseVectorTy) {
8403 JointMemOpVT = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem);
8404 } else {
8405 unsigned StoreBW = NumElem * ElementSizeBytes * 8;
8406 JointMemOpVT = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8407 }
8408
Andrew Trickac6d9be2013-05-25 02:42:55 +00008409 SDLoc LoadDL(LoadNodes[0].MemNode);
8410 SDLoc StoreDL(StoreNodes[0].MemNode);
Nadav Rotemc653de62012-10-03 16:11:15 +00008411
8412 LoadSDNode *FirstLoad = cast<LoadSDNode>(LoadNodes[0].MemNode);
8413 SDValue NewLoad = DAG.getLoad(JointMemOpVT, LoadDL,
8414 FirstLoad->getChain(),
8415 FirstLoad->getBasePtr(),
8416 FirstLoad->getPointerInfo(),
8417 false, false, false,
8418 FirstLoad->getAlignment());
8419
8420 SDValue NewStore = DAG.getStore(EarliestOp->getChain(), StoreDL, NewLoad,
8421 FirstInChain->getBasePtr(),
8422 FirstInChain->getPointerInfo(), false, false,
8423 FirstInChain->getAlignment());
8424
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008425 // Replace one of the loads with the new load.
8426 LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[0].MemNode);
8427 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1),
8428 SDValue(NewLoad.getNode(), 1));
8429
8430 // Remove the rest of the load chains.
8431 for (unsigned i = 1; i < NumElem ; ++i) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008432 // Replace all chain users of the old load nodes with the chain of the new
8433 // load node.
8434 LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[i].MemNode);
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008435 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), Ld->getChain());
8436 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008437
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008438 // Replace the first store with the new store.
8439 CombineTo(EarliestOp, NewStore);
8440 // Erase all other stores.
8441 for (unsigned i = 0; i < NumElem ; ++i) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008442 // Remove all Store nodes.
8443 if (StoreNodes[i].MemNode == EarliestOp)
8444 continue;
8445 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8446 DAG.ReplaceAllUsesOfValueWith(SDValue(St, 0), St->getChain());
8447 removeFromWorkList(St);
8448 DAG.DeleteNode(St);
8449 }
8450
8451 return true;
8452}
8453
Dan Gohman475871a2008-07-27 21:46:04 +00008454SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00008455 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00008456 SDValue Chain = ST->getChain();
8457 SDValue Value = ST->getValue();
8458 SDValue Ptr = ST->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00008459
Evan Cheng59d5b682007-05-07 21:27:48 +00008460 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00008461 // resultant store does not need a higher alignment than the original.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008462 if (Value.getOpcode() == ISD::BITCAST && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00008463 ST->isUnindexed()) {
Dan Gohman1ba519b2009-02-20 23:29:13 +00008464 unsigned OrigAlign = ST->getAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008465 EVT SVT = Value.getOperand(0).getValueType();
Micah Villmow3574eca2012-10-08 16:38:25 +00008466 unsigned Align = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00008467 getABITypeAlignment(SVT.getTypeForEVT(*DAG.getContext()));
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008468 if (Align <= OrigAlign &&
Duncan Sands25cf2272008-11-24 14:53:14 +00008469 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohmanf560ffa2009-01-28 17:46:25 +00008470 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00008471 return DAG.getStore(Chain, SDLoc(N), Value.getOperand(0),
Chris Lattner6229d0a2010-09-21 18:41:36 +00008472 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00008473 ST->isNonTemporal(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00008474 }
Owen Andersona34d9362011-04-14 17:30:49 +00008475
Chris Lattnerb3452ea2011-04-09 02:32:02 +00008476 // Turn 'store undef, Ptr' -> nothing.
8477 if (Value.getOpcode() == ISD::UNDEF && ST->isUnindexed())
8478 return Chain;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008479
Nate Begeman2cbba892006-12-11 02:23:46 +00008480 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00008481 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008482 // NOTE: If the original store is volatile, this transform must not increase
8483 // the number of stores. For example, on x86-32 an f64 can be stored in one
8484 // processor operation but an i64 (which is not legal) requires two. So the
8485 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00008486 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00008487 SDValue Tmp;
Craig Topper0ff11902013-08-15 02:44:19 +00008488 switch (CFP->getSimpleValueType(0).SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008489 default: llvm_unreachable("Unknown FP type");
Pete Cooper438c0402012-06-21 18:00:39 +00008490 case MVT::f16: // We don't do this for these yet.
8491 case MVT::f80:
Owen Anderson825b72b2009-08-11 20:47:22 +00008492 case MVT::f128:
8493 case MVT::ppcf128:
Dale Johannesenc7b21d52007-09-18 18:36:59 +00008494 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008495 case MVT::f32:
Chris Lattner2392ae72010-04-15 04:48:01 +00008496 if ((isTypeLegal(MVT::i32) && !LegalOperations && !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00008497 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00008498 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Owen Anderson825b72b2009-08-11 20:47:22 +00008499 bitcastToAPInt().getZExtValue(), MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008500 return DAG.getStore(Chain, SDLoc(N), Tmp,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008501 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00008502 ST->isNonTemporal(), ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00008503 }
8504 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008505 case MVT::f64:
Chris Lattner2392ae72010-04-15 04:48:01 +00008506 if ((TLI.isTypeLegal(MVT::i64) && !LegalOperations &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00008507 !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00008508 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen7111b022008-10-09 18:53:47 +00008509 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +00008510 getZExtValue(), MVT::i64);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008511 return DAG.getStore(Chain, SDLoc(N), Tmp,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008512 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00008513 ST->isNonTemporal(), ST->getAlignment());
Chris Lattnerb3452ea2011-04-09 02:32:02 +00008514 }
Owen Andersona34d9362011-04-14 17:30:49 +00008515
Chris Lattnerb3452ea2011-04-09 02:32:02 +00008516 if (!ST->isVolatile() &&
8517 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00008518 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00008519 // argument passing. Since this is so common, custom legalize the
8520 // 64-bit integer store into two 32-bit stores.
Dale Johannesen7111b022008-10-09 18:53:47 +00008521 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00008522 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
8523 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00008524 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00008525
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00008526 unsigned Alignment = ST->getAlignment();
8527 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00008528 bool isNonTemporal = ST->isNonTemporal();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00008529
Andrew Trickac6d9be2013-05-25 02:42:55 +00008530 SDValue St0 = DAG.getStore(Chain, SDLoc(ST), Lo,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008531 Ptr, ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00008532 isVolatile, isNonTemporal,
8533 ST->getAlignment());
Andrew Trickac6d9be2013-05-25 02:42:55 +00008534 Ptr = DAG.getNode(ISD::ADD, SDLoc(N), Ptr.getValueType(), Ptr,
Chris Lattner62be1a72006-12-12 04:16:14 +00008535 DAG.getConstant(4, Ptr.getValueType()));
Duncan Sandsdc846502007-10-28 12:59:45 +00008536 Alignment = MinAlign(Alignment, 4U);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008537 SDValue St1 = DAG.getStore(Chain, SDLoc(ST), Hi,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008538 Ptr, ST->getPointerInfo().getWithOffset(4),
8539 isVolatile, isNonTemporal,
David Greene1e559442010-02-15 17:00:31 +00008540 Alignment);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008541 return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other,
Bill Wendlingc144a572009-01-30 23:36:47 +00008542 St0, St1);
Chris Lattner62be1a72006-12-12 04:16:14 +00008543 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008544
Chris Lattner62be1a72006-12-12 04:16:14 +00008545 break;
Evan Cheng25ece662006-12-11 17:25:19 +00008546 }
Nate Begeman2cbba892006-12-11 02:23:46 +00008547 }
Nate Begeman2cbba892006-12-11 02:23:46 +00008548 }
8549
Evan Cheng255f20f2010-04-01 06:04:33 +00008550 // Try to infer better alignment information than the store already has.
8551 if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00008552 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
8553 if (Align > ST->getAlignment())
Andrew Trickac6d9be2013-05-25 02:42:55 +00008554 return DAG.getTruncStore(Chain, SDLoc(N), Value,
Evan Chenged1c0c72011-11-28 22:37:34 +00008555 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
8556 ST->isVolatile(), ST->isNonTemporal(), Align);
Evan Cheng255f20f2010-04-01 06:04:33 +00008557 }
8558 }
8559
Evan Cheng31959b12011-02-02 01:06:55 +00008560 // Try transforming a pair floating point load / store ops to integer
8561 // load / store ops.
8562 SDValue NewST = TransformFPLoadStorePair(N);
8563 if (NewST.getNode())
8564 return NewST;
8565
Hal Finkel253acef2013-08-29 03:29:55 +00008566 bool UseAA = CombinerAA.getNumOccurrences() > 0 ? CombinerAA :
8567 TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA();
8568 if (UseAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00008569 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008570 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00008571
Jim Laskey6ff23e52006-10-04 16:53:27 +00008572 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00008573 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00008574 SDValue ReplStore;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008575
8576 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00008577 if (ST->isTruncatingStore()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008578 ReplStore = DAG.getTruncStore(BetterChain, SDLoc(N), Value, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00008579 ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00008580 ST->getMemoryVT(), ST->isVolatile(),
8581 ST->isNonTemporal(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00008582 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008583 ReplStore = DAG.getStore(BetterChain, SDLoc(N), Value, Ptr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008584 ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00008585 ST->isVolatile(), ST->isNonTemporal(),
8586 ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00008587 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008588
Jim Laskey279f0532006-09-25 16:29:54 +00008589 // Create token to keep both nodes around.
Andrew Trickac6d9be2013-05-25 02:42:55 +00008590 SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson825b72b2009-08-11 20:47:22 +00008591 MVT::Other, Chain, ReplStore);
Bill Wendlingc144a572009-01-30 23:36:47 +00008592
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008593 // Make sure the new and old chains are cleaned up.
8594 AddToWorkList(Token.getNode());
8595
Jim Laskey274062c2006-10-13 23:32:28 +00008596 // Don't add users to work list.
8597 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00008598 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00008599 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008600
Evan Cheng33dbedc2006-11-05 09:31:14 +00008601 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00008602 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00008603 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00008604
Chris Lattner3c872852007-12-29 06:26:16 +00008605 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00008606 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Nadav Rotembaff46f2011-06-15 11:19:12 +00008607 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00008608 // See if we can simplify the input to this truncstore with knowledge that
8609 // only the low bits are being used. For example:
8610 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Scott Michelfdc40a02009-02-17 22:15:04 +00008611 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00008612 GetDemandedBits(Value,
Nadav Rotembaff46f2011-06-15 11:19:12 +00008613 APInt::getLowBitsSet(
8614 Value.getValueType().getScalarType().getSizeInBits(),
8615 ST->getMemoryVT().getScalarType().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00008616 AddToWorkList(Value.getNode());
8617 if (Shorter.getNode())
Andrew Trickac6d9be2013-05-25 02:42:55 +00008618 return DAG.getTruncStore(Chain, SDLoc(N), Shorter,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00008619 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00008620 ST->isVolatile(), ST->isNonTemporal(),
8621 ST->getAlignment());
Scott Michelfdc40a02009-02-17 22:15:04 +00008622
Chris Lattnere33544c2007-10-13 06:58:48 +00008623 // Otherwise, see if we can simplify the operation with
8624 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00008625 if (SimplifyDemandedBits(Value,
Eric Christopher503a64d2010-12-09 04:48:06 +00008626 APInt::getLowBitsSet(
8627 Value.getValueType().getScalarType().getSizeInBits(),
8628 ST->getMemoryVT().getScalarType().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00008629 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00008630 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008631
Chris Lattner3c872852007-12-29 06:26:16 +00008632 // If this is a load followed by a store to the same location, then the store
8633 // is dead/noop.
8634 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008635 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00008636 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00008637 // There can't be any side effects between the load and store, such as
8638 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00008639 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00008640 // The store is dead, remove it.
8641 return Chain;
8642 }
8643 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008644
Chris Lattnerddf89562008-01-17 19:59:44 +00008645 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
8646 // truncating store. We can do this even if this is already a truncstore.
8647 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00008648 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00008649 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008650 ST->getMemoryVT())) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008651 return DAG.getTruncStore(Chain, SDLoc(N), Value.getOperand(0),
Chris Lattnerda2d8e12010-09-21 17:42:31 +00008652 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00008653 ST->isVolatile(), ST->isNonTemporal(),
8654 ST->getAlignment());
Chris Lattnerddf89562008-01-17 19:59:44 +00008655 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008656
Nadav Rotemc653de62012-10-03 16:11:15 +00008657 // Only perform this optimization before the types are legal, because we
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008658 // don't want to perform this optimization on every DAGCombine invocation.
Nadav Rotema569a802012-12-02 17:14:09 +00008659 if (!LegalTypes) {
8660 bool EverChanged = false;
8661
8662 do {
8663 // There can be multiple store sequences on the same chain.
8664 // Keep trying to merge store sequences until we are unable to do so
8665 // or until we merge the last store on the chain.
8666 bool Changed = MergeConsecutiveStores(ST);
8667 EverChanged |= Changed;
8668 if (!Changed) break;
8669 } while (ST->getOpcode() != ISD::DELETED_NODE);
8670
8671 if (EverChanged)
8672 return SDValue(N, 0);
8673 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008674
Evan Cheng8b944d32009-05-28 00:35:15 +00008675 return ReduceLoadOpStoreWidth(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00008676}
8677
Dan Gohman475871a2008-07-27 21:46:04 +00008678SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
8679 SDValue InVec = N->getOperand(0);
8680 SDValue InVal = N->getOperand(1);
8681 SDValue EltNo = N->getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008682 SDLoc dl(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00008683
Bob Wilson492fd452010-05-19 23:42:58 +00008684 // If the inserted element is an UNDEF, just use the input vector.
8685 if (InVal.getOpcode() == ISD::UNDEF)
8686 return InVec;
8687
Nadav Rotem609d54e2011-02-12 14:40:33 +00008688 EVT VT = InVec.getValueType();
8689
Owen Anderson95771af2011-02-25 21:41:48 +00008690 // If we can't generate a legal BUILD_VECTOR, exit
Nadav Rotem609d54e2011-02-12 14:40:33 +00008691 if (LegalOperations && !TLI.isOperationLegal(ISD::BUILD_VECTOR, VT))
8692 return SDValue();
8693
Eli Friedman9db817f2011-09-09 21:04:06 +00008694 // Check that we know which element is being inserted
8695 if (!isa<ConstantSDNode>(EltNo))
8696 return SDValue();
8697 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008698
Eli Friedman9db817f2011-09-09 21:04:06 +00008699 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
8700 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
8701 // vector elements.
8702 SmallVector<SDValue, 8> Ops;
Quentin Colombet75c94332013-07-30 00:24:09 +00008703 // Do not combine these two vectors if the output vector will not replace
8704 // the input vector.
8705 if (InVec.getOpcode() == ISD::BUILD_VECTOR && InVec.hasOneUse()) {
Eli Friedman9db817f2011-09-09 21:04:06 +00008706 Ops.append(InVec.getNode()->op_begin(),
8707 InVec.getNode()->op_end());
8708 } else if (InVec.getOpcode() == ISD::UNDEF) {
8709 unsigned NElts = VT.getVectorNumElements();
8710 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
8711 } else {
8712 return SDValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00008713 }
Eli Friedman9db817f2011-09-09 21:04:06 +00008714
8715 // Insert the element
8716 if (Elt < Ops.size()) {
8717 // All the operands of BUILD_VECTOR must have the same type;
8718 // we enforce that here.
8719 EVT OpVT = Ops[0].getValueType();
8720 if (InVal.getValueType() != OpVT)
8721 InVal = OpVT.bitsGT(InVal.getValueType()) ?
8722 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
8723 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
8724 Ops[Elt] = InVal;
8725 }
8726
8727 // Return the new vector
8728 return DAG.getNode(ISD::BUILD_VECTOR, dl,
8729 VT, &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00008730}
8731
Dan Gohman475871a2008-07-27 21:46:04 +00008732SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00008733 // (vextract (scalar_to_vector val, 0) -> val
8734 SDValue InVec = N->getOperand(0);
Nadav Rotemba05c912012-01-17 21:44:01 +00008735 EVT VT = InVec.getValueType();
8736 EVT NVT = N->getValueType(0);
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00008737
Duncan Sandsc356f332011-05-09 08:03:33 +00008738 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
8739 // Check if the result type doesn't match the inserted element type. A
8740 // SCALAR_TO_VECTOR may truncate the inserted element and the
8741 // EXTRACT_VECTOR_ELT may widen the extracted vector.
8742 SDValue InOp = InVec.getOperand(0);
Duncan Sandsc356f332011-05-09 08:03:33 +00008743 if (InOp.getValueType() != NVT) {
8744 assert(InOp.getValueType().isInteger() && NVT.isInteger());
Andrew Trickac6d9be2013-05-25 02:42:55 +00008745 return DAG.getSExtOrTrunc(InOp, SDLoc(InVec), NVT);
Duncan Sandsc356f332011-05-09 08:03:33 +00008746 }
8747 return InOp;
8748 }
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008749
Nadav Rotemba05c912012-01-17 21:44:01 +00008750 SDValue EltNo = N->getOperand(1);
8751 bool ConstEltNo = isa<ConstantSDNode>(EltNo);
8752
8753 // Transform: (EXTRACT_VECTOR_ELT( VECTOR_SHUFFLE )) -> EXTRACT_VECTOR_ELT.
8754 // We only perform this optimization before the op legalization phase because
Nadav Rotem6dfabb62012-09-20 08:53:31 +00008755 // we may introduce new vector instructions which are not backed by TD
8756 // patterns. For example on AVX, extracting elements from a wide vector
8757 // without using extract_subvector.
Nadav Rotemba05c912012-01-17 21:44:01 +00008758 if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE
8759 && ConstEltNo && !LegalOperations) {
8760 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
8761 int NumElem = VT.getVectorNumElements();
8762 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(InVec);
8763 // Find the new index to extract from.
8764 int OrigElt = SVOp->getMaskElt(Elt);
8765
8766 // Extracting an undef index is undef.
8767 if (OrigElt == -1)
8768 return DAG.getUNDEF(NVT);
8769
8770 // Select the right vector half to extract from.
8771 if (OrigElt < NumElem) {
8772 InVec = InVec->getOperand(0);
8773 } else {
8774 InVec = InVec->getOperand(1);
8775 OrigElt -= NumElem;
8776 }
8777
Tom Stellard425b76c2013-08-05 22:22:01 +00008778 EVT IndexTy = TLI.getVectorIdxTy();
Andrew Trickac6d9be2013-05-25 02:42:55 +00008779 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N), NVT,
Jim Grosbacha249f7d2012-05-08 20:56:07 +00008780 InVec, DAG.getConstant(OrigElt, IndexTy));
Nadav Rotemba05c912012-01-17 21:44:01 +00008781 }
8782
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008783 // Perform only after legalization to ensure build_vector / vector_shuffle
8784 // optimizations have already been done.
Duncan Sands25cf2272008-11-24 14:53:14 +00008785 if (!LegalOperations) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008786
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00008787 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
8788 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
8789 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Evan Cheng513da432007-10-06 08:19:55 +00008790
Nadav Rotemba05c912012-01-17 21:44:01 +00008791 if (ConstEltNo) {
Eric Christophercaebdd42010-11-03 09:36:40 +00008792 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00008793 bool NewLoad = false;
Mon P Wanga60b5232008-12-11 00:26:16 +00008794 bool BCNumEltsChanged = false;
Owen Andersone50ed302009-08-10 22:56:29 +00008795 EVT ExtVT = VT.getVectorElementType();
8796 EVT LVT = ExtVT;
Bill Wendlingc144a572009-01-30 23:36:47 +00008797
Evan Cheng84387ea2012-03-13 22:00:52 +00008798 // If the result of load has to be truncated, then it's not necessarily
8799 // profitable.
Evan Chenga03d3662012-03-13 22:16:11 +00008800 if (NVT.bitsLT(LVT) && !TLI.isTruncateFree(LVT, NVT))
Evan Cheng84387ea2012-03-13 22:00:52 +00008801 return SDValue();
8802
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008803 if (InVec.getOpcode() == ISD::BITCAST) {
Eli Friedmand6e25602011-12-26 22:49:32 +00008804 // Don't duplicate a load with other uses.
8805 if (!InVec.hasOneUse())
8806 return SDValue();
8807
Owen Andersone50ed302009-08-10 22:56:29 +00008808 EVT BCVT = InVec.getOperand(0).getValueType();
8809 if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008810 return SDValue();
Mon P Wanga60b5232008-12-11 00:26:16 +00008811 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
8812 BCNumEltsChanged = true;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008813 InVec = InVec.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00008814 ExtVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008815 NewLoad = true;
8816 }
Evan Cheng513da432007-10-06 08:19:55 +00008817
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008818 LoadSDNode *LN0 = NULL;
Nate Begeman5a5ca152009-04-29 05:20:52 +00008819 const ShuffleVectorSDNode *SVN = NULL;
Bill Wendlingc144a572009-01-30 23:36:47 +00008820 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008821 LN0 = cast<LoadSDNode>(InVec);
Bill Wendlingc144a572009-01-30 23:36:47 +00008822 } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Owen Andersone50ed302009-08-10 22:56:29 +00008823 InVec.getOperand(0).getValueType() == ExtVT &&
Bill Wendlingc144a572009-01-30 23:36:47 +00008824 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Eli Friedmand6e25602011-12-26 22:49:32 +00008825 // Don't duplicate a load with other uses.
8826 if (!InVec.hasOneUse())
8827 return SDValue();
8828
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008829 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
Nate Begeman5a5ca152009-04-29 05:20:52 +00008830 } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008831 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
8832 // =>
8833 // (load $addr+1*size)
Scott Michelfdc40a02009-02-17 22:15:04 +00008834
Eli Friedmand6e25602011-12-26 22:49:32 +00008835 // Don't duplicate a load with other uses.
8836 if (!InVec.hasOneUse())
8837 return SDValue();
8838
Mon P Wanga60b5232008-12-11 00:26:16 +00008839 // If the bit convert changed the number of elements, it is unsafe
8840 // to examine the mask.
8841 if (BCNumEltsChanged)
8842 return SDValue();
Nate Begeman5a5ca152009-04-29 05:20:52 +00008843
8844 // Select the input vector, guarding against out of range extract vector.
8845 unsigned NumElems = VT.getVectorNumElements();
Eric Christophercaebdd42010-11-03 09:36:40 +00008846 int Idx = (Elt > (int)NumElems) ? -1 : SVN->getMaskElt(Elt);
Nate Begeman5a5ca152009-04-29 05:20:52 +00008847 InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
8848
Eli Friedmand6e25602011-12-26 22:49:32 +00008849 if (InVec.getOpcode() == ISD::BITCAST) {
8850 // Don't duplicate a load with other uses.
8851 if (!InVec.hasOneUse())
8852 return SDValue();
8853
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008854 InVec = InVec.getOperand(0);
Eli Friedmand6e25602011-12-26 22:49:32 +00008855 }
Gabor Greifba36cb52008-08-28 21:40:38 +00008856 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008857 LN0 = cast<LoadSDNode>(InVec);
Ted Kremenekd0e88f32010-04-08 18:49:30 +00008858 Elt = (Idx < (int)NumElems) ? Idx : Idx - (int)NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00008859 }
8860 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008861
Eli Friedmand6e25602011-12-26 22:49:32 +00008862 // Make sure we found a non-volatile load and the extractelement is
8863 // the only use.
Nadav Rotem42febc62011-05-11 14:40:50 +00008864 if (!LN0 || !LN0->hasNUsesOfValue(1,0) || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00008865 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008866
Eric Christopherd81f17a2010-11-03 20:44:42 +00008867 // If Idx was -1 above, Elt is going to be -1, so just return undef.
8868 if (Elt == -1)
Eli Friedmaned4b4272011-07-25 22:25:42 +00008869 return DAG.getUNDEF(LVT);
Eric Christopherd81f17a2010-11-03 20:44:42 +00008870
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008871 unsigned Align = LN0->getAlignment();
8872 if (NewLoad) {
8873 // Check the resultant load doesn't need a higher alignment than the
8874 // original load.
Bill Wendlingc144a572009-01-30 23:36:47 +00008875 unsigned NewAlign =
Micah Villmow3574eca2012-10-08 16:38:25 +00008876 TLI.getDataLayout()
Eric Christopher503a64d2010-12-09 04:48:06 +00008877 ->getABITypeAlignment(LVT.getTypeForEVT(*DAG.getContext()));
Bill Wendlingc144a572009-01-30 23:36:47 +00008878
Dan Gohmanf560ffa2009-01-28 17:46:25 +00008879 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00008880 return SDValue();
Bill Wendlingc144a572009-01-30 23:36:47 +00008881
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008882 Align = NewAlign;
8883 }
8884
Dan Gohman475871a2008-07-27 21:46:04 +00008885 SDValue NewPtr = LN0->getBasePtr();
Chris Lattnerfa459012010-09-21 16:08:50 +00008886 unsigned PtrOff = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008887
Eric Christopherd81f17a2010-11-03 20:44:42 +00008888 if (Elt) {
Chris Lattnerfa459012010-09-21 16:08:50 +00008889 PtrOff = LVT.getSizeInBits() * Elt / 8;
Owen Andersone50ed302009-08-10 22:56:29 +00008890 EVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008891 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00008892 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Andrew Trickac6d9be2013-05-25 02:42:55 +00008893 NewPtr = DAG.getNode(ISD::ADD, SDLoc(N), PtrType, NewPtr,
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008894 DAG.getConstant(PtrOff, PtrType));
8895 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008896
Eli Friedman4db4add2011-11-16 23:50:22 +00008897 // The replacement we need to do here is a little tricky: we need to
8898 // replace an extractelement of a load with a load.
8899 // Use ReplaceAllUsesOfValuesWith to do the replacement.
Eli Friedmand6e25602011-12-26 22:49:32 +00008900 // Note that this replacement assumes that the extractvalue is the only
8901 // use of the load; that's okay because we don't want to perform this
8902 // transformation in other cases anyway.
Evan Cheng84387ea2012-03-13 22:00:52 +00008903 SDValue Load;
Evan Chenga03d3662012-03-13 22:16:11 +00008904 SDValue Chain;
Evan Cheng84387ea2012-03-13 22:00:52 +00008905 if (NVT.bitsGT(LVT)) {
8906 // If the result type of vextract is wider than the load, then issue an
8907 // extending load instead.
8908 ISD::LoadExtType ExtType = TLI.isLoadExtLegal(ISD::ZEXTLOAD, LVT)
8909 ? ISD::ZEXTLOAD : ISD::EXTLOAD;
Andrew Trickac6d9be2013-05-25 02:42:55 +00008910 Load = DAG.getExtLoad(ExtType, SDLoc(N), NVT, LN0->getChain(),
Evan Cheng84387ea2012-03-13 22:00:52 +00008911 NewPtr, LN0->getPointerInfo().getWithOffset(PtrOff),
8912 LVT, LN0->isVolatile(), LN0->isNonTemporal(),Align);
Evan Chenga03d3662012-03-13 22:16:11 +00008913 Chain = Load.getValue(1);
8914 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008915 Load = DAG.getLoad(LVT, SDLoc(N), LN0->getChain(), NewPtr,
Evan Cheng84387ea2012-03-13 22:00:52 +00008916 LN0->getPointerInfo().getWithOffset(PtrOff),
Stephen Lin155615d2013-07-08 00:37:03 +00008917 LN0->isVolatile(), LN0->isNonTemporal(),
Evan Cheng84387ea2012-03-13 22:00:52 +00008918 LN0->isInvariant(), Align);
Evan Chenga03d3662012-03-13 22:16:11 +00008919 Chain = Load.getValue(1);
8920 if (NVT.bitsLT(LVT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00008921 Load = DAG.getNode(ISD::TRUNCATE, SDLoc(N), NVT, Load);
Evan Chenga03d3662012-03-13 22:16:11 +00008922 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00008923 Load = DAG.getNode(ISD::BITCAST, SDLoc(N), NVT, Load);
Evan Chenga03d3662012-03-13 22:16:11 +00008924 }
Eli Friedman4db4add2011-11-16 23:50:22 +00008925 WorkListRemover DeadNodes(*this);
8926 SDValue From[] = { SDValue(N, 0), SDValue(LN0,1) };
Evan Chenga03d3662012-03-13 22:16:11 +00008927 SDValue To[] = { Load, Chain };
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00008928 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
Eli Friedman4db4add2011-11-16 23:50:22 +00008929 // Since we're explcitly calling ReplaceAllUses, add the new node to the
8930 // worklist explicitly as well.
8931 AddToWorkList(Load.getNode());
Craig Topper0c9da212012-03-20 05:28:39 +00008932 AddUsersToWorkList(Load.getNode()); // Add users too
Eli Friedman4db4add2011-11-16 23:50:22 +00008933 // Make sure to revisit this node to clean it up; it will usually be dead.
8934 AddToWorkList(N);
8935 return SDValue(N, 0);
Evan Cheng513da432007-10-06 08:19:55 +00008936 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008937
Dan Gohman475871a2008-07-27 21:46:04 +00008938 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00008939}
Evan Cheng513da432007-10-06 08:19:55 +00008940
Michael Liaofac14ab2012-10-23 23:06:52 +00008941// Simplify (build_vec (ext )) to (bitcast (build_vec ))
8942SDValue DAGCombiner::reduceBuildVecExtToExtBuildVec(SDNode *N) {
8943 // We perform this optimization post type-legalization because
8944 // the type-legalizer often scalarizes integer-promoted vectors.
8945 // Performing this optimization before may create bit-casts which
8946 // will be type-legalized to complex code sequences.
8947 // We perform this optimization only before the operation legalizer because we
8948 // may introduce illegal operations.
8949 if (Level != AfterLegalizeVectorOps && Level != AfterLegalizeTypes)
8950 return SDValue();
8951
Dan Gohman7f321562007-06-25 16:23:39 +00008952 unsigned NumInScalars = N->getNumOperands();
Andrew Trickac6d9be2013-05-25 02:42:55 +00008953 SDLoc dl(N);
Owen Andersone50ed302009-08-10 22:56:29 +00008954 EVT VT = N->getValueType(0);
Nadav Rotemb87bdac2012-07-15 08:38:23 +00008955
Nadav Rotemb00418a2011-10-29 21:23:04 +00008956 // Check to see if this is a BUILD_VECTOR of a bunch of values
8957 // which come from any_extend or zero_extend nodes. If so, we can create
8958 // a new BUILD_VECTOR using bit-casts which may enable other BUILD_VECTOR
Nadav Rotemf47368b2011-10-31 20:08:25 +00008959 // optimizations. We do not handle sign-extend because we can't fill the sign
8960 // using shuffles.
Nadav Rotemb00418a2011-10-29 21:23:04 +00008961 EVT SourceType = MVT::Other;
Craig Topperd3b58892012-01-17 09:09:48 +00008962 bool AllAnyExt = true;
Nadav Rotemb87bdac2012-07-15 08:38:23 +00008963
Craig Topperd3b58892012-01-17 09:09:48 +00008964 for (unsigned i = 0; i != NumInScalars; ++i) {
Nadav Rotemb00418a2011-10-29 21:23:04 +00008965 SDValue In = N->getOperand(i);
8966 // Ignore undef inputs.
8967 if (In.getOpcode() == ISD::UNDEF) continue;
8968
8969 bool AnyExt = In.getOpcode() == ISD::ANY_EXTEND;
8970 bool ZeroExt = In.getOpcode() == ISD::ZERO_EXTEND;
8971
Nadav Rotemf47368b2011-10-31 20:08:25 +00008972 // Abort if the element is not an extension.
Nadav Rotemb00418a2011-10-29 21:23:04 +00008973 if (!ZeroExt && !AnyExt) {
Nadav Rotemf47368b2011-10-31 20:08:25 +00008974 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008975 break;
8976 }
8977
8978 // The input is a ZeroExt or AnyExt. Check the original type.
8979 EVT InTy = In.getOperand(0).getValueType();
8980
8981 // Check that all of the widened source types are the same.
8982 if (SourceType == MVT::Other)
Nadav Rotemf47368b2011-10-31 20:08:25 +00008983 // First time.
Nadav Rotemb00418a2011-10-29 21:23:04 +00008984 SourceType = InTy;
8985 else if (InTy != SourceType) {
8986 // Multiple income types. Abort.
Nadav Rotemf47368b2011-10-31 20:08:25 +00008987 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008988 break;
8989 }
8990
8991 // Check if all of the extends are ANY_EXTENDs.
Craig Topperd3b58892012-01-17 09:09:48 +00008992 AllAnyExt &= AnyExt;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008993 }
8994
Nadav Rotemf47368b2011-10-31 20:08:25 +00008995 // In order to have valid types, all of the inputs must be extended from the
8996 // same source type and all of the inputs must be any or zero extend.
8997 // Scalar sizes must be a power of two.
Michael Liaofac14ab2012-10-23 23:06:52 +00008998 EVT OutScalarTy = VT.getScalarType();
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008999 bool ValidTypes = SourceType != MVT::Other &&
Nadav Rotemf47368b2011-10-31 20:08:25 +00009000 isPowerOf2_32(OutScalarTy.getSizeInBits()) &&
9001 isPowerOf2_32(SourceType.getSizeInBits());
9002
Nadav Rotem6431ff92012-03-15 08:49:06 +00009003 // Create a new simpler BUILD_VECTOR sequence which other optimizations can
9004 // turn into a single shuffle instruction.
Michael Liaofac14ab2012-10-23 23:06:52 +00009005 if (!ValidTypes)
9006 return SDValue();
Nadav Rotemb00418a2011-10-29 21:23:04 +00009007
Michael Liaofac14ab2012-10-23 23:06:52 +00009008 bool isLE = TLI.isLittleEndian();
9009 unsigned ElemRatio = OutScalarTy.getSizeInBits()/SourceType.getSizeInBits();
9010 assert(ElemRatio > 1 && "Invalid element size ratio");
9011 SDValue Filler = AllAnyExt ? DAG.getUNDEF(SourceType):
9012 DAG.getConstant(0, SourceType);
Nadav Rotemb00418a2011-10-29 21:23:04 +00009013
Michael Liaofac14ab2012-10-23 23:06:52 +00009014 unsigned NewBVElems = ElemRatio * VT.getVectorNumElements();
9015 SmallVector<SDValue, 8> Ops(NewBVElems, Filler);
Nadav Rotemb00418a2011-10-29 21:23:04 +00009016
Michael Liaofac14ab2012-10-23 23:06:52 +00009017 // Populate the new build_vector
Jakub Staszakadf38912012-10-24 00:38:25 +00009018 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Michael Liaofac14ab2012-10-23 23:06:52 +00009019 SDValue Cast = N->getOperand(i);
9020 assert((Cast.getOpcode() == ISD::ANY_EXTEND ||
9021 Cast.getOpcode() == ISD::ZERO_EXTEND ||
9022 Cast.getOpcode() == ISD::UNDEF) && "Invalid cast opcode");
9023 SDValue In;
9024 if (Cast.getOpcode() == ISD::UNDEF)
9025 In = DAG.getUNDEF(SourceType);
9026 else
9027 In = Cast->getOperand(0);
9028 unsigned Index = isLE ? (i * ElemRatio) :
9029 (i * ElemRatio + (ElemRatio - 1));
Nadav Rotemb00418a2011-10-29 21:23:04 +00009030
Michael Liaofac14ab2012-10-23 23:06:52 +00009031 assert(Index < Ops.size() && "Invalid index");
9032 Ops[Index] = In;
Nadav Rotemb00418a2011-10-29 21:23:04 +00009033 }
Chris Lattnerca242442006-03-19 01:27:56 +00009034
Michael Liaofac14ab2012-10-23 23:06:52 +00009035 // The type of the new BUILD_VECTOR node.
9036 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), SourceType, NewBVElems);
9037 assert(VecVT.getSizeInBits() == VT.getSizeInBits() &&
9038 "Invalid vector size");
9039 // Check if the new vector type is legal.
9040 if (!isTypeLegal(VecVT)) return SDValue();
9041
9042 // Make the new BUILD_VECTOR.
9043 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], Ops.size());
9044
9045 // The new BUILD_VECTOR node has the potential to be further optimized.
9046 AddToWorkList(BV.getNode());
9047 // Bitcast to the desired type.
9048 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
9049}
9050
Michael Liao1a5cc712012-10-24 04:14:18 +00009051SDValue DAGCombiner::reduceBuildVecConvertToConvertBuildVec(SDNode *N) {
9052 EVT VT = N->getValueType(0);
9053
9054 unsigned NumInScalars = N->getNumOperands();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009055 SDLoc dl(N);
Michael Liao1a5cc712012-10-24 04:14:18 +00009056
9057 EVT SrcVT = MVT::Other;
9058 unsigned Opcode = ISD::DELETED_NODE;
9059 unsigned NumDefs = 0;
9060
9061 for (unsigned i = 0; i != NumInScalars; ++i) {
9062 SDValue In = N->getOperand(i);
9063 unsigned Opc = In.getOpcode();
9064
9065 if (Opc == ISD::UNDEF)
9066 continue;
9067
9068 // If all scalar values are floats and converted from integers.
9069 if (Opcode == ISD::DELETED_NODE &&
9070 (Opc == ISD::UINT_TO_FP || Opc == ISD::SINT_TO_FP)) {
9071 Opcode = Opc;
Michael Liao1a5cc712012-10-24 04:14:18 +00009072 }
Tom Stellardd40758b2013-01-02 22:13:01 +00009073
Michael Liao1a5cc712012-10-24 04:14:18 +00009074 if (Opc != Opcode)
9075 return SDValue();
9076
9077 EVT InVT = In.getOperand(0).getValueType();
9078
9079 // If all scalar values are typed differently, bail out. It's chosen to
9080 // simplify BUILD_VECTOR of integer types.
9081 if (SrcVT == MVT::Other)
9082 SrcVT = InVT;
9083 if (SrcVT != InVT)
9084 return SDValue();
9085 NumDefs++;
9086 }
9087
9088 // If the vector has just one element defined, it's not worth to fold it into
9089 // a vectorized one.
9090 if (NumDefs < 2)
9091 return SDValue();
9092
9093 assert((Opcode == ISD::UINT_TO_FP || Opcode == ISD::SINT_TO_FP)
9094 && "Should only handle conversion from integer to float.");
9095 assert(SrcVT != MVT::Other && "Cannot determine source type!");
9096
9097 EVT NVT = EVT::getVectorVT(*DAG.getContext(), SrcVT, NumInScalars);
Tom Stellardd40758b2013-01-02 22:13:01 +00009098
9099 if (!TLI.isOperationLegalOrCustom(Opcode, NVT))
9100 return SDValue();
9101
Michael Liao1a5cc712012-10-24 04:14:18 +00009102 SmallVector<SDValue, 8> Opnds;
9103 for (unsigned i = 0; i != NumInScalars; ++i) {
9104 SDValue In = N->getOperand(i);
9105
9106 if (In.getOpcode() == ISD::UNDEF)
9107 Opnds.push_back(DAG.getUNDEF(SrcVT));
9108 else
9109 Opnds.push_back(In.getOperand(0));
9110 }
9111 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT,
9112 &Opnds[0], Opnds.size());
9113 AddToWorkList(BV.getNode());
9114
9115 return DAG.getNode(Opcode, dl, VT, BV);
9116}
9117
Michael Liaofac14ab2012-10-23 23:06:52 +00009118SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
9119 unsigned NumInScalars = N->getNumOperands();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009120 SDLoc dl(N);
Michael Liaofac14ab2012-10-23 23:06:52 +00009121 EVT VT = N->getValueType(0);
9122
9123 // A vector built entirely of undefs is undef.
9124 if (ISD::allOperandsUndef(N))
9125 return DAG.getUNDEF(VT);
9126
9127 SDValue V = reduceBuildVecExtToExtBuildVec(N);
9128 if (V.getNode())
9129 return V;
9130
Michael Liao1a5cc712012-10-24 04:14:18 +00009131 V = reduceBuildVecConvertToConvertBuildVec(N);
9132 if (V.getNode())
9133 return V;
9134
Dan Gohman7f321562007-06-25 16:23:39 +00009135 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
9136 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
9137 // at most two distinct vectors, turn this into a shuffle node.
Duncan Sands00294ca2012-03-19 15:35:44 +00009138
9139 // May only combine to shuffle after legalize if shuffle is legal.
9140 if (LegalOperations &&
9141 !TLI.isOperationLegalOrCustom(ISD::VECTOR_SHUFFLE, VT))
9142 return SDValue();
9143
Dan Gohman475871a2008-07-27 21:46:04 +00009144 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00009145 for (unsigned i = 0; i != NumInScalars; ++i) {
9146 // Ignore undef inputs.
9147 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00009148
Dan Gohman7f321562007-06-25 16:23:39 +00009149 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00009150 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00009151 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00009152 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00009153 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009154 break;
9155 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009156
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009157 // We allow up to two distinct input vectors.
Dan Gohman475871a2008-07-27 21:46:04 +00009158 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009159 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
9160 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00009161
Gabor Greifba36cb52008-08-28 21:40:38 +00009162 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00009163 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00009164 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00009165 VecIn2 = ExtractedFromVec;
9166 } else {
9167 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009168 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009169 break;
9170 }
9171 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009172
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009173 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00009174 if (VecIn1.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00009175 SmallVector<int, 8> Mask;
Chris Lattnerd7648c82006-03-28 20:28:38 +00009176 for (unsigned i = 0; i != NumInScalars; ++i) {
9177 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Nate Begeman9008ca62009-04-27 18:41:29 +00009178 Mask.push_back(-1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009179 continue;
9180 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009181
Rafael Espindola15684b22009-04-24 12:40:33 +00009182 // If extracting from the first vector, just use the index directly.
Nate Begeman9008ca62009-04-27 18:41:29 +00009183 SDValue Extract = N->getOperand(i);
Mon P Wang93b74152009-03-17 06:33:10 +00009184 SDValue ExtVal = Extract.getOperand(1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009185 if (Extract.getOperand(0) == VecIn1) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00009186 unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue();
9187 if (ExtIndex > VT.getVectorNumElements())
9188 return SDValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009189
Nate Begeman5a5ca152009-04-29 05:20:52 +00009190 Mask.push_back(ExtIndex);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009191 continue;
9192 }
9193
9194 // Otherwise, use InIdx + VecSize
Mon P Wang93b74152009-03-17 06:33:10 +00009195 unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00009196 Mask.push_back(Idx+NumInScalars);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009197 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009198
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009199 // We can't generate a shuffle node with mismatched input and output types.
9200 // Attempt to transform a single input vector to the correct type.
9201 if ((VT != VecIn1.getValueType())) {
9202 // We don't support shuffeling between TWO values of different types.
9203 if (VecIn2.getNode() != 0)
9204 return SDValue();
9205
9206 // We only support widening of vectors which are half the size of the
9207 // output registers. For example XMM->YMM widening on X86 with AVX.
9208 if (VecIn1.getValueType().getSizeInBits()*2 != VT.getSizeInBits())
9209 return SDValue();
9210
James Molloy8cd08bf2012-09-10 14:01:21 +00009211 // If the input vector type has a different base type to the output
9212 // vector type, bail out.
9213 if (VecIn1.getValueType().getVectorElementType() !=
9214 VT.getVectorElementType())
9215 return SDValue();
9216
Stepan Dyatkovskiyfdeb9fe2012-08-22 09:33:55 +00009217 // Widen the input vector by adding undef values.
Michael Liaofac14ab2012-10-23 23:06:52 +00009218 VecIn1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
Stepan Dyatkovskiyfdeb9fe2012-08-22 09:33:55 +00009219 VecIn1, DAG.getUNDEF(VecIn1.getValueType()));
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009220 }
9221
9222 // If VecIn2 is unused then change it to undef.
9223 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
9224
Nadav Rotem6dfabb62012-09-20 08:53:31 +00009225 // Check that we were able to transform all incoming values to the same
9226 // type.
Nadav Rotem0877fdf2012-02-13 12:42:26 +00009227 if (VecIn2.getValueType() != VecIn1.getValueType() ||
9228 VecIn1.getValueType() != VT)
9229 return SDValue();
9230
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009231 // Only type-legal BUILD_VECTOR nodes are converted to shuffle nodes.
Nadav Rotem0877fdf2012-02-13 12:42:26 +00009232 if (!isTypeLegal(VT))
Duncan Sands25cf2272008-11-24 14:53:14 +00009233 return SDValue();
9234
Dan Gohman7f321562007-06-25 16:23:39 +00009235 // Return the new VECTOR_SHUFFLE node.
Nate Begeman9008ca62009-04-27 18:41:29 +00009236 SDValue Ops[2];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00009237 Ops[0] = VecIn1;
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009238 Ops[1] = VecIn2;
Michael Liaofac14ab2012-10-23 23:06:52 +00009239 return DAG.getVectorShuffle(VT, dl, Ops[0], Ops[1], &Mask[0]);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009240 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009241
Dan Gohman475871a2008-07-27 21:46:04 +00009242 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00009243}
9244
Dan Gohman475871a2008-07-27 21:46:04 +00009245SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00009246 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
9247 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
9248 // inputs come from at most two distinct vectors, turn this into a shuffle
9249 // node.
9250
9251 // If we only have one input vector, we don't need to do any concatenation.
Bill Wendlingc144a572009-01-30 23:36:47 +00009252 if (N->getNumOperands() == 1)
Dan Gohman7f321562007-06-25 16:23:39 +00009253 return N->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00009254
Nadav Rotemb7e230d2012-07-14 21:30:27 +00009255 // Check if all of the operands are undefs.
Nadav Rotemb87bdac2012-07-15 08:38:23 +00009256 if (ISD::allOperandsUndef(N))
Nadav Rotemb7e230d2012-07-14 21:30:27 +00009257 return DAG.getUNDEF(N->getValueType(0));
9258
Nadav Rotemb2ed5fa2013-05-01 19:18:51 +00009259 // Type legalization of vectors and DAG canonicalization of SHUFFLE_VECTOR
9260 // nodes often generate nop CONCAT_VECTOR nodes.
9261 // Scan the CONCAT_VECTOR operands and look for a CONCAT operations that
9262 // place the incoming vectors at the exact same location.
9263 SDValue SingleSource = SDValue();
9264 unsigned PartNumElem = N->getOperand(0).getValueType().getVectorNumElements();
9265
9266 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
9267 SDValue Op = N->getOperand(i);
9268
9269 if (Op.getOpcode() == ISD::UNDEF)
9270 continue;
9271
9272 // Check if this is the identity extract:
9273 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR)
9274 return SDValue();
9275
9276 // Find the single incoming vector for the extract_subvector.
9277 if (SingleSource.getNode()) {
9278 if (Op.getOperand(0) != SingleSource)
9279 return SDValue();
9280 } else {
9281 SingleSource = Op.getOperand(0);
Michael Kuperstein27202482013-05-06 08:06:13 +00009282
9283 // Check the source type is the same as the type of the result.
9284 // If not, this concat may extend the vector, so we can not
9285 // optimize it away.
9286 if (SingleSource.getValueType() != N->getValueType(0))
9287 return SDValue();
Nadav Rotemb2ed5fa2013-05-01 19:18:51 +00009288 }
9289
9290 unsigned IdentityIndex = i * PartNumElem;
9291 ConstantSDNode *CS = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9292 // The extract index must be constant.
9293 if (!CS)
9294 return SDValue();
Stephen Lin155615d2013-07-08 00:37:03 +00009295
Nadav Rotemb2ed5fa2013-05-01 19:18:51 +00009296 // Check that we are reading from the identity index.
9297 if (CS->getZExtValue() != IdentityIndex)
9298 return SDValue();
9299 }
9300
9301 if (SingleSource.getNode())
9302 return SingleSource;
Stephen Lin155615d2013-07-08 00:37:03 +00009303
Dan Gohman475871a2008-07-27 21:46:04 +00009304 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00009305}
9306
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00009307SDValue DAGCombiner::visitEXTRACT_SUBVECTOR(SDNode* N) {
9308 EVT NVT = N->getValueType(0);
9309 SDValue V = N->getOperand(0);
9310
Michael Liao13429e22012-10-17 20:48:33 +00009311 if (V->getOpcode() == ISD::CONCAT_VECTORS) {
9312 // Combine:
9313 // (extract_subvec (concat V1, V2, ...), i)
9314 // Into:
9315 // Vi if possible
Michael Liao9aecdb52012-10-19 03:17:00 +00009316 // Only operand 0 is checked as 'concat' assumes all inputs of the same type.
9317 if (V->getOperand(0).getValueType() != NVT)
9318 return SDValue();
Michael Liao13429e22012-10-17 20:48:33 +00009319 unsigned Idx = dyn_cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
9320 unsigned NumElems = NVT.getVectorNumElements();
9321 assert((Idx % NumElems) == 0 &&
9322 "IDX in concat is not a multiple of the result vector length.");
9323 return V->getOperand(Idx / NumElems);
9324 }
9325
Michael Liaob4f98ea2013-03-25 23:47:35 +00009326 // Skip bitcasting
9327 if (V->getOpcode() == ISD::BITCAST)
9328 V = V.getOperand(0);
9329
9330 if (V->getOpcode() == ISD::INSERT_SUBVECTOR) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009331 SDLoc dl(N);
Michael Liaob4f98ea2013-03-25 23:47:35 +00009332 // Handle only simple case where vector being inserted and vector
9333 // being extracted are of same type, and are half size of larger vectors.
9334 EVT BigVT = V->getOperand(0).getValueType();
9335 EVT SmallVT = V->getOperand(1).getValueType();
9336 if (!NVT.bitsEq(SmallVT) || NVT.getSizeInBits()*2 != BigVT.getSizeInBits())
9337 return SDValue();
9338
9339 // Only handle cases where both indexes are constants with the same type.
9340 ConstantSDNode *ExtIdx = dyn_cast<ConstantSDNode>(N->getOperand(1));
9341 ConstantSDNode *InsIdx = dyn_cast<ConstantSDNode>(V->getOperand(2));
9342
9343 if (InsIdx && ExtIdx &&
9344 InsIdx->getValueType(0).getSizeInBits() <= 64 &&
9345 ExtIdx->getValueType(0).getSizeInBits() <= 64) {
9346 // Combine:
9347 // (extract_subvec (insert_subvec V1, V2, InsIdx), ExtIdx)
9348 // Into:
9349 // indices are equal or bit offsets are equal => V1
9350 // otherwise => (extract_subvec V1, ExtIdx)
9351 if (InsIdx->getZExtValue() * SmallVT.getScalarType().getSizeInBits() ==
9352 ExtIdx->getZExtValue() * NVT.getScalarType().getSizeInBits())
9353 return DAG.getNode(ISD::BITCAST, dl, NVT, V->getOperand(1));
9354 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NVT,
9355 DAG.getNode(ISD::BITCAST, dl,
9356 N->getOperand(0).getValueType(),
9357 V->getOperand(0)), N->getOperand(1));
9358 }
9359 }
9360
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00009361 return SDValue();
9362}
9363
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009364// Tries to turn a shuffle of two CONCAT_VECTORS into a single concat.
9365static SDValue partitionShuffleOfConcats(SDNode *N, SelectionDAG &DAG) {
9366 EVT VT = N->getValueType(0);
9367 unsigned NumElts = VT.getVectorNumElements();
9368
9369 SDValue N0 = N->getOperand(0);
9370 SDValue N1 = N->getOperand(1);
9371 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9372
9373 SmallVector<SDValue, 4> Ops;
9374 EVT ConcatVT = N0.getOperand(0).getValueType();
9375 unsigned NumElemsPerConcat = ConcatVT.getVectorNumElements();
9376 unsigned NumConcats = NumElts / NumElemsPerConcat;
9377
9378 // Look at every vector that's inserted. We're looking for exact
9379 // subvector-sized copies from a concatenated vector
9380 for (unsigned I = 0; I != NumConcats; ++I) {
9381 // Make sure we're dealing with a copy.
9382 unsigned Begin = I * NumElemsPerConcat;
Hao Liu3778c042013-05-13 02:07:05 +00009383 bool AllUndef = true, NoUndef = true;
9384 for (unsigned J = Begin; J != Begin + NumElemsPerConcat; ++J) {
9385 if (SVN->getMaskElt(J) >= 0)
9386 AllUndef = false;
9387 else
9388 NoUndef = false;
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009389 }
9390
Hao Liu3778c042013-05-13 02:07:05 +00009391 if (NoUndef) {
Hao Liu3778c042013-05-13 02:07:05 +00009392 if (SVN->getMaskElt(Begin) % NumElemsPerConcat != 0)
9393 return SDValue();
9394
9395 for (unsigned J = 1; J != NumElemsPerConcat; ++J)
9396 if (SVN->getMaskElt(Begin + J - 1) + 1 != SVN->getMaskElt(Begin + J))
9397 return SDValue();
9398
9399 unsigned FirstElt = SVN->getMaskElt(Begin) / NumElemsPerConcat;
9400 if (FirstElt < N0.getNumOperands())
9401 Ops.push_back(N0.getOperand(FirstElt));
9402 else
9403 Ops.push_back(N1.getOperand(FirstElt - N0.getNumOperands()));
9404
9405 } else if (AllUndef) {
9406 Ops.push_back(DAG.getUNDEF(N0.getOperand(0).getValueType()));
9407 } else { // Mixed with general masks and undefs, can't do optimization.
9408 return SDValue();
9409 }
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009410 }
9411
Andrew Trickac6d9be2013-05-25 02:42:55 +00009412 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, Ops.data(),
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009413 Ops.size());
9414}
9415
Dan Gohman475871a2008-07-27 21:46:04 +00009416SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00009417 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00009418 unsigned NumElts = VT.getVectorNumElements();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00009419
Mon P Wangaeb06d22008-11-10 04:46:22 +00009420 SDValue N0 = N->getOperand(0);
Craig Topper481b79c2012-01-04 08:07:43 +00009421 SDValue N1 = N->getOperand(1);
Mon P Wangaeb06d22008-11-10 04:46:22 +00009422
Craig Topperae1bec52012-04-09 05:16:56 +00009423 assert(N0.getValueType() == VT && "Vector shuffle must be normalized in DAG");
Mon P Wangaeb06d22008-11-10 04:46:22 +00009424
Craig Topper481b79c2012-01-04 08:07:43 +00009425 // Canonicalize shuffle undef, undef -> undef
9426 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
9427 return DAG.getUNDEF(VT);
9428
9429 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9430
9431 // Canonicalize shuffle v, v -> v, undef
9432 if (N0 == N1) {
9433 SmallVector<int, 8> NewMask;
9434 for (unsigned i = 0; i != NumElts; ++i) {
9435 int Idx = SVN->getMaskElt(i);
9436 if (Idx >= (int)NumElts) Idx -= NumElts;
9437 NewMask.push_back(Idx);
9438 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00009439 return DAG.getVectorShuffle(VT, SDLoc(N), N0, DAG.getUNDEF(VT),
Craig Topper481b79c2012-01-04 08:07:43 +00009440 &NewMask[0]);
9441 }
9442
9443 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
9444 if (N0.getOpcode() == ISD::UNDEF) {
9445 SmallVector<int, 8> NewMask;
9446 for (unsigned i = 0; i != NumElts; ++i) {
9447 int Idx = SVN->getMaskElt(i);
Craig Topper4b206bd2012-04-09 05:55:33 +00009448 if (Idx >= 0) {
Craig Topper01d22aa2013-08-08 07:38:55 +00009449 if (Idx >= (int)NumElts)
Craig Topper4b206bd2012-04-09 05:55:33 +00009450 Idx -= NumElts;
Craig Topper01d22aa2013-08-08 07:38:55 +00009451 else
9452 Idx = -1; // remove reference to lhs
Craig Topper4b206bd2012-04-09 05:55:33 +00009453 }
9454 NewMask.push_back(Idx);
Craig Topper481b79c2012-01-04 08:07:43 +00009455 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00009456 return DAG.getVectorShuffle(VT, SDLoc(N), N1, DAG.getUNDEF(VT),
Craig Topper481b79c2012-01-04 08:07:43 +00009457 &NewMask[0]);
9458 }
9459
9460 // Remove references to rhs if it is undef
9461 if (N1.getOpcode() == ISD::UNDEF) {
9462 bool Changed = false;
9463 SmallVector<int, 8> NewMask;
9464 for (unsigned i = 0; i != NumElts; ++i) {
9465 int Idx = SVN->getMaskElt(i);
9466 if (Idx >= (int)NumElts) {
9467 Idx = -1;
9468 Changed = true;
9469 }
9470 NewMask.push_back(Idx);
9471 }
9472 if (Changed)
Andrew Trickac6d9be2013-05-25 02:42:55 +00009473 return DAG.getVectorShuffle(VT, SDLoc(N), N0, N1, &NewMask[0]);
Craig Topper481b79c2012-01-04 08:07:43 +00009474 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00009475
Bob Wilson0f1db1a2010-10-28 17:06:14 +00009476 // If it is a splat, check if the argument vector is another splat or a
9477 // build_vector with all scalar elements the same.
Bob Wilson0f1db1a2010-10-28 17:06:14 +00009478 if (SVN->isSplat() && SVN->getSplatIndex() < (int)NumElts) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009479 SDNode *V = N0.getNode();
Evan Cheng917ec982006-07-21 08:25:53 +00009480
Dan Gohman7f321562007-06-25 16:23:39 +00009481 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00009482 // not the number of vector elements, look through it. Be careful not to
9483 // look though conversions that change things like v4f32 to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009484 if (V->getOpcode() == ISD::BITCAST) {
Dan Gohman475871a2008-07-27 21:46:04 +00009485 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00009486 if (ConvInput.getValueType().isVector() &&
9487 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +00009488 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +00009489 }
9490
Dan Gohman7f321562007-06-25 16:23:39 +00009491 if (V->getOpcode() == ISD::BUILD_VECTOR) {
Bob Wilson0f1db1a2010-10-28 17:06:14 +00009492 assert(V->getNumOperands() == NumElts &&
9493 "BUILD_VECTOR has wrong number of operands");
9494 SDValue Base;
9495 bool AllSame = true;
9496 for (unsigned i = 0; i != NumElts; ++i) {
9497 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
9498 Base = V->getOperand(i);
9499 break;
Evan Cheng917ec982006-07-21 08:25:53 +00009500 }
Evan Cheng917ec982006-07-21 08:25:53 +00009501 }
Bob Wilson0f1db1a2010-10-28 17:06:14 +00009502 // Splat of <u, u, u, u>, return <u, u, u, u>
9503 if (!Base.getNode())
9504 return N0;
9505 for (unsigned i = 0; i != NumElts; ++i) {
9506 if (V->getOperand(i) != Base) {
9507 AllSame = false;
9508 break;
9509 }
9510 }
9511 // Splat of <x, x, x, x>, return <x, x, x, x>
9512 if (AllSame)
9513 return N0;
Evan Cheng917ec982006-07-21 08:25:53 +00009514 }
9515 }
Nadav Rotem4ac90812012-04-01 19:31:22 +00009516
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009517 if (N0.getOpcode() == ISD::CONCAT_VECTORS &&
9518 Level < AfterLegalizeVectorOps &&
9519 (N1.getOpcode() == ISD::UNDEF ||
9520 (N1.getOpcode() == ISD::CONCAT_VECTORS &&
9521 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()))) {
9522 SDValue V = partitionShuffleOfConcats(N, DAG);
9523
9524 if (V.getNode())
9525 return V;
9526 }
9527
Nadav Rotem4ac90812012-04-01 19:31:22 +00009528 // If this shuffle node is simply a swizzle of another shuffle node,
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009529 // and it reverses the swizzle of the previous shuffle then we can
9530 // optimize shuffle(shuffle(x, undef), undef) -> x.
Nadav Rotem4ac90812012-04-01 19:31:22 +00009531 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
9532 N1.getOpcode() == ISD::UNDEF) {
9533
Nadav Rotem4ac90812012-04-01 19:31:22 +00009534 ShuffleVectorSDNode *OtherSV = cast<ShuffleVectorSDNode>(N0);
9535
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009536 // Shuffle nodes can only reverse shuffles with a single non-undef value.
9537 if (N0.getOperand(1).getOpcode() != ISD::UNDEF)
9538 return SDValue();
9539
Craig Topperae1bec52012-04-09 05:16:56 +00009540 // The incoming shuffle must be of the same type as the result of the
9541 // current shuffle.
9542 assert(OtherSV->getOperand(0).getValueType() == VT &&
9543 "Shuffle types don't match");
Nadav Rotem4ac90812012-04-01 19:31:22 +00009544
9545 for (unsigned i = 0; i != NumElts; ++i) {
9546 int Idx = SVN->getMaskElt(i);
Craig Topperae1bec52012-04-09 05:16:56 +00009547 assert(Idx < (int)NumElts && "Index references undef operand");
Nadav Rotem4ac90812012-04-01 19:31:22 +00009548 // Next, this index comes from the first value, which is the incoming
9549 // shuffle. Adopt the incoming index.
9550 if (Idx >= 0)
9551 Idx = OtherSV->getMaskElt(Idx);
9552
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009553 // The combined shuffle must map each index to itself.
Craig Topperae1bec52012-04-09 05:16:56 +00009554 if (Idx >= 0 && (unsigned)Idx != i)
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009555 return SDValue();
Nadav Rotem4ac90812012-04-01 19:31:22 +00009556 }
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009557
9558 return OtherSV->getOperand(0);
Nadav Rotem4ac90812012-04-01 19:31:22 +00009559 }
9560
Dan Gohman475871a2008-07-27 21:46:04 +00009561 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00009562}
9563
Evan Cheng44f1f092006-04-20 08:56:16 +00009564/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00009565/// an AND to a vector_shuffle with the destination vector and a zero vector.
9566/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00009567/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00009568SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00009569 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00009570 SDLoc dl(N);
Dan Gohman475871a2008-07-27 21:46:04 +00009571 SDValue LHS = N->getOperand(0);
9572 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00009573 if (N->getOpcode() == ISD::AND) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009574 if (RHS.getOpcode() == ISD::BITCAST)
Evan Cheng44f1f092006-04-20 08:56:16 +00009575 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00009576 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Nate Begeman9008ca62009-04-27 18:41:29 +00009577 SmallVector<int, 8> Indices;
9578 unsigned NumElts = RHS.getNumOperands();
Evan Cheng44f1f092006-04-20 08:56:16 +00009579 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00009580 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00009581 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00009582 return SDValue();
Craig Topperb7135e52012-04-09 05:59:53 +00009583
9584 if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00009585 Indices.push_back(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00009586 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00009587 Indices.push_back(NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00009588 else
Dan Gohman475871a2008-07-27 21:46:04 +00009589 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00009590 }
9591
9592 // Let's see if the target supports this vector_shuffle.
Owen Andersone50ed302009-08-10 22:56:29 +00009593 EVT RVT = RHS.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00009594 if (!TLI.isVectorClearMaskLegal(Indices, RVT))
Dan Gohman475871a2008-07-27 21:46:04 +00009595 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00009596
Dan Gohman7f321562007-06-25 16:23:39 +00009597 // Return the new VECTOR_SHUFFLE node.
Dan Gohman8a55ce42009-09-23 21:02:20 +00009598 EVT EltVT = RVT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00009599 SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00009600 DAG.getConstant(0, EltVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00009601 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Nate Begeman9008ca62009-04-27 18:41:29 +00009602 RVT, &ZeroOps[0], ZeroOps.size());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009603 LHS = DAG.getNode(ISD::BITCAST, dl, RVT, LHS);
Nate Begeman9008ca62009-04-27 18:41:29 +00009604 SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009605 return DAG.getNode(ISD::BITCAST, dl, VT, Shuf);
Evan Cheng44f1f092006-04-20 08:56:16 +00009606 }
9607 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009608
Dan Gohman475871a2008-07-27 21:46:04 +00009609 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00009610}
9611
Dan Gohman7f321562007-06-25 16:23:39 +00009612/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00009613SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Bob Wilsond7273432010-12-17 23:06:49 +00009614 assert(N->getValueType(0).isVector() &&
9615 "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00009616
Dan Gohman475871a2008-07-27 21:46:04 +00009617 SDValue LHS = N->getOperand(0);
9618 SDValue RHS = N->getOperand(1);
9619 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00009620 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +00009621
Dan Gohman7f321562007-06-25 16:23:39 +00009622 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00009623 // this operation.
Scott Michelfdc40a02009-02-17 22:15:04 +00009624 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
Dan Gohman7f321562007-06-25 16:23:39 +00009625 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00009626 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00009627 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00009628 SDValue LHSOp = LHS.getOperand(i);
9629 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00009630 // If these two elements can't be folded, bail out.
9631 if ((LHSOp.getOpcode() != ISD::UNDEF &&
9632 LHSOp.getOpcode() != ISD::Constant &&
9633 LHSOp.getOpcode() != ISD::ConstantFP) ||
9634 (RHSOp.getOpcode() != ISD::UNDEF &&
9635 RHSOp.getOpcode() != ISD::Constant &&
9636 RHSOp.getOpcode() != ISD::ConstantFP))
9637 break;
Bill Wendling836ca7d2009-01-30 23:59:18 +00009638
Evan Cheng7b336a82006-05-31 06:08:35 +00009639 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00009640 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
9641 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00009642 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +00009643 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +00009644 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00009645 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00009646 break;
9647 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009648
Bob Wilsond7273432010-12-17 23:06:49 +00009649 EVT VT = LHSOp.getValueType();
Bob Wilsondb2b18f2011-10-18 17:34:47 +00009650 EVT RVT = RHSOp.getValueType();
9651 if (RVT != VT) {
9652 // Integer BUILD_VECTOR operands may have types larger than the element
9653 // size (e.g., when the element type is not legal). Prior to type
9654 // legalization, the types may not match between the two BUILD_VECTORS.
9655 // Truncate one of the operands to make them match.
9656 if (RVT.getSizeInBits() > VT.getSizeInBits()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009657 RHSOp = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, RHSOp);
Bob Wilsondb2b18f2011-10-18 17:34:47 +00009658 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009659 LHSOp = DAG.getNode(ISD::TRUNCATE, SDLoc(N), RVT, LHSOp);
Bob Wilsondb2b18f2011-10-18 17:34:47 +00009660 VT = RVT;
9661 }
9662 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00009663 SDValue FoldOp = DAG.getNode(N->getOpcode(), SDLoc(LHS), VT,
Evan Chenga0839882010-05-18 00:03:40 +00009664 LHSOp, RHSOp);
9665 if (FoldOp.getOpcode() != ISD::UNDEF &&
9666 FoldOp.getOpcode() != ISD::Constant &&
9667 FoldOp.getOpcode() != ISD::ConstantFP)
9668 break;
9669 Ops.push_back(FoldOp);
9670 AddToWorkList(FoldOp.getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +00009671 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009672
Bob Wilsond7273432010-12-17 23:06:49 +00009673 if (Ops.size() == LHS.getNumOperands())
Andrew Trickac6d9be2013-05-25 02:42:55 +00009674 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Bob Wilsond7273432010-12-17 23:06:49 +00009675 LHS.getValueType(), &Ops[0], Ops.size());
Chris Lattneredab1b92006-04-02 03:25:57 +00009676 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009677
Dan Gohman475871a2008-07-27 21:46:04 +00009678 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00009679}
9680
Craig Topperdd201ff2012-09-11 01:45:21 +00009681/// SimplifyVUnaryOp - Visit a binary vector operation, like FABS/FNEG.
9682SDValue DAGCombiner::SimplifyVUnaryOp(SDNode *N) {
Craig Topperdd201ff2012-09-11 01:45:21 +00009683 assert(N->getValueType(0).isVector() &&
9684 "SimplifyVUnaryOp only works on vectors!");
9685
9686 SDValue N0 = N->getOperand(0);
9687
9688 if (N0.getOpcode() != ISD::BUILD_VECTOR)
9689 return SDValue();
9690
9691 // Operand is a BUILD_VECTOR node, see if we can constant fold it.
9692 SmallVector<SDValue, 8> Ops;
9693 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) {
9694 SDValue Op = N0.getOperand(i);
9695 if (Op.getOpcode() != ISD::UNDEF &&
9696 Op.getOpcode() != ISD::ConstantFP)
9697 break;
9698 EVT EltVT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009699 SDValue FoldOp = DAG.getNode(N->getOpcode(), SDLoc(N0), EltVT, Op);
Craig Topperdd201ff2012-09-11 01:45:21 +00009700 if (FoldOp.getOpcode() != ISD::UNDEF &&
9701 FoldOp.getOpcode() != ISD::ConstantFP)
9702 break;
9703 Ops.push_back(FoldOp);
9704 AddToWorkList(FoldOp.getNode());
9705 }
9706
9707 if (Ops.size() != N0.getNumOperands())
9708 return SDValue();
9709
Andrew Trickac6d9be2013-05-25 02:42:55 +00009710 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Craig Topperdd201ff2012-09-11 01:45:21 +00009711 N0.getValueType(), &Ops[0], Ops.size());
9712}
9713
Andrew Trickac6d9be2013-05-25 02:42:55 +00009714SDValue DAGCombiner::SimplifySelect(SDLoc DL, SDValue N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +00009715 SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00009716 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
Scott Michelfdc40a02009-02-17 22:15:04 +00009717
Bill Wendling836ca7d2009-01-30 23:59:18 +00009718 SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00009719 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Bill Wendling836ca7d2009-01-30 23:59:18 +00009720
Nate Begemanf845b452005-10-08 00:29:44 +00009721 // If we got a simplified select_cc node back from SimplifySelectCC, then
9722 // break it down into a new SETCC node, and a new SELECT node, and then return
9723 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +00009724 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +00009725 // Check to see if we got a select_cc back (to turn into setcc/select).
9726 // Otherwise, just return whatever node we got back, like fabs.
9727 if (SCC.getOpcode() == ISD::SELECT_CC) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009728 SDValue SETCC = DAG.getNode(ISD::SETCC, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009729 N0.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009730 SCC.getOperand(0), SCC.getOperand(1),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009731 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +00009732 AddToWorkList(SETCC.getNode());
Matt Arsenaultb05e4772013-06-14 22:04:37 +00009733 return DAG.getSelect(SDLoc(SCC), SCC.getValueType(),
9734 SCC.getOperand(2), SCC.getOperand(3), SETCC);
Nate Begemanf845b452005-10-08 00:29:44 +00009735 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009736
Nate Begemanf845b452005-10-08 00:29:44 +00009737 return SCC;
9738 }
Dan Gohman475871a2008-07-27 21:46:04 +00009739 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00009740}
9741
Chris Lattner40c62d52005-10-18 06:04:22 +00009742/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
9743/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00009744/// select. Callers of this should assume that TheSelect is deleted if this
9745/// returns true. As such, they should return the appropriate thing (e.g. the
9746/// node) back to the top-level of the DAG combiner loop to avoid it being
9747/// looked at.
Scott Michelfdc40a02009-02-17 22:15:04 +00009748bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
Dan Gohman475871a2008-07-27 21:46:04 +00009749 SDValue RHS) {
Scott Michelfdc40a02009-02-17 22:15:04 +00009750
Nadav Rotemf94fdb62011-02-11 19:57:47 +00009751 // Cannot simplify select with vector condition
9752 if (TheSelect->getOperand(0).getValueType().isVector()) return false;
9753
Chris Lattner40c62d52005-10-18 06:04:22 +00009754 // If this is a select from two identical things, try to pull the operation
9755 // through the select.
Chris Lattner18061612010-09-21 15:46:59 +00009756 if (LHS.getOpcode() != RHS.getOpcode() ||
9757 !LHS.hasOneUse() || !RHS.hasOneUse())
9758 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009759
Chris Lattner18061612010-09-21 15:46:59 +00009760 // If this is a load and the token chain is identical, replace the select
9761 // of two loads with a load through a select of the address to load from.
9762 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
9763 // constants have been dropped into the constant pool.
9764 if (LHS.getOpcode() == ISD::LOAD) {
9765 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
9766 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009767
Chris Lattner18061612010-09-21 15:46:59 +00009768 // Token chains must be identical.
9769 if (LHS.getOperand(0) != RHS.getOperand(0) ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00009770 // Do not let this transformation reduce the number of volatile loads.
Chris Lattner18061612010-09-21 15:46:59 +00009771 LLD->isVolatile() || RLD->isVolatile() ||
9772 // If this is an EXTLOAD, the VT's must match.
9773 LLD->getMemoryVT() != RLD->getMemoryVT() ||
Duncan Sandsdcfd3a72010-11-18 20:05:18 +00009774 // If this is an EXTLOAD, the kind of extension must match.
9775 (LLD->getExtensionType() != RLD->getExtensionType() &&
9776 // The only exception is if one of the extensions is anyext.
9777 LLD->getExtensionType() != ISD::EXTLOAD &&
9778 RLD->getExtensionType() != ISD::EXTLOAD) ||
Dan Gohman75832d72009-10-31 14:14:04 +00009779 // FIXME: this discards src value information. This is
9780 // over-conservative. It would be beneficial to be able to remember
Mon P Wangfe240b12010-01-11 20:12:49 +00009781 // both potential memory locations. Since we are discarding
9782 // src value info, don't do the transformation if the memory
9783 // locations are not in the default address space.
Chris Lattner18061612010-09-21 15:46:59 +00009784 LLD->getPointerInfo().getAddrSpace() != 0 ||
Pete Cooperb0fde6d2013-02-12 03:14:50 +00009785 RLD->getPointerInfo().getAddrSpace() != 0 ||
9786 !TLI.isOperationLegalOrCustom(TheSelect->getOpcode(),
9787 LLD->getBasePtr().getValueType()))
Chris Lattner18061612010-09-21 15:46:59 +00009788 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009789
Chris Lattnerf1658062010-09-21 15:58:55 +00009790 // Check that the select condition doesn't reach either load. If so,
9791 // folding this will induce a cycle into the DAG. If not, this is safe to
9792 // xform, so create a select of the addresses.
Chris Lattner18061612010-09-21 15:46:59 +00009793 SDValue Addr;
9794 if (TheSelect->getOpcode() == ISD::SELECT) {
Chris Lattnerf1658062010-09-21 15:58:55 +00009795 SDNode *CondNode = TheSelect->getOperand(0).getNode();
9796 if ((LLD->hasAnyUseOfValue(1) && LLD->isPredecessorOf(CondNode)) ||
9797 (RLD->hasAnyUseOfValue(1) && RLD->isPredecessorOf(CondNode)))
9798 return false;
Nadav Rotem1c5bf3f2012-10-18 18:06:48 +00009799 // The loads must not depend on one another.
9800 if (LLD->isPredecessorOf(RLD) ||
9801 RLD->isPredecessorOf(LLD))
9802 return false;
Matt Arsenaultb05e4772013-06-14 22:04:37 +00009803 Addr = DAG.getSelect(SDLoc(TheSelect),
9804 LLD->getBasePtr().getValueType(),
9805 TheSelect->getOperand(0), LLD->getBasePtr(),
9806 RLD->getBasePtr());
Chris Lattner18061612010-09-21 15:46:59 +00009807 } else { // Otherwise SELECT_CC
Chris Lattnerf1658062010-09-21 15:58:55 +00009808 SDNode *CondLHS = TheSelect->getOperand(0).getNode();
9809 SDNode *CondRHS = TheSelect->getOperand(1).getNode();
9810
9811 if ((LLD->hasAnyUseOfValue(1) &&
9812 (LLD->isPredecessorOf(CondLHS) || LLD->isPredecessorOf(CondRHS))) ||
Chris Lattner77d95212012-03-27 16:27:21 +00009813 (RLD->hasAnyUseOfValue(1) &&
9814 (RLD->isPredecessorOf(CondLHS) || RLD->isPredecessorOf(CondRHS))))
Chris Lattnerf1658062010-09-21 15:58:55 +00009815 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009816
Andrew Trickac6d9be2013-05-25 02:42:55 +00009817 Addr = DAG.getNode(ISD::SELECT_CC, SDLoc(TheSelect),
Chris Lattnerf1658062010-09-21 15:58:55 +00009818 LLD->getBasePtr().getValueType(),
9819 TheSelect->getOperand(0),
9820 TheSelect->getOperand(1),
9821 LLD->getBasePtr(), RLD->getBasePtr(),
9822 TheSelect->getOperand(4));
Chris Lattner18061612010-09-21 15:46:59 +00009823 }
9824
Chris Lattnerf1658062010-09-21 15:58:55 +00009825 SDValue Load;
9826 if (LLD->getExtensionType() == ISD::NON_EXTLOAD) {
9827 Load = DAG.getLoad(TheSelect->getValueType(0),
Andrew Trickac6d9be2013-05-25 02:42:55 +00009828 SDLoc(TheSelect),
Chris Lattnerf1658062010-09-21 15:58:55 +00009829 // FIXME: Discards pointer info.
9830 LLD->getChain(), Addr, MachinePointerInfo(),
9831 LLD->isVolatile(), LLD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009832 LLD->isInvariant(), LLD->getAlignment());
Chris Lattnerf1658062010-09-21 15:58:55 +00009833 } else {
Duncan Sandsb9064bb2010-11-18 21:16:28 +00009834 Load = DAG.getExtLoad(LLD->getExtensionType() == ISD::EXTLOAD ?
9835 RLD->getExtensionType() : LLD->getExtensionType(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00009836 SDLoc(TheSelect),
Stuart Hastingsa9011292011-02-16 16:23:55 +00009837 TheSelect->getValueType(0),
Chris Lattnerf1658062010-09-21 15:58:55 +00009838 // FIXME: Discards pointer info.
9839 LLD->getChain(), Addr, MachinePointerInfo(),
9840 LLD->getMemoryVT(), LLD->isVolatile(),
9841 LLD->isNonTemporal(), LLD->getAlignment());
Chris Lattner40c62d52005-10-18 06:04:22 +00009842 }
Chris Lattnerf1658062010-09-21 15:58:55 +00009843
9844 // Users of the select now use the result of the load.
9845 CombineTo(TheSelect, Load);
9846
9847 // Users of the old loads now use the new load's chain. We know the
9848 // old-load value is dead now.
9849 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
9850 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
9851 return true;
Chris Lattner40c62d52005-10-18 06:04:22 +00009852 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009853
Chris Lattner40c62d52005-10-18 06:04:22 +00009854 return false;
9855}
9856
Chris Lattner600fec32009-03-11 05:08:08 +00009857/// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3
9858/// where 'cond' is the comparison specified by CC.
Andrew Trickac6d9be2013-05-25 02:42:55 +00009859SDValue DAGCombiner::SimplifySelectCC(SDLoc DL, SDValue N0, SDValue N1,
Dan Gohman475871a2008-07-27 21:46:04 +00009860 SDValue N2, SDValue N3,
9861 ISD::CondCode CC, bool NotExtCompare) {
Chris Lattner600fec32009-03-11 05:08:08 +00009862 // (x ? y : y) -> y.
9863 if (N2 == N3) return N2;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009864
Owen Andersone50ed302009-08-10 22:56:29 +00009865 EVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +00009866 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
9867 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
9868 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009869
9870 // Determine if the condition we're dealing with is constant
Matt Arsenault225ed702013-05-18 00:21:46 +00009871 SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00009872 N0, N1, CC, DL, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00009873 if (SCC.getNode()) AddToWorkList(SCC.getNode());
9874 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009875
9876 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00009877 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00009878 return N2;
9879 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00009880 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00009881 return N3;
Scott Michelfdc40a02009-02-17 22:15:04 +00009882
Nate Begemanf845b452005-10-08 00:29:44 +00009883 // Check to see if we can simplify the select into an fabs node
9884 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
9885 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00009886 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00009887 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
9888 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
9889 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
9890 N2 == N3.getOperand(0))
Bill Wendling836ca7d2009-01-30 23:59:18 +00009891 return DAG.getNode(ISD::FABS, DL, VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00009892
Nate Begemanf845b452005-10-08 00:29:44 +00009893 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
9894 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
9895 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
9896 N2.getOperand(0) == N3)
Bill Wendling836ca7d2009-01-30 23:59:18 +00009897 return DAG.getNode(ISD::FABS, DL, VT, N3);
Nate Begemanf845b452005-10-08 00:29:44 +00009898 }
9899 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009900
Chris Lattner600fec32009-03-11 05:08:08 +00009901 // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)"
9902 // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0
9903 // in it. This is a win when the constant is not otherwise available because
9904 // it replaces two constant pool loads with one. We only do this if the FP
9905 // type is known to be legal, because if it isn't, then we are before legalize
9906 // types an we want the other legalization to happen first (e.g. to avoid
Mon P Wang0b7a7862009-03-14 00:25:19 +00009907 // messing with soft float) and if the ConstantFP is not legal, because if
9908 // it is legal, we may not need to store the FP constant in a constant pool.
Chris Lattner600fec32009-03-11 05:08:08 +00009909 if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2))
9910 if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) {
9911 if (TLI.isTypeLegal(N2.getValueType()) &&
Mon P Wang0b7a7862009-03-14 00:25:19 +00009912 (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) !=
9913 TargetLowering::Legal) &&
Chris Lattner600fec32009-03-11 05:08:08 +00009914 // If both constants have multiple uses, then we won't need to do an
9915 // extra load, they are likely around in registers for other users.
9916 (TV->hasOneUse() || FV->hasOneUse())) {
9917 Constant *Elts[] = {
9918 const_cast<ConstantFP*>(FV->getConstantFPValue()),
9919 const_cast<ConstantFP*>(TV->getConstantFPValue())
9920 };
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009921 Type *FPTy = Elts[0]->getType();
Micah Villmow3574eca2012-10-08 16:38:25 +00009922 const DataLayout &TD = *TLI.getDataLayout();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009923
Chris Lattner600fec32009-03-11 05:08:08 +00009924 // Create a ConstantArray of the two constants.
Jay Foad26701082011-06-22 09:24:39 +00009925 Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts);
Chris Lattner600fec32009-03-11 05:08:08 +00009926 SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(),
9927 TD.getPrefTypeAlignment(FPTy));
Evan Cheng1606e8e2009-03-13 07:51:59 +00009928 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner600fec32009-03-11 05:08:08 +00009929
9930 // Get the offsets to the 0 and 1 element of the array so that we can
9931 // select between them.
9932 SDValue Zero = DAG.getIntPtrConstant(0);
Duncan Sands777d2302009-05-09 07:06:46 +00009933 unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType());
Chris Lattner600fec32009-03-11 05:08:08 +00009934 SDValue One = DAG.getIntPtrConstant(EltSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009935
Chris Lattner600fec32009-03-11 05:08:08 +00009936 SDValue Cond = DAG.getSetCC(DL,
Matt Arsenault225ed702013-05-18 00:21:46 +00009937 getSetCCResultType(N0.getValueType()),
Chris Lattner600fec32009-03-11 05:08:08 +00009938 N0, N1, CC);
Dan Gohman7b316c92011-09-22 23:01:29 +00009939 AddToWorkList(Cond.getNode());
Matt Arsenaultb05e4772013-06-14 22:04:37 +00009940 SDValue CstOffset = DAG.getSelect(DL, Zero.getValueType(),
9941 Cond, One, Zero);
Dan Gohman7b316c92011-09-22 23:01:29 +00009942 AddToWorkList(CstOffset.getNode());
Tom Stellardedd08f72013-08-26 15:06:10 +00009943 CPIdx = DAG.getNode(ISD::ADD, DL, CPIdx.getValueType(), CPIdx,
Chris Lattner600fec32009-03-11 05:08:08 +00009944 CstOffset);
Dan Gohman7b316c92011-09-22 23:01:29 +00009945 AddToWorkList(CPIdx.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +00009946 return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00009947 MachinePointerInfo::getConstantPool(), false,
Pete Cooperd752e0f2011-11-08 18:42:53 +00009948 false, false, Alignment);
Chris Lattner600fec32009-03-11 05:08:08 +00009949
9950 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009951 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009952
Nate Begemanf845b452005-10-08 00:29:44 +00009953 // Check to see if we can perform the "gzip trick", transforming
Bill Wendling836ca7d2009-01-30 23:59:18 +00009954 // (select_cc setlt X, 0, A, 0) -> (and (sra X, (sub size(X), 1), A)
Chris Lattnere3152e52006-09-20 06:41:35 +00009955 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +00009956 (N1C->isNullValue() || // (a < 0) ? b : 0
9957 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Owen Andersone50ed302009-08-10 22:56:29 +00009958 EVT XType = N0.getValueType();
9959 EVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00009960 if (XType.bitsGE(AType)) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +00009961 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00009962 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00009963 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
9964 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00009965 ShCtV = XType.getSizeInBits()-ShCtV-1;
Owen Anderson95771af2011-02-25 21:41:48 +00009966 SDValue ShCt = DAG.getConstant(ShCtV,
9967 getShiftAmountTy(N0.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00009968 SDValue Shift = DAG.getNode(ISD::SRL, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009969 XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +00009970 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00009971
Duncan Sands8e4eb092008-06-08 20:54:56 +00009972 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00009973 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00009974 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009975 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009976
9977 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00009978 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009979
Andrew Trickac6d9be2013-05-25 02:42:55 +00009980 SDValue Shift = DAG.getNode(ISD::SRA, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009981 XType, N0,
9982 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00009983 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00009984 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00009985
Duncan Sands8e4eb092008-06-08 20:54:56 +00009986 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00009987 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00009988 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009989 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009990
9991 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00009992 }
9993 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009994
Owen Andersoned1088a2010-09-22 22:58:22 +00009995 // fold (select_cc seteq (and x, y), 0, 0, A) -> (and (shr (shl x)) A)
9996 // where y is has a single bit set.
9997 // A plaintext description would be, we can turn the SELECT_CC into an AND
9998 // when the condition can be materialized as an all-ones register. Any
9999 // single bit-test can be materialized as an all-ones register with
10000 // shift-left and shift-right-arith.
10001 if (CC == ISD::SETEQ && N0->getOpcode() == ISD::AND &&
10002 N0->getValueType(0) == VT &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010003 N1C && N1C->isNullValue() &&
Owen Andersoned1088a2010-09-22 22:58:22 +000010004 N2C && N2C->isNullValue()) {
10005 SDValue AndLHS = N0->getOperand(0);
10006 ConstantSDNode *ConstAndRHS = dyn_cast<ConstantSDNode>(N0->getOperand(1));
10007 if (ConstAndRHS && ConstAndRHS->getAPIntValue().countPopulation() == 1) {
10008 // Shift the tested bit over the sign bit.
10009 APInt AndMask = ConstAndRHS->getAPIntValue();
10010 SDValue ShlAmt =
Owen Anderson95771af2011-02-25 21:41:48 +000010011 DAG.getConstant(AndMask.countLeadingZeros(),
10012 getShiftAmountTy(AndLHS.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +000010013 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(N0), VT, AndLHS, ShlAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010014
Owen Andersoned1088a2010-09-22 22:58:22 +000010015 // Now arithmetic right shift it all the way over, so the result is either
10016 // all-ones, or zero.
10017 SDValue ShrAmt =
Owen Anderson95771af2011-02-25 21:41:48 +000010018 DAG.getConstant(AndMask.getBitWidth()-1,
10019 getShiftAmountTy(Shl.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +000010020 SDValue Shr = DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl, ShrAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010021
Owen Andersoned1088a2010-09-22 22:58:22 +000010022 return DAG.getNode(ISD::AND, DL, VT, Shr, N3);
10023 }
10024 }
10025
Nate Begeman07ed4172005-10-10 21:26:48 +000010026 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +000010027 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands28b77e92011-09-06 19:07:46 +000010028 TLI.getBooleanContents(N0.getValueType().isVector()) ==
10029 TargetLowering::ZeroOrOneBooleanContent) {
Scott Michelfdc40a02009-02-17 22:15:04 +000010030
Chris Lattner1eba01e2007-04-11 06:50:51 +000010031 // If the caller doesn't want us to simplify this into a zext of a compare,
10032 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +000010033 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +000010034 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000010035
Nate Begeman07ed4172005-10-10 21:26:48 +000010036 // Get a SetCC of the condition
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010037 // NOTE: Don't create a SETCC if it's not legal on this target.
10038 if (!LegalOperations ||
10039 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault225ed702013-05-18 00:21:46 +000010040 LegalTypes ? getSetCCResultType(N0.getValueType()) : MVT::i1)) {
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010041 SDValue Temp, SCC;
10042 // cast from setcc result type to select result type
10043 if (LegalTypes) {
Matt Arsenault225ed702013-05-18 00:21:46 +000010044 SCC = DAG.getSetCC(DL, getSetCCResultType(N0.getValueType()),
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010045 N0, N1, CC);
10046 if (N2.getValueType().bitsLT(SCC.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +000010047 Temp = DAG.getZeroExtendInReg(SCC, SDLoc(N2),
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010048 N2.getValueType());
10049 else
Andrew Trickac6d9be2013-05-25 02:42:55 +000010050 Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2),
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010051 N2.getValueType(), SCC);
10052 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010053 SCC = DAG.getSetCC(SDLoc(N0), MVT::i1, N0, N1, CC);
10054 Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2),
Bill Wendling836ca7d2009-01-30 23:59:18 +000010055 N2.getValueType(), SCC);
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010056 }
10057
10058 AddToWorkList(SCC.getNode());
10059 AddToWorkList(Temp.getNode());
10060
10061 if (N2C->getAPIntValue() == 1)
10062 return Temp;
10063
10064 // shl setcc result by log2 n2c
10065 return DAG.getNode(ISD::SHL, DL, N2.getValueType(), Temp,
10066 DAG.getConstant(N2C->getAPIntValue().logBase2(),
10067 getShiftAmountTy(Temp.getValueType())));
Nate Begemanb0d04a72006-02-18 02:40:58 +000010068 }
Nate Begeman07ed4172005-10-10 21:26:48 +000010069 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010070
Nate Begemanf845b452005-10-08 00:29:44 +000010071 // Check to see if this is the equivalent of setcc
10072 // FIXME: Turn all of these into setcc if setcc if setcc is legal
10073 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +000010074 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Owen Andersone50ed302009-08-10 22:56:29 +000010075 EVT XType = N0.getValueType();
Duncan Sands25cf2272008-11-24 14:53:14 +000010076 if (!LegalOperations ||
Matt Arsenault225ed702013-05-18 00:21:46 +000010077 TLI.isOperationLegal(ISD::SETCC, getSetCCResultType(XType))) {
10078 SDValue Res = DAG.getSetCC(DL, getSetCCResultType(XType), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +000010079 if (Res.getValueType() != VT)
Bill Wendling836ca7d2009-01-30 23:59:18 +000010080 Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res);
Nate Begemanf845b452005-10-08 00:29:44 +000010081 return Res;
10082 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010083
Bill Wendling836ca7d2009-01-30 23:59:18 +000010084 // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X))))
Scott Michelfdc40a02009-02-17 22:15:04 +000010085 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands25cf2272008-11-24 14:53:14 +000010086 (!LegalOperations ||
Duncan Sands184a8762008-06-14 17:48:34 +000010087 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010088 SDValue Ctlz = DAG.getNode(ISD::CTLZ, SDLoc(N0), XType, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +000010089 return DAG.getNode(ISD::SRL, DL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +000010090 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Owen Anderson95771af2011-02-25 21:41:48 +000010091 getShiftAmountTy(Ctlz.getValueType())));
Nate Begemanf845b452005-10-08 00:29:44 +000010092 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010093 // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1))
Scott Michelfdc40a02009-02-17 22:15:04 +000010094 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010095 SDValue NegN0 = DAG.getNode(ISD::SUB, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +000010096 XType, DAG.getConstant(0, XType), N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +000010097 SDValue NotN0 = DAG.getNOT(SDLoc(N0), N0, XType);
Bill Wendling836ca7d2009-01-30 23:59:18 +000010098 return DAG.getNode(ISD::SRL, DL, XType,
Bill Wendlingfc4b6772009-02-01 11:19:36 +000010099 DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +000010100 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +000010101 getShiftAmountTy(XType)));
Nate Begemanf845b452005-10-08 00:29:44 +000010102 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010103 // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1))
Nate Begemanf845b452005-10-08 00:29:44 +000010104 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010105 SDValue Sign = DAG.getNode(ISD::SRL, SDLoc(N0), XType, N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +000010106 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +000010107 getShiftAmountTy(N0.getValueType())));
Bill Wendling836ca7d2009-01-30 23:59:18 +000010108 return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType));
Nate Begemanf845b452005-10-08 00:29:44 +000010109 }
10110 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010111
Benjamin Kramercde51102010-07-08 12:09:56 +000010112 // Check to see if this is an integer abs.
10113 // select_cc setg[te] X, 0, X, -X ->
10114 // select_cc setgt X, -1, X, -X ->
10115 // select_cc setl[te] X, 0, -X, X ->
10116 // select_cc setlt X, 1, -X, X ->
Nate Begemanf845b452005-10-08 00:29:44 +000010117 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
Benjamin Kramercde51102010-07-08 12:09:56 +000010118 if (N1C) {
10119 ConstantSDNode *SubC = NULL;
10120 if (((N1C->isNullValue() && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
10121 (N1C->isAllOnesValue() && CC == ISD::SETGT)) &&
10122 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1))
10123 SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0));
10124 else if (((N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE)) ||
10125 (N1C->isOne() && CC == ISD::SETLT)) &&
10126 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1))
10127 SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0));
10128
Owen Andersone50ed302009-08-10 22:56:29 +000010129 EVT XType = N0.getValueType();
Benjamin Kramercde51102010-07-08 12:09:56 +000010130 if (SubC && SubC->isNullValue() && XType.isInteger()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010131 SDValue Shift = DAG.getNode(ISD::SRA, SDLoc(N0), XType,
Benjamin Kramercde51102010-07-08 12:09:56 +000010132 N0,
10133 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +000010134 getShiftAmountTy(N0.getValueType())));
Andrew Trickac6d9be2013-05-25 02:42:55 +000010135 SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N0),
Benjamin Kramercde51102010-07-08 12:09:56 +000010136 XType, N0, Shift);
10137 AddToWorkList(Shift.getNode());
10138 AddToWorkList(Add.getNode());
10139 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Nate Begemanf845b452005-10-08 00:29:44 +000010140 }
10141 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010142
Dan Gohman475871a2008-07-27 21:46:04 +000010143 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +000010144}
10145
Evan Chengfa1eb272007-02-08 22:13:59 +000010146/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Owen Andersone50ed302009-08-10 22:56:29 +000010147SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0,
Dan Gohman475871a2008-07-27 21:46:04 +000010148 SDValue N1, ISD::CondCode Cond,
Andrew Trickac6d9be2013-05-25 02:42:55 +000010149 SDLoc DL, bool foldBooleans) {
Scott Michelfdc40a02009-02-17 22:15:04 +000010150 TargetLowering::DAGCombinerInfo
Nadav Rotem444b4bf2012-12-27 06:47:41 +000010151 DagCombineInfo(DAG, Level, false, this);
Dale Johannesenff97d4f2009-02-03 00:47:48 +000010152 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL);
Nate Begeman452d7be2005-09-16 00:54:12 +000010153}
10154
Nate Begeman69575232005-10-20 02:15:44 +000010155/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
10156/// return a DAG expression to select that will generate the same value by
10157/// multiplying by a magic number. See:
10158/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +000010159SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +000010160 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +000010161 SDValue S = TLI.BuildSDIV(N, DAG, LegalOperations, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010162
Andrew Lenharth232c9102006-06-12 16:07:18 +000010163 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010164 ii != ee; ++ii)
10165 AddToWorkList(*ii);
10166 return S;
Nate Begeman69575232005-10-20 02:15:44 +000010167}
10168
10169/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
10170/// return a DAG expression to select that will generate the same value by
10171/// multiplying by a magic number. See:
10172/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +000010173SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +000010174 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +000010175 SDValue S = TLI.BuildUDIV(N, DAG, LegalOperations, &Built);
Nate Begeman69575232005-10-20 02:15:44 +000010176
Andrew Lenharth232c9102006-06-12 16:07:18 +000010177 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010178 ii != ee; ++ii)
10179 AddToWorkList(*ii);
10180 return S;
Nate Begeman69575232005-10-20 02:15:44 +000010181}
10182
Nate Begemancc66cdd2009-09-25 06:05:26 +000010183/// FindBaseOffset - Return true if base is a frame index, which is known not
Eric Christopher503a64d2010-12-09 04:48:06 +000010184// to alias with anything but itself. Provides base object and offset as
10185// results.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010186static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset,
Roman Divacky2943e372012-09-05 22:15:49 +000010187 const GlobalValue *&GV, const void *&CV) {
Jim Laskey71382342006-10-07 23:37:56 +000010188 // Assume it is a primitive operation.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010189 Base = Ptr; Offset = 0; GV = 0; CV = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000010190
Jim Laskey71382342006-10-07 23:37:56 +000010191 // If it's an adding a simple constant then integrate the offset.
10192 if (Base.getOpcode() == ISD::ADD) {
10193 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
10194 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010195 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +000010196 }
10197 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010198
Nate Begemancc66cdd2009-09-25 06:05:26 +000010199 // Return the underlying GlobalValue, and update the Offset. Return false
10200 // for GlobalAddressSDNode since the same GlobalAddress may be represented
10201 // by multiple nodes with different offsets.
10202 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Base)) {
10203 GV = G->getGlobal();
10204 Offset += G->getOffset();
10205 return false;
10206 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010207
Nate Begemancc66cdd2009-09-25 06:05:26 +000010208 // Return the underlying Constant value, and update the Offset. Return false
10209 // for ConstantSDNodes since the same constant pool entry may be represented
10210 // by multiple nodes with different offsets.
10211 if (ConstantPoolSDNode *C = dyn_cast<ConstantPoolSDNode>(Base)) {
Roman Divacky2943e372012-09-05 22:15:49 +000010212 CV = C->isMachineConstantPoolEntry() ? (const void *)C->getMachineCPVal()
10213 : (const void *)C->getConstVal();
Nate Begemancc66cdd2009-09-25 06:05:26 +000010214 Offset += C->getOffset();
10215 return false;
10216 }
Jim Laskey71382342006-10-07 23:37:56 +000010217 // If it's any of the following then it can't alias with anything but itself.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010218 return isa<FrameIndexSDNode>(Base);
Jim Laskey71382342006-10-07 23:37:56 +000010219}
10220
10221/// isAlias - Return true if there is any possibility that the two addresses
10222/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +000010223bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +000010224 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010225 unsigned SrcValueAlign1,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010226 const MDNode *TBAAInfo1,
Dan Gohman475871a2008-07-27 21:46:04 +000010227 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010228 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010229 unsigned SrcValueAlign2,
10230 const MDNode *TBAAInfo2) const {
Jim Laskey71382342006-10-07 23:37:56 +000010231 // If they are the same then they must be aliases.
10232 if (Ptr1 == Ptr2) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +000010233
Jim Laskey71382342006-10-07 23:37:56 +000010234 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +000010235 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +000010236 int64_t Offset1, Offset2;
Dan Gohman46510a72010-04-15 01:51:59 +000010237 const GlobalValue *GV1, *GV2;
Roman Divacky2943e372012-09-05 22:15:49 +000010238 const void *CV1, *CV2;
Nate Begemancc66cdd2009-09-25 06:05:26 +000010239 bool isFrameIndex1 = FindBaseOffset(Ptr1, Base1, Offset1, GV1, CV1);
10240 bool isFrameIndex2 = FindBaseOffset(Ptr2, Base2, Offset2, GV2, CV2);
Scott Michelfdc40a02009-02-17 22:15:04 +000010241
Nate Begemancc66cdd2009-09-25 06:05:26 +000010242 // If they have a same base address then check to see if they overlap.
10243 if (Base1 == Base2 || (GV1 && (GV1 == GV2)) || (CV1 && (CV1 == CV2)))
Bill Wendling836ca7d2009-01-30 23:59:18 +000010244 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010245
Owen Anderson4a9f1502010-09-20 20:39:59 +000010246 // It is possible for different frame indices to alias each other, mostly
10247 // when tail call optimization reuses return address slots for arguments.
10248 // To catch this case, look up the actual index of frame indices to compute
10249 // the real alias relationship.
10250 if (isFrameIndex1 && isFrameIndex2) {
10251 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10252 Offset1 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base1)->getIndex());
10253 Offset2 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base2)->getIndex());
10254 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
10255 }
10256
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010257 // Otherwise, if we know what the bases are, and they aren't identical, then
Owen Anderson4a9f1502010-09-20 20:39:59 +000010258 // we know they cannot alias.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010259 if ((isFrameIndex1 || CV1 || GV1) && (isFrameIndex2 || CV2 || GV2))
10260 return false;
Jim Laskey096c22e2006-10-18 12:29:57 +000010261
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010262 // If we know required SrcValue1 and SrcValue2 have relatively large alignment
10263 // compared to the size and offset of the access, we may be able to prove they
10264 // do not alias. This check is conservative for now to catch cases created by
10265 // splitting vector types.
10266 if ((SrcValueAlign1 == SrcValueAlign2) &&
10267 (SrcValueOffset1 != SrcValueOffset2) &&
10268 (Size1 == Size2) && (SrcValueAlign1 > Size1)) {
10269 int64_t OffAlign1 = SrcValueOffset1 % SrcValueAlign1;
10270 int64_t OffAlign2 = SrcValueOffset2 % SrcValueAlign1;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010271
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010272 // There is no overlap between these relatively aligned accesses of similar
10273 // size, return no alias.
10274 if ((OffAlign1 + Size1) <= OffAlign2 || (OffAlign2 + Size2) <= OffAlign1)
10275 return false;
10276 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010277
Hal Finkel253acef2013-08-29 03:29:55 +000010278 bool UseAA = CombinerGlobalAA.getNumOccurrences() > 0 ? CombinerGlobalAA :
10279 TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA();
Hal Finkel77364b72013-09-15 02:19:49 +000010280 if (UseAA && SrcValue1 && SrcValue2) {
Jim Laskey07a27092006-10-18 19:08:31 +000010281 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +000010282 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
10283 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
10284 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Scott Michelfdc40a02009-02-17 22:15:04 +000010285 AliasAnalysis::AliasResult AAResult =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010286 AA.alias(AliasAnalysis::Location(SrcValue1, Overlap1, TBAAInfo1),
10287 AliasAnalysis::Location(SrcValue2, Overlap2, TBAAInfo2));
Jim Laskey07a27092006-10-18 19:08:31 +000010288 if (AAResult == AliasAnalysis::NoAlias)
10289 return false;
10290 }
Jim Laskey096c22e2006-10-18 12:29:57 +000010291
10292 // Otherwise we have to assume they alias.
10293 return true;
Jim Laskey71382342006-10-07 23:37:56 +000010294}
10295
Nadav Rotem90e11dc2012-11-29 00:00:08 +000010296bool DAGCombiner::isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1) {
10297 SDValue Ptr0, Ptr1;
10298 int64_t Size0, Size1;
10299 const Value *SrcValue0, *SrcValue1;
10300 int SrcValueOffset0, SrcValueOffset1;
10301 unsigned SrcValueAlign0, SrcValueAlign1;
10302 const MDNode *SrcTBAAInfo0, *SrcTBAAInfo1;
10303 FindAliasInfo(Op0, Ptr0, Size0, SrcValue0, SrcValueOffset0,
10304 SrcValueAlign0, SrcTBAAInfo0);
10305 FindAliasInfo(Op1, Ptr1, Size1, SrcValue1, SrcValueOffset1,
10306 SrcValueAlign1, SrcTBAAInfo1);
10307 return isAlias(Ptr0, Size0, SrcValue0, SrcValueOffset0,
Nadav Rotemdde785c2012-12-06 17:34:13 +000010308 SrcValueAlign0, SrcTBAAInfo0,
10309 Ptr1, Size1, SrcValue1, SrcValueOffset1,
10310 SrcValueAlign1, SrcTBAAInfo1);
Nadav Rotem90e11dc2012-11-29 00:00:08 +000010311}
10312
Jim Laskey71382342006-10-07 23:37:56 +000010313/// FindAliasInfo - Extracts the relevant alias information from the memory
10314/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +000010315bool DAGCombiner::FindAliasInfo(SDNode *N,
Benjamin Kramerae4746b2012-01-15 11:50:43 +000010316 SDValue &Ptr, int64_t &Size,
10317 const Value *&SrcValue,
10318 int &SrcValueOffset,
10319 unsigned &SrcValueAlign,
10320 const MDNode *&TBAAInfo) const {
10321 LSBaseSDNode *LS = cast<LSBaseSDNode>(N);
10322
10323 Ptr = LS->getBasePtr();
10324 Size = LS->getMemoryVT().getSizeInBits() >> 3;
10325 SrcValue = LS->getSrcValue();
10326 SrcValueOffset = LS->getSrcValueOffset();
10327 SrcValueAlign = LS->getOriginalAlignment();
10328 TBAAInfo = LS->getTBAAInfo();
10329 return isa<LoadSDNode>(LS);
Jim Laskey71382342006-10-07 23:37:56 +000010330}
10331
Jim Laskey6ff23e52006-10-04 16:53:27 +000010332/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
10333/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +000010334void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
Craig Toppera0ec3f92013-07-14 04:42:23 +000010335 SmallVectorImpl<SDValue> &Aliases) {
Dan Gohman475871a2008-07-27 21:46:04 +000010336 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010337 SmallPtrSet<SDNode *, 16> Visited; // Visited node set.
Scott Michelfdc40a02009-02-17 22:15:04 +000010338
Jim Laskey279f0532006-09-25 16:29:54 +000010339 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +000010340 SDValue Ptr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010341 int64_t Size;
10342 const Value *SrcValue;
10343 int SrcValueOffset;
10344 unsigned SrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010345 const MDNode *SrcTBAAInfo;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010346 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010347 SrcValueAlign, SrcTBAAInfo);
Jim Laskey279f0532006-09-25 16:29:54 +000010348
Jim Laskey6ff23e52006-10-04 16:53:27 +000010349 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +000010350 Chains.push_back(OriginalChain);
Nate Begeman677c89d2009-10-12 05:53:58 +000010351 unsigned Depth = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010352
Jim Laskeybc588b82006-10-05 15:07:25 +000010353 // Look at each chain and determine if it is an alias. If so, add it to the
10354 // aliases list. If not, then continue up the chain looking for the next
Scott Michelfdc40a02009-02-17 22:15:04 +000010355 // candidate.
Jim Laskeybc588b82006-10-05 15:07:25 +000010356 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010357 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +000010358 Chains.pop_back();
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010359
10360 // For TokenFactor nodes, look at each operand and only continue up the
10361 // chain until we find two aliases. If we've seen two aliases, assume we'll
Nate Begeman677c89d2009-10-12 05:53:58 +000010362 // find more and revert to original chain since the xform is unlikely to be
10363 // profitable.
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010364 //
10365 // FIXME: The depth check could be made to return the last non-aliasing
Nate Begeman677c89d2009-10-12 05:53:58 +000010366 // chain we found before we hit a tokenfactor rather than the original
10367 // chain.
10368 if (Depth > 6 || Aliases.size() == 2) {
10369 Aliases.clear();
10370 Aliases.push_back(OriginalChain);
10371 break;
10372 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010373
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010374 // Don't bother if we've been before.
10375 if (!Visited.insert(Chain.getNode()))
10376 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +000010377
Jim Laskeybc588b82006-10-05 15:07:25 +000010378 switch (Chain.getOpcode()) {
10379 case ISD::EntryToken:
10380 // Entry token is ideal chain operand, but handled in FindBetterChain.
10381 break;
Scott Michelfdc40a02009-02-17 22:15:04 +000010382
Jim Laskeybc588b82006-10-05 15:07:25 +000010383 case ISD::LOAD:
10384 case ISD::STORE: {
10385 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +000010386 SDValue OpPtr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010387 int64_t OpSize;
10388 const Value *OpSrcValue;
10389 int OpSrcValueOffset;
10390 unsigned OpSrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010391 const MDNode *OpSrcTBAAInfo;
Gabor Greifba36cb52008-08-28 21:40:38 +000010392 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010393 OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010394 OpSrcValueAlign,
10395 OpSrcTBAAInfo);
Scott Michelfdc40a02009-02-17 22:15:04 +000010396
Jim Laskeybc588b82006-10-05 15:07:25 +000010397 // If chain is alias then stop here.
10398 if (!(IsLoad && IsOpLoad) &&
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010399 isAlias(Ptr, Size, SrcValue, SrcValueOffset, SrcValueAlign,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010400 SrcTBAAInfo,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010401 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010402 OpSrcValueAlign, OpSrcTBAAInfo)) {
Jim Laskeybc588b82006-10-05 15:07:25 +000010403 Aliases.push_back(Chain);
10404 } else {
10405 // Look further up the chain.
Scott Michelfdc40a02009-02-17 22:15:04 +000010406 Chains.push_back(Chain.getOperand(0));
Nate Begeman677c89d2009-10-12 05:53:58 +000010407 ++Depth;
Jim Laskey279f0532006-09-25 16:29:54 +000010408 }
Jim Laskeybc588b82006-10-05 15:07:25 +000010409 break;
10410 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010411
Jim Laskeybc588b82006-10-05 15:07:25 +000010412 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010413 // We have to check each of the operands of the token factor for "small"
10414 // token factors, so we queue them up. Adding the operands to the queue
10415 // (stack) in reverse order maintains the original order and increases the
10416 // likelihood that getNode will find a matching token factor (CSE.)
10417 if (Chain.getNumOperands() > 16) {
10418 Aliases.push_back(Chain);
10419 break;
10420 }
Jim Laskeybc588b82006-10-05 15:07:25 +000010421 for (unsigned n = Chain.getNumOperands(); n;)
10422 Chains.push_back(Chain.getOperand(--n));
Nate Begeman677c89d2009-10-12 05:53:58 +000010423 ++Depth;
Jim Laskeybc588b82006-10-05 15:07:25 +000010424 break;
Scott Michelfdc40a02009-02-17 22:15:04 +000010425
Jim Laskeybc588b82006-10-05 15:07:25 +000010426 default:
10427 // For all other instructions we will just have to take what we can get.
10428 Aliases.push_back(Chain);
10429 break;
Jim Laskey279f0532006-09-25 16:29:54 +000010430 }
10431 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000010432}
10433
10434/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
10435/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +000010436SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
10437 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +000010438
Jim Laskey6ff23e52006-10-04 16:53:27 +000010439 // Accumulate all the aliases to this node.
10440 GatherAllAliases(N, OldChain, Aliases);
Scott Michelfdc40a02009-02-17 22:15:04 +000010441
Dan Gohman71dc7c92011-05-17 22:20:36 +000010442 // If no operands then chain to entry token.
10443 if (Aliases.size() == 0)
Jim Laskey6ff23e52006-10-04 16:53:27 +000010444 return DAG.getEntryNode();
Dan Gohman71dc7c92011-05-17 22:20:36 +000010445
10446 // If a single operand then chain to it. We don't need to revisit it.
10447 if (Aliases.size() == 1)
Jim Laskey6ff23e52006-10-04 16:53:27 +000010448 return Aliases[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010449
Jim Laskey6ff23e52006-10-04 16:53:27 +000010450 // Construct a custom tailored token factor.
Andrew Trickac6d9be2013-05-25 02:42:55 +000010451 return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010452 &Aliases[0], Aliases.size());
Jim Laskey279f0532006-09-25 16:29:54 +000010453}
10454
Nate Begeman1d4d4142005-09-01 00:19:25 +000010455// SelectionDAG::Combine - This is the entry point for the file.
10456//
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000010457void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA,
Bill Wendling98a366d2009-04-29 23:29:43 +000010458 CodeGenOpt::Level OptLevel) {
Nate Begeman1d4d4142005-09-01 00:19:25 +000010459 /// run - This is the main entry point to this class.
10460 ///
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000010461 DAGCombiner(*this, AA, OptLevel).Run(Level);
Nate Begeman1d4d4142005-09-01 00:19:25 +000010462}