blob: 8d6eab7c7b97e2365e29733c2b60df828c429aed [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"
Quentin Colombetc34693f2013-10-11 18:01:14 +000038#include "llvm/Target/TargetRegisterInfo.h"
Hal Finkel253acef2013-08-29 03:29:55 +000039#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000040#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000041using namespace llvm;
42
Chris Lattnercd3245a2006-12-19 22:41:21 +000043STATISTIC(NodesCombined , "Number of dag nodes combined");
44STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
45STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
Evan Cheng8b944d32009-05-28 00:35:15 +000046STATISTIC(OpsNarrowed , "Number of load/op/store narrowed");
Evan Cheng31959b12011-02-02 01:06:55 +000047STATISTIC(LdStFP2Int , "Number of fp load/store pairs transformed to int");
Quentin Colombetc34693f2013-10-11 18:01:14 +000048STATISTIC(SlicedLoads, "Number of load sliced");
Chris Lattnercd3245a2006-12-19 22:41:21 +000049
Nate Begeman1d4d4142005-09-01 00:19:25 +000050namespace {
Jim Laskey71382342006-10-07 23:37:56 +000051 static cl::opt<bool>
Owen Anderson0dcc8142010-09-19 21:01:26 +000052 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000053 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000054
Jim Laskey07a27092006-10-18 19:08:31 +000055 static cl::opt<bool>
56 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
57 cl::desc("Include global information in alias analysis"));
58
Quentin Colombetc34693f2013-10-11 18:01:14 +000059 /// Hidden option to stress test load slicing, i.e., when this option
60 /// is enabled, load slicing bypasses most of its profitability guards.
61 static cl::opt<bool>
62 StressLoadSlicing("combiner-stress-load-slicing", cl::Hidden,
63 cl::desc("Bypass the profitability model of load "
64 "slicing"),
65 cl::init(false));
66
Jim Laskeybc588b82006-10-05 15:07:25 +000067//------------------------------ DAGCombiner ---------------------------------//
68
Nick Lewycky6726b6d2009-10-25 06:33:48 +000069 class DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000070 SelectionDAG &DAG;
Dan Gohman79ce2762009-01-15 19:20:50 +000071 const TargetLowering &TLI;
Duncan Sands25cf2272008-11-24 14:53:14 +000072 CombineLevel Level;
Bill Wendling98a366d2009-04-29 23:29:43 +000073 CodeGenOpt::Level OptLevel;
Duncan Sands25cf2272008-11-24 14:53:14 +000074 bool LegalOperations;
75 bool LegalTypes;
Quentin Colombetc34693f2013-10-11 18:01:14 +000076 bool ForCodeSize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000077
78 // Worklist of all of the nodes that need to be simplified.
James Molloy6660c052012-02-16 09:17:04 +000079 //
80 // This has the semantics that when adding to the worklist,
81 // the item added must be next to be processed. It should
82 // also only appear once. The naive approach to this takes
83 // linear time.
84 //
85 // To reduce the insert/remove time to logarithmic, we use
86 // a set and a vector to maintain our worklist.
87 //
88 // The set contains the items on the worklist, but does not
89 // maintain the order they should be visited.
90 //
91 // The vector maintains the order nodes should be visited, but may
92 // contain duplicate or removed nodes. When choosing a node to
93 // visit, we pop off the order stack until we find an item that is
94 // also in the contents set. All operations are O(log N).
95 SmallPtrSet<SDNode*, 64> WorkListContents;
Benjamin Kramerd5f76902012-03-10 00:23:58 +000096 SmallVector<SDNode*, 64> WorkListOrder;
Nate Begeman1d4d4142005-09-01 00:19:25 +000097
Jim Laskeyc7c3f112006-10-16 20:52:31 +000098 // AA - Used for DAG load/store alias analysis.
99 AliasAnalysis &AA;
100
Nate Begeman1d4d4142005-09-01 00:19:25 +0000101 /// AddUsersToWorkList - When an instruction is simplified, add all users of
102 /// the instruction to the work lists because they might get more simplified
103 /// now.
104 ///
105 void AddUsersToWorkList(SDNode *N) {
106 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +0000107 UI != UE; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +0000108 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000109 }
110
Dan Gohman389079b2007-10-08 17:57:15 +0000111 /// visit - call the node-specific routine that knows how to fold each
112 /// particular type of node.
Dan Gohman475871a2008-07-27 21:46:04 +0000113 SDValue visit(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +0000114
Chris Lattner24664722006-03-01 04:53:38 +0000115 public:
James Molloy6afa3f72012-02-16 09:48:07 +0000116 /// AddToWorkList - Add to the work list making sure its instance is at the
James Molloy6660c052012-02-16 09:17:04 +0000117 /// back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +0000118 void AddToWorkList(SDNode *N) {
James Molloy6660c052012-02-16 09:17:04 +0000119 WorkListContents.insert(N);
120 WorkListOrder.push_back(N);
Chris Lattner5750df92006-03-01 04:03:14 +0000121 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000122
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000123 /// removeFromWorkList - remove all instances of N from the worklist.
124 ///
125 void removeFromWorkList(SDNode *N) {
James Molloy6660c052012-02-16 09:17:04 +0000126 WorkListContents.erase(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000127 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000128
Dan Gohman475871a2008-07-27 21:46:04 +0000129 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000130 bool AddTo = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000131
Dan Gohman475871a2008-07-27 21:46:04 +0000132 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Jim Laskey274062c2006-10-13 23:32:28 +0000133 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000134 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000135
Dan Gohman475871a2008-07-27 21:46:04 +0000136 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000137 bool AddTo = true) {
Dan Gohman475871a2008-07-27 21:46:04 +0000138 SDValue To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000139 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000140 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000141
142 void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO);
Scott Michelfdc40a02009-02-17 22:15:04 +0000143
144 private:
145
Chris Lattner012f2412006-02-17 21:58:01 +0000146 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000147 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000148 /// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000149 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman87862e72009-12-11 21:31:27 +0000150 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
151 APInt Demanded = APInt::getAllOnesValue(BitWidth);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000152 return SimplifyDemandedBits(Op, Demanded);
153 }
154
Dan Gohman475871a2008-07-27 21:46:04 +0000155 bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded);
Chris Lattner87514ca2005-10-10 22:31:19 +0000156
Chris Lattner448f2192006-11-11 00:39:41 +0000157 bool CombineToPreIndexedLoadStore(SDNode *N);
158 bool CombineToPostIndexedLoadStore(SDNode *N);
Quentin Colombetc34693f2013-10-11 18:01:14 +0000159 bool SliceUpLoad(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000160
Evan Cheng95c57ea2010-04-24 04:43:44 +0000161 void ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad);
162 SDValue PromoteOperand(SDValue Op, EVT PVT, bool &Replace);
163 SDValue SExtPromoteOperand(SDValue Op, EVT PVT);
164 SDValue ZExtPromoteOperand(SDValue Op, EVT PVT);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000165 SDValue PromoteIntBinOp(SDValue Op);
Evan Cheng07c4e102010-04-22 20:19:46 +0000166 SDValue PromoteIntShiftOp(SDValue Op);
Evan Cheng4c26e932010-04-19 19:29:22 +0000167 SDValue PromoteExtend(SDValue Op);
168 bool PromoteLoad(SDValue Op);
Scott Michelfdc40a02009-02-17 22:15:04 +0000169
Craig Topper6c64fba2013-07-13 07:43:40 +0000170 void ExtendSetCCUses(const SmallVectorImpl<SDNode *> &SetCCs,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000171 SDValue Trunc, SDValue ExtLoad, SDLoc DL,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +0000172 ISD::NodeType ExtType);
173
Dan Gohman389079b2007-10-08 17:57:15 +0000174 /// combine - call the node-specific routine that knows how to fold each
175 /// particular type of node. If that doesn't do anything, try the
176 /// target-specific DAG combines.
Dan Gohman475871a2008-07-27 21:46:04 +0000177 SDValue combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000178
179 // Visitation implementation - Implement dag node combining for different
180 // node types. The semantics are as follows:
181 // Return Value:
Evan Cheng17a568b2008-08-29 22:21:44 +0000182 // SDValue.getNode() == 0 - No change was made
183 // SDValue.getNode() == N - N was replaced, is dead and has been handled.
184 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000185 //
Dan Gohman475871a2008-07-27 21:46:04 +0000186 SDValue visitTokenFactor(SDNode *N);
187 SDValue visitMERGE_VALUES(SDNode *N);
188 SDValue visitADD(SDNode *N);
189 SDValue visitSUB(SDNode *N);
190 SDValue visitADDC(SDNode *N);
Craig Toppercc274522012-01-07 09:06:39 +0000191 SDValue visitSUBC(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000192 SDValue visitADDE(SDNode *N);
Craig Toppercc274522012-01-07 09:06:39 +0000193 SDValue visitSUBE(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000194 SDValue visitMUL(SDNode *N);
195 SDValue visitSDIV(SDNode *N);
196 SDValue visitUDIV(SDNode *N);
197 SDValue visitSREM(SDNode *N);
198 SDValue visitUREM(SDNode *N);
199 SDValue visitMULHU(SDNode *N);
200 SDValue visitMULHS(SDNode *N);
201 SDValue visitSMUL_LOHI(SDNode *N);
202 SDValue visitUMUL_LOHI(SDNode *N);
Benjamin Kramerf55d26e2011-05-21 18:31:55 +0000203 SDValue visitSMULO(SDNode *N);
204 SDValue visitUMULO(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000205 SDValue visitSDIVREM(SDNode *N);
206 SDValue visitUDIVREM(SDNode *N);
207 SDValue visitAND(SDNode *N);
208 SDValue visitOR(SDNode *N);
209 SDValue visitXOR(SDNode *N);
210 SDValue SimplifyVBinOp(SDNode *N);
Craig Topperdd201ff2012-09-11 01:45:21 +0000211 SDValue SimplifyVUnaryOp(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000212 SDValue visitSHL(SDNode *N);
213 SDValue visitSRA(SDNode *N);
214 SDValue visitSRL(SDNode *N);
215 SDValue visitCTLZ(SDNode *N);
Chandler Carruth63974b22011-12-13 01:56:10 +0000216 SDValue visitCTLZ_ZERO_UNDEF(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000217 SDValue visitCTTZ(SDNode *N);
Chandler Carruth63974b22011-12-13 01:56:10 +0000218 SDValue visitCTTZ_ZERO_UNDEF(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000219 SDValue visitCTPOP(SDNode *N);
220 SDValue visitSELECT(SDNode *N);
Benjamin Kramer6242fda2013-04-26 09:19:19 +0000221 SDValue visitVSELECT(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000222 SDValue visitSELECT_CC(SDNode *N);
223 SDValue visitSETCC(SDNode *N);
224 SDValue visitSIGN_EXTEND(SDNode *N);
225 SDValue visitZERO_EXTEND(SDNode *N);
226 SDValue visitANY_EXTEND(SDNode *N);
227 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
228 SDValue visitTRUNCATE(SDNode *N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000229 SDValue visitBITCAST(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000230 SDValue visitBUILD_PAIR(SDNode *N);
231 SDValue visitFADD(SDNode *N);
232 SDValue visitFSUB(SDNode *N);
233 SDValue visitFMUL(SDNode *N);
Owen Anderson062c0a52012-05-02 22:17:40 +0000234 SDValue visitFMA(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000235 SDValue visitFDIV(SDNode *N);
236 SDValue visitFREM(SDNode *N);
237 SDValue visitFCOPYSIGN(SDNode *N);
238 SDValue visitSINT_TO_FP(SDNode *N);
239 SDValue visitUINT_TO_FP(SDNode *N);
240 SDValue visitFP_TO_SINT(SDNode *N);
241 SDValue visitFP_TO_UINT(SDNode *N);
242 SDValue visitFP_ROUND(SDNode *N);
243 SDValue visitFP_ROUND_INREG(SDNode *N);
244 SDValue visitFP_EXTEND(SDNode *N);
245 SDValue visitFNEG(SDNode *N);
246 SDValue visitFABS(SDNode *N);
Owen Anderson7c626d32012-08-13 23:32:49 +0000247 SDValue visitFCEIL(SDNode *N);
248 SDValue visitFTRUNC(SDNode *N);
249 SDValue visitFFLOOR(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000250 SDValue visitBRCOND(SDNode *N);
251 SDValue visitBR_CC(SDNode *N);
252 SDValue visitLOAD(SDNode *N);
253 SDValue visitSTORE(SDNode *N);
254 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
255 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
256 SDValue visitBUILD_VECTOR(SDNode *N);
257 SDValue visitCONCAT_VECTORS(SDNode *N);
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +0000258 SDValue visitEXTRACT_SUBVECTOR(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000259 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000260
Dan Gohman475871a2008-07-27 21:46:04 +0000261 SDValue XformToShuffleWithZero(SDNode *N);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000262 SDValue ReassociateOps(unsigned Opc, SDLoc DL, SDValue LHS, SDValue RHS);
Scott Michelfdc40a02009-02-17 22:15:04 +0000263
Dan Gohman475871a2008-07-27 21:46:04 +0000264 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattnere70da202007-12-06 07:33:36 +0000265
Dan Gohman475871a2008-07-27 21:46:04 +0000266 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
267 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000268 SDValue SimplifySelect(SDLoc DL, SDValue N0, SDValue N1, SDValue N2);
269 SDValue SimplifySelectCC(SDLoc DL, SDValue N0, SDValue N1, SDValue N2,
Scott Michelfdc40a02009-02-17 22:15:04 +0000270 SDValue N3, ISD::CondCode CC,
Bill Wendling836ca7d2009-01-30 23:59:18 +0000271 bool NotExtCompare = false);
Owen Andersone50ed302009-08-10 22:56:29 +0000272 SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000273 SDLoc DL, bool foldBooleans = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000274 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner5eee4272008-01-26 01:09:19 +0000275 unsigned HiOp);
Owen Andersone50ed302009-08-10 22:56:29 +0000276 SDValue CombineConsecutiveLoads(SDNode *N, EVT VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000277 SDValue ConstantFoldBITCASTofBUILD_VECTOR(SDNode *, EVT);
Dan Gohman475871a2008-07-27 21:46:04 +0000278 SDValue BuildSDIV(SDNode *N);
279 SDValue BuildUDIV(SDNode *N);
Evan Cheng9568e5c2011-06-21 06:01:08 +0000280 SDValue MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
281 bool DemandHighBits = true);
282 SDValue MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000283 SDNode *MatchRotate(SDValue LHS, SDValue RHS, SDLoc DL);
Dan Gohman475871a2008-07-27 21:46:04 +0000284 SDValue ReduceLoadWidth(SDNode *N);
Evan Cheng8b944d32009-05-28 00:35:15 +0000285 SDValue ReduceLoadOpStoreWidth(SDNode *N);
Evan Cheng31959b12011-02-02 01:06:55 +0000286 SDValue TransformFPLoadStorePair(SDNode *N);
Michael Liaofac14ab2012-10-23 23:06:52 +0000287 SDValue reduceBuildVecExtToExtBuildVec(SDNode *N);
Michael Liao1a5cc712012-10-24 04:14:18 +0000288 SDValue reduceBuildVecConvertToConvertBuildVec(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000289
Dan Gohman475871a2008-07-27 21:46:04 +0000290 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Scott Michelfdc40a02009-02-17 22:15:04 +0000291
Jim Laskey6ff23e52006-10-04 16:53:27 +0000292 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
293 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000294 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
Craig Toppera0ec3f92013-07-14 04:42:23 +0000295 SmallVectorImpl<SDValue> &Aliases);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000296
Jim Laskey096c22e2006-10-18 12:29:57 +0000297 /// isAlias - Return true if there is any possibility that the two addresses
298 /// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +0000299 bool isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +0000300 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000301 unsigned SrcValueAlign1,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000302 const MDNode *TBAAInfo1,
Dan Gohman475871a2008-07-27 21:46:04 +0000303 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000304 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000305 unsigned SrcValueAlign2,
306 const MDNode *TBAAInfo2) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000307
Nadav Rotem90e11dc2012-11-29 00:00:08 +0000308 /// isAlias - Return true if there is any possibility that the two addresses
309 /// overlap.
310 bool isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1);
311
Jim Laskey7ca56af2006-10-11 13:47:09 +0000312 /// FindAliasInfo - Extracts the relevant alias information from the memory
313 /// node. Returns true if the operand was a load.
314 bool FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000315 SDValue &Ptr, int64_t &Size,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000316 const Value *&SrcValue, int &SrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000317 unsigned &SrcValueAlignment,
318 const MDNode *&TBAAInfo) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000319
Jim Laskey279f0532006-09-25 16:29:54 +0000320 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000321 /// looking for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +0000322 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Duncan Sands92abc622009-01-31 15:50:11 +0000323
Nadav Rotemc653de62012-10-03 16:11:15 +0000324 /// Merge consecutive store operations into a wide store.
325 /// This optimization uses wide integers or vectors when possible.
326 /// \return True if some memory operations were changed.
327 bool MergeConsecutiveStores(StoreSDNode *N);
328
Chris Lattner2392ae72010-04-15 04:48:01 +0000329 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000330 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, CodeGenOpt::Level OL)
Quentin Colombetc34693f2013-10-11 18:01:14 +0000331 : DAG(D), TLI(D.getTargetLoweringInfo()), Level(BeforeLegalizeTypes),
332 OptLevel(OL), LegalOperations(false), LegalTypes(false), AA(A) {
333 AttributeSet FnAttrs =
334 DAG.getMachineFunction().getFunction()->getAttributes();
335 ForCodeSize =
336 FnAttrs.hasAttribute(AttributeSet::FunctionIndex,
337 Attribute::OptimizeForSize) ||
338 FnAttrs.hasAttribute(AttributeSet::FunctionIndex, Attribute::MinSize);
339 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000340
Nate Begeman1d4d4142005-09-01 00:19:25 +0000341 /// Run - runs the dag combiner on all nodes in the work list
Duncan Sands25cf2272008-11-24 14:53:14 +0000342 void Run(CombineLevel AtLevel);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000343
Chris Lattner2392ae72010-04-15 04:48:01 +0000344 SelectionDAG &getDAG() const { return DAG; }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000345
Chris Lattner2392ae72010-04-15 04:48:01 +0000346 /// getShiftAmountTy - Returns a type large enough to hold any valid
347 /// shift amount - before type legalization these can be huge.
Owen Anderson95771af2011-02-25 21:41:48 +0000348 EVT getShiftAmountTy(EVT LHSTy) {
Elena Demikhovsky87070fe2013-06-26 10:55:03 +0000349 assert(LHSTy.isInteger() && "Shift amount is not an integer type!");
350 if (LHSTy.isVector())
351 return LHSTy;
352 return LegalTypes ? TLI.getScalarShiftAmountTy(LHSTy) : TLI.getPointerTy();
Chris Lattner2392ae72010-04-15 04:48:01 +0000353 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000354
Chris Lattner2392ae72010-04-15 04:48:01 +0000355 /// isTypeLegal - This method returns true if we are running before type
356 /// legalization or if the specified VT is legal.
357 bool isTypeLegal(const EVT &VT) {
358 if (!LegalTypes) return true;
359 return TLI.isTypeLegal(VT);
360 }
Matt Arsenault225ed702013-05-18 00:21:46 +0000361
362 /// getSetCCResultType - Convenience wrapper around
363 /// TargetLowering::getSetCCResultType
364 EVT getSetCCResultType(EVT VT) const {
365 return TLI.getSetCCResultType(*DAG.getContext(), VT);
366 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000367 };
368}
369
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000370
371namespace {
372/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
373/// nodes from the worklist.
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000374class WorkListRemover : public SelectionDAG::DAGUpdateListener {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000375 DAGCombiner &DC;
376public:
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000377 explicit WorkListRemover(DAGCombiner &dc)
378 : SelectionDAG::DAGUpdateListener(dc.getDAG()), DC(dc) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000379
Duncan Sandsedfcf592008-06-11 11:42:12 +0000380 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000381 DC.removeFromWorkList(N);
382 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000383};
384}
385
Chris Lattner24664722006-03-01 04:53:38 +0000386//===----------------------------------------------------------------------===//
387// TargetLowering::DAGCombinerInfo implementation
388//===----------------------------------------------------------------------===//
389
390void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
391 ((DAGCombiner*)DC)->AddToWorkList(N);
392}
393
Cameron Zwariched3caf92011-04-02 02:40:26 +0000394void TargetLowering::DAGCombinerInfo::RemoveFromWorklist(SDNode *N) {
395 ((DAGCombiner*)DC)->removeFromWorkList(N);
396}
397
Dan Gohman475871a2008-07-27 21:46:04 +0000398SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000399CombineTo(SDNode *N, const std::vector<SDValue> &To, bool AddTo) {
400 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000401}
402
Dan Gohman475871a2008-07-27 21:46:04 +0000403SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000404CombineTo(SDNode *N, SDValue Res, bool AddTo) {
405 return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000406}
407
408
Dan Gohman475871a2008-07-27 21:46:04 +0000409SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000410CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) {
411 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000412}
413
Dan Gohmane5af2d32009-01-29 01:59:02 +0000414void TargetLowering::DAGCombinerInfo::
415CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
416 return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO);
417}
Chris Lattner24664722006-03-01 04:53:38 +0000418
Chris Lattner24664722006-03-01 04:53:38 +0000419//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000420// Helper Functions
421//===----------------------------------------------------------------------===//
422
423/// isNegatibleForFree - Return 1 if we can compute the negated form of the
424/// specified expression for the same cost as the expression itself, or 2 if we
425/// can compute the negated form more cheaply than the expression itself.
Duncan Sands25cf2272008-11-24 14:53:14 +0000426static char isNegatibleForFree(SDValue Op, bool LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000427 const TargetLowering &TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000428 const TargetOptions *Options,
Chris Lattner0254e702008-02-26 07:04:54 +0000429 unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000430 // fneg is removable even if it has multiple uses.
431 if (Op.getOpcode() == ISD::FNEG) return 2;
Scott Michelfdc40a02009-02-17 22:15:04 +0000432
Chris Lattner29446522007-05-14 22:04:50 +0000433 // Don't allow anything with multiple uses.
434 if (!Op.hasOneUse()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000435
Chris Lattner3adf9512007-05-25 02:19:06 +0000436 // Don't recurse exponentially.
437 if (Depth > 6) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000438
Chris Lattner29446522007-05-14 22:04:50 +0000439 switch (Op.getOpcode()) {
440 default: return false;
441 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000442 // Don't invert constant FP values after legalize. The negated constant
443 // isn't necessarily legal.
Duncan Sands25cf2272008-11-24 14:53:14 +0000444 return LegalOperations ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000445 case ISD::FADD:
446 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000447 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000448
Owen Andersonafd3d562012-03-06 00:29:31 +0000449 // After operation legalization, it might not be legal to create new FSUBs.
450 if (LegalOperations &&
451 !TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType()))
452 return 0;
453
Craig Topper956342b2012-09-09 22:58:45 +0000454 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Owen Andersonafd3d562012-03-06 00:29:31 +0000455 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
456 Options, Depth + 1))
Chris Lattner29446522007-05-14 22:04:50 +0000457 return V;
Bill Wendlingd34470c2009-01-30 23:10:18 +0000458 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Owen Andersonafd3d562012-03-06 00:29:31 +0000459 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000460 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000461 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000462 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000463 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000464
Bill Wendlingd34470c2009-01-30 23:10:18 +0000465 // fold (fneg (fsub A, B)) -> (fsub B, A)
Chris Lattner29446522007-05-14 22:04:50 +0000466 return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000467
Chris Lattner29446522007-05-14 22:04:50 +0000468 case ISD::FMUL:
469 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000470 if (Options->HonorSignDependentRoundingFPMath()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000471
Bill Wendlingd34470c2009-01-30 23:10:18 +0000472 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y))
Owen Andersonafd3d562012-03-06 00:29:31 +0000473 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
474 Options, Depth + 1))
Chris Lattner29446522007-05-14 22:04:50 +0000475 return V;
Scott Michelfdc40a02009-02-17 22:15:04 +0000476
Owen Andersonafd3d562012-03-06 00:29:31 +0000477 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000478 Depth + 1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000479
Chris Lattner29446522007-05-14 22:04:50 +0000480 case ISD::FP_EXTEND:
481 case ISD::FP_ROUND:
482 case ISD::FSIN:
Owen Andersonafd3d562012-03-06 00:29:31 +0000483 return isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000484 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000485 }
486}
487
488/// GetNegatedExpression - If isNegatibleForFree returns true, this function
489/// returns the newly negated expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000490static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000491 bool LegalOperations, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000492 // fneg is removable even if it has multiple uses.
493 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000494
Chris Lattner29446522007-05-14 22:04:50 +0000495 // Don't allow anything with multiple uses.
496 assert(Op.hasOneUse() && "Unknown reuse!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000497
Chris Lattner3adf9512007-05-25 02:19:06 +0000498 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000499 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000500 default: llvm_unreachable("Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000501 case ISD::ConstantFP: {
502 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
503 V.changeSign();
504 return DAG.getConstantFP(V, Op.getValueType());
505 }
Chris Lattner29446522007-05-14 22:04:50 +0000506 case ISD::FADD:
507 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000508 assert(DAG.getTarget().Options.UnsafeFPMath);
Scott Michelfdc40a02009-02-17 22:15:04 +0000509
Bill Wendlingd34470c2009-01-30 23:10:18 +0000510 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000511 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000512 DAG.getTargetLoweringInfo(),
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000513 &DAG.getTarget().Options, Depth+1))
Andrew Trickac6d9be2013-05-25 02:42:55 +0000514 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000515 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000516 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000517 Op.getOperand(1));
Bill Wendlingd34470c2009-01-30 23:10:18 +0000518 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Andrew Trickac6d9be2013-05-25 02:42:55 +0000519 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000520 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000521 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000522 Op.getOperand(0));
523 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000524 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000525 assert(DAG.getTarget().Options.UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000526
Bill Wendlingd34470c2009-01-30 23:10:18 +0000527 // fold (fneg (fsub 0, B)) -> B
Dan Gohman23ff1822007-07-02 15:48:56 +0000528 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000529 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000530 return Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000531
Bill Wendlingd34470c2009-01-30 23:10:18 +0000532 // fold (fneg (fsub A, B)) -> (fsub B, A)
Andrew Trickac6d9be2013-05-25 02:42:55 +0000533 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Bill Wendling35247c32009-01-30 00:45:56 +0000534 Op.getOperand(1), Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +0000535
Chris Lattner29446522007-05-14 22:04:50 +0000536 case ISD::FMUL:
537 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000538 assert(!DAG.getTarget().Options.HonorSignDependentRoundingFPMath());
Scott Michelfdc40a02009-02-17 22:15:04 +0000539
Bill Wendlingd34470c2009-01-30 23:10:18 +0000540 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000541 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000542 DAG.getTargetLoweringInfo(),
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000543 &DAG.getTarget().Options, Depth+1))
Andrew Trickac6d9be2013-05-25 02:42:55 +0000544 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000545 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000546 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000547 Op.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000548
Bill Wendlingd34470c2009-01-30 23:10:18 +0000549 // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y))
Andrew Trickac6d9be2013-05-25 02:42:55 +0000550 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Chris Lattner29446522007-05-14 22:04:50 +0000551 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000552 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000553 LegalOperations, Depth+1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000554
Chris Lattner29446522007-05-14 22:04:50 +0000555 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000556 case ISD::FSIN:
Andrew Trickac6d9be2013-05-25 02:42:55 +0000557 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000558 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000559 LegalOperations, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000560 case ISD::FP_ROUND:
Andrew Trickac6d9be2013-05-25 02:42:55 +0000561 return DAG.getNode(ISD::FP_ROUND, SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000562 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000563 LegalOperations, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000564 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000565 }
566}
Chris Lattner24664722006-03-01 04:53:38 +0000567
568
Nate Begeman4ebd8052005-09-01 23:24:04 +0000569// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
570// that selects between the values 1 and 0, making it equivalent to a setcc.
Scott Michelfdc40a02009-02-17 22:15:04 +0000571// Also, set the incoming LHS, RHS, and CC references to the appropriate
Nate Begeman646d7e22005-09-02 21:18:40 +0000572// nodes based on the type of node we are checking. This simplifies life a
573// bit for the callers.
Dan Gohman475871a2008-07-27 21:46:04 +0000574static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
575 SDValue &CC) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000576 if (N.getOpcode() == ISD::SETCC) {
577 LHS = N.getOperand(0);
578 RHS = N.getOperand(1);
579 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000580 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000581 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000582 if (N.getOpcode() == ISD::SELECT_CC &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000583 N.getOperand(2).getOpcode() == ISD::Constant &&
584 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000585 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000586 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
587 LHS = N.getOperand(0);
588 RHS = N.getOperand(1);
589 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000590 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000591 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000592 return false;
593}
594
Nate Begeman99801192005-09-07 23:25:52 +0000595// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
596// one use. If this is true, it allows the users to invert the operation for
597// free when it is profitable to do so.
Dan Gohman475871a2008-07-27 21:46:04 +0000598static bool isOneUseSetCC(SDValue N) {
599 SDValue N0, N1, N2;
Gabor Greifba36cb52008-08-28 21:40:38 +0000600 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000601 return true;
602 return false;
603}
604
Andrew Trickac6d9be2013-05-25 02:42:55 +0000605SDValue DAGCombiner::ReassociateOps(unsigned Opc, SDLoc DL,
Bill Wendling35247c32009-01-30 00:45:56 +0000606 SDValue N0, SDValue N1) {
Owen Andersone50ed302009-08-10 22:56:29 +0000607 EVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000608 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
609 if (isa<ConstantSDNode>(N1)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000610 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000611 SDValue OpNode =
612 DAG.FoldConstantArithmetic(Opc, VT,
613 cast<ConstantSDNode>(N0.getOperand(1)),
614 cast<ConstantSDNode>(N1));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000615 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000616 }
617 if (N0.hasOneUse()) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000618 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
Andrew Trickac6d9be2013-05-25 02:42:55 +0000619 SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000620 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000621 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000622 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000623 }
624 }
Bill Wendling35247c32009-01-30 00:45:56 +0000625
Nate Begemancd4d58c2006-02-03 06:46:56 +0000626 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
627 if (isa<ConstantSDNode>(N0)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000628 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000629 SDValue OpNode =
630 DAG.FoldConstantArithmetic(Opc, VT,
631 cast<ConstantSDNode>(N1.getOperand(1)),
632 cast<ConstantSDNode>(N0));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000633 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000634 }
635 if (N1.hasOneUse()) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000636 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
Andrew Trickac6d9be2013-05-25 02:42:55 +0000637 SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000638 N1.getOperand(0), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000639 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000640 return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000641 }
642 }
Bill Wendling35247c32009-01-30 00:45:56 +0000643
Dan Gohman475871a2008-07-27 21:46:04 +0000644 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000645}
646
Dan Gohman475871a2008-07-27 21:46:04 +0000647SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
648 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000649 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
650 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +0000651 DEBUG(dbgs() << "\nReplacing.1 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000652 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000653 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000654 To[0].getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000655 dbgs() << " and " << NumTo-1 << " other values\n";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000656 for (unsigned i = 0, e = NumTo; i != e; ++i)
Jakob Stoklund Olesen9f0d4e62009-12-03 05:15:35 +0000657 assert((!To[i].getNode() ||
658 N->getValueType(i) == To[i].getValueType()) &&
Dan Gohman764fd0c2009-01-21 15:17:51 +0000659 "Cannot combine value to value of different type!"));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000660 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000661 DAG.ReplaceAllUsesWith(N, To);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000662 if (AddTo) {
663 // Push the new nodes and any users onto the worklist
664 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattnerd1980a52009-03-12 06:52:53 +0000665 if (To[i].getNode()) {
666 AddToWorkList(To[i].getNode());
667 AddUsersToWorkList(To[i].getNode());
668 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000669 }
670 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000671
Dan Gohmandbe664a2009-01-19 21:44:21 +0000672 // Finally, if the node is now dead, remove it from the graph. The node
673 // may not be dead if the replacement process recursively simplified to
674 // something else needing this node.
675 if (N->use_empty()) {
676 // Nodes can be reintroduced into the worklist. Make sure we do not
677 // process a node that has been replaced.
678 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000679
Dan Gohmandbe664a2009-01-19 21:44:21 +0000680 // Finally, since the node is now dead, remove it from the graph.
681 DAG.DeleteNode(N);
682 }
Dan Gohman475871a2008-07-27 21:46:04 +0000683 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000684}
685
Evan Chenge5b51ac2010-04-17 06:13:15 +0000686void DAGCombiner::
687CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000688 // Replace all uses. If any nodes become isomorphic to other nodes and
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000689 // are deleted, make sure to remove them from our worklist.
690 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000691 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000692
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000693 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000694 AddToWorkList(TLO.New.getNode());
695 AddUsersToWorkList(TLO.New.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000696
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000697 // Finally, if the node is now dead, remove it from the graph. The node
698 // may not be dead if the replacement process recursively simplified to
699 // something else needing this node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000700 if (TLO.Old.getNode()->use_empty()) {
701 removeFromWorkList(TLO.Old.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000702
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000703 // If the operands of this node are only used by the node, they will now
704 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greifba36cb52008-08-28 21:40:38 +0000705 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
706 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
707 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000708
Gabor Greifba36cb52008-08-28 21:40:38 +0000709 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000710 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000711}
712
713/// SimplifyDemandedBits - Check the specified integer node value to see if
714/// it can be simplified or if things it uses can be simplified by bit
715/// propagation. If so, return true.
716bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000717 TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000718 APInt KnownZero, KnownOne;
719 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
720 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000721
Dan Gohmane5af2d32009-01-29 01:59:02 +0000722 // Revisit the node.
723 AddToWorkList(Op.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000724
Dan Gohmane5af2d32009-01-29 01:59:02 +0000725 // Replace the old value with the new one.
726 ++NodesCombined;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000727 DEBUG(dbgs() << "\nReplacing.2 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000728 TLO.Old.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000729 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000730 TLO.New.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000731 dbgs() << '\n');
Scott Michelfdc40a02009-02-17 22:15:04 +0000732
Dan Gohmane5af2d32009-01-29 01:59:02 +0000733 CommitTargetLoweringOpt(TLO);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000734 return true;
735}
736
Evan Cheng95c57ea2010-04-24 04:43:44 +0000737void DAGCombiner::ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad) {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000738 SDLoc dl(Load);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000739 EVT VT = Load->getValueType(0);
740 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, VT, SDValue(ExtLoad, 0));
Evan Cheng4c26e932010-04-19 19:29:22 +0000741
Evan Cheng95c57ea2010-04-24 04:43:44 +0000742 DEBUG(dbgs() << "\nReplacing.9 ";
743 Load->dump(&DAG);
744 dbgs() << "\nWith: ";
745 Trunc.getNode()->dump(&DAG);
746 dbgs() << '\n');
747 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000748 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 0), Trunc);
749 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), SDValue(ExtLoad, 1));
Evan Cheng95c57ea2010-04-24 04:43:44 +0000750 removeFromWorkList(Load);
751 DAG.DeleteNode(Load);
Evan Chengac7eae52010-04-27 19:48:13 +0000752 AddToWorkList(Trunc.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000753}
754
755SDValue DAGCombiner::PromoteOperand(SDValue Op, EVT PVT, bool &Replace) {
756 Replace = false;
Andrew Trickac6d9be2013-05-25 02:42:55 +0000757 SDLoc dl(Op);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000758 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
Evan Chengac7eae52010-04-27 19:48:13 +0000759 EVT MemVT = LD->getMemoryVT();
760 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000761 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000762 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000763 : LD->getExtensionType();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000764 Replace = true;
Stuart Hastingsa9011292011-02-16 16:23:55 +0000765 return DAG.getExtLoad(ExtType, dl, PVT,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000766 LD->getChain(), LD->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000767 LD->getPointerInfo(),
Evan Chengac7eae52010-04-27 19:48:13 +0000768 MemVT, LD->isVolatile(),
Evan Chenge5b51ac2010-04-17 06:13:15 +0000769 LD->isNonTemporal(), LD->getAlignment());
770 }
771
Evan Cheng4c26e932010-04-19 19:29:22 +0000772 unsigned Opc = Op.getOpcode();
Evan Chengcaf77402010-04-23 19:10:30 +0000773 switch (Opc) {
774 default: break;
775 case ISD::AssertSext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000776 return DAG.getNode(ISD::AssertSext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000777 SExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000778 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000779 case ISD::AssertZext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000780 return DAG.getNode(ISD::AssertZext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000781 ZExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000782 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000783 case ISD::Constant: {
784 unsigned ExtOpc =
Evan Cheng4c26e932010-04-19 19:29:22 +0000785 Op.getValueType().isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Evan Chengcaf77402010-04-23 19:10:30 +0000786 return DAG.getNode(ExtOpc, dl, PVT, Op);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000787 }
Evan Chengcaf77402010-04-23 19:10:30 +0000788 }
789
790 if (!TLI.isOperationLegal(ISD::ANY_EXTEND, PVT))
Evan Chenge5b51ac2010-04-17 06:13:15 +0000791 return SDValue();
Evan Chengcaf77402010-04-23 19:10:30 +0000792 return DAG.getNode(ISD::ANY_EXTEND, dl, PVT, Op);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000793}
794
Evan Cheng95c57ea2010-04-24 04:43:44 +0000795SDValue DAGCombiner::SExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000796 if (!TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, PVT))
797 return SDValue();
798 EVT OldVT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +0000799 SDLoc dl(Op);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000800 bool Replace = false;
801 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
802 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000803 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000804 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000805
806 if (Replace)
807 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
808 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NewOp.getValueType(), NewOp,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000809 DAG.getValueType(OldVT));
810}
811
Evan Cheng95c57ea2010-04-24 04:43:44 +0000812SDValue DAGCombiner::ZExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000813 EVT OldVT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +0000814 SDLoc dl(Op);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000815 bool Replace = false;
816 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
817 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000818 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000819 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000820
821 if (Replace)
822 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
823 return DAG.getZeroExtendInReg(NewOp, dl, OldVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000824}
825
Evan Cheng64b7bf72010-04-16 06:14:10 +0000826/// PromoteIntBinOp - Promote the specified integer binary operation if the
827/// target indicates it is beneficial. e.g. On x86, it's usually better to
828/// promote i16 operations to i32 since i16 instructions are longer.
829SDValue DAGCombiner::PromoteIntBinOp(SDValue Op) {
830 if (!LegalOperations)
831 return SDValue();
832
833 EVT VT = Op.getValueType();
834 if (VT.isVector() || !VT.isInteger())
835 return SDValue();
836
Evan Chenge5b51ac2010-04-17 06:13:15 +0000837 // If operation type is 'undesirable', e.g. i16 on x86, consider
838 // promoting it.
839 unsigned Opc = Op.getOpcode();
840 if (TLI.isTypeDesirableForOp(Opc, VT))
841 return SDValue();
842
Evan Cheng64b7bf72010-04-16 06:14:10 +0000843 EVT PVT = VT;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000844 // Consult target whether it is a good idea to promote this operation and
845 // what's the right type to promote it to.
846 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
Evan Cheng64b7bf72010-04-16 06:14:10 +0000847 assert(PVT != VT && "Don't know what type to promote to!");
848
Evan Cheng95c57ea2010-04-24 04:43:44 +0000849 bool Replace0 = false;
850 SDValue N0 = Op.getOperand(0);
851 SDValue NN0 = PromoteOperand(N0, PVT, Replace0);
852 if (NN0.getNode() == 0)
Evan Cheng07c4e102010-04-22 20:19:46 +0000853 return SDValue();
854
Evan Cheng95c57ea2010-04-24 04:43:44 +0000855 bool Replace1 = false;
856 SDValue N1 = Op.getOperand(1);
Evan Chengaad753b2010-05-10 19:03:57 +0000857 SDValue NN1;
858 if (N0 == N1)
859 NN1 = NN0;
860 else {
861 NN1 = PromoteOperand(N1, PVT, Replace1);
862 if (NN1.getNode() == 0)
863 return SDValue();
864 }
Evan Cheng07c4e102010-04-22 20:19:46 +0000865
Evan Cheng95c57ea2010-04-24 04:43:44 +0000866 AddToWorkList(NN0.getNode());
Evan Chengaad753b2010-05-10 19:03:57 +0000867 if (NN1.getNode())
868 AddToWorkList(NN1.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000869
870 if (Replace0)
871 ReplaceLoadWithPromotedLoad(N0.getNode(), NN0.getNode());
872 if (Replace1)
873 ReplaceLoadWithPromotedLoad(N1.getNode(), NN1.getNode());
Evan Cheng07c4e102010-04-22 20:19:46 +0000874
Evan Chengac7eae52010-04-27 19:48:13 +0000875 DEBUG(dbgs() << "\nPromoting ";
876 Op.getNode()->dump(&DAG));
Andrew Trickac6d9be2013-05-25 02:42:55 +0000877 SDLoc dl(Op);
Evan Cheng07c4e102010-04-22 20:19:46 +0000878 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000879 DAG.getNode(Opc, dl, PVT, NN0, NN1));
Evan Cheng07c4e102010-04-22 20:19:46 +0000880 }
881 return SDValue();
882}
883
884/// PromoteIntShiftOp - Promote the specified integer shift operation if the
885/// target indicates it is beneficial. e.g. On x86, it's usually better to
886/// promote i16 operations to i32 since i16 instructions are longer.
887SDValue DAGCombiner::PromoteIntShiftOp(SDValue Op) {
888 if (!LegalOperations)
889 return SDValue();
890
891 EVT VT = Op.getValueType();
892 if (VT.isVector() || !VT.isInteger())
893 return SDValue();
894
895 // If operation type is 'undesirable', e.g. i16 on x86, consider
896 // promoting it.
897 unsigned Opc = Op.getOpcode();
898 if (TLI.isTypeDesirableForOp(Opc, VT))
899 return SDValue();
900
901 EVT PVT = VT;
902 // Consult target whether it is a good idea to promote this operation and
903 // what's the right type to promote it to.
904 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
905 assert(PVT != VT && "Don't know what type to promote to!");
906
Evan Cheng95c57ea2010-04-24 04:43:44 +0000907 bool Replace = false;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000908 SDValue N0 = Op.getOperand(0);
909 if (Opc == ISD::SRA)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000910 N0 = SExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000911 else if (Opc == ISD::SRL)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000912 N0 = ZExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000913 else
Evan Cheng95c57ea2010-04-24 04:43:44 +0000914 N0 = PromoteOperand(N0, PVT, Replace);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000915 if (N0.getNode() == 0)
916 return SDValue();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000917
Evan Chenge5b51ac2010-04-17 06:13:15 +0000918 AddToWorkList(N0.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000919 if (Replace)
920 ReplaceLoadWithPromotedLoad(Op.getOperand(0).getNode(), N0.getNode());
Evan Cheng64b7bf72010-04-16 06:14:10 +0000921
Evan Chengac7eae52010-04-27 19:48:13 +0000922 DEBUG(dbgs() << "\nPromoting ";
923 Op.getNode()->dump(&DAG));
Andrew Trickac6d9be2013-05-25 02:42:55 +0000924 SDLoc dl(Op);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000925 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng07c4e102010-04-22 20:19:46 +0000926 DAG.getNode(Opc, dl, PVT, N0, Op.getOperand(1)));
Evan Cheng64b7bf72010-04-16 06:14:10 +0000927 }
928 return SDValue();
929}
930
Evan Cheng4c26e932010-04-19 19:29:22 +0000931SDValue DAGCombiner::PromoteExtend(SDValue Op) {
932 if (!LegalOperations)
933 return SDValue();
934
935 EVT VT = Op.getValueType();
936 if (VT.isVector() || !VT.isInteger())
937 return SDValue();
938
939 // If operation type is 'undesirable', e.g. i16 on x86, consider
940 // promoting it.
941 unsigned Opc = Op.getOpcode();
942 if (TLI.isTypeDesirableForOp(Opc, VT))
943 return SDValue();
944
945 EVT PVT = VT;
946 // Consult target whether it is a good idea to promote this operation and
947 // what's the right type to promote it to.
948 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
949 assert(PVT != VT && "Don't know what type to promote to!");
950 // fold (aext (aext x)) -> (aext x)
951 // fold (aext (zext x)) -> (zext x)
952 // fold (aext (sext x)) -> (sext x)
Evan Chengac7eae52010-04-27 19:48:13 +0000953 DEBUG(dbgs() << "\nPromoting ";
954 Op.getNode()->dump(&DAG));
Andrew Trickac6d9be2013-05-25 02:42:55 +0000955 return DAG.getNode(Op.getOpcode(), SDLoc(Op), VT, Op.getOperand(0));
Evan Cheng4c26e932010-04-19 19:29:22 +0000956 }
957 return SDValue();
958}
959
960bool DAGCombiner::PromoteLoad(SDValue Op) {
961 if (!LegalOperations)
962 return false;
963
964 EVT VT = Op.getValueType();
965 if (VT.isVector() || !VT.isInteger())
966 return false;
967
968 // If operation type is 'undesirable', e.g. i16 on x86, consider
969 // promoting it.
970 unsigned Opc = Op.getOpcode();
971 if (TLI.isTypeDesirableForOp(Opc, VT))
972 return false;
973
974 EVT PVT = VT;
975 // Consult target whether it is a good idea to promote this operation and
976 // what's the right type to promote it to.
977 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
978 assert(PVT != VT && "Don't know what type to promote to!");
979
Andrew Trickac6d9be2013-05-25 02:42:55 +0000980 SDLoc dl(Op);
Evan Cheng4c26e932010-04-19 19:29:22 +0000981 SDNode *N = Op.getNode();
982 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chengac7eae52010-04-27 19:48:13 +0000983 EVT MemVT = LD->getMemoryVT();
984 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000985 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000986 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000987 : LD->getExtensionType();
Stuart Hastingsa9011292011-02-16 16:23:55 +0000988 SDValue NewLD = DAG.getExtLoad(ExtType, dl, PVT,
Evan Cheng4c26e932010-04-19 19:29:22 +0000989 LD->getChain(), LD->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000990 LD->getPointerInfo(),
Evan Chengac7eae52010-04-27 19:48:13 +0000991 MemVT, LD->isVolatile(),
Evan Cheng4c26e932010-04-19 19:29:22 +0000992 LD->isNonTemporal(), LD->getAlignment());
993 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, VT, NewLD);
994
Evan Cheng95c57ea2010-04-24 04:43:44 +0000995 DEBUG(dbgs() << "\nPromoting ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000996 N->dump(&DAG);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000997 dbgs() << "\nTo: ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000998 Result.getNode()->dump(&DAG);
999 dbgs() << '\n');
1000 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001001 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
1002 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), NewLD.getValue(1));
Evan Cheng4c26e932010-04-19 19:29:22 +00001003 removeFromWorkList(N);
1004 DAG.DeleteNode(N);
Evan Chengac7eae52010-04-27 19:48:13 +00001005 AddToWorkList(Result.getNode());
Evan Cheng4c26e932010-04-19 19:29:22 +00001006 return true;
1007 }
1008 return false;
1009}
1010
Evan Chenge5b51ac2010-04-17 06:13:15 +00001011
Chris Lattner29446522007-05-14 22:04:50 +00001012//===----------------------------------------------------------------------===//
1013// Main DAG Combiner implementation
1014//===----------------------------------------------------------------------===//
1015
Duncan Sands25cf2272008-11-24 14:53:14 +00001016void DAGCombiner::Run(CombineLevel AtLevel) {
1017 // set the instance variables, so that the various visit routines may use it.
1018 Level = AtLevel;
Eli Friedman50185242011-11-12 00:35:34 +00001019 LegalOperations = Level >= AfterLegalizeVectorOps;
1020 LegalTypes = Level >= AfterLegalizeTypes;
Nate Begeman4ebd8052005-09-01 23:24:04 +00001021
Evan Cheng17a568b2008-08-29 22:21:44 +00001022 // Add all the dag nodes to the worklist.
Evan Cheng17a568b2008-08-29 22:21:44 +00001023 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
1024 E = DAG.allnodes_end(); I != E; ++I)
James Molloy6660c052012-02-16 09:17:04 +00001025 AddToWorkList(I);
Duncan Sands25cf2272008-11-24 14:53:14 +00001026
Evan Cheng17a568b2008-08-29 22:21:44 +00001027 // Create a dummy node (which is not added to allnodes), that adds a reference
1028 // to the root node, preventing it from being deleted, and tracking any
1029 // changes of the root.
1030 HandleSDNode Dummy(DAG.getRoot());
Scott Michelfdc40a02009-02-17 22:15:04 +00001031
Jim Laskey26f7fa72006-10-17 19:33:52 +00001032 // The root of the dag may dangle to deleted nodes until the dag combiner is
1033 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +00001034 DAG.setRoot(SDValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00001035
James Molloy6660c052012-02-16 09:17:04 +00001036 // while the worklist isn't empty, find a node and
Evan Cheng17a568b2008-08-29 22:21:44 +00001037 // try and combine it.
James Molloy6660c052012-02-16 09:17:04 +00001038 while (!WorkListContents.empty()) {
1039 SDNode *N;
1040 // The WorkListOrder holds the SDNodes in order, but it may contain duplicates.
1041 // In order to avoid a linear scan, we use a set (O(log N)) to hold what the
1042 // worklist *should* contain, and check the node we want to visit is should
1043 // actually be visited.
1044 do {
Benjamin Kramerd5f76902012-03-10 00:23:58 +00001045 N = WorkListOrder.pop_back_val();
James Molloy6660c052012-02-16 09:17:04 +00001046 } while (!WorkListContents.erase(N));
Scott Michelfdc40a02009-02-17 22:15:04 +00001047
Evan Cheng17a568b2008-08-29 22:21:44 +00001048 // If N has no uses, it is dead. Make sure to revisit all N's operands once
1049 // N is deleted from the DAG, since they too may now be dead or may have a
1050 // reduced number of uses, allowing other xforms.
1051 if (N->use_empty() && N != &Dummy) {
1052 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1053 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001054
Evan Cheng17a568b2008-08-29 22:21:44 +00001055 DAG.DeleteNode(N);
1056 continue;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001057 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001058
Evan Cheng17a568b2008-08-29 22:21:44 +00001059 SDValue RV = combine(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001060
Evan Cheng17a568b2008-08-29 22:21:44 +00001061 if (RV.getNode() == 0)
1062 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001063
Evan Cheng17a568b2008-08-29 22:21:44 +00001064 ++NodesCombined;
Scott Michelfdc40a02009-02-17 22:15:04 +00001065
Evan Cheng17a568b2008-08-29 22:21:44 +00001066 // If we get back the same node we passed in, rather than a new node or
1067 // zero, we know that the node must have defined multiple values and
Scott Michelfdc40a02009-02-17 22:15:04 +00001068 // CombineTo was used. Since CombineTo takes care of the worklist
Evan Cheng17a568b2008-08-29 22:21:44 +00001069 // mechanics for us, we have no work to do in this case.
1070 if (RV.getNode() == N)
1071 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001072
Evan Cheng17a568b2008-08-29 22:21:44 +00001073 assert(N->getOpcode() != ISD::DELETED_NODE &&
1074 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
1075 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +00001076
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001077 DEBUG(dbgs() << "\nReplacing.3 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001078 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001079 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001080 RV.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001081 dbgs() << '\n');
Eric Christopher7332e6e2011-07-14 01:12:15 +00001082
Devang Patel9728ea22011-05-23 22:04:42 +00001083 // Transfer debug value.
1084 DAG.TransferDbgValues(SDValue(N, 0), RV);
Evan Cheng17a568b2008-08-29 22:21:44 +00001085 WorkListRemover DeadNodes(*this);
1086 if (N->getNumValues() == RV.getNode()->getNumValues())
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001087 DAG.ReplaceAllUsesWith(N, RV.getNode());
Evan Cheng17a568b2008-08-29 22:21:44 +00001088 else {
1089 assert(N->getValueType(0) == RV.getValueType() &&
1090 N->getNumValues() == 1 && "Type mismatch");
1091 SDValue OpV = RV;
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001092 DAG.ReplaceAllUsesWith(N, &OpV);
Evan Cheng17a568b2008-08-29 22:21:44 +00001093 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001094
Evan Cheng17a568b2008-08-29 22:21:44 +00001095 // Push the new node and any users onto the worklist
1096 AddToWorkList(RV.getNode());
1097 AddUsersToWorkList(RV.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001098
Evan Cheng17a568b2008-08-29 22:21:44 +00001099 // Add any uses of the old node to the worklist in case this node is the
1100 // last one that uses them. They may become dead after this node is
1101 // deleted.
1102 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1103 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001104
Dan Gohmandbe664a2009-01-19 21:44:21 +00001105 // Finally, if the node is now dead, remove it from the graph. The node
1106 // may not be dead if the replacement process recursively simplified to
1107 // something else needing this node.
1108 if (N->use_empty()) {
1109 // Nodes can be reintroduced into the worklist. Make sure we do not
1110 // process a node that has been replaced.
1111 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001112
Dan Gohmandbe664a2009-01-19 21:44:21 +00001113 // Finally, since the node is now dead, remove it from the graph.
1114 DAG.DeleteNode(N);
1115 }
Evan Cheng17a568b2008-08-29 22:21:44 +00001116 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001117
Chris Lattner95038592005-10-05 06:35:28 +00001118 // If the root changed (e.g. it was a dead load, update the root).
1119 DAG.setRoot(Dummy.getValue());
Hal Finkel31490ba2012-04-16 03:33:22 +00001120 DAG.RemoveDeadNodes();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001121}
1122
Dan Gohman475871a2008-07-27 21:46:04 +00001123SDValue DAGCombiner::visit(SDNode *N) {
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001124 switch (N->getOpcode()) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001125 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +00001126 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001127 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001128 case ISD::ADD: return visitADD(N);
1129 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +00001130 case ISD::ADDC: return visitADDC(N);
Craig Toppercc274522012-01-07 09:06:39 +00001131 case ISD::SUBC: return visitSUBC(N);
Chris Lattner91153682007-03-04 20:03:15 +00001132 case ISD::ADDE: return visitADDE(N);
Craig Toppercc274522012-01-07 09:06:39 +00001133 case ISD::SUBE: return visitSUBE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001134 case ISD::MUL: return visitMUL(N);
1135 case ISD::SDIV: return visitSDIV(N);
1136 case ISD::UDIV: return visitUDIV(N);
1137 case ISD::SREM: return visitSREM(N);
1138 case ISD::UREM: return visitUREM(N);
1139 case ISD::MULHU: return visitMULHU(N);
1140 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001141 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
1142 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00001143 case ISD::SMULO: return visitSMULO(N);
1144 case ISD::UMULO: return visitUMULO(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001145 case ISD::SDIVREM: return visitSDIVREM(N);
1146 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001147 case ISD::AND: return visitAND(N);
1148 case ISD::OR: return visitOR(N);
1149 case ISD::XOR: return visitXOR(N);
1150 case ISD::SHL: return visitSHL(N);
1151 case ISD::SRA: return visitSRA(N);
1152 case ISD::SRL: return visitSRL(N);
1153 case ISD::CTLZ: return visitCTLZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001154 case ISD::CTLZ_ZERO_UNDEF: return visitCTLZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001155 case ISD::CTTZ: return visitCTTZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001156 case ISD::CTTZ_ZERO_UNDEF: return visitCTTZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001157 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +00001158 case ISD::SELECT: return visitSELECT(N);
Benjamin Kramer6242fda2013-04-26 09:19:19 +00001159 case ISD::VSELECT: return visitVSELECT(N);
Nate Begeman452d7be2005-09-16 00:54:12 +00001160 case ISD::SELECT_CC: return visitSELECT_CC(N);
1161 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001162 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
1163 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001164 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001165 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
1166 case ISD::TRUNCATE: return visitTRUNCATE(N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001167 case ISD::BITCAST: return visitBITCAST(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00001168 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001169 case ISD::FADD: return visitFADD(N);
1170 case ISD::FSUB: return visitFSUB(N);
1171 case ISD::FMUL: return visitFMUL(N);
Owen Anderson062c0a52012-05-02 22:17:40 +00001172 case ISD::FMA: return visitFMA(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001173 case ISD::FDIV: return visitFDIV(N);
1174 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +00001175 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001176 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
1177 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
1178 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
1179 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
1180 case ISD::FP_ROUND: return visitFP_ROUND(N);
1181 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
1182 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
1183 case ISD::FNEG: return visitFNEG(N);
1184 case ISD::FABS: return visitFABS(N);
Owen Anderson7c626d32012-08-13 23:32:49 +00001185 case ISD::FFLOOR: return visitFFLOOR(N);
1186 case ISD::FCEIL: return visitFCEIL(N);
1187 case ISD::FTRUNC: return visitFTRUNC(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001188 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001189 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +00001190 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00001191 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +00001192 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +00001193 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +00001194 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
1195 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00001196 case ISD::EXTRACT_SUBVECTOR: return visitEXTRACT_SUBVECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +00001197 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001198 }
Dan Gohman475871a2008-07-27 21:46:04 +00001199 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001200}
1201
Dan Gohman475871a2008-07-27 21:46:04 +00001202SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00001203 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001204
1205 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +00001206 if (RV.getNode() == 0) {
Dan Gohman389079b2007-10-08 17:57:15 +00001207 assert(N->getOpcode() != ISD::DELETED_NODE &&
1208 "Node was deleted but visit returned NULL!");
1209
1210 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
1211 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
1212
1213 // Expose the DAG combiner to the target combiner impls.
Scott Michelfdc40a02009-02-17 22:15:04 +00001214 TargetLowering::DAGCombinerInfo
Nadav Rotem444b4bf2012-12-27 06:47:41 +00001215 DagCombineInfo(DAG, Level, false, this);
Dan Gohman389079b2007-10-08 17:57:15 +00001216
1217 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
1218 }
1219 }
1220
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001221 // If nothing happened still, try promoting the operation.
1222 if (RV.getNode() == 0) {
1223 switch (N->getOpcode()) {
1224 default: break;
1225 case ISD::ADD:
1226 case ISD::SUB:
1227 case ISD::MUL:
1228 case ISD::AND:
1229 case ISD::OR:
1230 case ISD::XOR:
1231 RV = PromoteIntBinOp(SDValue(N, 0));
1232 break;
1233 case ISD::SHL:
1234 case ISD::SRA:
1235 case ISD::SRL:
1236 RV = PromoteIntShiftOp(SDValue(N, 0));
1237 break;
1238 case ISD::SIGN_EXTEND:
1239 case ISD::ZERO_EXTEND:
1240 case ISD::ANY_EXTEND:
1241 RV = PromoteExtend(SDValue(N, 0));
1242 break;
1243 case ISD::LOAD:
1244 if (PromoteLoad(SDValue(N, 0)))
1245 RV = SDValue(N, 0);
1246 break;
1247 }
1248 }
1249
Scott Michelfdc40a02009-02-17 22:15:04 +00001250 // If N is a commutative binary node, try commuting it to enable more
Evan Cheng08b11732008-03-22 01:55:50 +00001251 // sdisel CSE.
Scott Michelfdc40a02009-02-17 22:15:04 +00001252 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +00001253 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
1254 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00001255 SDValue N0 = N->getOperand(0);
1256 SDValue N1 = N->getOperand(1);
Bill Wendling5c71acf2009-01-30 01:13:16 +00001257
Evan Cheng08b11732008-03-22 01:55:50 +00001258 // Constant operands are canonicalized to RHS.
1259 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001260 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +00001261 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
1262 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +00001263 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +00001264 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +00001265 }
1266 }
1267
Dan Gohman389079b2007-10-08 17:57:15 +00001268 return RV;
Scott Michelfdc40a02009-02-17 22:15:04 +00001269}
Dan Gohman389079b2007-10-08 17:57:15 +00001270
Chris Lattner6270f682006-10-08 22:57:01 +00001271/// getInputChainForNode - Given a node, return its input chain if it has one,
1272/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +00001273static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001274 if (unsigned NumOps = N->getNumOperands()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001275 if (N->getOperand(0).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001276 return N->getOperand(0);
Stephen Linb4940152013-07-09 00:44:49 +00001277 if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001278 return N->getOperand(NumOps-1);
1279 for (unsigned i = 1; i < NumOps-1; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +00001280 if (N->getOperand(i).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001281 return N->getOperand(i);
1282 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001283 return SDValue();
Chris Lattner6270f682006-10-08 22:57:01 +00001284}
1285
Dan Gohman475871a2008-07-27 21:46:04 +00001286SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001287 // If N has two operands, where one has an input chain equal to the other,
1288 // the 'other' chain is redundant.
1289 if (N->getNumOperands() == 2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001290 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +00001291 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001292 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +00001293 return N->getOperand(1);
1294 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001295
Chris Lattnerc76d4412007-05-16 06:37:59 +00001296 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +00001297 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001298 SmallPtrSet<SDNode*, 16> SeenOps;
Chris Lattnerc76d4412007-05-16 06:37:59 +00001299 bool Changed = false; // If we should replace this token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001300
Jim Laskey6ff23e52006-10-04 16:53:27 +00001301 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00001302 TFs.push_back(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001303
Jim Laskey71382342006-10-07 23:37:56 +00001304 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +00001305 // encountered.
1306 for (unsigned i = 0; i < TFs.size(); ++i) {
1307 SDNode *TF = TFs[i];
Scott Michelfdc40a02009-02-17 22:15:04 +00001308
Jim Laskey6ff23e52006-10-04 16:53:27 +00001309 // Check each of the operands.
1310 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001311 SDValue Op = TF->getOperand(i);
Scott Michelfdc40a02009-02-17 22:15:04 +00001312
Jim Laskey6ff23e52006-10-04 16:53:27 +00001313 switch (Op.getOpcode()) {
1314 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +00001315 // Entry tokens don't need to be added to the list. They are
1316 // rededundant.
1317 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001318 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001319
Jim Laskey6ff23e52006-10-04 16:53:27 +00001320 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +00001321 if (Op.hasOneUse() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001322 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001323 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +00001324 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001325 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +00001326 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001327 Changed = true;
1328 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001329 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001330 // Fall thru
Scott Michelfdc40a02009-02-17 22:15:04 +00001331
Jim Laskey6ff23e52006-10-04 16:53:27 +00001332 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +00001333 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +00001334 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +00001335 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +00001336 else
1337 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001338 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001339 }
1340 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001341 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001342
Dan Gohman475871a2008-07-27 21:46:04 +00001343 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001344
1345 // If we've change things around then replace token factor.
1346 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +00001347 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001348 // The entry token is the only possible outcome.
1349 Result = DAG.getEntryNode();
1350 } else {
1351 // New and improved token factor.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001352 Result = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson825b72b2009-08-11 20:47:22 +00001353 MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +00001354 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001355
Jim Laskey274062c2006-10-13 23:32:28 +00001356 // Don't add users to work list.
1357 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +00001358 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001359
Jim Laskey6ff23e52006-10-04 16:53:27 +00001360 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001361}
1362
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001363/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +00001364SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001365 WorkListRemover DeadNodes(*this);
Dan Gohman00edf392009-08-10 23:43:19 +00001366 // Replacing results may cause a different MERGE_VALUES to suddenly
1367 // be CSE'd with N, and carry its uses with it. Iterate until no
1368 // uses remain, to ensure that the node can be safely deleted.
Pete Cooper3affd9e2012-06-20 19:35:43 +00001369 // First add the users of this node to the work list so that they
1370 // can be tried again once they have new operands.
1371 AddUsersToWorkList(N);
Dan Gohman00edf392009-08-10 23:43:19 +00001372 do {
1373 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001374 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i));
Dan Gohman00edf392009-08-10 23:43:19 +00001375 } while (!N->use_empty());
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001376 removeFromWorkList(N);
1377 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001378 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001379}
1380
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001381static
Andrew Trickac6d9be2013-05-25 02:42:55 +00001382SDValue combineShlAddConstant(SDLoc DL, SDValue N0, SDValue N1,
Bill Wendlingd69c3142009-01-30 02:23:43 +00001383 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001384 EVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001385 SDValue N00 = N0.getOperand(0);
1386 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001387 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Bill Wendlingd69c3142009-01-30 02:23:43 +00001388
Gabor Greifba36cb52008-08-28 21:40:38 +00001389 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001390 isa<ConstantSDNode>(N00.getOperand(1))) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001391 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Andrew Trickac6d9be2013-05-25 02:42:55 +00001392 N0 = DAG.getNode(ISD::ADD, SDLoc(N0), VT,
1393 DAG.getNode(ISD::SHL, SDLoc(N00), VT,
Bill Wendlingd69c3142009-01-30 02:23:43 +00001394 N00.getOperand(0), N01),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001395 DAG.getNode(ISD::SHL, SDLoc(N01), VT,
Bill Wendlingd69c3142009-01-30 02:23:43 +00001396 N00.getOperand(1), N01));
1397 return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001398 }
Bill Wendlingd69c3142009-01-30 02:23:43 +00001399
Dan Gohman475871a2008-07-27 21:46:04 +00001400 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001401}
1402
Dan Gohman475871a2008-07-27 21:46:04 +00001403SDValue DAGCombiner::visitADD(SDNode *N) {
1404 SDValue N0 = N->getOperand(0);
1405 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001406 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1407 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001408 EVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00001409
1410 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001411 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001412 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001413 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper48b509c2012-12-10 08:12:29 +00001414
1415 // fold (add x, 0) -> x, vector edition
1416 if (ISD::isBuildVectorAllZeros(N1.getNode()))
1417 return N0;
1418 if (ISD::isBuildVectorAllZeros(N0.getNode()))
1419 return N1;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001420 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001421
Dan Gohman613e0d82007-07-03 14:03:57 +00001422 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001423 if (N0.getOpcode() == ISD::UNDEF)
1424 return N0;
1425 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001426 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001427 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001428 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001429 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001430 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001431 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001432 return DAG.getNode(ISD::ADD, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001433 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001434 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001435 return N0;
Dan Gohman6520e202008-10-18 02:06:02 +00001436 // fold (add Sym, c) -> Sym+c
1437 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001438 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman6520e202008-10-18 02:06:02 +00001439 GA->getOpcode() == ISD::GlobalAddress)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001440 return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001441 GA->getOffset() +
1442 (uint64_t)N1C->getSExtValue());
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001443 // fold ((c1-A)+c2) -> (c1+c2)-A
1444 if (N1C && N0.getOpcode() == ISD::SUB)
1445 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001446 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Dan Gohman002e5d02008-03-13 22:13:53 +00001447 DAG.getConstant(N1C->getAPIntValue()+
1448 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001449 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +00001450 // reassociate add
Andrew Trickac6d9be2013-05-25 02:42:55 +00001451 SDValue RADD = ReassociateOps(ISD::ADD, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001452 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001453 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001454 // fold ((0-A) + B) -> B-A
1455 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1456 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001457 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001458 // fold (A + (0-B)) -> A-B
1459 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1460 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001461 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001462 // fold (A+(B-A)) -> B
1463 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001464 return N1.getOperand(0);
Dale Johannesen56eca912008-11-27 00:43:21 +00001465 // fold ((B-A)+A) -> B
1466 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1467 return N0.getOperand(0);
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001468 // fold (A+(B-(A+C))) to (B-C)
1469 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001470 N0 == N1.getOperand(1).getOperand(0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001471 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001472 N1.getOperand(1).getOperand(1));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001473 // fold (A+(B-(C+A))) to (B-C)
1474 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001475 N0 == N1.getOperand(1).getOperand(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001476 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001477 N1.getOperand(1).getOperand(0));
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001478 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesen34d79852008-12-02 18:40:40 +00001479 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1480 N1.getOperand(0).getOpcode() == ISD::SUB &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001481 N0 == N1.getOperand(0).getOperand(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001482 return DAG.getNode(N1.getOpcode(), SDLoc(N), VT,
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001483 N1.getOperand(0).getOperand(0), N1.getOperand(1));
Dale Johannesen34d79852008-12-02 18:40:40 +00001484
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001485 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1486 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1487 SDValue N00 = N0.getOperand(0);
1488 SDValue N01 = N0.getOperand(1);
1489 SDValue N10 = N1.getOperand(0);
1490 SDValue N11 = N1.getOperand(1);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001491
1492 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001493 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
1494 DAG.getNode(ISD::ADD, SDLoc(N0), VT, N00, N10),
1495 DAG.getNode(ISD::ADD, SDLoc(N1), VT, N01, N11));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001496 }
Chris Lattner947c2892006-03-13 06:51:27 +00001497
Dan Gohman475871a2008-07-27 21:46:04 +00001498 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1499 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001500
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001501 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001502 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001503 APInt LHSZero, LHSOne;
1504 APInt RHSZero, RHSOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001505 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001506
Dan Gohman948d8ea2008-02-20 16:33:30 +00001507 if (LHSZero.getBoolValue()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001508 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001509
Chris Lattner947c2892006-03-13 06:51:27 +00001510 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1511 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001512 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001513 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N1);
Chris Lattner947c2892006-03-13 06:51:27 +00001514 }
1515 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001516
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001517 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001518 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001519 SDValue Result = combineShlAddConstant(SDLoc(N), N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001520 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001521 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001522 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001523 SDValue Result = combineShlAddConstant(SDLoc(N), N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001524 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001525 }
1526
Dan Gohmancd9e1552010-01-19 23:30:49 +00001527 // fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n))
1528 if (N1.getOpcode() == ISD::SHL &&
1529 N1.getOperand(0).getOpcode() == ISD::SUB)
1530 if (ConstantSDNode *C =
1531 dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(0)))
1532 if (C->getAPIntValue() == 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001533 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N0,
1534 DAG.getNode(ISD::SHL, SDLoc(N), VT,
Dan Gohmancd9e1552010-01-19 23:30:49 +00001535 N1.getOperand(0).getOperand(1),
1536 N1.getOperand(1)));
1537 if (N0.getOpcode() == ISD::SHL &&
1538 N0.getOperand(0).getOpcode() == ISD::SUB)
1539 if (ConstantSDNode *C =
1540 dyn_cast<ConstantSDNode>(N0.getOperand(0).getOperand(0)))
1541 if (C->getAPIntValue() == 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001542 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1,
1543 DAG.getNode(ISD::SHL, SDLoc(N), VT,
Dan Gohmancd9e1552010-01-19 23:30:49 +00001544 N0.getOperand(0).getOperand(1),
1545 N0.getOperand(1)));
1546
Owen Andersonbc146b02010-09-21 20:42:50 +00001547 if (N1.getOpcode() == ISD::AND) {
1548 SDValue AndOp0 = N1.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001549 ConstantSDNode *AndOp1 = dyn_cast<ConstantSDNode>(N1->getOperand(1));
Owen Andersonbc146b02010-09-21 20:42:50 +00001550 unsigned NumSignBits = DAG.ComputeNumSignBits(AndOp0);
1551 unsigned DestBits = VT.getScalarType().getSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001552
Owen Andersonbc146b02010-09-21 20:42:50 +00001553 // (add z, (and (sbbl x, x), 1)) -> (sub z, (sbbl x, x))
1554 // and similar xforms where the inner op is either ~0 or 0.
1555 if (NumSignBits == DestBits && AndOp1 && AndOp1->isOne()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001556 SDLoc DL(N);
Owen Andersonbc146b02010-09-21 20:42:50 +00001557 return DAG.getNode(ISD::SUB, DL, VT, N->getOperand(0), AndOp0);
1558 }
1559 }
1560
Benjamin Kramerf50125e2010-12-22 23:17:45 +00001561 // add (sext i1), X -> sub X, (zext i1)
1562 if (N0.getOpcode() == ISD::SIGN_EXTEND &&
1563 N0.getOperand(0).getValueType() == MVT::i1 &&
1564 !TLI.isOperationLegal(ISD::SIGN_EXTEND, MVT::i1)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001565 SDLoc DL(N);
Benjamin Kramerf50125e2010-12-22 23:17:45 +00001566 SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0));
1567 return DAG.getNode(ISD::SUB, DL, VT, N1, ZExt);
1568 }
1569
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001570 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001571}
1572
Dan Gohman475871a2008-07-27 21:46:04 +00001573SDValue DAGCombiner::visitADDC(SDNode *N) {
1574 SDValue N0 = N->getOperand(0);
1575 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001576 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1577 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001578 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001579
Chris Lattner91153682007-03-04 20:03:15 +00001580 // If the flag result is dead, turn this into an ADD.
Craig Topper704e1a02012-01-07 18:31:09 +00001581 if (!N->hasAnyUseOfValue(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001582 return CombineTo(N, DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001583 DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001584 SDLoc(N), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001585
Chris Lattner91153682007-03-04 20:03:15 +00001586 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001587 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001588 return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N1, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001589
Chris Lattnerb6541762007-03-04 20:40:38 +00001590 // fold (addc x, 0) -> x + no carry out
1591 if (N1C && N1C->isNullValue())
Dale Johannesen874ae252009-06-02 03:12:52 +00001592 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001593 SDLoc(N), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001594
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001595 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001596 APInt LHSZero, LHSOne;
1597 APInt RHSZero, RHSOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001598 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendling14036c02009-01-30 02:38:00 +00001599
Dan Gohman948d8ea2008-02-20 16:33:30 +00001600 if (LHSZero.getBoolValue()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001601 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001602
Chris Lattnerb6541762007-03-04 20:40:38 +00001603 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1604 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001605 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001606 return CombineTo(N, DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001607 DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001608 SDLoc(N), MVT::Glue));
Chris Lattnerb6541762007-03-04 20:40:38 +00001609 }
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
Dan Gohman475871a2008-07-27 21:46:04 +00001614SDValue DAGCombiner::visitADDE(SDNode *N) {
1615 SDValue N0 = N->getOperand(0);
1616 SDValue N1 = N->getOperand(1);
1617 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001618 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1619 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001620
Chris Lattner91153682007-03-04 20:03:15 +00001621 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001622 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001623 return DAG.getNode(ISD::ADDE, SDLoc(N), N->getVTList(),
Bill Wendling14036c02009-01-30 02:38:00 +00001624 N1, N0, CarryIn);
Scott Michelfdc40a02009-02-17 22:15:04 +00001625
Chris Lattnerb6541762007-03-04 20:40:38 +00001626 // fold (adde x, y, false) -> (addc x, y)
Dale Johannesen874ae252009-06-02 03:12:52 +00001627 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001628 return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001629
Dan Gohman475871a2008-07-27 21:46:04 +00001630 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001631}
1632
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001633// Since it may not be valid to emit a fold to zero for vector initializers
1634// check if we can before folding.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001635static SDValue tryFoldToZero(SDLoc DL, const TargetLowering &TLI, EVT VT,
Hal Finkelbd6f1f62013-07-09 17:02:45 +00001636 SelectionDAG &DAG,
1637 bool LegalOperations, bool LegalTypes) {
Stephen Linb4940152013-07-09 00:44:49 +00001638 if (!VT.isVector())
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001639 return DAG.getConstant(0, VT);
Dan Gohman71dc7c92011-05-17 22:20:36 +00001640 if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001641 // Produce a vector of zeros.
Hal Finkelbd6f1f62013-07-09 17:02:45 +00001642 EVT ElemTy = VT.getVectorElementType();
1643 if (LegalTypes && TLI.getTypeAction(*DAG.getContext(), ElemTy) ==
1644 TargetLowering::TypePromoteInteger)
1645 ElemTy = TLI.getTypeToTransformTo(*DAG.getContext(), ElemTy);
1646 assert((!LegalTypes || TLI.isTypeLegal(ElemTy)) &&
1647 "Type for zero vector elements is not legal");
1648 SDValue El = DAG.getConstant(0, ElemTy);
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001649 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
1650 return DAG.getNode(ISD::BUILD_VECTOR, DL, VT,
1651 &Ops[0], Ops.size());
1652 }
1653 return SDValue();
1654}
1655
Dan Gohman475871a2008-07-27 21:46:04 +00001656SDValue DAGCombiner::visitSUB(SDNode *N) {
1657 SDValue N0 = N->getOperand(0);
1658 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001659 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1660 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Eric Christopher7332e6e2011-07-14 01:12:15 +00001661 ConstantSDNode *N1C1 = N1.getOpcode() != ISD::ADD ? 0 :
1662 dyn_cast<ConstantSDNode>(N1.getOperand(1).getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001663 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001664
Dan Gohman7f321562007-06-25 16:23:39 +00001665 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001666 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001667 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001668 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper48b509c2012-12-10 08:12:29 +00001669
1670 // fold (sub x, 0) -> x, vector edition
1671 if (ISD::isBuildVectorAllZeros(N1.getNode()))
1672 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001673 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001674
Chris Lattner854077d2005-10-17 01:07:11 +00001675 // fold (sub x, x) -> 0
Eric Christopher169e1552011-02-16 01:10:03 +00001676 // FIXME: Refactor this and xor and other similar operations together.
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001677 if (N0 == N1)
Hal Finkelbd6f1f62013-07-09 17:02:45 +00001678 return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations, LegalTypes);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001679 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001680 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001681 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattner05b57432005-10-11 06:07:15 +00001682 // fold (sub x, c) -> (add x, -c)
1683 if (N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001684 return DAG.getNode(ISD::ADD, SDLoc(N), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001685 DAG.getConstant(-N1C->getAPIntValue(), VT));
Evan Cheng1ad0e8b2010-01-18 21:38:44 +00001686 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1)
1687 if (N0C && N0C->isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001688 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0);
Benjamin Kramer2c94b422011-01-29 12:34:05 +00001689 // fold A-(A-B) -> B
1690 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(0))
1691 return N1.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001692 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001693 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001694 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001695 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001696 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Scott Michelfdc40a02009-02-17 22:15:04 +00001697 return N0.getOperand(0);
Eric Christopher7332e6e2011-07-14 01:12:15 +00001698 // fold C2-(A+C1) -> (C2-C1)-A
1699 if (N1.getOpcode() == ISD::ADD && N0C && N1C1) {
Nadav Rotem6dfabb62012-09-20 08:53:31 +00001700 SDValue NewC = DAG.getConstant(N0C->getAPIntValue() - N1C1->getAPIntValue(),
1701 VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001702 return DAG.getNode(ISD::SUB, SDLoc(N), VT, NewC,
Bill Wendling96cb1122012-07-19 00:04:14 +00001703 N1.getOperand(0));
Eric Christopher7332e6e2011-07-14 01:12:15 +00001704 }
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001705 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001706 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001707 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1708 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001709 N0.getOperand(1).getOperand(0) == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001710 return DAG.getNode(N0.getOperand(1).getOpcode(), SDLoc(N), VT,
Bill Wendlingb0702e02009-01-30 02:42:10 +00001711 N0.getOperand(0), N0.getOperand(1).getOperand(1));
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001712 // fold ((A+(C+B))-B) -> A+C
1713 if (N0.getOpcode() == ISD::ADD &&
1714 N0.getOperand(1).getOpcode() == ISD::ADD &&
1715 N0.getOperand(1).getOperand(1) == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001716 return DAG.getNode(ISD::ADD, SDLoc(N), VT,
Bill Wendlingb0702e02009-01-30 02:42:10 +00001717 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dale Johannesen58e39b02008-12-23 01:59:54 +00001718 // fold ((A-(B-C))-C) -> A-B
1719 if (N0.getOpcode() == ISD::SUB &&
1720 N0.getOperand(1).getOpcode() == ISD::SUB &&
1721 N0.getOperand(1).getOperand(1) == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001722 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendlingb0702e02009-01-30 02:42:10 +00001723 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Bill Wendlingb0702e02009-01-30 02:42:10 +00001724
Dan Gohman613e0d82007-07-03 14:03:57 +00001725 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001726 if (N0.getOpcode() == ISD::UNDEF)
1727 return N0;
1728 if (N1.getOpcode() == ISD::UNDEF)
1729 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001730
Dan Gohman6520e202008-10-18 02:06:02 +00001731 // If the relocation model supports it, consider symbol offsets.
1732 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001733 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman6520e202008-10-18 02:06:02 +00001734 // fold (sub Sym, c) -> Sym-c
1735 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001736 return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001737 GA->getOffset() -
1738 (uint64_t)N1C->getSExtValue());
1739 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1740 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1741 if (GA->getGlobal() == GB->getGlobal())
1742 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1743 VT);
1744 }
1745
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001746 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001747}
1748
Craig Toppercc274522012-01-07 09:06:39 +00001749SDValue DAGCombiner::visitSUBC(SDNode *N) {
1750 SDValue N0 = N->getOperand(0);
1751 SDValue N1 = N->getOperand(1);
1752 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1753 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1754 EVT VT = N0.getValueType();
1755
1756 // If the flag result is dead, turn this into an SUB.
Craig Topper704e1a02012-01-07 18:31:09 +00001757 if (!N->hasAnyUseOfValue(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001758 return CombineTo(N, DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, N1),
1759 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001760 MVT::Glue));
1761
1762 // fold (subc x, x) -> 0 + no borrow
1763 if (N0 == N1)
1764 return CombineTo(N, DAG.getConstant(0, VT),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001765 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001766 MVT::Glue));
1767
1768 // fold (subc x, 0) -> x + no borrow
1769 if (N1C && N1C->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001770 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001771 MVT::Glue));
1772
1773 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) + no borrow
1774 if (N0C && N0C->isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001775 return CombineTo(N, DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0),
1776 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001777 MVT::Glue));
1778
1779 return SDValue();
1780}
1781
1782SDValue DAGCombiner::visitSUBE(SDNode *N) {
1783 SDValue N0 = N->getOperand(0);
1784 SDValue N1 = N->getOperand(1);
1785 SDValue CarryIn = N->getOperand(2);
1786
1787 // fold (sube x, y, false) -> (subc x, y)
1788 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001789 return DAG.getNode(ISD::SUBC, SDLoc(N), N->getVTList(), N0, N1);
Craig Toppercc274522012-01-07 09:06:39 +00001790
1791 return SDValue();
1792}
1793
Elena Demikhovskyd8026702013-06-26 12:15:53 +00001794/// isConstantSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
1795/// all the same constant or undefined.
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001796static bool isConstantSplatVector(SDNode *N, APInt& SplatValue) {
1797 BuildVectorSDNode *C = dyn_cast<BuildVectorSDNode>(N);
1798 if (!C)
1799 return false;
1800
1801 APInt SplatUndef;
1802 unsigned SplatBitSize;
1803 bool HasAnyUndefs;
1804 EVT EltVT = N->getValueType(0).getVectorElementType();
1805 return (C->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
1806 HasAnyUndefs) &&
1807 EltVT.getSizeInBits() >= SplatBitSize);
1808}
1809
Dan Gohman475871a2008-07-27 21:46:04 +00001810SDValue DAGCombiner::visitMUL(SDNode *N) {
1811 SDValue N0 = N->getOperand(0);
1812 SDValue N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00001813 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001814
Dan Gohman613e0d82007-07-03 14:03:57 +00001815 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001816 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001817 return DAG.getConstant(0, VT);
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001818
1819 bool N0IsConst = false;
1820 bool N1IsConst = false;
1821 APInt ConstValue0, ConstValue1;
1822 // fold vector ops
1823 if (VT.isVector()) {
1824 SDValue FoldedVOp = SimplifyVBinOp(N);
1825 if (FoldedVOp.getNode()) return FoldedVOp;
1826
1827 N0IsConst = isConstantSplatVector(N0.getNode(), ConstValue0);
1828 N1IsConst = isConstantSplatVector(N1.getNode(), ConstValue1);
1829 } else {
1830 N0IsConst = dyn_cast<ConstantSDNode>(N0) != 0;
1831 ConstValue0 = N0IsConst? (dyn_cast<ConstantSDNode>(N0))->getAPIntValue() : APInt();
1832 N1IsConst = dyn_cast<ConstantSDNode>(N1) != 0;
1833 ConstValue1 = N1IsConst? (dyn_cast<ConstantSDNode>(N1))->getAPIntValue() : APInt();
1834 }
1835
Nate Begeman1d4d4142005-09-01 00:19:25 +00001836 // fold (mul c1, c2) -> c1*c2
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001837 if (N0IsConst && N1IsConst)
1838 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0.getNode(), N1.getNode());
1839
Nate Begeman99801192005-09-07 23:25:52 +00001840 // canonicalize constant to RHS
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001841 if (N0IsConst && !N1IsConst)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001842 return DAG.getNode(ISD::MUL, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001843 // fold (mul x, 0) -> 0
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001844 if (N1IsConst && ConstValue1 == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001845 return N1;
Benjamin Kramer530d09a2013-09-19 13:28:20 +00001846 // We require a splat of the entire scalar bit width for non-contiguous
1847 // bit patterns.
1848 bool IsFullSplat =
1849 ConstValue1.getBitWidth() == VT.getScalarType().getSizeInBits();
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001850 // fold (mul x, 1) -> x
Benjamin Kramer530d09a2013-09-19 13:28:20 +00001851 if (N1IsConst && ConstValue1 == 1 && IsFullSplat)
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001852 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001853 // fold (mul x, -1) -> 0-x
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001854 if (N1IsConst && ConstValue1.isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001855 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001856 DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001857 // fold (mul x, (1 << c)) -> x << c
Benjamin Kramer530d09a2013-09-19 13:28:20 +00001858 if (N1IsConst && ConstValue1.isPowerOf2() && IsFullSplat)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001859 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001860 DAG.getConstant(ConstValue1.logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00001861 getShiftAmountTy(N0.getValueType())));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001862 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Benjamin Kramer530d09a2013-09-19 13:28:20 +00001863 if (N1IsConst && (-ConstValue1).isPowerOf2() && IsFullSplat) {
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001864 unsigned Log2Val = (-ConstValue1).logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00001865 // FIXME: If the input is something that is easily negated (e.g. a
Chris Lattner3e6099b2005-10-30 06:41:49 +00001866 // single-use add), we should put the negate there.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001867 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001868 DAG.getConstant(0, VT),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001869 DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
Owen Anderson95771af2011-02-25 21:41:48 +00001870 DAG.getConstant(Log2Val,
1871 getShiftAmountTy(N0.getValueType()))));
Chris Lattner66b8bc32009-03-09 20:22:18 +00001872 }
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001873
1874 APInt Val;
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001875 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
Stephen Lin155615d2013-07-08 00:37:03 +00001876 if (N1IsConst && N0.getOpcode() == ISD::SHL &&
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001877 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1878 isa<ConstantSDNode>(N0.getOperand(1)))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001879 SDValue C3 = DAG.getNode(ISD::SHL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001880 N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001881 AddToWorkList(C3.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00001882 return DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001883 N0.getOperand(0), C3);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001884 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001885
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001886 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1887 // use.
1888 {
Dan Gohman475871a2008-07-27 21:46:04 +00001889 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001890 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
Stephen Lin155615d2013-07-08 00:37:03 +00001891 if (N0.getOpcode() == ISD::SHL &&
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001892 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1893 isa<ConstantSDNode>(N0.getOperand(1))) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001894 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001895 Sh = N0; Y = N1;
Scott Michelfdc40a02009-02-17 22:15:04 +00001896 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001897 isa<ConstantSDNode>(N1.getOperand(1)) &&
1898 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001899 Sh = N1; Y = N0;
1900 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001901
Gabor Greifba36cb52008-08-28 21:40:38 +00001902 if (Sh.getNode()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001903 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001904 Sh.getOperand(0), Y);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001905 return DAG.getNode(ISD::SHL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001906 Mul, Sh.getOperand(1));
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001907 }
1908 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001909
Chris Lattnera1deca32006-03-04 23:33:26 +00001910 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001911 if (N1IsConst && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
1912 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1913 isa<ConstantSDNode>(N0.getOperand(1))))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001914 return DAG.getNode(ISD::ADD, SDLoc(N), VT,
1915 DAG.getNode(ISD::MUL, SDLoc(N0), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001916 N0.getOperand(0), N1),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001917 DAG.getNode(ISD::MUL, SDLoc(N1), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001918 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001919
Nate Begemancd4d58c2006-02-03 06:46:56 +00001920 // reassociate mul
Andrew Trickac6d9be2013-05-25 02:42:55 +00001921 SDValue RMUL = ReassociateOps(ISD::MUL, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001922 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001923 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001924
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001925 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001926}
1927
Dan Gohman475871a2008-07-27 21:46:04 +00001928SDValue DAGCombiner::visitSDIV(SDNode *N) {
1929 SDValue N0 = N->getOperand(0);
1930 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001931 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1932 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001933 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001934
Dan Gohman7f321562007-06-25 16:23:39 +00001935 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001936 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001937 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001938 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001939 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001940
Nate Begeman1d4d4142005-09-01 00:19:25 +00001941 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001942 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001943 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001944 // fold (sdiv X, 1) -> X
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001945 if (N1C && N1C->getAPIntValue() == 1LL)
Nate Begeman405e3ec2005-10-21 00:02:42 +00001946 return N0;
1947 // fold (sdiv X, -1) -> 0-X
1948 if (N1C && N1C->isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001949 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling944d34b2009-01-30 02:52:17 +00001950 DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001951 // If we know the sign bits of both operands are zero, strength reduce to a
1952 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001953 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001954 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001955 return DAG.getNode(ISD::UDIV, SDLoc(N), N1.getValueType(),
Bill Wendling944d34b2009-01-30 02:52:17 +00001956 N0, N1);
Chris Lattnerf32aac32008-01-27 23:32:17 +00001957 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001958 // fold (sdiv X, pow2) -> simple ops after legalize
Eli Friedman1c663fe2011-12-07 03:55:52 +00001959 if (N1C && !N1C->isNullValue() &&
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001960 (N1C->getAPIntValue().isPowerOf2() ||
1961 (-N1C->getAPIntValue()).isPowerOf2())) {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001962 // If dividing by powers of two is cheap, then don't perform the following
1963 // fold.
1964 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001965 return SDValue();
Bill Wendling944d34b2009-01-30 02:52:17 +00001966
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001967 unsigned lg2 = N1C->getAPIntValue().countTrailingZeros();
Bill Wendling944d34b2009-01-30 02:52:17 +00001968
Chris Lattner8f4880b2006-02-16 08:02:36 +00001969 // Splat the sign bit into the register
Andrew Trickac6d9be2013-05-25 02:42:55 +00001970 SDValue SGN = DAG.getNode(ISD::SRA, SDLoc(N), VT, N0,
Bill Wendling944d34b2009-01-30 02:52:17 +00001971 DAG.getConstant(VT.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00001972 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00001973 AddToWorkList(SGN.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001974
Chris Lattner8f4880b2006-02-16 08:02:36 +00001975 // Add (N0 < 0) ? abs2 - 1 : 0;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001976 SDValue SRL = DAG.getNode(ISD::SRL, SDLoc(N), VT, SGN,
Bill Wendling944d34b2009-01-30 02:52:17 +00001977 DAG.getConstant(VT.getSizeInBits() - lg2,
Owen Anderson95771af2011-02-25 21:41:48 +00001978 getShiftAmountTy(SGN.getValueType())));
Andrew Trickac6d9be2013-05-25 02:42:55 +00001979 SDValue ADD = DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001980 AddToWorkList(SRL.getNode());
1981 AddToWorkList(ADD.getNode()); // Divide by pow2
Andrew Trickac6d9be2013-05-25 02:42:55 +00001982 SDValue SRA = DAG.getNode(ISD::SRA, SDLoc(N), VT, ADD,
Owen Anderson95771af2011-02-25 21:41:48 +00001983 DAG.getConstant(lg2, getShiftAmountTy(ADD.getValueType())));
Bill Wendling944d34b2009-01-30 02:52:17 +00001984
Nate Begeman405e3ec2005-10-21 00:02:42 +00001985 // If we're dividing by a positive value, we're done. Otherwise, we must
1986 // negate the result.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001987 if (N1C->getAPIntValue().isNonNegative())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001988 return SRA;
Bill Wendling944d34b2009-01-30 02:52:17 +00001989
Gabor Greifba36cb52008-08-28 21:40:38 +00001990 AddToWorkList(SRA.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00001991 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling944d34b2009-01-30 02:52:17 +00001992 DAG.getConstant(0, VT), SRA);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001993 }
Bill Wendling944d34b2009-01-30 02:52:17 +00001994
Nate Begeman69575232005-10-20 02:15:44 +00001995 // if integer divide is expensive and we satisfy the requirements, emit an
1996 // alternate sequence.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001997 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001998 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001999 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00002000 }
Dan Gohman7f321562007-06-25 16:23:39 +00002001
Dan Gohman613e0d82007-07-03 14:03:57 +00002002 // undef / X -> 0
2003 if (N0.getOpcode() == ISD::UNDEF)
2004 return DAG.getConstant(0, VT);
2005 // X / undef -> undef
2006 if (N1.getOpcode() == ISD::UNDEF)
2007 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002008
Dan Gohman475871a2008-07-27 21:46:04 +00002009 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002010}
2011
Dan Gohman475871a2008-07-27 21:46:04 +00002012SDValue DAGCombiner::visitUDIV(SDNode *N) {
2013 SDValue N0 = N->getOperand(0);
2014 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002015 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
2016 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00002017 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002018
Dan Gohman7f321562007-06-25 16:23:39 +00002019 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002020 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002021 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002022 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002023 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002024
Nate Begeman1d4d4142005-09-01 00:19:25 +00002025 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002026 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002027 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002028 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00002029 if (N1C && N1C->getAPIntValue().isPowerOf2())
Andrew Trickac6d9be2013-05-25 02:42:55 +00002030 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00002031 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00002032 getShiftAmountTy(N0.getValueType())));
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002033 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00002034 if (N1.getOpcode() == ISD::SHL) {
2035 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002036 if (SHC->getAPIntValue().isPowerOf2()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002037 EVT ADDVT = N1.getOperand(1).getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002038 SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N), ADDVT,
Bill Wendling07d85142009-01-30 02:55:25 +00002039 N1.getOperand(1),
2040 DAG.getConstant(SHC->getAPIntValue()
2041 .logBase2(),
2042 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002043 AddToWorkList(Add.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002044 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00002045 }
2046 }
2047 }
Nate Begeman69575232005-10-20 02:15:44 +00002048 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00002049 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002050 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002051 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00002052 }
Dan Gohman7f321562007-06-25 16:23:39 +00002053
Dan Gohman613e0d82007-07-03 14:03:57 +00002054 // undef / X -> 0
2055 if (N0.getOpcode() == ISD::UNDEF)
2056 return DAG.getConstant(0, VT);
2057 // X / undef -> undef
2058 if (N1.getOpcode() == ISD::UNDEF)
2059 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002060
Dan Gohman475871a2008-07-27 21:46:04 +00002061 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002062}
2063
Dan Gohman475871a2008-07-27 21:46:04 +00002064SDValue DAGCombiner::visitSREM(SDNode *N) {
2065 SDValue N0 = N->getOperand(0);
2066 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002067 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2068 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002069 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002070
Nate Begeman1d4d4142005-09-01 00:19:25 +00002071 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002072 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002073 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00002074 // If we know the sign bits of both operands are zero, strength reduce to a
2075 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00002076 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002077 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00002078 return DAG.getNode(ISD::UREM, SDLoc(N), VT, N0, N1);
Chris Lattneree339f42008-01-27 23:21:58 +00002079 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002080
Dan Gohman77003042007-11-26 23:46:11 +00002081 // If X/C can be simplified by the division-by-constant logic, lower
2082 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00002083 if (N1C && !N1C->isNullValue()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002084 SDValue Div = DAG.getNode(ISD::SDIV, SDLoc(N), VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002085 AddToWorkList(Div.getNode());
2086 SDValue OptimizedDiv = combine(Div.getNode());
2087 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002088 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002089 OptimizedDiv, N1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002090 SDValue Sub = DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00002091 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00002092 return Sub;
2093 }
Chris Lattner26d29902006-10-12 20:58:32 +00002094 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002095
Dan Gohman613e0d82007-07-03 14:03:57 +00002096 // undef % X -> 0
2097 if (N0.getOpcode() == ISD::UNDEF)
2098 return DAG.getConstant(0, VT);
2099 // X % undef -> undef
2100 if (N1.getOpcode() == ISD::UNDEF)
2101 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002102
Dan Gohman475871a2008-07-27 21:46:04 +00002103 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002104}
2105
Dan Gohman475871a2008-07-27 21:46:04 +00002106SDValue DAGCombiner::visitUREM(SDNode *N) {
2107 SDValue N0 = N->getOperand(0);
2108 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002109 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2110 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002111 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002112
Nate Begeman1d4d4142005-09-01 00:19:25 +00002113 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002114 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002115 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00002116 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002117 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
Andrew Trickac6d9be2013-05-25 02:42:55 +00002118 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00002119 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00002120 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
2121 if (N1.getOpcode() == ISD::SHL) {
2122 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002123 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002124 SDValue Add =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002125 DAG.getNode(ISD::ADD, SDLoc(N), VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002126 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00002127 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002128 AddToWorkList(Add.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002129 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, Add);
Nate Begemanc031e332006-02-05 07:36:48 +00002130 }
2131 }
2132 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002133
Dan Gohman77003042007-11-26 23:46:11 +00002134 // If X/C can be simplified by the division-by-constant logic, lower
2135 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00002136 if (N1C && !N1C->isNullValue()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002137 SDValue Div = DAG.getNode(ISD::UDIV, SDLoc(N), VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00002138 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00002139 SDValue OptimizedDiv = combine(Div.getNode());
2140 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002141 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002142 OptimizedDiv, N1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002143 SDValue Sub = DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00002144 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00002145 return Sub;
2146 }
Chris Lattner26d29902006-10-12 20:58:32 +00002147 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002148
Dan Gohman613e0d82007-07-03 14:03:57 +00002149 // undef % X -> 0
2150 if (N0.getOpcode() == ISD::UNDEF)
2151 return DAG.getConstant(0, VT);
2152 // X % undef -> undef
2153 if (N1.getOpcode() == ISD::UNDEF)
2154 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002155
Dan Gohman475871a2008-07-27 21:46:04 +00002156 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002157}
2158
Dan Gohman475871a2008-07-27 21:46:04 +00002159SDValue DAGCombiner::visitMULHS(SDNode *N) {
2160 SDValue N0 = N->getOperand(0);
2161 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002162 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002163 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002164 SDLoc DL(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00002165
Nate Begeman1d4d4142005-09-01 00:19:25 +00002166 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002167 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002168 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002169 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002170 if (N1C && N1C->getAPIntValue() == 1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002171 return DAG.getNode(ISD::SRA, SDLoc(N), N0.getValueType(), N0,
Bill Wendling326411d2009-01-30 03:00:18 +00002172 DAG.getConstant(N0.getValueType().getSizeInBits() - 1,
Owen Anderson95771af2011-02-25 21:41:48 +00002173 getShiftAmountTy(N0.getValueType())));
Dan Gohman613e0d82007-07-03 14:03:57 +00002174 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002175 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002176 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002177
Chris Lattnerde1c3602010-12-13 08:39:01 +00002178 // If the type twice as wide is legal, transform the mulhs to a wider multiply
2179 // plus a shift.
2180 if (VT.isSimple() && !VT.isVector()) {
2181 MVT Simple = VT.getSimpleVT();
2182 unsigned SimpleSize = Simple.getSizeInBits();
2183 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2184 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2185 N0 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N0);
2186 N1 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N1);
2187 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
Chris Lattner1a0fbe22010-12-15 05:51:39 +00002188 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002189 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002190 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2191 }
2192 }
Owen Anderson95771af2011-02-25 21:41:48 +00002193
Dan Gohman475871a2008-07-27 21:46:04 +00002194 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002195}
2196
Dan Gohman475871a2008-07-27 21:46:04 +00002197SDValue DAGCombiner::visitMULHU(SDNode *N) {
2198 SDValue N0 = N->getOperand(0);
2199 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002200 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002201 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002202 SDLoc DL(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00002203
Nate Begeman1d4d4142005-09-01 00:19:25 +00002204 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002205 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002206 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002207 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00002208 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002209 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00002210 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002211 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002212 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002213
Chris Lattnerde1c3602010-12-13 08:39:01 +00002214 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2215 // plus a shift.
2216 if (VT.isSimple() && !VT.isVector()) {
2217 MVT Simple = VT.getSimpleVT();
2218 unsigned SimpleSize = Simple.getSizeInBits();
2219 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2220 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2221 N0 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N0);
2222 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N1);
2223 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
2224 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002225 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002226 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2227 }
2228 }
Owen Anderson95771af2011-02-25 21:41:48 +00002229
Dan Gohman475871a2008-07-27 21:46:04 +00002230 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002231}
2232
Dan Gohman389079b2007-10-08 17:57:15 +00002233/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
2234/// compute two values. LoOp and HiOp give the opcodes for the two computations
2235/// that are being performed. Return true if a simplification was made.
2236///
Scott Michelfdc40a02009-02-17 22:15:04 +00002237SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Dan Gohman475871a2008-07-27 21:46:04 +00002238 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00002239 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00002240 bool HiExists = N->hasAnyUseOfValue(1);
2241 if (!HiExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002242 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002243 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002244 SDValue Res = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0),
Bill Wendling826d1142009-01-30 03:08:40 +00002245 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002246 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002247 }
2248
2249 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00002250 bool LoExists = N->hasAnyUseOfValue(0);
2251 if (!LoExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002252 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002253 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002254 SDValue Res = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1),
Bill Wendling826d1142009-01-30 03:08:40 +00002255 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002256 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002257 }
2258
Evan Cheng44711942007-11-08 09:25:29 +00002259 // If both halves are used, return as it is.
2260 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00002261 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00002262
2263 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00002264 if (LoExists) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002265 SDValue Lo = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0),
Bill Wendling826d1142009-01-30 03:08:40 +00002266 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002267 AddToWorkList(Lo.getNode());
2268 SDValue LoOpt = combine(Lo.getNode());
2269 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002270 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002271 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002272 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00002273 }
2274
Evan Cheng44711942007-11-08 09:25:29 +00002275 if (HiExists) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002276 SDValue Hi = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1),
Duncan Sands25cf2272008-11-24 14:53:14 +00002277 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002278 AddToWorkList(Hi.getNode());
2279 SDValue HiOpt = combine(Hi.getNode());
2280 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002281 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002282 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002283 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00002284 }
Bill Wendling826d1142009-01-30 03:08:40 +00002285
Dan Gohman475871a2008-07-27 21:46:04 +00002286 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002287}
2288
Dan Gohman475871a2008-07-27 21:46:04 +00002289SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
2290 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00002291 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002292
Chris Lattner33e77d32010-12-15 06:04:19 +00002293 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002294 SDLoc DL(N);
Chris Lattner33e77d32010-12-15 06:04:19 +00002295
2296 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2297 // plus a shift.
2298 if (VT.isSimple() && !VT.isVector()) {
2299 MVT Simple = VT.getSimpleVT();
2300 unsigned SimpleSize = Simple.getSizeInBits();
2301 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2302 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2303 SDValue Lo = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(0));
2304 SDValue Hi = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(1));
2305 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2306 // Compute the high part as N1.
2307 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002308 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002309 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2310 // Compute the low part as N0.
2311 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2312 return CombineTo(N, Lo, Hi);
2313 }
2314 }
Owen Anderson95771af2011-02-25 21:41:48 +00002315
Dan Gohman475871a2008-07-27 21:46:04 +00002316 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002317}
2318
Dan Gohman475871a2008-07-27 21:46:04 +00002319SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
2320 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00002321 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002322
Chris Lattner33e77d32010-12-15 06:04:19 +00002323 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002324 SDLoc DL(N);
Owen Anderson95771af2011-02-25 21:41:48 +00002325
Chris Lattner33e77d32010-12-15 06:04:19 +00002326 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2327 // plus a shift.
2328 if (VT.isSimple() && !VT.isVector()) {
2329 MVT Simple = VT.getSimpleVT();
2330 unsigned SimpleSize = Simple.getSizeInBits();
2331 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2332 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2333 SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(0));
2334 SDValue Hi = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(1));
2335 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2336 // Compute the high part as N1.
2337 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002338 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002339 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2340 // Compute the low part as N0.
2341 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2342 return CombineTo(N, Lo, Hi);
2343 }
2344 }
Owen Anderson95771af2011-02-25 21:41:48 +00002345
Dan Gohman475871a2008-07-27 21:46:04 +00002346 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002347}
2348
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002349SDValue DAGCombiner::visitSMULO(SDNode *N) {
2350 // (smulo x, 2) -> (saddo x, x)
2351 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2352 if (C2->getAPIntValue() == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002353 return DAG.getNode(ISD::SADDO, SDLoc(N), N->getVTList(),
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002354 N->getOperand(0), N->getOperand(0));
2355
2356 return SDValue();
2357}
2358
2359SDValue DAGCombiner::visitUMULO(SDNode *N) {
2360 // (umulo x, 2) -> (uaddo x, x)
2361 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2362 if (C2->getAPIntValue() == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002363 return DAG.getNode(ISD::UADDO, SDLoc(N), N->getVTList(),
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002364 N->getOperand(0), N->getOperand(0));
2365
2366 return SDValue();
2367}
2368
Dan Gohman475871a2008-07-27 21:46:04 +00002369SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
2370 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002371 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002372
Dan Gohman475871a2008-07-27 21:46:04 +00002373 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002374}
2375
Dan Gohman475871a2008-07-27 21:46:04 +00002376SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
2377 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002378 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002379
Dan Gohman475871a2008-07-27 21:46:04 +00002380 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002381}
2382
Chris Lattner35e5c142006-05-05 05:51:50 +00002383/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
2384/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00002385SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
2386 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00002387 EVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00002388 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
Scott Michelfdc40a02009-02-17 22:15:04 +00002389
Dan Gohmanff00a552010-01-14 03:08:49 +00002390 // Bail early if none of these transforms apply.
2391 if (N0.getNode()->getNumOperands() == 0) return SDValue();
2392
Chris Lattner540121f2006-05-05 06:31:05 +00002393 // For each of OP in AND/OR/XOR:
2394 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
2395 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
2396 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002397 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y)) (if trunc isn't free)
Nate Begeman93e0ed32009-12-03 07:11:29 +00002398 //
2399 // do not sink logical op inside of a vector extend, since it may combine
2400 // into a vsetcc.
Evan Chengd40d03e2010-01-06 19:38:29 +00002401 EVT Op0VT = N0.getOperand(0).getValueType();
2402 if ((N0.getOpcode() == ISD::ZERO_EXTEND ||
Dan Gohman97121ba2009-04-08 00:15:30 +00002403 N0.getOpcode() == ISD::SIGN_EXTEND ||
Evan Chenge5b51ac2010-04-17 06:13:15 +00002404 // Avoid infinite looping with PromoteIntBinOp.
2405 (N0.getOpcode() == ISD::ANY_EXTEND &&
2406 (!LegalTypes || TLI.isTypeDesirableForOp(N->getOpcode(), Op0VT))) ||
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002407 (N0.getOpcode() == ISD::TRUNCATE &&
2408 (!TLI.isZExtFree(VT, Op0VT) ||
2409 !TLI.isTruncateFree(Op0VT, VT)) &&
2410 TLI.isTypeLegal(Op0VT))) &&
Nate Begeman93e0ed32009-12-03 07:11:29 +00002411 !VT.isVector() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00002412 Op0VT == N1.getOperand(0).getValueType() &&
2413 (!LegalOperations || TLI.isOperationLegal(N->getOpcode(), Op0VT))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002414 SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0),
Bill Wendlingb74c8672009-01-30 19:25:47 +00002415 N0.getOperand(0).getValueType(),
2416 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002417 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002418 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00002419 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002420
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002421 // For each of OP in SHL/SRL/SRA/AND...
2422 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
2423 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
2424 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00002425 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002426 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00002427 N0.getOperand(1) == N1.getOperand(1)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002428 SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0),
Bill Wendlingb74c8672009-01-30 19:25:47 +00002429 N0.getOperand(0).getValueType(),
2430 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002431 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002432 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Bill Wendlingb74c8672009-01-30 19:25:47 +00002433 ORNode, N0.getOperand(1));
Chris Lattner35e5c142006-05-05 05:51:50 +00002434 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002435
Nadav Rotem4ac90812012-04-01 19:31:22 +00002436 // Simplify xor/and/or (bitcast(A), bitcast(B)) -> bitcast(op (A,B))
2437 // Only perform this optimization after type legalization and before
2438 // LegalizeVectorOprs. LegalizeVectorOprs promotes vector operations by
2439 // adding bitcasts. For example (xor v4i32) is promoted to (v2i64), and
2440 // we don't want to undo this promotion.
2441 // We also handle SCALAR_TO_VECTOR because xor/or/and operations are cheaper
2442 // on scalars.
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002443 if ((N0.getOpcode() == ISD::BITCAST ||
2444 N0.getOpcode() == ISD::SCALAR_TO_VECTOR) &&
2445 Level == AfterLegalizeTypes) {
Nadav Rotem4ac90812012-04-01 19:31:22 +00002446 SDValue In0 = N0.getOperand(0);
2447 SDValue In1 = N1.getOperand(0);
2448 EVT In0Ty = In0.getValueType();
2449 EVT In1Ty = In1.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002450 SDLoc DL(N);
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002451 // If both incoming values are integers, and the original types are the
2452 // same.
Nadav Rotem4ac90812012-04-01 19:31:22 +00002453 if (In0Ty.isInteger() && In1Ty.isInteger() && In0Ty == In1Ty) {
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002454 SDValue Op = DAG.getNode(N->getOpcode(), DL, In0Ty, In0, In1);
2455 SDValue BC = DAG.getNode(N0.getOpcode(), DL, VT, Op);
Nadav Rotem4ac90812012-04-01 19:31:22 +00002456 AddToWorkList(Op.getNode());
2457 return BC;
2458 }
2459 }
2460
2461 // Xor/and/or are indifferent to the swizzle operation (shuffle of one value).
2462 // Simplify xor/and/or (shuff(A), shuff(B)) -> shuff(op (A,B))
2463 // If both shuffles use the same mask, and both shuffle within a single
2464 // vector, then it is worthwhile to move the swizzle after the operation.
2465 // The type-legalizer generates this pattern when loading illegal
2466 // vector types from memory. In many cases this allows additional shuffle
2467 // optimizations.
Craig Topperf9204232012-04-09 07:19:09 +00002468 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
2469 N0.getOperand(1).getOpcode() == ISD::UNDEF &&
2470 N1.getOperand(1).getOpcode() == ISD::UNDEF) {
Nadav Rotem4ac90812012-04-01 19:31:22 +00002471 ShuffleVectorSDNode *SVN0 = cast<ShuffleVectorSDNode>(N0);
2472 ShuffleVectorSDNode *SVN1 = cast<ShuffleVectorSDNode>(N1);
Craig Topperf9204232012-04-09 07:19:09 +00002473
2474 assert(N0.getOperand(0).getValueType() == N1.getOperand(1).getValueType() &&
2475 "Inputs to shuffles are not the same type");
Nadav Rotem4ac90812012-04-01 19:31:22 +00002476
2477 unsigned NumElts = VT.getVectorNumElements();
Nadav Rotem4ac90812012-04-01 19:31:22 +00002478
2479 // Check that both shuffles use the same mask. The masks are known to be of
2480 // the same length because the result vector type is the same.
2481 bool SameMask = true;
2482 for (unsigned i = 0; i != NumElts; ++i) {
2483 int Idx0 = SVN0->getMaskElt(i);
2484 int Idx1 = SVN1->getMaskElt(i);
2485 if (Idx0 != Idx1) {
2486 SameMask = false;
2487 break;
2488 }
2489 }
2490
Craig Topperf9204232012-04-09 07:19:09 +00002491 if (SameMask) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002492 SDValue Op = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Craig Topperf9204232012-04-09 07:19:09 +00002493 N0.getOperand(0), N1.getOperand(0));
Nadav Rotem4ac90812012-04-01 19:31:22 +00002494 AddToWorkList(Op.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002495 return DAG.getVectorShuffle(VT, SDLoc(N), Op,
Craig Topperf9204232012-04-09 07:19:09 +00002496 DAG.getUNDEF(VT), &SVN0->getMask()[0]);
Nadav Rotem4ac90812012-04-01 19:31:22 +00002497 }
2498 }
Craig Topperf9204232012-04-09 07:19:09 +00002499
Dan Gohman475871a2008-07-27 21:46:04 +00002500 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00002501}
2502
Dan Gohman475871a2008-07-27 21:46:04 +00002503SDValue DAGCombiner::visitAND(SDNode *N) {
2504 SDValue N0 = N->getOperand(0);
2505 SDValue N1 = N->getOperand(1);
2506 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00002507 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2508 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002509 EVT VT = N1.getValueType();
Dan Gohman6900a392010-03-04 00:23:16 +00002510 unsigned BitWidth = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002511
Dan Gohman7f321562007-06-25 16:23:39 +00002512 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002513 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002514 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002515 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00002516
2517 // fold (and x, 0) -> 0, vector edition
2518 if (ISD::isBuildVectorAllZeros(N0.getNode()))
2519 return N0;
2520 if (ISD::isBuildVectorAllZeros(N1.getNode()))
2521 return N1;
2522
2523 // fold (and x, -1) -> x, vector edition
2524 if (ISD::isBuildVectorAllOnes(N0.getNode()))
2525 return N1;
2526 if (ISD::isBuildVectorAllOnes(N1.getNode()))
2527 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002528 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002529
Dan Gohman613e0d82007-07-03 14:03:57 +00002530 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002531 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002532 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002533 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002534 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002535 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002536 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002537 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002538 return DAG.getNode(ISD::AND, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002539 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002540 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002541 return N0;
2542 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002543 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002544 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002545 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00002546 // reassociate and
Andrew Trickac6d9be2013-05-25 02:42:55 +00002547 SDValue RAND = ReassociateOps(ISD::AND, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002548 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002549 return RAND;
Bill Wendling7d9f2b92010-03-03 00:35:56 +00002550 // fold (and (or x, C), D) -> D if (C & D) == D
Nate Begeman5dc7e862005-11-02 18:42:59 +00002551 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002552 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002553 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002554 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00002555 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
2556 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00002557 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002558 APInt Mask = ~N1C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00002559 Mask = Mask.trunc(N0Op0.getValueSizeInBits());
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002560 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002561 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N),
Bill Wendling2627a882009-01-30 20:43:18 +00002562 N0.getValueType(), N0Op0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002563
Chris Lattner1ec05d12006-03-01 21:47:21 +00002564 // Replace uses of the AND with uses of the Zero extend node.
2565 CombineTo(N, Zext);
Scott Michelfdc40a02009-02-17 22:15:04 +00002566
Chris Lattner3603cd62006-02-02 07:17:31 +00002567 // We actually want to replace all uses of the any_extend with the
2568 // zero_extend, to avoid duplicating things. This will later cause this
2569 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00002570 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00002571 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00002572 }
2573 }
Stephen Lin155615d2013-07-08 00:37:03 +00002574 // similarly fold (and (X (load ([non_ext|any_ext|zero_ext] V))), c) ->
James Molloy6259dcd2012-02-20 12:02:38 +00002575 // (X (load ([non_ext|zero_ext] V))) if 'and' only clears top bits which must
2576 // already be zero by virtue of the width of the base type of the load.
2577 //
2578 // the 'X' node here can either be nothing or an extract_vector_elt to catch
2579 // more cases.
2580 if ((N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
2581 N0.getOperand(0).getOpcode() == ISD::LOAD) ||
2582 N0.getOpcode() == ISD::LOAD) {
2583 LoadSDNode *Load = cast<LoadSDNode>( (N0.getOpcode() == ISD::LOAD) ?
2584 N0 : N0.getOperand(0) );
2585
2586 // Get the constant (if applicable) the zero'th operand is being ANDed with.
2587 // This can be a pure constant or a vector splat, in which case we treat the
2588 // vector as a scalar and use the splat value.
2589 APInt Constant = APInt::getNullValue(1);
2590 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2591 Constant = C->getAPIntValue();
2592 } else if (BuildVectorSDNode *Vector = dyn_cast<BuildVectorSDNode>(N1)) {
2593 APInt SplatValue, SplatUndef;
2594 unsigned SplatBitSize;
2595 bool HasAnyUndefs;
2596 bool IsSplat = Vector->isConstantSplat(SplatValue, SplatUndef,
2597 SplatBitSize, HasAnyUndefs);
2598 if (IsSplat) {
2599 // Undef bits can contribute to a possible optimisation if set, so
2600 // set them.
2601 SplatValue |= SplatUndef;
2602
2603 // The splat value may be something like "0x00FFFFFF", which means 0 for
2604 // the first vector value and FF for the rest, repeating. We need a mask
2605 // that will apply equally to all members of the vector, so AND all the
2606 // lanes of the constant together.
2607 EVT VT = Vector->getValueType(0);
2608 unsigned BitWidth = VT.getVectorElementType().getSizeInBits();
Silviu Baranga3d5e1612012-09-05 08:57:21 +00002609
2610 // If the splat value has been compressed to a bitlength lower
2611 // than the size of the vector lane, we need to re-expand it to
2612 // the lane size.
2613 if (BitWidth > SplatBitSize)
2614 for (SplatValue = SplatValue.zextOrTrunc(BitWidth);
2615 SplatBitSize < BitWidth;
2616 SplatBitSize = SplatBitSize * 2)
2617 SplatValue |= SplatValue.shl(SplatBitSize);
2618
James Molloy6259dcd2012-02-20 12:02:38 +00002619 Constant = APInt::getAllOnesValue(BitWidth);
Silviu Baranga3d5e1612012-09-05 08:57:21 +00002620 for (unsigned i = 0, n = SplatBitSize/BitWidth; i < n; ++i)
James Molloy6259dcd2012-02-20 12:02:38 +00002621 Constant &= SplatValue.lshr(i*BitWidth).zextOrTrunc(BitWidth);
2622 }
2623 }
2624
2625 // If we want to change an EXTLOAD to a ZEXTLOAD, ensure a ZEXTLOAD is
2626 // actually legal and isn't going to get expanded, else this is a false
2627 // optimisation.
2628 bool CanZextLoadProfitably = TLI.isLoadExtLegal(ISD::ZEXTLOAD,
2629 Load->getMemoryVT());
2630
2631 // Resize the constant to the same size as the original memory access before
2632 // extension. If it is still the AllOnesValue then this AND is completely
2633 // unneeded.
2634 Constant =
2635 Constant.zextOrTrunc(Load->getMemoryVT().getScalarType().getSizeInBits());
2636
2637 bool B;
2638 switch (Load->getExtensionType()) {
2639 default: B = false; break;
2640 case ISD::EXTLOAD: B = CanZextLoadProfitably; break;
2641 case ISD::ZEXTLOAD:
2642 case ISD::NON_EXTLOAD: B = true; break;
2643 }
2644
2645 if (B && Constant.isAllOnesValue()) {
2646 // If the load type was an EXTLOAD, convert to ZEXTLOAD in order to
2647 // preserve semantics once we get rid of the AND.
2648 SDValue NewLoad(Load, 0);
2649 if (Load->getExtensionType() == ISD::EXTLOAD) {
2650 NewLoad = DAG.getLoad(Load->getAddressingMode(), ISD::ZEXTLOAD,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002651 Load->getValueType(0), SDLoc(Load),
James Molloy6259dcd2012-02-20 12:02:38 +00002652 Load->getChain(), Load->getBasePtr(),
2653 Load->getOffset(), Load->getMemoryVT(),
2654 Load->getMemOperand());
2655 // Replace uses of the EXTLOAD with the new ZEXTLOAD.
Hal Finkeld65e4632012-06-20 15:42:48 +00002656 if (Load->getNumValues() == 3) {
2657 // PRE/POST_INC loads have 3 values.
2658 SDValue To[] = { NewLoad.getValue(0), NewLoad.getValue(1),
2659 NewLoad.getValue(2) };
2660 CombineTo(Load, To, 3, true);
2661 } else {
2662 CombineTo(Load, NewLoad.getValue(0), NewLoad.getValue(1));
2663 }
James Molloy6259dcd2012-02-20 12:02:38 +00002664 }
2665
2666 // Fold the AND away, taking care not to fold to the old load node if we
2667 // replaced it.
2668 CombineTo(N, (N0.getNode() == Load) ? NewLoad : N0);
2669
2670 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2671 }
2672 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002673 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
2674 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
2675 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
2676 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00002677
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002678 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002679 LL.getValueType().isInteger()) {
Bill Wendling2627a882009-01-30 20:43:18 +00002680 // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00002681 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002682 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0),
Bill Wendling2627a882009-01-30 20:43:18 +00002683 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002684 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002685 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002686 }
Bill Wendling2627a882009-01-30 20:43:18 +00002687 // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002688 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002689 SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(N0),
Bill Wendling2627a882009-01-30 20:43:18 +00002690 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002691 AddToWorkList(ANDNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002692 return DAG.getSetCC(SDLoc(N), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002693 }
Bill Wendling2627a882009-01-30 20:43:18 +00002694 // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002695 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002696 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0),
Bill Wendling2627a882009-01-30 20:43:18 +00002697 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002698 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002699 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002700 }
2701 }
Jim Grosbach51a02802013-08-13 21:30:58 +00002702 // Simplify (and (setne X, 0), (setne X, -1)) -> (setuge (add X, 1), 2)
2703 if (LL == RL && isa<ConstantSDNode>(LR) && isa<ConstantSDNode>(RR) &&
2704 Op0 == Op1 && LL.getValueType().isInteger() &&
2705 Op0 == ISD::SETNE && ((cast<ConstantSDNode>(LR)->isNullValue() &&
2706 cast<ConstantSDNode>(RR)->isAllOnesValue()) ||
2707 (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
2708 cast<ConstantSDNode>(RR)->isNullValue()))) {
2709 SDValue ADDNode = DAG.getNode(ISD::ADD, SDLoc(N0), LL.getValueType(),
2710 LL, DAG.getConstant(1, LL.getValueType()));
2711 AddToWorkList(ADDNode.getNode());
2712 return DAG.getSetCC(SDLoc(N), VT, ADDNode,
2713 DAG.getConstant(2, LL.getValueType()), ISD::SETUGE);
2714 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002715 // canonicalize equivalent to ll == rl
2716 if (LL == RR && LR == RL) {
2717 Op1 = ISD::getSetCCSwappedOperands(Op1);
2718 std::swap(RL, RR);
2719 }
2720 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002721 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002722 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00002723 if (Result != ISD::SETCC_INVALID &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00002724 (!LegalOperations ||
Owen Anderson39125d92013-02-14 09:07:33 +00002725 (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) &&
2726 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault225ed702013-05-18 00:21:46 +00002727 getSetCCResultType(N0.getSimpleValueType())))))
Andrew Trickac6d9be2013-05-25 02:42:55 +00002728 return DAG.getSetCC(SDLoc(N), N0.getValueType(),
Bill Wendling2627a882009-01-30 20:43:18 +00002729 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002730 }
2731 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002732
Bill Wendling2627a882009-01-30 20:43:18 +00002733 // Simplify: (and (op x...), (op y...)) -> (op (and x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00002734 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002735 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002736 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002737 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002738
Nate Begemande996292006-02-03 22:24:05 +00002739 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
2740 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002741 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002742 SimplifyDemandedBits(SDValue(N, 0)))
2743 return SDValue(N, 0);
Evan Chengd40d03e2010-01-06 19:38:29 +00002744
Nate Begemanded49632005-10-13 03:11:28 +00002745 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002746 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00002747 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002748 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002749 // If we zero all the possible extended bits, then we can turn this into
2750 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002751 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002752 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002753 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002754 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002755 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002756 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT,
Bill Wendling2627a882009-01-30 20:43:18 +00002757 LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002758 LN0->getPointerInfo(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00002759 LN0->isVolatile(), LN0->isNonTemporal(),
2760 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002761 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002762 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002763 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002764 }
2765 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002766 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00002767 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00002768 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002769 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002770 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002771 // If we zero all the possible extended bits, then we can turn this into
2772 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002773 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002774 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002775 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002776 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002777 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002778 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT,
Bill Wendling2627a882009-01-30 20:43:18 +00002779 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002780 LN0->getBasePtr(), LN0->getPointerInfo(),
2781 MemVT,
David Greene1e559442010-02-15 17:00:31 +00002782 LN0->isVolatile(), LN0->isNonTemporal(),
2783 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002784 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002785 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002786 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002787 }
2788 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002789
Chris Lattner35a9f5a2006-02-28 06:49:37 +00002790 // fold (and (load x), 255) -> (zextload x, i8)
2791 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Chengd40d03e2010-01-06 19:38:29 +00002792 // fold (and (any_ext (extload x, i16)), 255) -> (zextload x, i8)
2793 if (N1C && (N0.getOpcode() == ISD::LOAD ||
2794 (N0.getOpcode() == ISD::ANY_EXTEND &&
2795 N0.getOperand(0).getOpcode() == ISD::LOAD))) {
2796 bool HasAnyExt = N0.getOpcode() == ISD::ANY_EXTEND;
2797 LoadSDNode *LN0 = HasAnyExt
2798 ? cast<LoadSDNode>(N0.getOperand(0))
2799 : cast<LoadSDNode>(N0);
Evan Cheng466685d2006-10-09 20:57:25 +00002800 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Tim Northover5bce67a2013-07-02 09:58:53 +00002801 LN0->isUnindexed() && N0.hasOneUse() && SDValue(LN0, 0).hasOneUse()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00002802 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
Evan Chengd40d03e2010-01-06 19:38:29 +00002803 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue())){
2804 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits);
2805 EVT LoadedVT = LN0->getMemoryVT();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002806
Evan Chengd40d03e2010-01-06 19:38:29 +00002807 if (ExtVT == LoadedVT &&
2808 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
Chris Lattneref7634c2010-01-07 21:53:27 +00002809 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002810
2811 SDValue NewLoad =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002812 DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy,
Chris Lattneref7634c2010-01-07 21:53:27 +00002813 LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002814 LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002815 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2816 LN0->getAlignment());
Chris Lattneref7634c2010-01-07 21:53:27 +00002817 AddToWorkList(N);
2818 CombineTo(LN0, NewLoad, NewLoad.getValue(1));
2819 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2820 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002821
Chris Lattneref7634c2010-01-07 21:53:27 +00002822 // Do not change the width of a volatile load.
2823 // Do not generate loads of non-round integer types since these can
2824 // be expensive (and would be wrong if the type is not byte sized).
2825 if (!LN0->isVolatile() && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() &&
2826 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
2827 EVT PtrType = LN0->getOperand(1).getValueType();
Bill Wendling2627a882009-01-30 20:43:18 +00002828
Chris Lattneref7634c2010-01-07 21:53:27 +00002829 unsigned Alignment = LN0->getAlignment();
2830 SDValue NewPtr = LN0->getBasePtr();
2831
2832 // For big endian targets, we need to add an offset to the pointer
2833 // to load the correct bytes. For little endian systems, we merely
2834 // need to read fewer bytes from the same pointer.
2835 if (TLI.isBigEndian()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00002836 unsigned LVTStoreBytes = LoadedVT.getStoreSize();
2837 unsigned EVTStoreBytes = ExtVT.getStoreSize();
2838 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Andrew Trickac6d9be2013-05-25 02:42:55 +00002839 NewPtr = DAG.getNode(ISD::ADD, SDLoc(LN0), PtrType,
Chris Lattneref7634c2010-01-07 21:53:27 +00002840 NewPtr, DAG.getConstant(PtrOff, PtrType));
2841 Alignment = MinAlign(Alignment, PtrOff);
Evan Chengd40d03e2010-01-06 19:38:29 +00002842 }
Chris Lattneref7634c2010-01-07 21:53:27 +00002843
2844 AddToWorkList(NewPtr.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002845
Chris Lattneref7634c2010-01-07 21:53:27 +00002846 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
2847 SDValue Load =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002848 DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy,
Chris Lattneref7634c2010-01-07 21:53:27 +00002849 LN0->getChain(), NewPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002850 LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002851 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2852 Alignment);
Chris Lattneref7634c2010-01-07 21:53:27 +00002853 AddToWorkList(N);
2854 CombineTo(LN0, Load, Load.getValue(1));
2855 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsdc846502007-10-28 12:59:45 +00002856 }
Evan Cheng466685d2006-10-09 20:57:25 +00002857 }
Chris Lattner15045b62006-02-28 06:35:35 +00002858 }
2859 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002860
Evan Chenga9e13ba2012-07-17 18:54:11 +00002861 if (N0.getOpcode() == ISD::ADD && N1.getOpcode() == ISD::SRL &&
2862 VT.getSizeInBits() <= 64) {
2863 if (ConstantSDNode *ADDI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2864 APInt ADDC = ADDI->getAPIntValue();
2865 if (!TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2866 // Look for (and (add x, c1), (lshr y, c2)). If C1 wasn't a legal
2867 // immediate for an add, but it is legal if its top c2 bits are set,
2868 // transform the ADD so the immediate doesn't need to be materialized
2869 // in a register.
2870 if (ConstantSDNode *SRLI = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
2871 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
2872 SRLI->getZExtValue());
2873 if (DAG.MaskedValueIsZero(N0.getOperand(1), Mask)) {
2874 ADDC |= Mask;
2875 if (TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2876 SDValue NewAdd =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002877 DAG.getNode(ISD::ADD, SDLoc(N0), VT,
Evan Chenga9e13ba2012-07-17 18:54:11 +00002878 N0.getOperand(0), DAG.getConstant(ADDC, VT));
2879 CombineTo(N0.getNode(), NewAdd);
2880 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2881 }
2882 }
2883 }
2884 }
2885 }
2886 }
Evan Chenga9e13ba2012-07-17 18:54:11 +00002887
Tim Northover5d8c2e42013-08-27 13:46:45 +00002888 // fold (and (or (srl N, 8), (shl N, 8)), 0xffff) -> (srl (bswap N), const)
2889 if (N1C && N1C->getAPIntValue() == 0xffff && N0.getOpcode() == ISD::OR) {
2890 SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0),
2891 N0.getOperand(1), false);
2892 if (BSwap.getNode())
2893 return BSwap;
2894 }
2895
Evan Chengb3a3d5e2010-04-28 07:10:39 +00002896 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002897}
2898
Evan Cheng9568e5c2011-06-21 06:01:08 +00002899/// MatchBSwapHWord - Match (a >> 8) | (a << 8) as (bswap a) >> 16
2900///
2901SDValue DAGCombiner::MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
2902 bool DemandHighBits) {
2903 if (!LegalOperations)
2904 return SDValue();
2905
2906 EVT VT = N->getValueType(0);
2907 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16)
2908 return SDValue();
2909 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
2910 return SDValue();
2911
2912 // Recognize (and (shl a, 8), 0xff), (and (srl a, 8), 0xff00)
2913 bool LookPassAnd0 = false;
2914 bool LookPassAnd1 = false;
2915 if (N0.getOpcode() == ISD::AND && N0.getOperand(0).getOpcode() == ISD::SRL)
2916 std::swap(N0, N1);
2917 if (N1.getOpcode() == ISD::AND && N1.getOperand(0).getOpcode() == ISD::SHL)
2918 std::swap(N0, N1);
2919 if (N0.getOpcode() == ISD::AND) {
2920 if (!N0.getNode()->hasOneUse())
2921 return SDValue();
2922 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2923 if (!N01C || N01C->getZExtValue() != 0xFF00)
2924 return SDValue();
2925 N0 = N0.getOperand(0);
2926 LookPassAnd0 = true;
2927 }
2928
2929 if (N1.getOpcode() == ISD::AND) {
2930 if (!N1.getNode()->hasOneUse())
2931 return SDValue();
2932 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2933 if (!N11C || N11C->getZExtValue() != 0xFF)
2934 return SDValue();
2935 N1 = N1.getOperand(0);
2936 LookPassAnd1 = true;
2937 }
2938
2939 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
2940 std::swap(N0, N1);
2941 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
2942 return SDValue();
2943 if (!N0.getNode()->hasOneUse() ||
2944 !N1.getNode()->hasOneUse())
2945 return SDValue();
2946
2947 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2948 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2949 if (!N01C || !N11C)
2950 return SDValue();
2951 if (N01C->getZExtValue() != 8 || N11C->getZExtValue() != 8)
2952 return SDValue();
2953
2954 // Look for (shl (and a, 0xff), 8), (srl (and a, 0xff00), 8)
2955 SDValue N00 = N0->getOperand(0);
2956 if (!LookPassAnd0 && N00.getOpcode() == ISD::AND) {
2957 if (!N00.getNode()->hasOneUse())
2958 return SDValue();
2959 ConstantSDNode *N001C = dyn_cast<ConstantSDNode>(N00.getOperand(1));
2960 if (!N001C || N001C->getZExtValue() != 0xFF)
2961 return SDValue();
2962 N00 = N00.getOperand(0);
2963 LookPassAnd0 = true;
2964 }
2965
2966 SDValue N10 = N1->getOperand(0);
2967 if (!LookPassAnd1 && N10.getOpcode() == ISD::AND) {
2968 if (!N10.getNode()->hasOneUse())
2969 return SDValue();
2970 ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N10.getOperand(1));
2971 if (!N101C || N101C->getZExtValue() != 0xFF00)
2972 return SDValue();
2973 N10 = N10.getOperand(0);
2974 LookPassAnd1 = true;
2975 }
2976
2977 if (N00 != N10)
2978 return SDValue();
2979
Tim Northover5d8c2e42013-08-27 13:46:45 +00002980 // Make sure everything beyond the low halfword gets set to zero since the SRL
2981 // 16 will clear the top bits.
Evan Cheng9568e5c2011-06-21 06:01:08 +00002982 unsigned OpSizeInBits = VT.getSizeInBits();
Tim Northover5d8c2e42013-08-27 13:46:45 +00002983 if (DemandHighBits && OpSizeInBits > 16) {
2984 // If the left-shift isn't masked out then the only way this is a bswap is
2985 // if all bits beyond the low 8 are 0. In that case the entire pattern
2986 // reduces to a left shift anyway: leave it for other parts of the combiner.
2987 if (!LookPassAnd0)
2988 return SDValue();
2989
2990 // However, if the right shift isn't masked out then it might be because
2991 // it's not needed. See if we can spot that too.
2992 if (!LookPassAnd1 &&
2993 !DAG.MaskedValueIsZero(
2994 N10, APInt::getHighBitsSet(OpSizeInBits, OpSizeInBits - 16)))
2995 return SDValue();
2996 }
Eric Christopher7332e6e2011-07-14 01:12:15 +00002997
Andrew Trickac6d9be2013-05-25 02:42:55 +00002998 SDValue Res = DAG.getNode(ISD::BSWAP, SDLoc(N), VT, N00);
Evan Cheng9568e5c2011-06-21 06:01:08 +00002999 if (OpSizeInBits > 16)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003000 Res = DAG.getNode(ISD::SRL, SDLoc(N), VT, Res,
Evan Cheng9568e5c2011-06-21 06:01:08 +00003001 DAG.getConstant(OpSizeInBits-16, getShiftAmountTy(VT)));
3002 return Res;
3003}
3004
3005/// isBSwapHWordElement - Return true if the specified node is an element
3006/// that makes up a 32-bit packed halfword byteswap. i.e.
3007/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
Craig Toppera0ec3f92013-07-14 04:42:23 +00003008static bool isBSwapHWordElement(SDValue N, SmallVectorImpl<SDNode *> &Parts) {
Evan Cheng9568e5c2011-06-21 06:01:08 +00003009 if (!N.getNode()->hasOneUse())
3010 return false;
3011
3012 unsigned Opc = N.getOpcode();
3013 if (Opc != ISD::AND && Opc != ISD::SHL && Opc != ISD::SRL)
3014 return false;
3015
3016 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N.getOperand(1));
3017 if (!N1C)
3018 return false;
3019
3020 unsigned Num;
3021 switch (N1C->getZExtValue()) {
3022 default:
3023 return false;
3024 case 0xFF: Num = 0; break;
3025 case 0xFF00: Num = 1; break;
3026 case 0xFF0000: Num = 2; break;
3027 case 0xFF000000: Num = 3; break;
3028 }
3029
3030 // Look for (x & 0xff) << 8 as well as ((x << 8) & 0xff00).
3031 SDValue N0 = N.getOperand(0);
3032 if (Opc == ISD::AND) {
3033 if (Num == 0 || Num == 2) {
3034 // (x >> 8) & 0xff
3035 // (x >> 8) & 0xff0000
3036 if (N0.getOpcode() != ISD::SRL)
3037 return false;
3038 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3039 if (!C || C->getZExtValue() != 8)
3040 return false;
3041 } else {
3042 // (x << 8) & 0xff00
3043 // (x << 8) & 0xff000000
3044 if (N0.getOpcode() != ISD::SHL)
3045 return false;
3046 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3047 if (!C || C->getZExtValue() != 8)
3048 return false;
3049 }
3050 } else if (Opc == ISD::SHL) {
3051 // (x & 0xff) << 8
3052 // (x & 0xff0000) << 8
3053 if (Num != 0 && Num != 2)
3054 return false;
3055 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
3056 if (!C || C->getZExtValue() != 8)
3057 return false;
3058 } else { // Opc == ISD::SRL
3059 // (x & 0xff00) >> 8
3060 // (x & 0xff000000) >> 8
3061 if (Num != 1 && Num != 3)
3062 return false;
3063 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
3064 if (!C || C->getZExtValue() != 8)
3065 return false;
3066 }
3067
3068 if (Parts[Num])
3069 return false;
3070
3071 Parts[Num] = N0.getOperand(0).getNode();
3072 return true;
3073}
3074
3075/// MatchBSwapHWord - Match a 32-bit packed halfword bswap. That is
3076/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
3077/// => (rotl (bswap x), 16)
3078SDValue DAGCombiner::MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1) {
3079 if (!LegalOperations)
3080 return SDValue();
3081
3082 EVT VT = N->getValueType(0);
3083 if (VT != MVT::i32)
3084 return SDValue();
3085 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
3086 return SDValue();
3087
3088 SmallVector<SDNode*,4> Parts(4, (SDNode*)0);
3089 // Look for either
3090 // (or (or (and), (and)), (or (and), (and)))
3091 // (or (or (or (and), (and)), (and)), (and))
3092 if (N0.getOpcode() != ISD::OR)
3093 return SDValue();
3094 SDValue N00 = N0.getOperand(0);
3095 SDValue N01 = N0.getOperand(1);
3096
Evan Cheng9a65a012012-12-13 01:34:32 +00003097 if (N1.getOpcode() == ISD::OR &&
3098 N00.getNumOperands() == 2 && N01.getNumOperands() == 2) {
Evan Cheng9568e5c2011-06-21 06:01:08 +00003099 // (or (or (and), (and)), (or (and), (and)))
3100 SDValue N000 = N00.getOperand(0);
3101 if (!isBSwapHWordElement(N000, Parts))
3102 return SDValue();
3103
3104 SDValue N001 = N00.getOperand(1);
3105 if (!isBSwapHWordElement(N001, Parts))
3106 return SDValue();
3107 SDValue N010 = N01.getOperand(0);
3108 if (!isBSwapHWordElement(N010, Parts))
3109 return SDValue();
3110 SDValue N011 = N01.getOperand(1);
3111 if (!isBSwapHWordElement(N011, Parts))
3112 return SDValue();
3113 } else {
3114 // (or (or (or (and), (and)), (and)), (and))
3115 if (!isBSwapHWordElement(N1, Parts))
3116 return SDValue();
3117 if (!isBSwapHWordElement(N01, Parts))
3118 return SDValue();
3119 if (N00.getOpcode() != ISD::OR)
3120 return SDValue();
3121 SDValue N000 = N00.getOperand(0);
3122 if (!isBSwapHWordElement(N000, Parts))
3123 return SDValue();
3124 SDValue N001 = N00.getOperand(1);
3125 if (!isBSwapHWordElement(N001, Parts))
3126 return SDValue();
3127 }
3128
3129 // Make sure the parts are all coming from the same node.
3130 if (Parts[0] != Parts[1] || Parts[0] != Parts[2] || Parts[0] != Parts[3])
3131 return SDValue();
3132
Andrew Trickac6d9be2013-05-25 02:42:55 +00003133 SDValue BSwap = DAG.getNode(ISD::BSWAP, SDLoc(N), VT,
Evan Cheng9568e5c2011-06-21 06:01:08 +00003134 SDValue(Parts[0],0));
3135
Kay Tiong Khoo670711e2013-09-23 18:43:51 +00003136 // Result of the bswap should be rotated by 16. If it's not legal, then
Evan Cheng9568e5c2011-06-21 06:01:08 +00003137 // do (x << 16) | (x >> 16).
3138 SDValue ShAmt = DAG.getConstant(16, getShiftAmountTy(VT));
3139 if (TLI.isOperationLegalOrCustom(ISD::ROTL, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003140 return DAG.getNode(ISD::ROTL, SDLoc(N), VT, BSwap, ShAmt);
Craig Topper0eb5dad2012-09-29 07:18:53 +00003141 if (TLI.isOperationLegalOrCustom(ISD::ROTR, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003142 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, BSwap, ShAmt);
3143 return DAG.getNode(ISD::OR, SDLoc(N), VT,
3144 DAG.getNode(ISD::SHL, SDLoc(N), VT, BSwap, ShAmt),
3145 DAG.getNode(ISD::SRL, SDLoc(N), VT, BSwap, ShAmt));
Evan Cheng9568e5c2011-06-21 06:01:08 +00003146}
3147
Dan Gohman475871a2008-07-27 21:46:04 +00003148SDValue DAGCombiner::visitOR(SDNode *N) {
3149 SDValue N0 = N->getOperand(0);
3150 SDValue N1 = N->getOperand(1);
3151 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00003152 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3153 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003154 EVT VT = N1.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003155
Dan Gohman7f321562007-06-25 16:23:39 +00003156 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003157 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003158 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003159 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00003160
3161 // fold (or x, 0) -> x, vector edition
3162 if (ISD::isBuildVectorAllZeros(N0.getNode()))
3163 return N1;
3164 if (ISD::isBuildVectorAllZeros(N1.getNode()))
3165 return N0;
3166
3167 // fold (or x, -1) -> -1, vector edition
3168 if (ISD::isBuildVectorAllOnes(N0.getNode()))
3169 return N0;
3170 if (ISD::isBuildVectorAllOnes(N1.getNode()))
3171 return N1;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003172 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003173
Dan Gohman613e0d82007-07-03 14:03:57 +00003174 // fold (or x, undef) -> -1
Bob Wilson86749492010-06-28 23:40:25 +00003175 if (!LegalOperations &&
3176 (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)) {
Nate Begeman93e0ed32009-12-03 07:11:29 +00003177 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
3178 return DAG.getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
3179 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003180 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003181 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003182 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00003183 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003184 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003185 return DAG.getNode(ISD::OR, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003186 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003187 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003188 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003189 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00003190 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003191 return N1;
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003192 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003193 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003194 return N1;
Evan Cheng9568e5c2011-06-21 06:01:08 +00003195
3196 // Recognize halfword bswaps as (bswap + rotl 16) or (bswap + shl 16)
3197 SDValue BSwap = MatchBSwapHWord(N, N0, N1);
3198 if (BSwap.getNode() != 0)
3199 return BSwap;
3200 BSwap = MatchBSwapHWordLow(N, N0, N1);
3201 if (BSwap.getNode() != 0)
3202 return BSwap;
3203
Nate Begemancd4d58c2006-02-03 06:46:56 +00003204 // reassociate or
Andrew Trickac6d9be2013-05-25 02:42:55 +00003205 SDValue ROR = ReassociateOps(ISD::OR, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003206 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00003207 return ROR;
3208 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003209 // iff (c1 & c2) == 0.
Gabor Greifba36cb52008-08-28 21:40:38 +00003210 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00003211 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00003212 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
Bill Wendling32f9eb22010-03-03 01:58:01 +00003213 if ((C1->getAPIntValue() & N1C->getAPIntValue()) != 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003214 return DAG.getNode(ISD::AND, SDLoc(N), VT,
3215 DAG.getNode(ISD::OR, SDLoc(N0), VT,
Bill Wendling7d9f2b92010-03-03 00:35:56 +00003216 N0.getOperand(0), N1),
3217 DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1));
Nate Begeman223df222005-09-08 20:18:10 +00003218 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003219 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
3220 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
3221 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
3222 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00003223
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003224 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003225 LL.getValueType().isInteger()) {
Bill Wendling09025642009-01-30 20:59:34 +00003226 // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0)
3227 // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00003228 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003229 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003230 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(LR),
Bill Wendling09025642009-01-30 20:59:34 +00003231 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003232 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003233 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003234 }
Bill Wendling09025642009-01-30 20:59:34 +00003235 // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1)
3236 // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1)
Scott Michelfdc40a02009-02-17 22:15:04 +00003237 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003238 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003239 SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(LR),
Bill Wendling09025642009-01-30 20:59:34 +00003240 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003241 AddToWorkList(ANDNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003242 return DAG.getSetCC(SDLoc(N), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003243 }
3244 }
3245 // canonicalize equivalent to ll == rl
3246 if (LL == RR && LR == RL) {
3247 Op1 = ISD::getSetCCSwappedOperands(Op1);
3248 std::swap(RL, RR);
3249 }
3250 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003251 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003252 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00003253 if (Result != ISD::SETCC_INVALID &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00003254 (!LegalOperations ||
Owen Anderson39125d92013-02-14 09:07:33 +00003255 (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) &&
3256 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault225ed702013-05-18 00:21:46 +00003257 getSetCCResultType(N0.getValueType())))))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003258 return DAG.getSetCC(SDLoc(N), N0.getValueType(),
Bill Wendling09025642009-01-30 20:59:34 +00003259 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003260 }
3261 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003262
Bill Wendling09025642009-01-30 20:59:34 +00003263 // Simplify: (or (op x...), (op y...)) -> (op (or x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00003264 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003265 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003266 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003267 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003268
Bill Wendling09025642009-01-30 20:59:34 +00003269 // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible.
Chris Lattner1ec72732006-09-14 21:11:37 +00003270 if (N0.getOpcode() == ISD::AND &&
3271 N1.getOpcode() == ISD::AND &&
3272 N0.getOperand(1).getOpcode() == ISD::Constant &&
3273 N1.getOperand(1).getOpcode() == ISD::Constant &&
3274 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00003275 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00003276 // We can only do this xform if we know that bits from X that are set in C2
3277 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003278 const APInt &LHSMask =
3279 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
3280 const APInt &RHSMask =
3281 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003282
Dan Gohmanea859be2007-06-22 14:59:07 +00003283 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
3284 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003285 SDValue X = DAG.getNode(ISD::OR, SDLoc(N0), VT,
Bill Wendling09025642009-01-30 20:59:34 +00003286 N0.getOperand(0), N1.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003287 return DAG.getNode(ISD::AND, SDLoc(N), VT, X,
Bill Wendling09025642009-01-30 20:59:34 +00003288 DAG.getConstant(LHSMask | RHSMask, VT));
Chris Lattner1ec72732006-09-14 21:11:37 +00003289 }
3290 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003291
Chris Lattner516b9622006-09-14 20:50:57 +00003292 // See if this is some rotate idiom.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003293 if (SDNode *Rot = MatchRotate(N0, N1, SDLoc(N)))
Dan Gohman475871a2008-07-27 21:46:04 +00003294 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00003295
Dan Gohman4e39e9d2010-06-24 14:30:44 +00003296 // Simplify the operands using demanded-bits information.
3297 if (!VT.isVector() &&
3298 SimplifyDemandedBits(SDValue(N, 0)))
3299 return SDValue(N, 0);
3300
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003301 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003302}
3303
Chris Lattner516b9622006-09-14 20:50:57 +00003304/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00003305static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00003306 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00003307 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00003308 Mask = Op.getOperand(1);
3309 Op = Op.getOperand(0);
3310 } else {
3311 return false;
3312 }
3313 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003314
Chris Lattner516b9622006-09-14 20:50:57 +00003315 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
3316 Shift = Op;
3317 return true;
3318 }
Bill Wendling09025642009-01-30 20:59:34 +00003319
Scott Michelfdc40a02009-02-17 22:15:04 +00003320 return false;
Chris Lattner516b9622006-09-14 20:50:57 +00003321}
3322
Chris Lattner516b9622006-09-14 20:50:57 +00003323// MatchRotate - Handle an 'or' of two operands. If this is one of the many
3324// idioms for rotate, and if the target supports rotation instructions, generate
3325// a rot[lr].
Andrew Trickac6d9be2013-05-25 02:42:55 +00003326SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, SDLoc DL) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003327 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Owen Andersone50ed302009-08-10 22:56:29 +00003328 EVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00003329 if (!TLI.isTypeLegal(VT)) return 0;
3330
3331 // The target must have at least one rotate flavor.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003332 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
3333 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Chris Lattner516b9622006-09-14 20:50:57 +00003334 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003335
Chris Lattner516b9622006-09-14 20:50:57 +00003336 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00003337 SDValue LHSShift; // The shift.
3338 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00003339 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
3340 return 0; // Not part of a rotate.
3341
Dan Gohman475871a2008-07-27 21:46:04 +00003342 SDValue RHSShift; // The shift.
3343 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00003344 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
3345 return 0; // Not part of a rotate.
Scott Michelfdc40a02009-02-17 22:15:04 +00003346
Chris Lattner516b9622006-09-14 20:50:57 +00003347 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
3348 return 0; // Not shifting the same value.
3349
3350 if (LHSShift.getOpcode() == RHSShift.getOpcode())
3351 return 0; // Shifts must disagree.
Scott Michelfdc40a02009-02-17 22:15:04 +00003352
Chris Lattner516b9622006-09-14 20:50:57 +00003353 // Canonicalize shl to left side in a shl/srl pair.
3354 if (RHSShift.getOpcode() == ISD::SHL) {
3355 std::swap(LHS, RHS);
3356 std::swap(LHSShift, RHSShift);
3357 std::swap(LHSMask , RHSMask );
3358 }
3359
Duncan Sands83ec4b62008-06-06 12:08:01 +00003360 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00003361 SDValue LHSShiftArg = LHSShift.getOperand(0);
3362 SDValue LHSShiftAmt = LHSShift.getOperand(1);
Kai Nackeceb3b462013-09-19 23:00:28 +00003363 SDValue RHSShiftArg = RHSShift.getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00003364 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00003365
3366 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
3367 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00003368 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
3369 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003370 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
3371 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00003372 if ((LShVal + RShVal) != OpSizeInBits)
3373 return 0;
3374
Craig Topper32b73432012-09-29 06:54:22 +00003375 SDValue Rot = DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
3376 LHSShiftArg, HasROTL ? LHSShiftAmt : RHSShiftAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00003377
Chris Lattner516b9622006-09-14 20:50:57 +00003378 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00003379 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003380 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00003381
Gabor Greifba36cb52008-08-28 21:40:38 +00003382 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003383 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
3384 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003385 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003386 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003387 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
3388 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003389 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003390
Bill Wendling317bd702009-01-30 21:14:50 +00003391 Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT));
Chris Lattner516b9622006-09-14 20:50:57 +00003392 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003393
Gabor Greifba36cb52008-08-28 21:40:38 +00003394 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00003395 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003396
Chris Lattner516b9622006-09-14 20:50:57 +00003397 // If there is a mask here, and we have a variable shift, we can't be sure
3398 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00003399 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00003400 return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00003401
Benjamin Kramercd216b22013-09-24 14:21:28 +00003402 // If the shift amount is sign/zext/any-extended just peel it off.
3403 SDValue LExtOp0 = LHSShiftAmt;
3404 SDValue RExtOp0 = RHSShiftAmt;
Craig Topper0eb5dad2012-09-29 07:18:53 +00003405 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND ||
3406 LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND ||
3407 LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND ||
3408 LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
3409 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND ||
3410 RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND ||
3411 RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND ||
3412 RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Benjamin Kramercd216b22013-09-24 14:21:28 +00003413 LExtOp0 = LHSShiftAmt.getOperand(0);
3414 RExtOp0 = RHSShiftAmt.getOperand(0);
3415 }
3416
3417 if (RExtOp0.getOpcode() == ISD::SUB && RExtOp0.getOperand(1) == LExtOp0) {
3418 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
3419 // (rotl x, y)
3420 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
3421 // (rotr x, (sub 32, y))
3422 if (ConstantSDNode *SUBC =
3423 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
3424 if (SUBC->getAPIntValue() == OpSizeInBits) {
3425 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT, LHSShiftArg,
3426 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
3427 } else if (LHSShiftArg.getOpcode() == ISD::ZERO_EXTEND ||
Kai Nackeceb3b462013-09-19 23:00:28 +00003428 LHSShiftArg.getOpcode() == ISD::ANY_EXTEND) {
Benjamin Kramercd216b22013-09-24 14:21:28 +00003429 // fold (or (shl (*ext x), (*ext y)),
3430 // (srl (*ext x), (*ext (sub 32, y)))) ->
3431 // (*ext (rotl x, y))
3432 // fold (or (shl (*ext x), (*ext y)),
3433 // (srl (*ext x), (*ext (sub 32, y)))) ->
3434 // (*ext (rotr x, (sub 32, y)))
3435 SDValue LArgExtOp0 = LHSShiftArg.getOperand(0);
3436 EVT LArgVT = LArgExtOp0.getValueType();
Jin-Gu Kangb70a05a2013-10-03 15:58:48 +00003437 bool HasROTRWithLArg = TLI.isOperationLegalOrCustom(ISD::ROTR, LArgVT);
3438 bool HasROTLWithLArg = TLI.isOperationLegalOrCustom(ISD::ROTL, LArgVT);
3439 if (HasROTRWithLArg || HasROTLWithLArg) {
3440 if (LArgVT.getSizeInBits() == SUBC->getAPIntValue()) {
3441 SDValue V =
3442 DAG.getNode(HasROTLWithLArg ? ISD::ROTL : ISD::ROTR, DL, LArgVT,
3443 LArgExtOp0, HasROTL ? LHSShiftAmt : RHSShiftAmt);
3444 return DAG.getNode(LHSShiftArg.getOpcode(), DL, VT, V).getNode();
3445 }
3446 }
David Blaikiec2286722013-09-20 00:33:11 +00003447 }
Benjamin Kramercd216b22013-09-24 14:21:28 +00003448 }
3449 } else if (LExtOp0.getOpcode() == ISD::SUB &&
3450 RExtOp0 == LExtOp0.getOperand(1)) {
3451 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
3452 // (rotr x, y)
3453 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
3454 // (rotl x, (sub 32, y))
3455 if (ConstantSDNode *SUBC =
3456 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
3457 if (SUBC->getAPIntValue() == OpSizeInBits) {
3458 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT, LHSShiftArg,
3459 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
3460 } else if (RHSShiftArg.getOpcode() == ISD::ZERO_EXTEND ||
Kai Nackeceb3b462013-09-19 23:00:28 +00003461 RHSShiftArg.getOpcode() == ISD::ANY_EXTEND) {
Benjamin Kramercd216b22013-09-24 14:21:28 +00003462 // fold (or (shl (*ext x), (*ext (sub 32, y))),
3463 // (srl (*ext x), (*ext y))) ->
3464 // (*ext (rotl x, y))
3465 // fold (or (shl (*ext x), (*ext (sub 32, y))),
3466 // (srl (*ext x), (*ext y))) ->
3467 // (*ext (rotr x, (sub 32, y)))
3468 SDValue RArgExtOp0 = RHSShiftArg.getOperand(0);
3469 EVT RArgVT = RArgExtOp0.getValueType();
Jin-Gu Kangb70a05a2013-10-03 15:58:48 +00003470 bool HasROTRWithRArg = TLI.isOperationLegalOrCustom(ISD::ROTR, RArgVT);
3471 bool HasROTLWithRArg = TLI.isOperationLegalOrCustom(ISD::ROTL, RArgVT);
3472 if (HasROTRWithRArg || HasROTLWithRArg) {
3473 if (RArgVT.getSizeInBits() == SUBC->getAPIntValue()) {
3474 SDValue V =
3475 DAG.getNode(HasROTRWithRArg ? ISD::ROTR : ISD::ROTL, DL, RArgVT,
3476 RArgExtOp0, HasROTR ? RHSShiftAmt : LHSShiftAmt);
3477 return DAG.getNode(RHSShiftArg.getOpcode(), DL, VT, V).getNode();
3478 }
Kai Nackeceb3b462013-09-19 23:00:28 +00003479 }
David Blaikiec2286722013-09-20 00:33:11 +00003480 }
Chris Lattner516b9622006-09-14 20:50:57 +00003481 }
3482 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003483
Chris Lattner516b9622006-09-14 20:50:57 +00003484 return 0;
3485}
3486
Dan Gohman475871a2008-07-27 21:46:04 +00003487SDValue DAGCombiner::visitXOR(SDNode *N) {
3488 SDValue N0 = N->getOperand(0);
3489 SDValue N1 = N->getOperand(1);
3490 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00003491 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3492 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003493 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003494
Dan Gohman7f321562007-06-25 16:23:39 +00003495 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003496 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003497 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003498 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00003499
3500 // fold (xor x, 0) -> x, vector edition
3501 if (ISD::isBuildVectorAllZeros(N0.getNode()))
3502 return N1;
3503 if (ISD::isBuildVectorAllZeros(N1.getNode()))
3504 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003505 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003506
Evan Cheng26471c42008-03-25 20:08:07 +00003507 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
3508 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
3509 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00003510 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00003511 if (N0.getOpcode() == ISD::UNDEF)
3512 return N0;
3513 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00003514 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003515 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003516 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003517 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00003518 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003519 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003520 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003521 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003522 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003523 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00003524 // reassociate xor
Andrew Trickac6d9be2013-05-25 02:42:55 +00003525 SDValue RXOR = ReassociateOps(ISD::XOR, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003526 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00003527 return RXOR;
Bill Wendlingae89bb12008-11-11 08:25:46 +00003528
Nate Begeman1d4d4142005-09-01 00:19:25 +00003529 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00003530 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003531 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00003532 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
3533 isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003534
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00003535 if (!LegalOperations ||
3536 TLI.isCondCodeLegal(NotCC, LHS.getSimpleValueType())) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00003537 switch (N0.getOpcode()) {
3538 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003539 llvm_unreachable("Unhandled SetCC Equivalent!");
Bill Wendlingae89bb12008-11-11 08:25:46 +00003540 case ISD::SETCC:
Andrew Trickac6d9be2013-05-25 02:42:55 +00003541 return DAG.getSetCC(SDLoc(N), VT, LHS, RHS, NotCC);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003542 case ISD::SELECT_CC:
Andrew Trickac6d9be2013-05-25 02:42:55 +00003543 return DAG.getSelectCC(SDLoc(N), LHS, RHS, N0.getOperand(2),
Bill Wendlingae89bb12008-11-11 08:25:46 +00003544 N0.getOperand(3), NotCC);
3545 }
3546 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003547 }
Bill Wendlingae89bb12008-11-11 08:25:46 +00003548
Chris Lattner61c5ff42007-09-10 21:39:07 +00003549 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00003550 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00003551 N0.getNode()->hasOneUse() &&
3552 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00003553 SDValue V = N0.getOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003554 V = DAG.getNode(ISD::XOR, SDLoc(N0), V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00003555 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003556 AddToWorkList(V.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003557 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, V);
Chris Lattner61c5ff42007-09-10 21:39:07 +00003558 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003559
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003560 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc
Owen Anderson825b72b2009-08-11 20:47:22 +00003561 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00003562 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003563 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003564 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
3565 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Andrew Trickac6d9be2013-05-25 02:42:55 +00003566 LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS
3567 RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003568 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003569 return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003570 }
3571 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003572 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants
Scott Michelfdc40a02009-02-17 22:15:04 +00003573 if (N1C && N1C->isAllOnesValue() &&
Nate Begeman99801192005-09-07 23:25:52 +00003574 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003575 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003576 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
3577 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Andrew Trickac6d9be2013-05-25 02:42:55 +00003578 LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS
3579 RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003580 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003581 return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003582 }
3583 }
David Majnemer363160a2013-05-08 06:44:42 +00003584 // fold (xor (and x, y), y) -> (and (not x), y)
3585 if (N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
3586 N0->getOperand(1) == N1) {
3587 SDValue X = N0->getOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003588 SDValue NotX = DAG.getNOT(SDLoc(X), X, VT);
David Majnemer363160a2013-05-08 06:44:42 +00003589 AddToWorkList(NotX.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003590 return DAG.getNode(ISD::AND, SDLoc(N), VT, NotX, N1);
David Majnemer363160a2013-05-08 06:44:42 +00003591 }
Bill Wendling317bd702009-01-30 21:14:50 +00003592 // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2))
Nate Begeman223df222005-09-08 20:18:10 +00003593 if (N1C && N0.getOpcode() == ISD::XOR) {
3594 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
3595 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3596 if (N00C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003597 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N0.getOperand(1),
Bill Wendling317bd702009-01-30 21:14:50 +00003598 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003599 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003600 if (N01C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003601 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N0.getOperand(0),
Bill Wendling317bd702009-01-30 21:14:50 +00003602 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003603 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003604 }
3605 // fold (xor x, x) -> 0
Eric Christopher7bccf6a2011-02-16 04:50:12 +00003606 if (N0 == N1)
Hal Finkelbd6f1f62013-07-09 17:02:45 +00003607 return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations, LegalTypes);
Scott Michelfdc40a02009-02-17 22:15:04 +00003608
Chris Lattner35e5c142006-05-05 05:51:50 +00003609 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
3610 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003611 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003612 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003613 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003614
Chris Lattner3e104b12006-04-08 04:15:24 +00003615 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003616 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00003617 SimplifyDemandedBits(SDValue(N, 0)))
3618 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003619
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003620 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003621}
3622
Chris Lattnere70da202007-12-06 07:33:36 +00003623/// visitShiftByConstant - Handle transforms common to the three shifts, when
3624/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00003625SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003626 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00003627 if (!LHS->hasOneUse()) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003628
Chris Lattnere70da202007-12-06 07:33:36 +00003629 // We want to pull some binops through shifts, so that we have (and (shift))
3630 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
3631 // thing happens with address calculations, so it's important to canonicalize
3632 // it.
3633 bool HighBitSet = false; // Can we transform this if the high bit is set?
Scott Michelfdc40a02009-02-17 22:15:04 +00003634
Chris Lattnere70da202007-12-06 07:33:36 +00003635 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003636 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003637 case ISD::OR:
3638 case ISD::XOR:
3639 HighBitSet = false; // We can only transform sra if the high bit is clear.
3640 break;
3641 case ISD::AND:
3642 HighBitSet = true; // We can only transform sra if the high bit is set.
3643 break;
3644 case ISD::ADD:
Scott Michelfdc40a02009-02-17 22:15:04 +00003645 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00003646 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00003647 HighBitSet = false; // We can only transform sra if the high bit is clear.
3648 break;
3649 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003650
Chris Lattnere70da202007-12-06 07:33:36 +00003651 // We require the RHS of the binop to be a constant as well.
3652 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003653 if (!BinOpCst) return SDValue();
Bill Wendling88103372009-01-30 21:37:17 +00003654
3655 // FIXME: disable this unless the input to the binop is a shift by a constant.
3656 // If it is not a shift, it pessimizes some common cases like:
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003657 //
Bill Wendling88103372009-01-30 21:37:17 +00003658 // void foo(int *X, int i) { X[i & 1235] = 1; }
3659 // int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00003660 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00003661 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003662 BinOpLHSVal->getOpcode() != ISD::SRA &&
3663 BinOpLHSVal->getOpcode() != ISD::SRL) ||
3664 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00003665 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003666
Owen Andersone50ed302009-08-10 22:56:29 +00003667 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003668
Bill Wendling88103372009-01-30 21:37:17 +00003669 // If this is a signed shift right, and the high bit is modified by the
3670 // logical operation, do not perform the transformation. The highBitSet
3671 // boolean indicates the value of the high bit of the constant which would
3672 // cause it to be modified for this operation.
Chris Lattnere70da202007-12-06 07:33:36 +00003673 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00003674 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
3675 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00003676 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003677 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003678
Chris Lattnere70da202007-12-06 07:33:36 +00003679 // Fold the constants, shifting the binop RHS by the shift amount.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003680 SDValue NewRHS = DAG.getNode(N->getOpcode(), SDLoc(LHS->getOperand(1)),
Bill Wendling88103372009-01-30 21:37:17 +00003681 N->getValueType(0),
3682 LHS->getOperand(1), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003683
3684 // Create the new shift.
Eric Christopher503a64d2010-12-09 04:48:06 +00003685 SDValue NewShift = DAG.getNode(N->getOpcode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00003686 SDLoc(LHS->getOperand(0)),
Bill Wendling88103372009-01-30 21:37:17 +00003687 VT, LHS->getOperand(0), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003688
3689 // Create the new binop.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003690 return DAG.getNode(LHS->getOpcode(), SDLoc(N), VT, NewShift, NewRHS);
Chris Lattnere70da202007-12-06 07:33:36 +00003691}
3692
Dan Gohman475871a2008-07-27 21:46:04 +00003693SDValue DAGCombiner::visitSHL(SDNode *N) {
3694 SDValue N0 = N->getOperand(0);
3695 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003696 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3697 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003698 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003699 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003700
Nate Begeman1d4d4142005-09-01 00:19:25 +00003701 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003702 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003703 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003704 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003705 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003706 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003707 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003708 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003709 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003710 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003711 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003712 return N0;
Chad Rosier92bcd962011-06-14 22:29:10 +00003713 // fold (shl undef, x) -> 0
3714 if (N0.getOpcode() == ISD::UNDEF)
3715 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003716 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00003717 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman87862e72009-12-11 21:31:27 +00003718 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003719 return DAG.getConstant(0, VT);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003720 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003721 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003722 N1.getOperand(0).getOpcode() == ISD::AND &&
3723 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003724 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003725 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003726 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003727 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003728 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003729 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003730 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
3731 DAG.getNode(ISD::AND, SDLoc(N), TruncVT,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003732 DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003733 SDLoc(N),
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003734 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003735 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003736 }
3737 }
3738
Dan Gohman475871a2008-07-27 21:46:04 +00003739 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3740 return SDValue(N, 0);
Bill Wendling88103372009-01-30 21:37:17 +00003741
3742 // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00003743 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003744 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003745 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3746 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003747 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003748 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003749 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00003750 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003751 }
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003752
3753 // fold (shl (ext (shl x, c1)), c2) -> (ext (shl x, (add c1, c2)))
3754 // For this to be valid, the second form must not preserve any of the bits
3755 // that are shifted out by the inner shift in the first form. This means
3756 // the outer shift size must be >= the number of bits added by the ext.
3757 // As a corollary, we don't care what kind of ext it is.
3758 if (N1C && (N0.getOpcode() == ISD::ZERO_EXTEND ||
3759 N0.getOpcode() == ISD::ANY_EXTEND ||
3760 N0.getOpcode() == ISD::SIGN_EXTEND) &&
3761 N0.getOperand(0).getOpcode() == ISD::SHL &&
3762 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00003763 uint64_t c1 =
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003764 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3765 uint64_t c2 = N1C->getZExtValue();
3766 EVT InnerShiftVT = N0.getOperand(0).getValueType();
3767 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
3768 if (c2 >= OpSizeInBits - InnerShiftSize) {
3769 if (c1 + c2 >= OpSizeInBits)
3770 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003771 return DAG.getNode(ISD::SHL, SDLoc(N0), VT,
3772 DAG.getNode(N0.getOpcode(), SDLoc(N0), VT,
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003773 N0.getOperand(0)->getOperand(0)),
3774 DAG.getConstant(c1 + c2, N1.getValueType()));
3775 }
3776 }
3777
Andrea Di Biagioa9f113d2013-09-27 11:37:05 +00003778 // fold (shl (zext (srl x, C)), C) -> (zext (shl (srl x, C), C))
3779 // Only fold this if the inner zext has no other uses to avoid increasing
3780 // the total number of instructions.
3781 if (N1C && N0.getOpcode() == ISD::ZERO_EXTEND && N0.hasOneUse() &&
3782 N0.getOperand(0).getOpcode() == ISD::SRL &&
3783 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
3784 uint64_t c1 =
3785 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3786 if (c1 < VT.getSizeInBits()) {
3787 uint64_t c2 = N1C->getZExtValue();
3788 if (c1 == c2) {
3789 SDValue NewOp0 = N0.getOperand(0);
3790 EVT CountVT = NewOp0.getOperand(1).getValueType();
3791 SDValue NewSHL = DAG.getNode(ISD::SHL, SDLoc(N), NewOp0.getValueType(),
3792 NewOp0, DAG.getConstant(c2, CountVT));
3793 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N0), VT, NewSHL);
3794 }
3795 }
3796 }
3797
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003798 // fold (shl (srl x, c1), c2) -> (and (shl x, (sub c2, c1), MASK) or
3799 // (and (srl x, (sub c1, c2), MASK)
Chandler Carruth62dfc512012-01-05 11:05:55 +00003800 // Only fold this if the inner shift has no other uses -- if it does, folding
3801 // this will increase the total number of instructions.
3802 if (N1C && N0.getOpcode() == ISD::SRL && N0.hasOneUse() &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003803 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003804 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
Evan Chengd101a722009-07-21 05:40:15 +00003805 if (c1 < VT.getSizeInBits()) {
3806 uint64_t c2 = N1C->getZExtValue();
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003807 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
3808 VT.getSizeInBits() - c1);
3809 SDValue Shift;
3810 if (c2 > c1) {
3811 Mask = Mask.shl(c2-c1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003812 Shift = DAG.getNode(ISD::SHL, SDLoc(N), VT, N0.getOperand(0),
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003813 DAG.getConstant(c2-c1, N1.getValueType()));
3814 } else {
3815 Mask = Mask.lshr(c1-c2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003816 Shift = DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0),
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003817 DAG.getConstant(c1-c2, N1.getValueType()));
3818 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00003819 return DAG.getNode(ISD::AND, SDLoc(N0), VT, Shift,
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003820 DAG.getConstant(Mask, VT));
Evan Chengd101a722009-07-21 05:40:15 +00003821 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003822 }
Bill Wendling88103372009-01-30 21:37:17 +00003823 // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1))
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003824 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) {
3825 SDValue HiBitsMask =
3826 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
3827 VT.getSizeInBits() -
3828 N1C->getZExtValue()),
3829 VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003830 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0.getOperand(0),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003831 HiBitsMask);
3832 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003833
Evan Chenge5b51ac2010-04-17 06:13:15 +00003834 if (N1C) {
3835 SDValue NewSHL = visitShiftByConstant(N, N1C->getZExtValue());
3836 if (NewSHL.getNode())
3837 return NewSHL;
3838 }
3839
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003840 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003841}
3842
Dan Gohman475871a2008-07-27 21:46:04 +00003843SDValue DAGCombiner::visitSRA(SDNode *N) {
3844 SDValue N0 = N->getOperand(0);
3845 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003846 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3847 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003848 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003849 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003850
Bill Wendling88103372009-01-30 21:37:17 +00003851 // fold (sra c1, c2) -> (sra c1, c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00003852 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003853 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003854 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003855 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003856 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003857 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00003858 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003859 return N0;
Bill Wendling88103372009-01-30 21:37:17 +00003860 // fold (sra x, (setge c, size(x))) -> undef
Dan Gohman87862e72009-12-11 21:31:27 +00003861 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003862 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003863 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003864 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003865 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00003866 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
3867 // sext_inreg.
3868 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohman87862e72009-12-11 21:31:27 +00003869 unsigned LowBits = OpSizeInBits - (unsigned)N1C->getZExtValue();
Dan Gohmand1996362010-01-09 02:13:55 +00003870 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), LowBits);
3871 if (VT.isVector())
3872 ExtVT = EVT::getVectorVT(*DAG.getContext(),
3873 ExtVT, VT.getVectorNumElements());
3874 if ((!LegalOperations ||
3875 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, ExtVT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003876 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Dan Gohmand1996362010-01-09 02:13:55 +00003877 N0.getOperand(0), DAG.getValueType(ExtVT));
Nate Begemanfb7217b2006-02-17 19:54:08 +00003878 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003879
Bill Wendling88103372009-01-30 21:37:17 +00003880 // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2))
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003881 if (N1C && N0.getOpcode() == ISD::SRA) {
3882 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003883 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Dan Gohman87862e72009-12-11 21:31:27 +00003884 if (Sum >= OpSizeInBits) Sum = OpSizeInBits-1;
Andrew Trickac6d9be2013-05-25 02:42:55 +00003885 return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0.getOperand(0),
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003886 DAG.getConstant(Sum, N1C->getValueType(0)));
3887 }
3888 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00003889
Bill Wendling88103372009-01-30 21:37:17 +00003890 // fold (sra (shl X, m), (sub result_size, n))
3891 // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for
Scott Michelfdc40a02009-02-17 22:15:04 +00003892 // result_size - n != m.
3893 // If truncate is free for the target sext(shl) is likely to result in better
Christopher Lambb9b04282008-03-20 04:31:39 +00003894 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00003895 if (N0.getOpcode() == ISD::SHL) {
3896 // Get the two constanst of the shifts, CN0 = m, CN = n.
3897 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3898 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003899 // Determine what the truncate's result bitsize and type would be.
Owen Andersone50ed302009-08-10 22:56:29 +00003900 EVT TruncVT =
Eric Christopher503a64d2010-12-09 04:48:06 +00003901 EVT::getIntegerVT(*DAG.getContext(),
3902 OpSizeInBits - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00003903 // Determine the residual right-shift amount.
Torok Edwin6bb49582009-05-23 17:29:48 +00003904 signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003905
Scott Michelfdc40a02009-02-17 22:15:04 +00003906 // If the shift is not a no-op (in which case this should be just a sign
3907 // extend already), the truncated to type is legal, sign_extend is legal
Dan Gohmanf451cb82010-02-10 16:03:48 +00003908 // on that type, and the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00003909 // perform the transform.
Torok Edwin6bb49582009-05-23 17:29:48 +00003910 if ((ShiftAmt > 0) &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003911 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
3912 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00003913 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003914
Owen Anderson95771af2011-02-25 21:41:48 +00003915 SDValue Amt = DAG.getConstant(ShiftAmt,
3916 getShiftAmountTy(N0.getOperand(0).getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003917 SDValue Shift = DAG.getNode(ISD::SRL, SDLoc(N0), VT,
Bill Wendling88103372009-01-30 21:37:17 +00003918 N0.getOperand(0), Amt);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003919 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), TruncVT,
Bill Wendling88103372009-01-30 21:37:17 +00003920 Shift);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003921 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N),
Bill Wendling88103372009-01-30 21:37:17 +00003922 N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00003923 }
3924 }
3925 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003926
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003927 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003928 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003929 N1.getOperand(0).getOpcode() == ISD::AND &&
3930 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003931 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003932 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003933 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003934 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003935 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003936 TruncC = TruncC.trunc(TruncVT.getScalarType().getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003937 return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0,
3938 DAG.getNode(ISD::AND, SDLoc(N),
Bill Wendling88103372009-01-30 21:37:17 +00003939 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003940 DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003941 SDLoc(N),
Bill Wendling9729c5a2009-01-31 03:12:48 +00003942 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003943 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003944 }
3945 }
3946
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003947 // fold (sra (trunc (sr x, c1)), c2) -> (trunc (sra x, c1+c2))
3948 // if c1 is equal to the number of bits the trunc removes
3949 if (N0.getOpcode() == ISD::TRUNCATE &&
3950 (N0.getOperand(0).getOpcode() == ISD::SRL ||
3951 N0.getOperand(0).getOpcode() == ISD::SRA) &&
3952 N0.getOperand(0).hasOneUse() &&
3953 N0.getOperand(0).getOperand(1).hasOneUse() &&
3954 N1C && isa<ConstantSDNode>(N0.getOperand(0).getOperand(1))) {
3955 EVT LargeVT = N0.getOperand(0).getValueType();
3956 ConstantSDNode *LargeShiftAmt =
3957 cast<ConstantSDNode>(N0.getOperand(0).getOperand(1));
3958
3959 if (LargeVT.getScalarType().getSizeInBits() - OpSizeInBits ==
3960 LargeShiftAmt->getZExtValue()) {
3961 SDValue Amt =
3962 DAG.getConstant(LargeShiftAmt->getZExtValue() + N1C->getZExtValue(),
Owen Anderson95771af2011-02-25 21:41:48 +00003963 getShiftAmountTy(N0.getOperand(0).getOperand(0).getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003964 SDValue SRA = DAG.getNode(ISD::SRA, SDLoc(N), LargeVT,
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003965 N0.getOperand(0).getOperand(0), Amt);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003966 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, SRA);
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003967 }
3968 }
3969
Scott Michelfdc40a02009-02-17 22:15:04 +00003970 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00003971 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3972 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003973
3974
Nate Begeman1d4d4142005-09-01 00:19:25 +00003975 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003976 if (DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003977 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00003978
Evan Chenge5b51ac2010-04-17 06:13:15 +00003979 if (N1C) {
3980 SDValue NewSRA = visitShiftByConstant(N, N1C->getZExtValue());
3981 if (NewSRA.getNode())
3982 return NewSRA;
3983 }
3984
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003985 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003986}
3987
Dan Gohman475871a2008-07-27 21:46:04 +00003988SDValue DAGCombiner::visitSRL(SDNode *N) {
3989 SDValue N0 = N->getOperand(0);
3990 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003991 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3992 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003993 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003994 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003995
Nate Begeman1d4d4142005-09-01 00:19:25 +00003996 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003997 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003998 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003999 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00004000 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00004001 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00004002 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004003 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00004004 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004005 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00004006 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00004007 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00004008 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00004009 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004010 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00004011 return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004012
Bill Wendling88103372009-01-30 21:37:17 +00004013 // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00004014 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00004015 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004016 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
4017 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00004018 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00004019 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004020 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00004021 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00004022 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004023
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00004024 // fold (srl (trunc (srl x, c1)), c2) -> 0 or (trunc (srl x, (add c1, c2)))
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00004025 if (N1C && N0.getOpcode() == ISD::TRUNCATE &&
4026 N0.getOperand(0).getOpcode() == ISD::SRL &&
Dale Johannesen025cc6e2010-12-20 20:10:50 +00004027 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00004028 uint64_t c1 =
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00004029 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
4030 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00004031 EVT InnerShiftVT = N0.getOperand(0).getValueType();
4032 EVT ShiftCountVT = N0.getOperand(0)->getOperand(1).getValueType();
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00004033 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
Dale Johannesen025cc6e2010-12-20 20:10:50 +00004034 // This is only valid if the OpSizeInBits + c1 = size of inner shift.
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00004035 if (c1 + OpSizeInBits == InnerShiftSize) {
4036 if (c1 + c2 >= InnerShiftSize)
4037 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004038 return DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT,
4039 DAG.getNode(ISD::SRL, SDLoc(N0), InnerShiftVT,
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00004040 N0.getOperand(0)->getOperand(0),
Dale Johannesenc72b18c2010-12-21 21:55:50 +00004041 DAG.getConstant(c1 + c2, ShiftCountVT)));
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00004042 }
4043 }
4044
Chris Lattnerefcddc32010-04-15 05:28:43 +00004045 // fold (srl (shl x, c), c) -> (and x, cst2)
4046 if (N1C && N0.getOpcode() == ISD::SHL && N0.getOperand(1) == N1 &&
4047 N0.getValueSizeInBits() <= 64) {
4048 uint64_t ShAmt = N1C->getZExtValue()+64-N0.getValueSizeInBits();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004049 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0.getOperand(0),
Chris Lattnerefcddc32010-04-15 05:28:43 +00004050 DAG.getConstant(~0ULL >> ShAmt, VT));
4051 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004052
Michael Liao2da86392013-06-21 18:45:27 +00004053 // fold (srl (anyextend x), c) -> (and (anyextend (srl x, c)), mask)
Chris Lattner06afe072006-05-05 22:53:17 +00004054 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
4055 // Shifting in all undef bits?
Owen Andersone50ed302009-08-10 22:56:29 +00004056 EVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004057 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Dale Johannesene8d72302009-02-06 23:05:02 +00004058 return DAG.getUNDEF(VT);
Chris Lattner06afe072006-05-05 22:53:17 +00004059
Evan Chenge5b51ac2010-04-17 06:13:15 +00004060 if (!LegalTypes || TLI.isTypeDesirableForOp(ISD::SRL, SmallVT)) {
Owen Andersona34d9362011-04-14 17:30:49 +00004061 uint64_t ShiftAmt = N1C->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004062 SDValue SmallShift = DAG.getNode(ISD::SRL, SDLoc(N0), SmallVT,
Owen Andersona34d9362011-04-14 17:30:49 +00004063 N0.getOperand(0),
4064 DAG.getConstant(ShiftAmt, getShiftAmountTy(SmallVT)));
Evan Chenge5b51ac2010-04-17 06:13:15 +00004065 AddToWorkList(SmallShift.getNode());
Michael Liao2da86392013-06-21 18:45:27 +00004066 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits()).lshr(ShiftAmt);
4067 return DAG.getNode(ISD::AND, SDLoc(N), VT,
4068 DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, SmallShift),
4069 DAG.getConstant(Mask, VT));
Evan Chenge5b51ac2010-04-17 06:13:15 +00004070 }
Chris Lattner06afe072006-05-05 22:53:17 +00004071 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004072
Chris Lattner3657ffe2006-10-12 20:23:19 +00004073 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
4074 // bit, which is unmodified by sra.
Bill Wendling88103372009-01-30 21:37:17 +00004075 if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00004076 if (N0.getOpcode() == ISD::SRA)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004077 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0), N1);
Chris Lattner3657ffe2006-10-12 20:23:19 +00004078 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004079
Sylvestre Ledru94c22712012-09-27 10:14:43 +00004080 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
Scott Michelfdc40a02009-02-17 22:15:04 +00004081 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004082 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00004083 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00004084 DAG.ComputeMaskedBits(N0.getOperand(0), KnownZero, KnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00004085
Chris Lattner350bec02006-04-02 06:11:11 +00004086 // If any of the input bits are KnownOne, then the input couldn't be all
4087 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00004088 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004089
Chris Lattner350bec02006-04-02 06:11:11 +00004090 // If all of the bits input the to ctlz node are known to be zero, then
4091 // the result of the ctlz is "32" and the result of the shift is one.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00004092 APInt UnknownBits = ~KnownZero;
Chris Lattner350bec02006-04-02 06:11:11 +00004093 if (UnknownBits == 0) return DAG.getConstant(1, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004094
Chris Lattner350bec02006-04-02 06:11:11 +00004095 // Otherwise, check to see if there is exactly one bit input to the ctlz.
Bill Wendling88103372009-01-30 21:37:17 +00004096 if ((UnknownBits & (UnknownBits - 1)) == 0) {
Chris Lattner350bec02006-04-02 06:11:11 +00004097 // Okay, we know that only that the single bit specified by UnknownBits
Bill Wendling88103372009-01-30 21:37:17 +00004098 // could be set on input to the CTLZ node. If this bit is set, the SRL
4099 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
4100 // to an SRL/XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00004101 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00004102 SDValue Op = N0.getOperand(0);
Bill Wendling88103372009-01-30 21:37:17 +00004103
Chris Lattner350bec02006-04-02 06:11:11 +00004104 if (ShAmt) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004105 Op = DAG.getNode(ISD::SRL, SDLoc(N0), VT, Op,
Owen Anderson95771af2011-02-25 21:41:48 +00004106 DAG.getConstant(ShAmt, getShiftAmountTy(Op.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00004107 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00004108 }
Bill Wendling88103372009-01-30 21:37:17 +00004109
Andrew Trickac6d9be2013-05-25 02:42:55 +00004110 return DAG.getNode(ISD::XOR, SDLoc(N), VT,
Bill Wendling88103372009-01-30 21:37:17 +00004111 Op, DAG.getConstant(1, VT));
Chris Lattner350bec02006-04-02 06:11:11 +00004112 }
4113 }
Evan Chengeb9f8922008-08-30 02:03:58 +00004114
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00004115 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00004116 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00004117 N1.getOperand(0).getOpcode() == ISD::AND &&
4118 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00004119 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00004120 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00004121 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00004122 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00004123 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004124 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004125 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0,
4126 DAG.getNode(ISD::AND, SDLoc(N),
Bill Wendling88103372009-01-30 21:37:17 +00004127 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00004128 DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004129 SDLoc(N),
Bill Wendling9729c5a2009-01-31 03:12:48 +00004130 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00004131 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00004132 }
4133 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004134
Chris Lattner61a4c072007-04-18 03:06:49 +00004135 // fold operands of srl based on knowledge that the low bits are not
4136 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00004137 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
4138 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004139
Evan Cheng9ab2b982009-12-18 21:31:31 +00004140 if (N1C) {
4141 SDValue NewSRL = visitShiftByConstant(N, N1C->getZExtValue());
4142 if (NewSRL.getNode())
4143 return NewSRL;
4144 }
4145
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004146 // Attempt to convert a srl of a load into a narrower zero-extending load.
4147 SDValue NarrowLoad = ReduceLoadWidth(N);
4148 if (NarrowLoad.getNode())
4149 return NarrowLoad;
4150
Evan Cheng9ab2b982009-12-18 21:31:31 +00004151 // Here is a common situation. We want to optimize:
4152 //
4153 // %a = ...
4154 // %b = and i32 %a, 2
4155 // %c = srl i32 %b, 1
4156 // brcond i32 %c ...
4157 //
4158 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004159 //
Evan Cheng9ab2b982009-12-18 21:31:31 +00004160 // %a = ...
4161 // %b = and %a, 2
4162 // %c = setcc eq %b, 0
4163 // brcond %c ...
4164 //
4165 // However when after the source operand of SRL is optimized into AND, the SRL
4166 // itself may not be optimized further. Look for it and add the BRCOND into
4167 // the worklist.
Evan Chengd40d03e2010-01-06 19:38:29 +00004168 if (N->hasOneUse()) {
4169 SDNode *Use = *N->use_begin();
4170 if (Use->getOpcode() == ISD::BRCOND)
4171 AddToWorkList(Use);
4172 else if (Use->getOpcode() == ISD::TRUNCATE && Use->hasOneUse()) {
4173 // Also look pass the truncate.
4174 Use = *Use->use_begin();
4175 if (Use->getOpcode() == ISD::BRCOND)
4176 AddToWorkList(Use);
4177 }
4178 }
Evan Cheng9ab2b982009-12-18 21:31:31 +00004179
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004180 return SDValue();
Evan Cheng4c26e932010-04-19 19:29:22 +00004181}
4182
Dan Gohman475871a2008-07-27 21:46:04 +00004183SDValue DAGCombiner::visitCTLZ(SDNode *N) {
4184 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004185 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004186
4187 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004188 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004189 return DAG.getNode(ISD::CTLZ, SDLoc(N), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004190 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004191}
4192
Chandler Carruth63974b22011-12-13 01:56:10 +00004193SDValue DAGCombiner::visitCTLZ_ZERO_UNDEF(SDNode *N) {
4194 SDValue N0 = N->getOperand(0);
4195 EVT VT = N->getValueType(0);
4196
4197 // fold (ctlz_zero_undef c1) -> c2
4198 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004199 return DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SDLoc(N), VT, N0);
Chandler Carruth63974b22011-12-13 01:56:10 +00004200 return SDValue();
4201}
4202
Dan Gohman475871a2008-07-27 21:46:04 +00004203SDValue DAGCombiner::visitCTTZ(SDNode *N) {
4204 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004205 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004206
Nate Begeman1d4d4142005-09-01 00:19:25 +00004207 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004208 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004209 return DAG.getNode(ISD::CTTZ, SDLoc(N), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004210 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004211}
4212
Chandler Carruth63974b22011-12-13 01:56:10 +00004213SDValue DAGCombiner::visitCTTZ_ZERO_UNDEF(SDNode *N) {
4214 SDValue N0 = N->getOperand(0);
4215 EVT VT = N->getValueType(0);
4216
4217 // fold (cttz_zero_undef c1) -> c2
4218 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004219 return DAG.getNode(ISD::CTTZ_ZERO_UNDEF, SDLoc(N), VT, N0);
Chandler Carruth63974b22011-12-13 01:56:10 +00004220 return SDValue();
4221}
4222
Dan Gohman475871a2008-07-27 21:46:04 +00004223SDValue DAGCombiner::visitCTPOP(SDNode *N) {
4224 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004225 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004226
Nate Begeman1d4d4142005-09-01 00:19:25 +00004227 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004228 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004229 return DAG.getNode(ISD::CTPOP, SDLoc(N), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004230 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004231}
4232
Dan Gohman475871a2008-07-27 21:46:04 +00004233SDValue DAGCombiner::visitSELECT(SDNode *N) {
4234 SDValue N0 = N->getOperand(0);
4235 SDValue N1 = N->getOperand(1);
4236 SDValue N2 = N->getOperand(2);
Nate Begeman452d7be2005-09-16 00:54:12 +00004237 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
4238 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4239 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Owen Andersone50ed302009-08-10 22:56:29 +00004240 EVT VT = N->getValueType(0);
4241 EVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00004242
Bill Wendling34584e62009-01-30 22:02:18 +00004243 // fold (select C, X, X) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00004244 if (N1 == N2)
4245 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00004246 // fold (select true, X, Y) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00004247 if (N0C && !N0C->isNullValue())
4248 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00004249 // fold (select false, X, Y) -> Y
Nate Begeman452d7be2005-09-16 00:54:12 +00004250 if (N0C && N0C->isNullValue())
4251 return N2;
Bill Wendling34584e62009-01-30 22:02:18 +00004252 // fold (select C, 1, X) -> (or C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004253 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004254 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2);
Bill Wendling34584e62009-01-30 22:02:18 +00004255 // fold (select C, 0, 1) -> (xor C, 1)
Bob Wilson67ba2232009-01-22 22:05:48 +00004256 if (VT.isInteger() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004257 (VT0 == MVT::i1 ||
Bob Wilson67ba2232009-01-22 22:05:48 +00004258 (VT0.isInteger() &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00004259 TLI.getBooleanContents(false) ==
4260 TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004261 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00004262 SDValue XORNode;
Evan Cheng571c4782007-08-18 05:57:05 +00004263 if (VT == VT0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004264 return DAG.getNode(ISD::XOR, SDLoc(N), VT0,
Bill Wendling34584e62009-01-30 22:02:18 +00004265 N0, DAG.getConstant(1, VT0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004266 XORNode = DAG.getNode(ISD::XOR, SDLoc(N0), VT0,
Bill Wendling34584e62009-01-30 22:02:18 +00004267 N0, DAG.getConstant(1, VT0));
Gabor Greifba36cb52008-08-28 21:40:38 +00004268 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00004269 if (VT.bitsGT(VT0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004270 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, XORNode);
4271 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, XORNode);
Evan Cheng571c4782007-08-18 05:57:05 +00004272 }
Bill Wendling34584e62009-01-30 22:02:18 +00004273 // fold (select C, 0, X) -> (and (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004274 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004275 SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00004276 AddToWorkList(NOTNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004277 return DAG.getNode(ISD::AND, SDLoc(N), VT, NOTNode, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00004278 }
Bill Wendling34584e62009-01-30 22:02:18 +00004279 // fold (select C, X, 1) -> (or (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004280 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004281 SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00004282 AddToWorkList(NOTNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004283 return DAG.getNode(ISD::OR, SDLoc(N), VT, NOTNode, N1);
Nate Begeman452d7be2005-09-16 00:54:12 +00004284 }
Bill Wendling34584e62009-01-30 22:02:18 +00004285 // fold (select C, X, 0) -> (and C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004286 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00004287 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, N1);
Bill Wendling34584e62009-01-30 22:02:18 +00004288 // fold (select X, X, Y) -> (or X, Y)
4289 // fold (select X, 1, Y) -> (or X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00004290 if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004291 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2);
Bill Wendling34584e62009-01-30 22:02:18 +00004292 // fold (select X, Y, X) -> (and X, Y)
4293 // fold (select X, Y, 0) -> (and X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00004294 if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004295 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004296
Chris Lattner40c62d52005-10-18 06:04:22 +00004297 // If we can fold this based on the true/false value, do so.
4298 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00004299 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004300
Nate Begeman44728a72005-09-19 22:34:01 +00004301 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004302 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004303 // FIXME:
Owen Anderson825b72b2009-08-11 20:47:22 +00004304 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
Nate Begeman750ac1b2006-02-01 07:19:44 +00004305 // having to say they don't support SELECT_CC on every type the DAG knows
4306 // about, since there is no way to mark an opcode illegal at all value types
Owen Anderson825b72b2009-08-11 20:47:22 +00004307 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) &&
Dan Gohman4ea48042009-08-02 16:19:38 +00004308 TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004309 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT,
Bill Wendling34584e62009-01-30 22:02:18 +00004310 N0.getOperand(0), N0.getOperand(1),
Nate Begeman750ac1b2006-02-01 07:19:44 +00004311 N1, N2, N0.getOperand(2));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004312 return SimplifySelect(SDLoc(N), N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004313 }
Bill Wendling34584e62009-01-30 22:02:18 +00004314
Dan Gohman475871a2008-07-27 21:46:04 +00004315 return SDValue();
Nate Begeman452d7be2005-09-16 00:54:12 +00004316}
4317
Benjamin Kramer6242fda2013-04-26 09:19:19 +00004318SDValue DAGCombiner::visitVSELECT(SDNode *N) {
4319 SDValue N0 = N->getOperand(0);
4320 SDValue N1 = N->getOperand(1);
4321 SDValue N2 = N->getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004322 SDLoc DL(N);
Benjamin Kramer6242fda2013-04-26 09:19:19 +00004323
4324 // Canonicalize integer abs.
4325 // vselect (setg[te] X, 0), X, -X ->
4326 // vselect (setgt X, -1), X, -X ->
4327 // vselect (setl[te] X, 0), -X, X ->
4328 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4329 if (N0.getOpcode() == ISD::SETCC) {
4330 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
4331 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
4332 bool isAbs = false;
4333 bool RHSIsAllZeros = ISD::isBuildVectorAllZeros(RHS.getNode());
4334
4335 if (((RHSIsAllZeros && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
4336 (ISD::isBuildVectorAllOnes(RHS.getNode()) && CC == ISD::SETGT)) &&
4337 N1 == LHS && N2.getOpcode() == ISD::SUB && N1 == N2.getOperand(1))
4338 isAbs = ISD::isBuildVectorAllZeros(N2.getOperand(0).getNode());
4339 else if ((RHSIsAllZeros && (CC == ISD::SETLT || CC == ISD::SETLE)) &&
4340 N2 == LHS && N1.getOpcode() == ISD::SUB && N2 == N1.getOperand(1))
4341 isAbs = ISD::isBuildVectorAllZeros(N1.getOperand(0).getNode());
4342
4343 if (isAbs) {
4344 EVT VT = LHS.getValueType();
4345 SDValue Shift = DAG.getNode(
4346 ISD::SRA, DL, VT, LHS,
4347 DAG.getConstant(VT.getScalarType().getSizeInBits() - 1, VT));
4348 SDValue Add = DAG.getNode(ISD::ADD, DL, VT, LHS, Shift);
4349 AddToWorkList(Shift.getNode());
4350 AddToWorkList(Add.getNode());
4351 return DAG.getNode(ISD::XOR, DL, VT, Add, Shift);
4352 }
4353 }
4354
4355 return SDValue();
4356}
4357
Dan Gohman475871a2008-07-27 21:46:04 +00004358SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
4359 SDValue N0 = N->getOperand(0);
4360 SDValue N1 = N->getOperand(1);
4361 SDValue N2 = N->getOperand(2);
4362 SDValue N3 = N->getOperand(3);
4363 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00004364 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00004365
Nate Begeman44728a72005-09-19 22:34:01 +00004366 // fold select_cc lhs, rhs, x, x, cc -> x
4367 if (N2 == N3)
4368 return N2;
Scott Michelfdc40a02009-02-17 22:15:04 +00004369
Chris Lattner5f42a242006-09-20 06:19:26 +00004370 // Determine if the condition we're dealing with is constant
Matt Arsenault225ed702013-05-18 00:21:46 +00004371 SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004372 N0, N1, CC, SDLoc(N), false);
Stephen Lin7e6d6202013-06-15 04:03:33 +00004373 if (SCC.getNode()) {
4374 AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00004375
Stephen Lin7e6d6202013-06-15 04:03:33 +00004376 if (ConstantSDNode *SCCC = dyn_cast<ConstantSDNode>(SCC.getNode())) {
4377 if (!SCCC->isNullValue())
4378 return N2; // cond always true -> true val
4379 else
4380 return N3; // cond always false -> false val
4381 }
4382
4383 // Fold to a simpler select_cc
4384 if (SCC.getOpcode() == ISD::SETCC)
4385 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N2.getValueType(),
4386 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
4387 SCC.getOperand(2));
Chris Lattner5f42a242006-09-20 06:19:26 +00004388 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004389
Chris Lattner40c62d52005-10-18 06:04:22 +00004390 // If we can fold this based on the true/false value, do so.
4391 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00004392 return SDValue(N, 0); // Don't revisit N.
Scott Michelfdc40a02009-02-17 22:15:04 +00004393
Nate Begeman44728a72005-09-19 22:34:01 +00004394 // fold select_cc into other things, such as min/max/abs
Andrew Trickac6d9be2013-05-25 02:42:55 +00004395 return SimplifySelectCC(SDLoc(N), N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00004396}
4397
Dan Gohman475871a2008-07-27 21:46:04 +00004398SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7be2005-09-16 00:54:12 +00004399 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00004400 cast<CondCodeSDNode>(N->getOperand(2))->get(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004401 SDLoc(N));
Nate Begeman452d7be2005-09-16 00:54:12 +00004402}
4403
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004404// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
Dan Gohman57fc82d2009-04-09 03:51:29 +00004405// "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))"
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004406// transformation. Returns true if extension are possible and the above
Scott Michelfdc40a02009-02-17 22:15:04 +00004407// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00004408static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004409 unsigned ExtOpc,
Craig Toppera0ec3f92013-07-14 04:42:23 +00004410 SmallVectorImpl<SDNode *> &ExtendNodes,
Dan Gohman79ce2762009-01-15 19:20:50 +00004411 const TargetLowering &TLI) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004412 bool HasCopyToRegUses = false;
4413 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00004414 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
4415 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004416 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00004417 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004418 if (User == N)
4419 continue;
Dan Gohman57fc82d2009-04-09 03:51:29 +00004420 if (UI.getUse().getResNo() != N0.getResNo())
4421 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004422 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
Dan Gohman57fc82d2009-04-09 03:51:29 +00004423 if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004424 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
4425 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
4426 // Sign bits will be lost after a zext.
4427 return false;
4428 bool Add = false;
4429 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004430 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004431 if (UseOp == N0)
4432 continue;
4433 if (!isa<ConstantSDNode>(UseOp))
4434 return false;
4435 Add = true;
4436 }
4437 if (Add)
4438 ExtendNodes.push_back(User);
Dan Gohman57fc82d2009-04-09 03:51:29 +00004439 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004440 }
Dan Gohman57fc82d2009-04-09 03:51:29 +00004441 // If truncates aren't free and there are users we can't
4442 // extend, it isn't worthwhile.
4443 if (!isTruncFree)
4444 return false;
4445 // Remember if this value is live-out.
4446 if (User->getOpcode() == ISD::CopyToReg)
4447 HasCopyToRegUses = true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004448 }
4449
4450 if (HasCopyToRegUses) {
4451 bool BothLiveOut = false;
4452 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
4453 UI != UE; ++UI) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00004454 SDUse &Use = UI.getUse();
4455 if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) {
4456 BothLiveOut = true;
4457 break;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004458 }
4459 }
4460 if (BothLiveOut)
4461 // Both unextended and extended values are live out. There had better be
Bob Wilsonbebfbc52010-11-28 06:51:19 +00004462 // a good reason for the transformation.
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004463 return ExtendNodes.size();
4464 }
4465 return true;
4466}
4467
Craig Topper6c64fba2013-07-13 07:43:40 +00004468void DAGCombiner::ExtendSetCCUses(const SmallVectorImpl<SDNode *> &SetCCs,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004469 SDValue Trunc, SDValue ExtLoad, SDLoc DL,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004470 ISD::NodeType ExtType) {
4471 // Extend SetCC uses if necessary.
4472 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
4473 SDNode *SetCC = SetCCs[i];
4474 SmallVector<SDValue, 4> Ops;
4475
4476 for (unsigned j = 0; j != 2; ++j) {
4477 SDValue SOp = SetCC->getOperand(j);
4478 if (SOp == Trunc)
4479 Ops.push_back(ExtLoad);
4480 else
4481 Ops.push_back(DAG.getNode(ExtType, DL, ExtLoad->getValueType(0), SOp));
4482 }
4483
4484 Ops.push_back(SetCC->getOperand(2));
4485 CombineTo(SetCC, DAG.getNode(ISD::SETCC, DL, SetCC->getValueType(0),
4486 &Ops[0], Ops.size()));
4487 }
4488}
4489
Dan Gohman475871a2008-07-27 21:46:04 +00004490SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
4491 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004492 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004493
Nate Begeman1d4d4142005-09-01 00:19:25 +00004494 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004495 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004496 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004497
Nadav Rotem0c8607b2013-01-20 08:35:56 +00004498 // fold (sext (sext x)) -> (sext x)
4499 // fold (sext (aext x)) -> (sext x)
4500 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004501 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT,
Nadav Rotem0c8607b2013-01-20 08:35:56 +00004502 N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004503
Chris Lattner22558872007-02-26 03:13:59 +00004504 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004505 // fold (sext (truncate (load x))) -> (sext (smaller load x))
4506 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004507 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4508 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004509 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4510 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004511 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004512 // CombineTo deleted the truncate, if needed, but not what's under it.
4513 AddToWorkList(oye);
4514 }
Dan Gohmanc7b34442009-04-27 02:00:55 +00004515 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004516 }
Evan Chengc88138f2007-03-22 01:54:19 +00004517
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004518 // See if the value being truncated is already sign extended. If so, just
4519 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00004520 SDValue Op = N0.getOperand(0);
Dan Gohmand1996362010-01-09 02:13:55 +00004521 unsigned OpBits = Op.getValueType().getScalarType().getSizeInBits();
4522 unsigned MidBits = N0.getValueType().getScalarType().getSizeInBits();
4523 unsigned DestBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00004524 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004525
Chris Lattner22558872007-02-26 03:13:59 +00004526 if (OpBits == DestBits) {
4527 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
4528 // bits, it is already ready.
4529 if (NumSignBits > DestBits-MidBits)
4530 return Op;
4531 } else if (OpBits < DestBits) {
4532 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
4533 // bits, just sext from i32.
4534 if (NumSignBits > OpBits-MidBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004535 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, Op);
Chris Lattner22558872007-02-26 03:13:59 +00004536 } else {
4537 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
4538 // bits, just truncate to i32.
4539 if (NumSignBits > OpBits-MidBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004540 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00004541 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004542
Chris Lattner22558872007-02-26 03:13:59 +00004543 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sands25cf2272008-11-24 14:53:14 +00004544 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
4545 N0.getValueType())) {
Dan Gohmand1996362010-01-09 02:13:55 +00004546 if (OpBits < DestBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004547 Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N0), VT, Op);
Dan Gohmand1996362010-01-09 02:13:55 +00004548 else if (OpBits > DestBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004549 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT, Op);
4550 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, Op,
Dan Gohmand1996362010-01-09 02:13:55 +00004551 DAG.getValueType(N0.getValueType()));
Chris Lattner22558872007-02-26 03:13:59 +00004552 }
Chris Lattner6007b842006-09-21 06:00:20 +00004553 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004554
Evan Cheng110dec22005-12-14 02:19:23 +00004555 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004556 // None of the supported targets knows how to perform load and sign extend
Nadav Rotemfcd96192011-02-27 07:40:43 +00004557 // on vectors in one instruction. We only perform this transformation on
4558 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004559 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004560 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004561 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004562 bool DoXform = true;
4563 SmallVector<SDNode*, 4> SetCCs;
4564 if (!N0.hasOneUse())
4565 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
4566 if (DoXform) {
4567 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004568 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00004569 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004570 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004571 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004572 LN0->isVolatile(), LN0->isNonTemporal(),
4573 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004574 CombineTo(N, ExtLoad);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004575 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004576 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004577 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004578 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004579 ISD::SIGN_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004580 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004581 }
Nate Begeman3df4d522005-10-12 20:40:40 +00004582 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004583
4584 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
4585 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004586 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4587 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004588 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004589 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004590 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004591 TLI.isLoadExtLegal(ISD::SEXTLOAD, MemVT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004592 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004593 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004594 LN0->getBasePtr(), LN0->getPointerInfo(),
4595 MemVT,
David Greene1e559442010-02-15 17:00:31 +00004596 LN0->isVolatile(), LN0->isNonTemporal(),
4597 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004598 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004599 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004600 DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004601 N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004602 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004603 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004604 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004605 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004606
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004607 // fold (sext (and/or/xor (load x), cst)) ->
4608 // (and/or/xor (sextload x), (sext cst))
4609 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4610 N0.getOpcode() == ISD::XOR) &&
4611 isa<LoadSDNode>(N0.getOperand(0)) &&
4612 N0.getOperand(1).getOpcode() == ISD::Constant &&
4613 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()) &&
4614 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4615 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4616 if (LN0->getExtensionType() != ISD::ZEXTLOAD) {
4617 bool DoXform = true;
4618 SmallVector<SDNode*, 4> SetCCs;
4619 if (!N0.hasOneUse())
4620 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::SIGN_EXTEND,
4621 SetCCs, TLI);
4622 if (DoXform) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004623 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(LN0), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004624 LN0->getChain(), LN0->getBasePtr(),
4625 LN0->getPointerInfo(),
4626 LN0->getMemoryVT(),
4627 LN0->isVolatile(),
4628 LN0->isNonTemporal(),
4629 LN0->getAlignment());
4630 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4631 Mask = Mask.sext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004632 SDValue And = DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004633 ExtLoad, DAG.getConstant(Mask, VT));
4634 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004635 SDLoc(N0.getOperand(0)),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004636 N0.getOperand(0).getValueType(), ExtLoad);
4637 CombineTo(N, And);
4638 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004639 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004640 ISD::SIGN_EXTEND);
4641 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4642 }
4643 }
4644 }
4645
Chris Lattner20a35c32007-04-11 05:32:27 +00004646 if (N0.getOpcode() == ISD::SETCC) {
Chris Lattner2b7a2712009-07-08 00:31:33 +00004647 // sext(setcc) -> sext_in_reg(vsetcc) for vectors.
Dan Gohman3ce89f42010-04-30 17:19:19 +00004648 // Only do this before legalize for now.
Owen Andersoned5707b2013-04-23 18:09:28 +00004649 if (VT.isVector() && !LegalOperations &&
Stephen Lin155615d2013-07-08 00:37:03 +00004650 TLI.getBooleanContents(true) ==
Owen Andersoned5707b2013-04-23 18:09:28 +00004651 TargetLowering::ZeroOrNegativeOneBooleanContent) {
Dan Gohman3ce89f42010-04-30 17:19:19 +00004652 EVT N0VT = N0.getOperand(0).getValueType();
Nadav Rotem2e506192012-04-11 08:26:11 +00004653 // On some architectures (such as SSE/NEON/etc) the SETCC result type is
4654 // of the same size as the compared operands. Only optimize sext(setcc())
4655 // if this is the case.
Matt Arsenault225ed702013-05-18 00:21:46 +00004656 EVT SVT = getSetCCResultType(N0VT);
Nadav Rotem2e506192012-04-11 08:26:11 +00004657
4658 // We know that the # elements of the results is the same as the
4659 // # elements of the compare (and the # elements of the compare result
4660 // for that matter). Check to see that they are the same size. If so,
4661 // we know that the element size of the sext'd result matches the
4662 // element size of the compare operands.
4663 if (VT.getSizeInBits() == SVT.getSizeInBits())
Andrew Trickac6d9be2013-05-25 02:42:55 +00004664 return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00004665 N0.getOperand(1),
4666 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Matt Arsenault9aa8fdf2013-05-17 21:43:43 +00004667
Dan Gohman3ce89f42010-04-30 17:19:19 +00004668 // If the desired elements are smaller or larger than the source
4669 // elements we can use a matching integer vector type and then
4670 // truncate/sign extend
Matt Arsenault9aa8fdf2013-05-17 21:43:43 +00004671 EVT MatchingVectorType = N0VT.changeVectorElementTypeToInteger();
Craig Topper0eb5dad2012-09-29 07:18:53 +00004672 if (SVT == MatchingVectorType) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004673 SDValue VsetCC = DAG.getSetCC(SDLoc(N), MatchingVectorType,
Craig Topper0eb5dad2012-09-29 07:18:53 +00004674 N0.getOperand(0), N0.getOperand(1),
4675 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004676 return DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT);
Dan Gohman3ce89f42010-04-30 17:19:19 +00004677 }
Chris Lattner2b7a2712009-07-08 00:31:33 +00004678 }
Dan Gohman3ce89f42010-04-30 17:19:19 +00004679
Chris Lattner2b7a2712009-07-08 00:31:33 +00004680 // sext(setcc x, y, cc) -> (select_cc x, y, -1, 0, cc)
Dan Gohmana7bcef12010-04-24 01:17:30 +00004681 unsigned ElementWidth = VT.getScalarType().getSizeInBits();
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004682 SDValue NegOne =
Dan Gohmana7bcef12010-04-24 01:17:30 +00004683 DAG.getConstant(APInt::getAllOnesValue(ElementWidth), VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004684 SDValue SCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00004685 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004686 NegOne, DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004687 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004688 if (SCC.getNode()) return SCC;
Matt Arsenaultb05e4772013-06-14 22:04:37 +00004689 if (!VT.isVector() &&
4690 (!LegalOperations ||
4691 TLI.isOperationLegal(ISD::SETCC, getSetCCResultType(VT)))) {
4692 return DAG.getSelect(SDLoc(N), VT,
4693 DAG.getSetCC(SDLoc(N),
4694 getSetCCResultType(VT),
4695 N0.getOperand(0), N0.getOperand(1),
4696 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
4697 NegOne, DAG.getConstant(0, VT));
4698 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004699 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004700
Dan Gohman8f0ad582008-04-28 16:58:24 +00004701 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sands25cf2272008-11-24 14:53:14 +00004702 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman187db7b2008-04-28 18:47:17 +00004703 DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004704 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004705
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004706 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004707}
4708
Rafael Espindoladecbc432012-04-09 16:06:03 +00004709// isTruncateOf - If N is a truncate of some other value, return true, record
4710// the value being truncated in Op and which of Op's bits are zero in KnownZero.
4711// This function computes KnownZero to avoid a duplicated call to
4712// ComputeMaskedBits in the caller.
4713static bool isTruncateOf(SelectionDAG &DAG, SDValue N, SDValue &Op,
4714 APInt &KnownZero) {
4715 APInt KnownOne;
4716 if (N->getOpcode() == ISD::TRUNCATE) {
4717 Op = N->getOperand(0);
4718 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4719 return true;
4720 }
4721
4722 if (N->getOpcode() != ISD::SETCC || N->getValueType(0) != MVT::i1 ||
4723 cast<CondCodeSDNode>(N->getOperand(2))->get() != ISD::SETNE)
4724 return false;
4725
4726 SDValue Op0 = N->getOperand(0);
4727 SDValue Op1 = N->getOperand(1);
4728 assert(Op0.getValueType() == Op1.getValueType());
4729
4730 ConstantSDNode *COp0 = dyn_cast<ConstantSDNode>(Op0);
4731 ConstantSDNode *COp1 = dyn_cast<ConstantSDNode>(Op1);
Rafael Espindolafdb230a2012-04-10 00:16:22 +00004732 if (COp0 && COp0->isNullValue())
Rafael Espindoladecbc432012-04-09 16:06:03 +00004733 Op = Op1;
Rafael Espindolafdb230a2012-04-10 00:16:22 +00004734 else if (COp1 && COp1->isNullValue())
Rafael Espindoladecbc432012-04-09 16:06:03 +00004735 Op = Op0;
4736 else
4737 return false;
4738
4739 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4740
4741 if (!(KnownZero | APInt(Op.getValueSizeInBits(), 1)).isAllOnesValue())
4742 return false;
4743
4744 return true;
4745}
4746
Dan Gohman475871a2008-07-27 21:46:04 +00004747SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
4748 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004749 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004750
Nate Begeman1d4d4142005-09-01 00:19:25 +00004751 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004752 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004753 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004754 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00004755 // fold (zext (aext x)) -> (zext x)
4756 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004757 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004758 N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00004759
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004760 // fold (zext (truncate x)) -> (zext x) or
4761 // (zext (truncate x)) -> (truncate x)
4762 // This is valid when the truncated bits of x are already zero.
4763 // FIXME: We should extend this to work for vectors too.
Rafael Espindoladecbc432012-04-09 16:06:03 +00004764 SDValue Op;
4765 APInt KnownZero;
4766 if (!VT.isVector() && isTruncateOf(DAG, N0, Op, KnownZero)) {
4767 APInt TruncatedBits =
4768 (Op.getValueSizeInBits() == N0.getValueSizeInBits()) ?
4769 APInt(Op.getValueSizeInBits(), 0) :
4770 APInt::getBitsSet(Op.getValueSizeInBits(),
4771 N0.getValueSizeInBits(),
4772 std::min(Op.getValueSizeInBits(),
4773 VT.getSizeInBits()));
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00004774 if (TruncatedBits == (KnownZero & TruncatedBits)) {
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004775 if (VT.bitsGT(Op.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004776 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, Op);
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004777 if (VT.bitsLT(Op.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004778 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004779
4780 return Op;
4781 }
4782 }
4783
Evan Chengc88138f2007-03-22 01:54:19 +00004784 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4785 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00004786 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004787 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4788 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004789 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4790 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004791 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004792 // CombineTo deleted the truncate, if needed, but not what's under it.
4793 AddToWorkList(oye);
4794 }
Eli Friedmane545d382011-04-16 23:25:34 +00004795 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004796 }
Evan Chengc88138f2007-03-22 01:54:19 +00004797 }
4798
Chris Lattner6007b842006-09-21 06:00:20 +00004799 // fold (zext (truncate x)) -> (and x, mask)
4800 if (N0.getOpcode() == ISD::TRUNCATE &&
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004801 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman394d6292010-11-03 01:47:46 +00004802
4803 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4804 // fold (zext (truncate (srl (load x), c))) -> (zext (smaller load (x+c/n)))
4805 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4806 if (NarrowLoad.getNode()) {
4807 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4808 if (NarrowLoad.getNode() != N0.getNode()) {
4809 CombineTo(N0.getNode(), NarrowLoad);
4810 // CombineTo deleted the truncate, if needed, but not what's under it.
4811 AddToWorkList(oye);
4812 }
4813 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4814 }
4815
Dan Gohman475871a2008-07-27 21:46:04 +00004816 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004817 if (Op.getValueType().bitsLT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004818 Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00004819 AddToWorkList(Op.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00004820 } else if (Op.getValueType().bitsGT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004821 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00004822 AddToWorkList(Op.getNode());
Chris Lattner6007b842006-09-21 06:00:20 +00004823 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00004824 return DAG.getZeroExtendInReg(Op, SDLoc(N),
Dan Gohman87862e72009-12-11 21:31:27 +00004825 N0.getValueType().getScalarType());
Chris Lattner6007b842006-09-21 06:00:20 +00004826 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004827
Dan Gohman97121ba2009-04-08 00:15:30 +00004828 // Fold (zext (and (trunc x), cst)) -> (and x, cst),
4829 // if either of the casts is not free.
Chris Lattner111c2282006-09-21 06:14:31 +00004830 if (N0.getOpcode() == ISD::AND &&
4831 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00004832 N0.getOperand(1).getOpcode() == ISD::Constant &&
4833 (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
4834 N0.getValueType()) ||
4835 !TLI.isZExtFree(N0.getValueType(), VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00004836 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004837 if (X.getValueType().bitsLT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004838 X = DAG.getNode(ISD::ANY_EXTEND, SDLoc(X), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004839 } else if (X.getValueType().bitsGT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004840 X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X);
Chris Lattner111c2282006-09-21 06:14:31 +00004841 }
Dan Gohman220a8232008-03-03 23:51:38 +00004842 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004843 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004844 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004845 X, DAG.getConstant(Mask, VT));
Chris Lattner111c2282006-09-21 06:14:31 +00004846 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004847
Evan Cheng110dec22005-12-14 02:19:23 +00004848 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Nadav Rotemed9b9342011-02-20 12:37:50 +00004849 // None of the supported targets knows how to perform load and vector_zext
Nadav Rotemfcd96192011-02-27 07:40:43 +00004850 // on vectors in one instruction. We only perform this transformation on
4851 // scalars.
Nadav Rotemed9b9342011-02-20 12:37:50 +00004852 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004853 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004854 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004855 bool DoXform = true;
4856 SmallVector<SDNode*, 4> SetCCs;
4857 if (!N0.hasOneUse())
4858 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
4859 if (DoXform) {
4860 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004861 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004862 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004863 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004864 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004865 LN0->isVolatile(), LN0->isNonTemporal(),
4866 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004867 CombineTo(N, ExtLoad);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004868 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004869 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004870 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling6ce610f2009-01-30 22:23:15 +00004871
Andrew Trickac6d9be2013-05-25 02:42:55 +00004872 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004873 ISD::ZERO_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004874 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004875 }
Evan Cheng110dec22005-12-14 02:19:23 +00004876 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004877
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004878 // fold (zext (and/or/xor (load x), cst)) ->
4879 // (and/or/xor (zextload x), (zext cst))
4880 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4881 N0.getOpcode() == ISD::XOR) &&
4882 isa<LoadSDNode>(N0.getOperand(0)) &&
4883 N0.getOperand(1).getOpcode() == ISD::Constant &&
4884 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()) &&
4885 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4886 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4887 if (LN0->getExtensionType() != ISD::SEXTLOAD) {
4888 bool DoXform = true;
4889 SmallVector<SDNode*, 4> SetCCs;
4890 if (!N0.hasOneUse())
4891 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::ZERO_EXTEND,
4892 SetCCs, TLI);
4893 if (DoXform) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004894 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004895 LN0->getChain(), LN0->getBasePtr(),
4896 LN0->getPointerInfo(),
4897 LN0->getMemoryVT(),
4898 LN0->isVolatile(),
4899 LN0->isNonTemporal(),
4900 LN0->getAlignment());
4901 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4902 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004903 SDValue And = DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004904 ExtLoad, DAG.getConstant(Mask, VT));
4905 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004906 SDLoc(N0.getOperand(0)),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004907 N0.getOperand(0).getValueType(), ExtLoad);
4908 CombineTo(N, And);
4909 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004910 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004911 ISD::ZERO_EXTEND);
4912 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4913 }
4914 }
4915 }
4916
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004917 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
4918 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004919 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4920 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004921 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004922 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004923 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004924 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004925 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004926 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004927 LN0->getBasePtr(), LN0->getPointerInfo(),
4928 MemVT,
David Greene1e559442010-02-15 17:00:31 +00004929 LN0->isVolatile(), LN0->isNonTemporal(),
4930 LN0->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004931 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004932 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004933 DAG.getNode(ISD::TRUNCATE, SDLoc(N0), N0.getValueType(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004934 ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004935 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004936 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004937 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004938 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004939
Chris Lattner20a35c32007-04-11 05:32:27 +00004940 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00004941 if (!LegalOperations && VT.isVector()) {
4942 // zext(setcc) -> (and (vsetcc), (1, 1, ...) for vectors.
4943 // Only do this before legalize for now.
4944 EVT N0VT = N0.getOperand(0).getValueType();
4945 EVT EltVT = VT.getVectorElementType();
4946 SmallVector<SDValue,8> OneOps(VT.getVectorNumElements(),
4947 DAG.getConstant(1, EltVT));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004948 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Evan Cheng0a942db2010-05-19 01:08:17 +00004949 // We know that the # elements of the results is the same as the
4950 // # elements of the compare (and the # elements of the compare result
4951 // for that matter). Check to see that they are the same size. If so,
4952 // we know that the element size of the sext'd result matches the
4953 // element size of the compare operands.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004954 return DAG.getNode(ISD::AND, SDLoc(N), VT,
4955 DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Evan Cheng0a942db2010-05-19 01:08:17 +00004956 N0.getOperand(1),
4957 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004958 DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT,
Evan Cheng0a942db2010-05-19 01:08:17 +00004959 &OneOps[0], OneOps.size()));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004960
4961 // If the desired elements are smaller or larger than the source
4962 // elements we can use a matching integer vector type and then
4963 // truncate/sign extend
4964 EVT MatchingElementType =
4965 EVT::getIntegerVT(*DAG.getContext(),
4966 N0VT.getScalarType().getSizeInBits());
4967 EVT MatchingVectorType =
4968 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
4969 N0VT.getVectorNumElements());
4970 SDValue VsetCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00004971 DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0),
Dan Gohman71dc7c92011-05-17 22:20:36 +00004972 N0.getOperand(1),
4973 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004974 return DAG.getNode(ISD::AND, SDLoc(N), VT,
4975 DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT),
4976 DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT,
Dan Gohman71dc7c92011-05-17 22:20:36 +00004977 &OneOps[0], OneOps.size()));
Evan Cheng0a942db2010-05-19 01:08:17 +00004978 }
4979
4980 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00004981 SDValue SCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00004982 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Chris Lattner20a35c32007-04-11 05:32:27 +00004983 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004984 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004985 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00004986 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004987
Evan Cheng9818c042009-12-15 03:00:32 +00004988 // (zext (shl (zext x), cst)) -> (shl (zext x), cst)
Evan Cheng99b653c2009-12-15 00:41:36 +00004989 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL) &&
Evan Cheng9818c042009-12-15 03:00:32 +00004990 isa<ConstantSDNode>(N0.getOperand(1)) &&
Evan Cheng99b653c2009-12-15 00:41:36 +00004991 N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND &&
4992 N0.hasOneUse()) {
Chris Lattnere0751182011-02-13 19:09:16 +00004993 SDValue ShAmt = N0.getOperand(1);
4994 unsigned ShAmtVal = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Evan Cheng9818c042009-12-15 03:00:32 +00004995 if (N0.getOpcode() == ISD::SHL) {
Chris Lattnere0751182011-02-13 19:09:16 +00004996 SDValue InnerZExt = N0.getOperand(0);
Evan Cheng9818c042009-12-15 03:00:32 +00004997 // If the original shl may be shifting out bits, do not perform this
4998 // transformation.
Chris Lattnere0751182011-02-13 19:09:16 +00004999 unsigned KnownZeroBits = InnerZExt.getValueType().getSizeInBits() -
5000 InnerZExt.getOperand(0).getValueType().getSizeInBits();
5001 if (ShAmtVal > KnownZeroBits)
Evan Cheng9818c042009-12-15 03:00:32 +00005002 return SDValue();
5003 }
Chris Lattnere0751182011-02-13 19:09:16 +00005004
Andrew Trickac6d9be2013-05-25 02:42:55 +00005005 SDLoc DL(N);
Owen Anderson95771af2011-02-25 21:41:48 +00005006
5007 // Ensure that the shift amount is wide enough for the shifted value.
Chris Lattnere0751182011-02-13 19:09:16 +00005008 if (VT.getSizeInBits() >= 256)
5009 ShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, ShAmt);
Owen Anderson95771af2011-02-25 21:41:48 +00005010
Chris Lattnere0751182011-02-13 19:09:16 +00005011 return DAG.getNode(N0.getOpcode(), DL, VT,
5012 DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)),
5013 ShAmt);
Evan Cheng99b653c2009-12-15 00:41:36 +00005014 }
5015
Evan Chengb3a3d5e2010-04-28 07:10:39 +00005016 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005017}
5018
Dan Gohman475871a2008-07-27 21:46:04 +00005019SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
5020 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005021 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005022
Chris Lattner5ffc0662006-05-05 05:58:59 +00005023 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00005024 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005025 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, N0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00005026 // fold (aext (aext x)) -> (aext x)
5027 // fold (aext (zext x)) -> (zext x)
5028 // fold (aext (sext x)) -> (sext x)
5029 if (N0.getOpcode() == ISD::ANY_EXTEND ||
5030 N0.getOpcode() == ISD::ZERO_EXTEND ||
5031 N0.getOpcode() == ISD::SIGN_EXTEND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005032 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00005033
Evan Chengc88138f2007-03-22 01:54:19 +00005034 // fold (aext (truncate (load x))) -> (aext (smaller load x))
5035 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
5036 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005037 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
5038 if (NarrowLoad.getNode()) {
Dale Johannesen86234c32010-05-25 18:47:23 +00005039 SDNode* oye = N0.getNode()->getOperand(0).getNode();
5040 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005041 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen86234c32010-05-25 18:47:23 +00005042 // CombineTo deleted the truncate, if needed, but not what's under it.
5043 AddToWorkList(oye);
5044 }
Eli Friedmane545d382011-04-16 23:25:34 +00005045 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00005046 }
Evan Chengc88138f2007-03-22 01:54:19 +00005047 }
5048
Chris Lattner84750582006-09-20 06:29:17 +00005049 // fold (aext (truncate x))
5050 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00005051 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00005052 if (TruncOp.getValueType() == VT)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005053 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005054 if (TruncOp.getValueType().bitsGT(VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005055 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, TruncOp);
5056 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, TruncOp);
Chris Lattner84750582006-09-20 06:29:17 +00005057 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005058
Dan Gohman97121ba2009-04-08 00:15:30 +00005059 // Fold (aext (and (trunc x), cst)) -> (and x, cst)
5060 // if the trunc is not free.
Chris Lattner0e4b9222006-09-21 06:40:43 +00005061 if (N0.getOpcode() == ISD::AND &&
5062 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00005063 N0.getOperand(1).getOpcode() == ISD::Constant &&
5064 !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
5065 N0.getValueType())) {
Dan Gohman475871a2008-07-27 21:46:04 +00005066 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005067 if (X.getValueType().bitsLT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005068 X = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005069 } else if (X.getValueType().bitsGT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005070 X = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, X);
Chris Lattner0e4b9222006-09-21 06:40:43 +00005071 }
Dan Gohman220a8232008-03-03 23:51:38 +00005072 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00005073 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005074 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling683c9572009-01-30 22:27:33 +00005075 X, DAG.getConstant(Mask, VT));
Chris Lattner0e4b9222006-09-21 06:40:43 +00005076 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005077
Chris Lattner5ffc0662006-05-05 05:58:59 +00005078 // fold (aext (load x)) -> (aext (truncate (extload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005079 // None of the supported targets knows how to perform load and any_ext
Nadav Rotemfcd96192011-02-27 07:40:43 +00005080 // on vectors in one instruction. We only perform this transformation on
5081 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005082 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005083 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005084 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00005085 bool DoXform = true;
5086 SmallVector<SDNode*, 4> SetCCs;
5087 if (!N0.hasOneUse())
5088 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI);
5089 if (DoXform) {
5090 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005091 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00005092 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005093 LN0->getBasePtr(), LN0->getPointerInfo(),
Dan Gohman57fc82d2009-04-09 03:51:29 +00005094 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00005095 LN0->isVolatile(), LN0->isNonTemporal(),
5096 LN0->getAlignment());
Dan Gohman57fc82d2009-04-09 03:51:29 +00005097 CombineTo(N, ExtLoad);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005098 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Dan Gohman57fc82d2009-04-09 03:51:29 +00005099 N0.getValueType(), ExtLoad);
5100 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005101 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00005102 ISD::ANY_EXTEND);
Dan Gohman57fc82d2009-04-09 03:51:29 +00005103 return SDValue(N, 0); // Return N so it doesn't get rechecked!
5104 }
Chris Lattner5ffc0662006-05-05 05:58:59 +00005105 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005106
Chris Lattner5ffc0662006-05-05 05:58:59 +00005107 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
5108 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
5109 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00005110 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005111 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00005112 N0.hasOneUse()) {
5113 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005114 EVT MemVT = LN0->getMemoryVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005115 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), SDLoc(N),
Stuart Hastingsa9011292011-02-16 16:23:55 +00005116 VT, LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005117 LN0->getPointerInfo(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00005118 LN0->isVolatile(), LN0->isNonTemporal(),
5119 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00005120 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00005121 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005122 DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling683c9572009-01-30 22:27:33 +00005123 N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00005124 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005125 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00005126 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005127
Chris Lattner20a35c32007-04-11 05:32:27 +00005128 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00005129 // aext(setcc) -> sext_in_reg(vsetcc) for vectors.
5130 // Only do this before legalize for now.
5131 if (VT.isVector() && !LegalOperations) {
5132 EVT N0VT = N0.getOperand(0).getValueType();
5133 // We know that the # elements of the results is the same as the
5134 // # elements of the compare (and the # elements of the compare result
5135 // for that matter). Check to see that they are the same size. If so,
5136 // we know that the element size of the sext'd result matches the
5137 // element size of the compare operands.
5138 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Andrew Trickac6d9be2013-05-25 02:42:55 +00005139 return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00005140 N0.getOperand(1),
5141 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Evan Cheng0a942db2010-05-19 01:08:17 +00005142 // If the desired elements are smaller or larger than the source
5143 // elements we can use a matching integer vector type and then
5144 // truncate/sign extend
5145 else {
Duncan Sands34727662010-07-12 08:16:59 +00005146 EVT MatchingElementType =
5147 EVT::getIntegerVT(*DAG.getContext(),
5148 N0VT.getScalarType().getSizeInBits());
5149 EVT MatchingVectorType =
5150 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
5151 N0VT.getVectorNumElements());
5152 SDValue VsetCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00005153 DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00005154 N0.getOperand(1),
5155 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005156 return DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT);
Evan Cheng0a942db2010-05-19 01:08:17 +00005157 }
5158 }
5159
5160 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00005161 SDValue SCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00005162 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Chris Lattner1eba01e2007-04-11 06:50:51 +00005163 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00005164 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00005165 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005166 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00005167 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005168
Evan Chengb3a3d5e2010-04-28 07:10:39 +00005169 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00005170}
5171
Chris Lattner2b4c2792007-10-13 06:35:54 +00005172/// GetDemandedBits - See if the specified operand can be simplified with the
5173/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00005174/// simpler operand, otherwise return a null SDValue.
5175SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00005176 switch (V.getOpcode()) {
5177 default: break;
Lang Hames5207bf22011-11-08 18:56:23 +00005178 case ISD::Constant: {
5179 const ConstantSDNode *CV = cast<ConstantSDNode>(V.getNode());
5180 assert(CV != 0 && "Const value should be ConstSDNode.");
5181 const APInt &CVal = CV->getAPIntValue();
5182 APInt NewVal = CVal & Mask;
Stephen Linb4940152013-07-09 00:44:49 +00005183 if (NewVal != CVal)
Lang Hames5207bf22011-11-08 18:56:23 +00005184 return DAG.getConstant(NewVal, V.getValueType());
Lang Hames5207bf22011-11-08 18:56:23 +00005185 break;
5186 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00005187 case ISD::OR:
5188 case ISD::XOR:
5189 // If the LHS or RHS don't contribute bits to the or, drop them.
5190 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
5191 return V.getOperand(1);
5192 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
5193 return V.getOperand(0);
5194 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00005195 case ISD::SRL:
5196 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00005197 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00005198 break;
5199 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
5200 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005201 unsigned Amt = RHSC->getZExtValue();
Bill Wendling8509c902009-01-30 22:33:24 +00005202
Dan Gohmancc91d632009-01-03 19:22:06 +00005203 // Watch out for shift count overflow though.
5204 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005205 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00005206 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Bill Wendling8509c902009-01-30 22:33:24 +00005207 if (SimplifyLHS.getNode())
Andrew Trickac6d9be2013-05-25 02:42:55 +00005208 return DAG.getNode(ISD::SRL, SDLoc(V), V.getValueType(),
Chris Lattnere33544c2007-10-13 06:58:48 +00005209 SimplifyLHS, V.getOperand(1));
Chris Lattnere33544c2007-10-13 06:58:48 +00005210 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00005211 }
Dan Gohman475871a2008-07-27 21:46:04 +00005212 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00005213}
5214
Evan Chengc88138f2007-03-22 01:54:19 +00005215/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
5216/// bits and then truncated to a narrower type and where N is a multiple
5217/// of number of bits of the narrower type, transform it to a narrower load
5218/// from address + N / num of bits of new type. If the result is to be
5219/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00005220SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00005221 unsigned Opc = N->getOpcode();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005222
Evan Chengc88138f2007-03-22 01:54:19 +00005223 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00005224 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005225 EVT VT = N->getValueType(0);
5226 EVT ExtVT = VT;
Evan Chengc88138f2007-03-22 01:54:19 +00005227
Dan Gohman7f8613e2008-08-14 20:04:46 +00005228 // This transformation isn't valid for vector loads.
5229 if (VT.isVector())
5230 return SDValue();
5231
Dan Gohmand1996362010-01-09 02:13:55 +00005232 // Special case: SIGN_EXTEND_INREG is basically truncating to ExtVT then
Evan Chenge177e302007-03-23 22:13:36 +00005233 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00005234 if (Opc == ISD::SIGN_EXTEND_INREG) {
5235 ExtType = ISD::SEXTLOAD;
Owen Andersone50ed302009-08-10 22:56:29 +00005236 ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005237 } else if (Opc == ISD::SRL) {
Chris Lattner90b03642010-12-21 18:05:22 +00005238 // Another special-case: SRL is basically zero-extending a narrower value.
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005239 ExtType = ISD::ZEXTLOAD;
5240 N0 = SDValue(N, 0);
5241 ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1));
5242 if (!N01) return SDValue();
5243 ExtVT = EVT::getIntegerVT(*DAG.getContext(),
5244 VT.getSizeInBits() - N01->getZExtValue());
Evan Chengc88138f2007-03-22 01:54:19 +00005245 }
Richard Osborne4e3740e2011-01-31 17:41:44 +00005246 if (LegalOperations && !TLI.isLoadExtLegal(ExtType, ExtVT))
5247 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005248
Owen Andersone50ed302009-08-10 22:56:29 +00005249 unsigned EVTBits = ExtVT.getSizeInBits();
Owen Anderson95771af2011-02-25 21:41:48 +00005250
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005251 // Do not generate loads of non-round integer types since these can
5252 // be expensive (and would be wrong if the type is not byte sized).
5253 if (!ExtVT.isRound())
5254 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005255
Evan Chengc88138f2007-03-22 01:54:19 +00005256 unsigned ShAmt = 0;
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005257 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
Evan Chengc88138f2007-03-22 01:54:19 +00005258 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005259 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005260 // Is the shift amount a multiple of size of VT?
5261 if ((ShAmt & (EVTBits-1)) == 0) {
5262 N0 = N0.getOperand(0);
Eli Friedmand68eea22009-08-19 08:46:10 +00005263 // Is the load width a multiple of size of VT?
5264 if ((N0.getValueType().getSizeInBits() & (EVTBits-1)) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00005265 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005266 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005267
Chris Lattnercbf68df2010-12-22 08:02:57 +00005268 // At this point, we must have a load or else we can't do the transform.
5269 if (!isa<LoadSDNode>(N0)) return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005270
Chandler Carruth1c49fda2012-12-11 00:36:57 +00005271 // Because a SRL must be assumed to *need* to zero-extend the high bits
5272 // (as opposed to anyext the high bits), we can't combine the zextload
5273 // lowering of SRL and an sextload.
5274 if (cast<LoadSDNode>(N0)->getExtensionType() == ISD::SEXTLOAD)
5275 return SDValue();
5276
Chris Lattner2831a192010-10-01 05:36:09 +00005277 // If the shift amount is larger than the input type then we're not
5278 // accessing any of the loaded bytes. If the load was a zextload/extload
5279 // then the result of the shift+trunc is zero/undef (handled elsewhere).
Chris Lattnercbf68df2010-12-22 08:02:57 +00005280 if (ShAmt >= cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits())
Chris Lattner2831a192010-10-01 05:36:09 +00005281 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005282 }
5283 }
5284
Dan Gohman394d6292010-11-03 01:47:46 +00005285 // If the load is shifted left (and the result isn't shifted back right),
5286 // we can fold the truncate through the shift.
5287 unsigned ShLeftAmt = 0;
5288 if (ShAmt == 0 && N0.getOpcode() == ISD::SHL && N0.hasOneUse() &&
Chris Lattner4c32bc22010-12-22 07:36:50 +00005289 ExtVT == VT && TLI.isNarrowingProfitable(N0.getValueType(), VT)) {
Dan Gohman394d6292010-11-03 01:47:46 +00005290 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
5291 ShLeftAmt = N01->getZExtValue();
5292 N0 = N0.getOperand(0);
5293 }
5294 }
Owen Anderson95771af2011-02-25 21:41:48 +00005295
Chris Lattner4c32bc22010-12-22 07:36:50 +00005296 // If we haven't found a load, we can't narrow it. Don't transform one with
5297 // multiple uses, this would require adding a new load.
Bill Schmidt89e88e32013-01-14 22:04:38 +00005298 if (!isa<LoadSDNode>(N0) || !N0.hasOneUse())
5299 return SDValue();
5300
5301 // Don't change the width of a volatile load.
5302 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
5303 if (LN0->isVolatile())
Chris Lattner4c32bc22010-12-22 07:36:50 +00005304 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005305
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005306 // Verify that we are actually reducing a load width here.
Bill Schmidt89e88e32013-01-14 22:04:38 +00005307 if (LN0->getMemoryVT().getSizeInBits() < EVTBits)
Chris Lattner4c32bc22010-12-22 07:36:50 +00005308 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005309
Bill Schmidt89e88e32013-01-14 22:04:38 +00005310 // For the transform to be legal, the load must produce only two values
5311 // (the value loaded and the chain). Don't transform a pre-increment
Stephen Lin155615d2013-07-08 00:37:03 +00005312 // load, for example, which produces an extra value. Otherwise the
Bill Schmidt89e88e32013-01-14 22:04:38 +00005313 // transformation is not equivalent, and the downstream logic to replace
5314 // uses gets things wrong.
5315 if (LN0->getNumValues() > 2)
5316 return SDValue();
5317
Benjamin Kramerf4eeab42013-07-06 14:05:09 +00005318 // If the load that we're shrinking is an extload and we're not just
5319 // discarding the extension we can't simply shrink the load. Bail.
5320 // TODO: It would be possible to merge the extensions in some cases.
5321 if (LN0->getExtensionType() != ISD::NON_EXTLOAD &&
5322 LN0->getMemoryVT().getSizeInBits() < ExtVT.getSizeInBits() + ShAmt)
5323 return SDValue();
5324
Chris Lattner4c32bc22010-12-22 07:36:50 +00005325 EVT PtrType = N0.getOperand(1).getValueType();
Bill Wendling8509c902009-01-30 22:33:24 +00005326
Evan Cheng16436df2012-06-26 01:19:33 +00005327 if (PtrType == MVT::Untyped || PtrType.isExtended())
5328 // It's not possible to generate a constant of extended or untyped type.
5329 return SDValue();
5330
Chris Lattner4c32bc22010-12-22 07:36:50 +00005331 // For big endian targets, we need to adjust the offset to the pointer to
5332 // load the correct bytes.
5333 if (TLI.isBigEndian()) {
5334 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
5335 unsigned EVTStoreBits = ExtVT.getStoreSizeInBits();
5336 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
Evan Chengc88138f2007-03-22 01:54:19 +00005337 }
5338
Chris Lattner4c32bc22010-12-22 07:36:50 +00005339 uint64_t PtrOff = ShAmt / 8;
5340 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005341 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(LN0),
Chris Lattner4c32bc22010-12-22 07:36:50 +00005342 PtrType, LN0->getBasePtr(),
5343 DAG.getConstant(PtrOff, PtrType));
5344 AddToWorkList(NewPtr.getNode());
5345
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005346 SDValue Load;
5347 if (ExtType == ISD::NON_EXTLOAD)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005348 Load = DAG.getLoad(VT, SDLoc(N0), LN0->getChain(), NewPtr,
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005349 LN0->getPointerInfo().getWithOffset(PtrOff),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005350 LN0->isVolatile(), LN0->isNonTemporal(),
5351 LN0->isInvariant(), NewAlign);
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005352 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00005353 Load = DAG.getExtLoad(ExtType, SDLoc(N0), VT, LN0->getChain(),NewPtr,
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005354 LN0->getPointerInfo().getWithOffset(PtrOff),
5355 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
5356 NewAlign);
Chris Lattner4c32bc22010-12-22 07:36:50 +00005357
5358 // Replace the old load's chain with the new load's chain.
5359 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005360 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1));
Chris Lattner4c32bc22010-12-22 07:36:50 +00005361
5362 // Shift the result left, if we've swallowed a left shift.
5363 SDValue Result = Load;
5364 if (ShLeftAmt != 0) {
Owen Anderson95771af2011-02-25 21:41:48 +00005365 EVT ShImmTy = getShiftAmountTy(Result.getValueType());
Chris Lattner4c32bc22010-12-22 07:36:50 +00005366 if (!isUIntN(ShImmTy.getSizeInBits(), ShLeftAmt))
5367 ShImmTy = VT;
Paul Redmond5c974502013-02-12 15:21:21 +00005368 // If the shift amount is as large as the result size (but, presumably,
5369 // no larger than the source) then the useful bits of the result are
5370 // zero; we can't simply return the shortened shift, because the result
5371 // of that operation is undefined.
5372 if (ShLeftAmt >= VT.getSizeInBits())
5373 Result = DAG.getConstant(0, VT);
5374 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00005375 Result = DAG.getNode(ISD::SHL, SDLoc(N0), VT,
Paul Redmond5c974502013-02-12 15:21:21 +00005376 Result, DAG.getConstant(ShLeftAmt, ShImmTy));
Chris Lattner4c32bc22010-12-22 07:36:50 +00005377 }
5378
5379 // Return the new loaded value.
5380 return Result;
Evan Chengc88138f2007-03-22 01:54:19 +00005381}
5382
Dan Gohman475871a2008-07-27 21:46:04 +00005383SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
5384 SDValue N0 = N->getOperand(0);
5385 SDValue N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005386 EVT VT = N->getValueType(0);
5387 EVT EVT = cast<VTSDNode>(N1)->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00005388 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmand1996362010-01-09 02:13:55 +00005389 unsigned EVTBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00005390
Nate Begeman1d4d4142005-09-01 00:19:25 +00005391 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00005392 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005393 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005394
Chris Lattner541a24f2006-05-06 22:43:44 +00005395 // If the input is already sign extended, just drop the extension.
Dan Gohman87862e72009-12-11 21:31:27 +00005396 if (DAG.ComputeNumSignBits(N0) >= VTBits-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00005397 return N0;
Scott Michelfdc40a02009-02-17 22:15:04 +00005398
Nate Begeman646d7e22005-09-02 21:18:40 +00005399 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
5400 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Stephen Linb4940152013-07-09 00:44:49 +00005401 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005402 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005403 N0.getOperand(0), N1);
Chris Lattner4b37e872006-05-08 21:18:59 +00005404
Dan Gohman75dcf082008-07-31 00:50:31 +00005405 // fold (sext_in_reg (sext x)) -> (sext x)
5406 // fold (sext_in_reg (aext x)) -> (sext x)
5407 // if x is small enough.
5408 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
5409 SDValue N00 = N0.getOperand(0);
Evan Cheng003d7c42010-04-16 22:26:19 +00005410 if (N00.getValueType().getScalarType().getSizeInBits() <= EVTBits &&
5411 (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005412 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N00, N1);
Dan Gohman75dcf082008-07-31 00:50:31 +00005413 }
5414
Chris Lattner95a5e052007-04-17 19:03:21 +00005415 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005416 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005417 return DAG.getZeroExtendInReg(N0, SDLoc(N), EVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00005418
Chris Lattner95a5e052007-04-17 19:03:21 +00005419 // fold operands of sext_in_reg based on knowledge that the top bits are not
5420 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00005421 if (SimplifyDemandedBits(SDValue(N, 0)))
5422 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005423
Evan Chengc88138f2007-03-22 01:54:19 +00005424 // fold (sext_in_reg (load x)) -> (smaller sextload x)
5425 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00005426 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005427 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00005428 return NarrowLoad;
5429
Bill Wendling8509c902009-01-30 22:33:24 +00005430 // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005431 // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible.
Chris Lattner4b37e872006-05-08 21:18:59 +00005432 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
5433 if (N0.getOpcode() == ISD::SRL) {
5434 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman87862e72009-12-11 21:31:27 +00005435 if (ShAmt->getZExtValue()+EVTBits <= VTBits) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005436 // We can turn this into an SRA iff the input to the SRL is already sign
Chris Lattner4b37e872006-05-08 21:18:59 +00005437 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00005438 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohman87862e72009-12-11 21:31:27 +00005439 if (VTBits-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005440 return DAG.getNode(ISD::SRA, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005441 N0.getOperand(0), N0.getOperand(1));
Chris Lattner4b37e872006-05-08 21:18:59 +00005442 }
5443 }
Evan Chengc88138f2007-03-22 01:54:19 +00005444
Nate Begemanded49632005-10-13 03:11:28 +00005445 // fold (sext_inreg (extload x)) -> (sextload x)
Scott Michelfdc40a02009-02-17 22:15:04 +00005446 if (ISD::isEXTLoad(N0.getNode()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005447 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005448 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005449 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005450 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005451 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005452 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005453 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005454 LN0->getBasePtr(), LN0->getPointerInfo(),
5455 EVT,
David Greene1e559442010-02-15 17:00:31 +00005456 LN0->isVolatile(), LN0->isNonTemporal(),
5457 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00005458 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00005459 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Elena Demikhovsky4b977312012-12-19 07:50:20 +00005460 AddToWorkList(ExtLoad.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00005461 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00005462 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005463 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00005464 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00005465 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005466 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005467 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005468 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005469 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005470 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005471 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005472 LN0->getBasePtr(), LN0->getPointerInfo(),
5473 EVT,
David Greene1e559442010-02-15 17:00:31 +00005474 LN0->isVolatile(), LN0->isNonTemporal(),
5475 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00005476 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00005477 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005478 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00005479 }
Evan Cheng9568e5c2011-06-21 06:01:08 +00005480
5481 // Form (sext_inreg (bswap >> 16)) or (sext_inreg (rotl (bswap) 16))
5482 if (EVTBits <= 16 && N0.getOpcode() == ISD::OR) {
5483 SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0),
5484 N0.getOperand(1), false);
5485 if (BSwap.getNode() != 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005486 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Evan Cheng9568e5c2011-06-21 06:01:08 +00005487 BSwap, N1);
5488 }
5489
Dan Gohman475871a2008-07-27 21:46:04 +00005490 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005491}
5492
Dan Gohman475871a2008-07-27 21:46:04 +00005493SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
5494 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005495 EVT VT = N->getValueType(0);
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005496 bool isLE = TLI.isLittleEndian();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005497
5498 // noop truncate
5499 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00005500 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00005501 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00005502 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005503 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005504 // fold (truncate (truncate x)) -> (truncate x)
5505 if (N0.getOpcode() == ISD::TRUNCATE)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005506 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00005507 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattner7f893c02010-04-07 18:13:33 +00005508 if (N0.getOpcode() == ISD::ZERO_EXTEND ||
5509 N0.getOpcode() == ISD::SIGN_EXTEND ||
Chris Lattnerb72773b2006-05-05 22:56:26 +00005510 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00005511 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00005512 // if the source is smaller than the dest, we still need an extend
Andrew Trickac6d9be2013-05-25 02:42:55 +00005513 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005514 N0.getOperand(0));
Craig Topper0eb5dad2012-09-29 07:18:53 +00005515 if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00005516 // if the source is larger than the dest, than we just need the truncate
Andrew Trickac6d9be2013-05-25 02:42:55 +00005517 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0));
Craig Topper0eb5dad2012-09-29 07:18:53 +00005518 // if the source and dest are the same type, we can drop both the extend
5519 // and the truncate.
5520 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005521 }
Evan Cheng007b69e2007-03-21 20:14:05 +00005522
Nadav Rotemcc870a82012-02-05 11:39:23 +00005523 // Fold extract-and-trunc into a narrow extract. For example:
5524 // i64 x = EXTRACT_VECTOR_ELT(v2i64 val, i32 1)
5525 // i32 y = TRUNCATE(i64 x)
5526 // -- becomes --
5527 // v16i8 b = BITCAST (v2i64 val)
5528 // i8 x = EXTRACT_VECTOR_ELT(v16i8 b, i32 8)
5529 //
5530 // Note: We only run this optimization after type legalization (which often
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005531 // creates this pattern) and before operation legalization after which
5532 // we need to be more careful about the vector instructions that we generate.
5533 if (N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5534 LegalTypes && !LegalOperations && N0->hasOneUse()) {
5535
5536 EVT VecTy = N0.getOperand(0).getValueType();
5537 EVT ExTy = N0.getValueType();
5538 EVT TrTy = N->getValueType(0);
5539
5540 unsigned NumElem = VecTy.getVectorNumElements();
5541 unsigned SizeRatio = ExTy.getSizeInBits()/TrTy.getSizeInBits();
5542
5543 EVT NVT = EVT::getVectorVT(*DAG.getContext(), TrTy, SizeRatio * NumElem);
5544 assert(NVT.getSizeInBits() == VecTy.getSizeInBits() && "Invalid Size");
5545
5546 SDValue EltNo = N0->getOperand(1);
5547 if (isa<ConstantSDNode>(EltNo) && isTypeLegal(NVT)) {
5548 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Tom Stellard425b76c2013-08-05 22:22:01 +00005549 EVT IndexTy = TLI.getVectorIdxTy();
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005550 int Index = isLE ? (Elt*SizeRatio) : (Elt*SizeRatio + (SizeRatio-1));
5551
Andrew Trickac6d9be2013-05-25 02:42:55 +00005552 SDValue V = DAG.getNode(ISD::BITCAST, SDLoc(N),
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005553 NVT, N0.getOperand(0));
5554
5555 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005556 SDLoc(N), TrTy, V,
Jim Grosbacha249f7d2012-05-08 20:56:07 +00005557 DAG.getConstant(Index, IndexTy));
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005558 }
5559 }
5560
Arnold Schwaighoferc46e2df2013-02-20 21:33:32 +00005561 // Fold a series of buildvector, bitcast, and truncate if possible.
5562 // For example fold
5563 // (2xi32 trunc (bitcast ((4xi32)buildvector x, x, y, y) 2xi64)) to
5564 // (2xi32 (buildvector x, y)).
5565 if (Level == AfterLegalizeVectorOps && VT.isVector() &&
5566 N0.getOpcode() == ISD::BITCAST && N0.hasOneUse() &&
5567 N0.getOperand(0).getOpcode() == ISD::BUILD_VECTOR &&
5568 N0.getOperand(0).hasOneUse()) {
5569
5570 SDValue BuildVect = N0.getOperand(0);
5571 EVT BuildVectEltTy = BuildVect.getValueType().getVectorElementType();
5572 EVT TruncVecEltTy = VT.getVectorElementType();
5573
5574 // Check that the element types match.
5575 if (BuildVectEltTy == TruncVecEltTy) {
5576 // Now we only need to compute the offset of the truncated elements.
5577 unsigned BuildVecNumElts = BuildVect.getNumOperands();
5578 unsigned TruncVecNumElts = VT.getVectorNumElements();
5579 unsigned TruncEltOffset = BuildVecNumElts / TruncVecNumElts;
5580
5581 assert((BuildVecNumElts % TruncVecNumElts) == 0 &&
5582 "Invalid number of elements");
5583
5584 SmallVector<SDValue, 8> Opnds;
5585 for (unsigned i = 0, e = BuildVecNumElts; i != e; i += TruncEltOffset)
5586 Opnds.push_back(BuildVect.getOperand(i));
5587
Andrew Trickac6d9be2013-05-25 02:42:55 +00005588 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT, &Opnds[0],
Arnold Schwaighoferc46e2df2013-02-20 21:33:32 +00005589 Opnds.size());
5590 }
5591 }
5592
Chris Lattner2b4c2792007-10-13 06:35:54 +00005593 // See if we can simplify the input to this truncate through knowledge that
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005594 // only the low bits are being used.
5595 // For example "trunc (or (shl x, 8), y)" // -> trunc y
Nadav Rotemfcd96192011-02-27 07:40:43 +00005596 // Currently we only perform this optimization on scalars because vectors
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005597 // may have different active low bits.
5598 if (!VT.isVector()) {
5599 SDValue Shorter =
5600 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
5601 VT.getSizeInBits()));
5602 if (Shorter.getNode())
Andrew Trickac6d9be2013-05-25 02:42:55 +00005603 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Shorter);
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005604 }
Nate Begeman3df4d522005-10-12 20:40:40 +00005605 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00005606 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005607 if (!LegalTypes || TLI.isTypeDesirableForOp(N0.getOpcode(), VT)) {
5608 SDValue Reduced = ReduceLoadWidth(N);
5609 if (Reduced.getNode())
5610 return Reduced;
5611 }
Michael Liao07edaf32012-10-17 23:45:54 +00005612 // fold (trunc (concat ... x ...)) -> (concat ..., (trunc x), ...)),
5613 // where ... are all 'undef'.
5614 if (N0.getOpcode() == ISD::CONCAT_VECTORS && !LegalTypes) {
5615 SmallVector<EVT, 8> VTs;
5616 SDValue V;
5617 unsigned Idx = 0;
5618 unsigned NumDefs = 0;
5619
5620 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) {
5621 SDValue X = N0.getOperand(i);
5622 if (X.getOpcode() != ISD::UNDEF) {
5623 V = X;
5624 Idx = i;
5625 NumDefs++;
5626 }
5627 // Stop if more than one members are non-undef.
5628 if (NumDefs > 1)
5629 break;
5630 VTs.push_back(EVT::getVectorVT(*DAG.getContext(),
5631 VT.getVectorElementType(),
5632 X.getValueType().getVectorNumElements()));
5633 }
5634
5635 if (NumDefs == 0)
5636 return DAG.getUNDEF(VT);
5637
5638 if (NumDefs == 1) {
5639 assert(V.getNode() && "The single defined operand is empty!");
5640 SmallVector<SDValue, 8> Opnds;
5641 for (unsigned i = 0, e = VTs.size(); i != e; ++i) {
5642 if (i != Idx) {
5643 Opnds.push_back(DAG.getUNDEF(VTs[i]));
5644 continue;
5645 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005646 SDValue NV = DAG.getNode(ISD::TRUNCATE, SDLoc(V), VTs[i], V);
Michael Liao07edaf32012-10-17 23:45:54 +00005647 AddToWorkList(NV.getNode());
5648 Opnds.push_back(NV);
5649 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005650 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Michael Liao07edaf32012-10-17 23:45:54 +00005651 &Opnds[0], Opnds.size());
5652 }
5653 }
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005654
5655 // Simplify the operands using demanded-bits information.
5656 if (!VT.isVector() &&
5657 SimplifyDemandedBits(SDValue(N, 0)))
5658 return SDValue(N, 0);
5659
Evan Chenge5b51ac2010-04-17 06:13:15 +00005660 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005661}
5662
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005663static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005664 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005665 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00005666 return Elt.getNode();
5667 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005668}
5669
5670/// CombineConsecutiveLoads - build_pair (load, load) -> load
Scott Michelfdc40a02009-02-17 22:15:04 +00005671/// if load locations are consecutive.
Owen Andersone50ed302009-08-10 22:56:29 +00005672SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005673 assert(N->getOpcode() == ISD::BUILD_PAIR);
5674
Nate Begemanabc01992009-06-05 21:37:30 +00005675 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0));
5676 LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1));
Chris Lattnerfa459012010-09-21 16:08:50 +00005677 if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse() ||
5678 LD1->getPointerInfo().getAddrSpace() !=
5679 LD2->getPointerInfo().getAddrSpace())
Dan Gohman475871a2008-07-27 21:46:04 +00005680 return SDValue();
Owen Andersone50ed302009-08-10 22:56:29 +00005681 EVT LD1VT = LD1->getValueType(0);
Bill Wendling67a67682009-01-30 22:44:24 +00005682
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005683 if (ISD::isNON_EXTLoad(LD2) &&
5684 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005685 // If both are volatile this would reduce the number of volatile loads.
5686 // If one is volatile it might be ok, but play conservative and bail out.
Nate Begemanabc01992009-06-05 21:37:30 +00005687 !LD1->isVolatile() &&
5688 !LD2->isVolatile() &&
Evan Cheng64fa4a92009-12-09 01:36:00 +00005689 DAG.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1)) {
Nate Begemanabc01992009-06-05 21:37:30 +00005690 unsigned Align = LD1->getAlignment();
Micah Villmow3574eca2012-10-08 16:38:25 +00005691 unsigned NewAlign = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005692 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Bill Wendling67a67682009-01-30 22:44:24 +00005693
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005694 if (NewAlign <= Align &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005695 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005696 return DAG.getLoad(VT, SDLoc(N), LD1->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005697 LD1->getBasePtr(), LD1->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005698 false, false, false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005699 }
Bill Wendling67a67682009-01-30 22:44:24 +00005700
Dan Gohman475871a2008-07-27 21:46:04 +00005701 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005702}
5703
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005704SDValue DAGCombiner::visitBITCAST(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00005705 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005706 EVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00005707
Dan Gohman7f321562007-06-25 16:23:39 +00005708 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
5709 // Only do this before legalize, since afterward the target may be depending
5710 // on the bitconvert.
5711 // First check to see if this is all constant.
Duncan Sands25cf2272008-11-24 14:53:14 +00005712 if (!LegalTypes &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005713 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005714 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00005715 bool isSimple = true;
5716 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
5717 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
5718 N0.getOperand(i).getOpcode() != ISD::Constant &&
5719 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005720 isSimple = false;
Dan Gohman7f321562007-06-25 16:23:39 +00005721 break;
5722 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005723
Owen Andersone50ed302009-08-10 22:56:29 +00005724 EVT DestEltVT = N->getValueType(0).getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005725 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00005726 "Element type of vector ValueType must not be vector!");
Bill Wendling67a67682009-01-30 22:44:24 +00005727 if (isSimple)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005728 return ConstantFoldBITCASTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00005729 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005730
Dan Gohman3dd168d2008-09-05 01:58:21 +00005731 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00005732 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005733 SDValue Res = DAG.getNode(ISD::BITCAST, SDLoc(N), VT, N0);
Dan Gohmana407ca12009-08-10 23:15:10 +00005734 if (Res.getNode() != N) {
5735 if (!LegalOperations ||
5736 TLI.isOperationLegal(Res.getNode()->getOpcode(), VT))
5737 return Res;
5738
5739 // Folding it resulted in an illegal node, and it's too late to
5740 // do that. Clean up the old node and forego the transformation.
5741 // Ideally this won't happen very often, because instcombine
5742 // and the earlier dagcombine runs (where illegal nodes are
5743 // permitted) should have folded most of them already.
5744 DAG.DeleteNode(Res.getNode());
5745 }
Chris Lattner94683772005-12-23 05:30:37 +00005746 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005747
Bill Wendling67a67682009-01-30 22:44:24 +00005748 // (conv (conv x, t1), t2) -> (conv x, t2)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005749 if (N0.getOpcode() == ISD::BITCAST)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005750 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005751 N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00005752
Chris Lattner57104102005-12-23 05:44:41 +00005753 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00005754 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00005755 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005756 // Do not change the width of a volatile load.
5757 !cast<LoadSDNode>(N0)->isVolatile() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005758 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005759 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Micah Villmow3574eca2012-10-08 16:38:25 +00005760 unsigned Align = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005761 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Evan Cheng59d5b682007-05-07 21:27:48 +00005762 unsigned OrigAlign = LN0->getAlignment();
Bill Wendling67a67682009-01-30 22:44:24 +00005763
Evan Cheng59d5b682007-05-07 21:27:48 +00005764 if (Align <= OrigAlign) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005765 SDValue Load = DAG.getLoad(VT, SDLoc(N), LN0->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005766 LN0->getBasePtr(), LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00005767 LN0->isVolatile(), LN0->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005768 LN0->isInvariant(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00005769 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00005770 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005771 DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling67a67682009-01-30 22:44:24 +00005772 N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00005773 Load.getValue(1));
5774 return Load;
5775 }
Chris Lattner57104102005-12-23 05:44:41 +00005776 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005777
Bill Wendling67a67682009-01-30 22:44:24 +00005778 // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
5779 // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
Chris Lattner3bd39d42008-01-27 17:42:27 +00005780 // This often reduces constant pool loads.
Tom Stellard1f67c632013-07-23 23:55:03 +00005781 if (((N0.getOpcode() == ISD::FNEG && !TLI.isFNegFree(N0.getValueType())) ||
5782 (N0.getOpcode() == ISD::FABS && !TLI.isFAbsFree(N0.getValueType()))) &&
Nadav Rotem91a7e012012-09-13 14:54:28 +00005783 N0.getNode()->hasOneUse() && VT.isInteger() &&
5784 !VT.isVector() && !N0.getValueType().isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005785 SDValue NewConv = DAG.getNode(ISD::BITCAST, SDLoc(N0), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005786 N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00005787 AddToWorkList(NewConv.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005788
Duncan Sands83ec4b62008-06-06 12:08:01 +00005789 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005790 if (N0.getOpcode() == ISD::FNEG)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005791 return DAG.getNode(ISD::XOR, SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005792 NewConv, DAG.getConstant(SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005793 assert(N0.getOpcode() == ISD::FABS);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005794 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005795 NewConv, DAG.getConstant(~SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005796 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005797
Bill Wendling67a67682009-01-30 22:44:24 +00005798 // fold (bitconvert (fcopysign cst, x)) ->
5799 // (or (and (bitconvert x), sign), (and cst, (not sign)))
5800 // Note that we don't handle (copysign x, cst) because this can always be
5801 // folded to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00005802 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00005803 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005804 VT.isInteger() && !VT.isVector()) {
5805 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Owen Anderson23b9b192009-08-12 00:36:31 +00005806 EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth);
Chris Lattner2392ae72010-04-15 04:48:01 +00005807 if (isTypeLegal(IntXVT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005808 SDValue X = DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling67a67682009-01-30 22:44:24 +00005809 IntXVT, N0.getOperand(1));
Duncan Sands25cf2272008-11-24 14:53:14 +00005810 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005811
Duncan Sands25cf2272008-11-24 14:53:14 +00005812 // If X has a different width than the result/lhs, sext it or truncate it.
5813 unsigned VTWidth = VT.getSizeInBits();
5814 if (OrigXWidth < VTWidth) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005815 X = DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005816 AddToWorkList(X.getNode());
5817 } else if (OrigXWidth > VTWidth) {
5818 // To get the sign bit in the right place, we have to shift it right
5819 // before truncating.
Andrew Trickac6d9be2013-05-25 02:42:55 +00005820 X = DAG.getNode(ISD::SRL, SDLoc(X),
Bill Wendling67a67682009-01-30 22:44:24 +00005821 X.getValueType(), X,
Duncan Sands25cf2272008-11-24 14:53:14 +00005822 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
5823 AddToWorkList(X.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005824 X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005825 AddToWorkList(X.getNode());
5826 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005827
Duncan Sands25cf2272008-11-24 14:53:14 +00005828 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005829 X = DAG.getNode(ISD::AND, SDLoc(X), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005830 X, DAG.getConstant(SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005831 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005832
Andrew Trickac6d9be2013-05-25 02:42:55 +00005833 SDValue Cst = DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling67a67682009-01-30 22:44:24 +00005834 VT, N0.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005835 Cst = DAG.getNode(ISD::AND, SDLoc(Cst), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005836 Cst, DAG.getConstant(~SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005837 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005838
Andrew Trickac6d9be2013-05-25 02:42:55 +00005839 return DAG.getNode(ISD::OR, SDLoc(N), VT, X, Cst);
Duncan Sands25cf2272008-11-24 14:53:14 +00005840 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00005841 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005842
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005843 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005844 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005845 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
5846 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005847 return CombineLD;
5848 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005849
Dan Gohman475871a2008-07-27 21:46:04 +00005850 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00005851}
5852
Dan Gohman475871a2008-07-27 21:46:04 +00005853SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00005854 EVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005855 return CombineConsecutiveLoads(N, VT);
5856}
5857
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005858/// ConstantFoldBITCASTofBUILD_VECTOR - We know that BV is a build_vector
Scott Michelfdc40a02009-02-17 22:15:04 +00005859/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
Chris Lattner6258fb22006-04-02 02:53:43 +00005860/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00005861SDValue DAGCombiner::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005862ConstantFoldBITCASTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) {
Owen Andersone50ed302009-08-10 22:56:29 +00005863 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005864
Chris Lattner6258fb22006-04-02 02:53:43 +00005865 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00005866 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005867
Duncan Sands83ec4b62008-06-06 12:08:01 +00005868 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
5869 unsigned DstBitSize = DstEltVT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00005870
Chris Lattner6258fb22006-04-02 02:53:43 +00005871 // If this is a conversion of N elements of one type to N elements of another
5872 // type, convert each element. This handles FP<->INT cases.
5873 if (SrcBitSize == DstBitSize) {
Nate Begemane0efc212010-07-27 18:02:18 +00005874 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5875 BV->getValueType(0).getVectorNumElements());
5876
5877 // Due to the FP element handling below calling this routine recursively,
5878 // we can end up with a scalar-to-vector node here.
5879 if (BV->getOpcode() == ISD::SCALAR_TO_VECTOR)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005880 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(BV), VT,
5881 DAG.getNode(ISD::BITCAST, SDLoc(BV),
Nate Begemane0efc212010-07-27 18:02:18 +00005882 DstEltVT, BV->getOperand(0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005883
Dan Gohman475871a2008-07-27 21:46:04 +00005884 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005885 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Bob Wilsonb1303d02009-04-13 22:05:19 +00005886 SDValue Op = BV->getOperand(i);
5887 // If the vector element type is not legal, the BUILD_VECTOR operands
5888 // are promoted and implicitly truncated. Make that explicit here.
Bob Wilsonc8851652009-04-20 17:27:09 +00005889 if (Op.getValueType() != SrcEltVT)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005890 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(BV), SrcEltVT, Op);
5891 Ops.push_back(DAG.getNode(ISD::BITCAST, SDLoc(BV),
Bob Wilsonb1303d02009-04-13 22:05:19 +00005892 DstEltVT, Op));
Gabor Greifba36cb52008-08-28 21:40:38 +00005893 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00005894 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005895 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga87008d2009-02-25 22:49:59 +00005896 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005897 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005898
Chris Lattner6258fb22006-04-02 02:53:43 +00005899 // Otherwise, we're growing or shrinking the elements. To avoid having to
5900 // handle annoying details of growing/shrinking FP values, we convert them to
5901 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005902 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005903 // Convert the input float vector to a int vector where the elements are the
5904 // same sizes.
Owen Anderson825b72b2009-08-11 20:47:22 +00005905 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005906 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005907 BV = ConstantFoldBITCASTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00005908 SrcEltVT = IntVT;
5909 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005910
Chris Lattner6258fb22006-04-02 02:53:43 +00005911 // Now we know the input is an integer vector. If the output is a FP type,
5912 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005913 if (DstEltVT.isFloatingPoint()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005914 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005915 EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005916 SDNode *Tmp = ConstantFoldBITCASTofBUILD_VECTOR(BV, TmpVT).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005917
Chris Lattner6258fb22006-04-02 02:53:43 +00005918 // Next, convert to FP elements of the same size.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005919 return ConstantFoldBITCASTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00005920 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005921
Chris Lattner6258fb22006-04-02 02:53:43 +00005922 // Okay, we know the src/dst types are both integers of differing types.
5923 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005924 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00005925 if (SrcBitSize < DstBitSize) {
5926 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
Scott Michelfdc40a02009-02-17 22:15:04 +00005927
Dan Gohman475871a2008-07-27 21:46:04 +00005928 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005929 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00005930 i += NumInputsPerOutput) {
5931 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00005932 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00005933 bool EltIsUndef = true;
5934 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
5935 // Shift the previously computed bits over.
5936 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00005937 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00005938 if (Op.getOpcode() == ISD::UNDEF) continue;
5939 EltIsUndef = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005940
Jay Foad40f8f622010-12-07 08:25:19 +00005941 NewBits |= cast<ConstantSDNode>(Op)->getAPIntValue().
Dan Gohman58c25872010-04-12 02:24:01 +00005942 zextOrTrunc(SrcBitSize).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005943 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005944
Chris Lattner6258fb22006-04-02 02:53:43 +00005945 if (EltIsUndef)
Dale Johannesene8d72302009-02-06 23:05:02 +00005946 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005947 else
5948 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
5949 }
5950
Owen Anderson23b9b192009-08-12 00:36:31 +00005951 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005952 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga87008d2009-02-25 22:49:59 +00005953 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005954 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005955
Chris Lattner6258fb22006-04-02 02:53:43 +00005956 // Finally, this must be the case where we are shrinking elements: each input
5957 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00005958 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00005959 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Owen Anderson23b9b192009-08-12 00:36:31 +00005960 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5961 NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00005962 SmallVector<SDValue, 8> Ops;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005963
Dan Gohman7f321562007-06-25 16:23:39 +00005964 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005965 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
5966 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
Dale Johannesene8d72302009-02-06 23:05:02 +00005967 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005968 continue;
5969 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005970
Jay Foad40f8f622010-12-07 08:25:19 +00005971 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->
5972 getAPIntValue().zextOrTrunc(SrcBitSize);
Bill Wendlingb0162f52009-01-30 22:53:48 +00005973
Chris Lattner6258fb22006-04-02 02:53:43 +00005974 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Jay Foad40f8f622010-12-07 08:25:19 +00005975 APInt ThisVal = OpVal.trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005976 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Jay Foad40f8f622010-12-07 08:25:19 +00005977 if (isS2V && i == 0 && j == 0 && ThisVal.zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00005978 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
Andrew Trickac6d9be2013-05-25 02:42:55 +00005979 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(BV), VT,
Bill Wendlingb0162f52009-01-30 22:53:48 +00005980 Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00005981 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005982 }
5983
5984 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00005985 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00005986 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
5987 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005988
Andrew Trickac6d9be2013-05-25 02:42:55 +00005989 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga87008d2009-02-25 22:49:59 +00005990 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005991}
5992
Dan Gohman475871a2008-07-27 21:46:04 +00005993SDValue DAGCombiner::visitFADD(SDNode *N) {
5994 SDValue N0 = N->getOperand(0);
5995 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005996 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
5997 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00005998 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005999
Dan Gohman7f321562007-06-25 16:23:39 +00006000 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006001 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006002 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006003 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006004 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006005
Lang Hames01806942012-06-14 20:37:15 +00006006 // fold (fadd c1, c2) -> c1 + c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006007 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006008 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00006009 // canonicalize constant to RHS
6010 if (N0CFP && !N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006011 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N0);
Bill Wendlingb0162f52009-01-30 22:53:48 +00006012 // fold (fadd A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006013 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6014 N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00006015 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00006016 // fold (fadd A, (fneg B)) -> (fsub A, B)
Owen Andersonafd3d562012-03-06 00:29:31 +00006017 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00006018 isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006019 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00006020 GetNegatedExpression(N1, DAG, LegalOperations));
Bill Wendlingb0162f52009-01-30 22:53:48 +00006021 // fold (fadd (fneg A), B) -> (fsub B, A)
Owen Andersonafd3d562012-03-06 00:29:31 +00006022 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00006023 isNegatibleForFree(N0, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006024 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N1,
Duncan Sands25cf2272008-11-24 14:53:14 +00006025 GetNegatedExpression(N0, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00006026
Chris Lattnerddae4bd2007-01-08 23:04:05 +00006027 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006028 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6029 N0.getOpcode() == ISD::FADD && N0.getNode()->hasOneUse() &&
6030 isa<ConstantFPSDNode>(N0.getOperand(1)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006031 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0.getOperand(0),
6032 DAG.getNode(ISD::FADD, SDLoc(N), VT,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00006033 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006034
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006035 // No FP constant should be created after legalization as Instruction
6036 // Selection pass has hard time in dealing with FP constant.
6037 //
6038 // We don't need test this condition for transformation like following, as
6039 // the DAG being transformed implies it is legal to take FP constant as
6040 // operand.
Stephen Lin155615d2013-07-08 00:37:03 +00006041 //
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006042 // (fadd (fmul c, x), x) -> (fmul c+1, x)
Stephen Lin155615d2013-07-08 00:37:03 +00006043 //
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006044 bool AllowNewFpConst = (Level < AfterLegalizeDAG);
6045
Owen Anderson607ebde2012-11-01 02:00:53 +00006046 // If allow, fold (fadd (fneg x), x) -> 0.0
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006047 if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath &&
Stephen Linb4940152013-07-09 00:44:49 +00006048 N0.getOpcode() == ISD::FNEG && N0.getOperand(0) == N1)
Owen Anderson607ebde2012-11-01 02:00:53 +00006049 return DAG.getConstantFP(0.0, VT);
Owen Anderson607ebde2012-11-01 02:00:53 +00006050
6051 // If allow, fold (fadd x, (fneg x)) -> 0.0
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006052 if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath &&
Stephen Linb4940152013-07-09 00:44:49 +00006053 N1.getOpcode() == ISD::FNEG && N1.getOperand(0) == N0)
Owen Anderson607ebde2012-11-01 02:00:53 +00006054 return DAG.getConstantFP(0.0, VT);
Owen Anderson607ebde2012-11-01 02:00:53 +00006055
Owen Anderson43da6c72012-08-30 23:35:16 +00006056 // In unsafe math mode, we can fold chains of FADD's of the same value
6057 // into multiplications. This transform is not safe in general because
6058 // we are reducing the number of rounding steps.
6059 if (DAG.getTarget().Options.UnsafeFPMath &&
6060 TLI.isOperationLegalOrCustom(ISD::FMUL, VT) &&
6061 !N0CFP && !N1CFP) {
6062 if (N0.getOpcode() == ISD::FMUL) {
6063 ConstantFPSDNode *CFP00 = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
6064 ConstantFPSDNode *CFP01 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
6065
Stephen Lin38103d12013-06-14 18:17:35 +00006066 // (fadd (fmul c, x), x) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00006067 if (CFP00 && !CFP01 && N0.getOperand(1) == N1) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006068 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006069 SDValue(CFP00, 0),
6070 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006071 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006072 N1, NewCFP);
6073 }
6074
Stephen Lin38103d12013-06-14 18:17:35 +00006075 // (fadd (fmul x, c), x) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00006076 if (CFP01 && !CFP00 && N0.getOperand(0) == N1) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006077 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006078 SDValue(CFP01, 0),
6079 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006080 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006081 N1, NewCFP);
6082 }
6083
Stephen Lin38103d12013-06-14 18:17:35 +00006084 // (fadd (fmul c, x), (fadd x, x)) -> (fmul x, c+2)
Owen Anderson43da6c72012-08-30 23:35:16 +00006085 if (CFP00 && !CFP01 && N1.getOpcode() == ISD::FADD &&
6086 N1.getOperand(0) == N1.getOperand(1) &&
6087 N0.getOperand(1) == N1.getOperand(0)) {
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(CFP00, 0),
6090 DAG.getConstantFP(2.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.getOperand(1), NewCFP);
6093 }
6094
Stephen Lin38103d12013-06-14 18:17:35 +00006095 // (fadd (fmul x, c), (fadd x, x)) -> (fmul x, c+2)
Owen Anderson43da6c72012-08-30 23:35:16 +00006096 if (CFP01 && !CFP00 && N1.getOpcode() == ISD::FADD &&
6097 N1.getOperand(0) == N1.getOperand(1) &&
6098 N0.getOperand(0) == N1.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006099 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006100 SDValue(CFP01, 0),
6101 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006102 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006103 N0.getOperand(0), NewCFP);
6104 }
6105 }
6106
6107 if (N1.getOpcode() == ISD::FMUL) {
6108 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
6109 ConstantFPSDNode *CFP11 = dyn_cast<ConstantFPSDNode>(N1.getOperand(1));
6110
Stephen Lin38103d12013-06-14 18:17:35 +00006111 // (fadd x, (fmul c, x)) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00006112 if (CFP10 && !CFP11 && N1.getOperand(1) == N0) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006113 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006114 SDValue(CFP10, 0),
6115 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006116 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006117 N0, NewCFP);
6118 }
6119
Stephen Lin38103d12013-06-14 18:17:35 +00006120 // (fadd x, (fmul x, c)) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00006121 if (CFP11 && !CFP10 && N1.getOperand(0) == N0) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006122 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006123 SDValue(CFP11, 0),
6124 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006125 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006126 N0, NewCFP);
6127 }
6128
Owen Anderson43da6c72012-08-30 23:35:16 +00006129
Stephen Lin38103d12013-06-14 18:17:35 +00006130 // (fadd (fadd x, x), (fmul c, x)) -> (fmul x, c+2)
6131 if (CFP10 && !CFP11 && N0.getOpcode() == ISD::FADD &&
6132 N0.getOperand(0) == N0.getOperand(1) &&
6133 N1.getOperand(1) == N0.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006134 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006135 SDValue(CFP10, 0),
6136 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006137 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Stephen Lin38103d12013-06-14 18:17:35 +00006138 N1.getOperand(1), NewCFP);
Owen Anderson43da6c72012-08-30 23:35:16 +00006139 }
6140
Stephen Lin38103d12013-06-14 18:17:35 +00006141 // (fadd (fadd x, x), (fmul x, c)) -> (fmul x, c+2)
6142 if (CFP11 && !CFP10 && N0.getOpcode() == ISD::FADD &&
6143 N0.getOperand(0) == N0.getOperand(1) &&
6144 N1.getOperand(0) == N0.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006145 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006146 SDValue(CFP11, 0),
6147 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006148 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Stephen Lin38103d12013-06-14 18:17:35 +00006149 N1.getOperand(0), NewCFP);
Owen Anderson43da6c72012-08-30 23:35:16 +00006150 }
6151 }
6152
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006153 if (N0.getOpcode() == ISD::FADD && AllowNewFpConst) {
Shuxin Yang98b93e52013-02-02 00:22:03 +00006154 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
Stephen Lina553bed2013-06-14 21:33:58 +00006155 // (fadd (fadd x, x), x) -> (fmul x, 3.0)
Shuxin Yang98b93e52013-02-02 00:22:03 +00006156 if (!CFP && N0.getOperand(0) == N0.getOperand(1) &&
Stephen Linb4940152013-07-09 00:44:49 +00006157 (N0.getOperand(0) == N1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006158 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Shuxin Yang98b93e52013-02-02 00:22:03 +00006159 N1, DAG.getConstantFP(3.0, VT));
Shuxin Yang98b93e52013-02-02 00:22:03 +00006160 }
6161
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006162 if (N1.getOpcode() == ISD::FADD && AllowNewFpConst) {
Shuxin Yang98b93e52013-02-02 00:22:03 +00006163 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
Stephen Lina553bed2013-06-14 21:33:58 +00006164 // (fadd x, (fadd x, x)) -> (fmul x, 3.0)
Shuxin Yang98b93e52013-02-02 00:22:03 +00006165 if (!CFP10 && N1.getOperand(0) == N1.getOperand(1) &&
Stephen Linb4940152013-07-09 00:44:49 +00006166 N1.getOperand(0) == N0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006167 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Shuxin Yang98b93e52013-02-02 00:22:03 +00006168 N0, DAG.getConstantFP(3.0, VT));
Shuxin Yang98b93e52013-02-02 00:22:03 +00006169 }
6170
Stephen Lina553bed2013-06-14 21:33:58 +00006171 // (fadd (fadd x, x), (fadd x, x)) -> (fmul x, 4.0)
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006172 if (AllowNewFpConst &&
6173 N0.getOpcode() == ISD::FADD && N1.getOpcode() == ISD::FADD &&
Owen Anderson43da6c72012-08-30 23:35:16 +00006174 N0.getOperand(0) == N0.getOperand(1) &&
6175 N1.getOperand(0) == N1.getOperand(1) &&
Stephen Linb4940152013-07-09 00:44:49 +00006176 N0.getOperand(0) == N1.getOperand(0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006177 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006178 N0.getOperand(0),
6179 DAG.getConstantFP(4.0, VT));
Owen Anderson43da6c72012-08-30 23:35:16 +00006180 }
6181
Lang Hamesd693caf2012-06-19 22:51:23 +00006182 // FADD -> FMA combines:
Lang Hamese0231412012-06-22 01:09:09 +00006183 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hamesd693caf2012-06-19 22:51:23 +00006184 DAG.getTarget().Options.UnsafeFPMath) &&
Stephen Line54885a2013-07-09 18:16:56 +00006185 DAG.getTarget().getTargetLowering()->isFMAFasterThanFMulAndFAdd(VT) &&
6186 (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FMA, VT))) {
Lang Hamesd693caf2012-06-19 22:51:23 +00006187
6188 // fold (fadd (fmul x, y), z) -> (fma x, y, z)
Stephen Linb4940152013-07-09 00:44:49 +00006189 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse())
Andrew Trickac6d9be2013-05-25 02:42:55 +00006190 return DAG.getNode(ISD::FMA, SDLoc(N), VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006191 N0.getOperand(0), N0.getOperand(1), N1);
Owen Anderson43da6c72012-08-30 23:35:16 +00006192
Michael Liaob79bff52012-09-01 04:09:16 +00006193 // fold (fadd x, (fmul y, z)) -> (fma y, z, x)
Lang Hamesd693caf2012-06-19 22:51:23 +00006194 // Note: Commutes FADD operands.
Stephen Linb4940152013-07-09 00:44:49 +00006195 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse())
Andrew Trickac6d9be2013-05-25 02:42:55 +00006196 return DAG.getNode(ISD::FMA, SDLoc(N), VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006197 N1.getOperand(0), N1.getOperand(1), N0);
Lang Hamesd693caf2012-06-19 22:51:23 +00006198 }
6199
Dan Gohman475871a2008-07-27 21:46:04 +00006200 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006201}
6202
Dan Gohman475871a2008-07-27 21:46:04 +00006203SDValue DAGCombiner::visitFSUB(SDNode *N) {
6204 SDValue N0 = N->getOperand(0);
6205 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00006206 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6207 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006208 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006209 SDLoc dl(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00006210
Dan Gohman7f321562007-06-25 16:23:39 +00006211 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006212 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006213 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006214 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006215 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006216
Nate Begemana0e221d2005-10-18 00:28:13 +00006217 // fold (fsub c1, c2) -> c1-c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006218 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006219 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N0, N1);
Bill Wendlingb0162f52009-01-30 22:53:48 +00006220 // fold (fsub A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006221 if (DAG.getTarget().Options.UnsafeFPMath &&
6222 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohmana90c8e62009-01-23 19:10:37 +00006223 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00006224 // fold (fsub 0, B) -> -B
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006225 if (DAG.getTarget().Options.UnsafeFPMath &&
6226 N0CFP && N0CFP->getValueAPF().isZero()) {
Owen Andersonafd3d562012-03-06 00:29:31 +00006227 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00006228 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman760f86f2009-01-22 21:58:43 +00006229 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006230 return DAG.getNode(ISD::FNEG, dl, VT, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00006231 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00006232 // fold (fsub A, (fneg B)) -> (fadd A, B)
Owen Andersonafd3d562012-03-06 00:29:31 +00006233 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006234 return DAG.getNode(ISD::FADD, dl, VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00006235 GetNegatedExpression(N1, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00006236
Bill Wendling5a894342012-03-15 05:12:00 +00006237 // If 'unsafe math' is enabled, fold
Owen Anderson713e9532012-05-07 20:51:25 +00006238 // (fsub x, x) -> 0.0 &
Bill Wendling5a894342012-03-15 05:12:00 +00006239 // (fsub x, (fadd x, y)) -> (fneg y) &
6240 // (fsub x, (fadd y, x)) -> (fneg y)
6241 if (DAG.getTarget().Options.UnsafeFPMath) {
Owen Anderson713e9532012-05-07 20:51:25 +00006242 if (N0 == N1)
6243 return DAG.getConstantFP(0.0f, VT);
6244
Bill Wendling5a894342012-03-15 05:12:00 +00006245 if (N1.getOpcode() == ISD::FADD) {
6246 SDValue N10 = N1->getOperand(0);
6247 SDValue N11 = N1->getOperand(1);
6248
6249 if (N10 == N0 && isNegatibleForFree(N11, LegalOperations, TLI,
6250 &DAG.getTarget().Options))
6251 return GetNegatedExpression(N11, DAG, LegalOperations);
Stephen Lin75d13062013-07-10 20:47:39 +00006252
Stephen Linb4940152013-07-09 00:44:49 +00006253 if (N11 == N0 && isNegatibleForFree(N10, LegalOperations, TLI,
6254 &DAG.getTarget().Options))
Bill Wendling5a894342012-03-15 05:12:00 +00006255 return GetNegatedExpression(N10, DAG, LegalOperations);
6256 }
6257 }
6258
Lang Hamesd693caf2012-06-19 22:51:23 +00006259 // FSUB -> FMA combines:
Lang Hamese0231412012-06-22 01:09:09 +00006260 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hamesd693caf2012-06-19 22:51:23 +00006261 DAG.getTarget().Options.UnsafeFPMath) &&
Stephen Line54885a2013-07-09 18:16:56 +00006262 DAG.getTarget().getTargetLowering()->isFMAFasterThanFMulAndFAdd(VT) &&
6263 (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FMA, VT))) {
Lang Hamesd693caf2012-06-19 22:51:23 +00006264
6265 // fold (fsub (fmul x, y), z) -> (fma x, y, (fneg z))
Stephen Linb4940152013-07-09 00:44:49 +00006266 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse())
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006267 return DAG.getNode(ISD::FMA, dl, VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006268 N0.getOperand(0), N0.getOperand(1),
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006269 DAG.getNode(ISD::FNEG, dl, VT, N1));
Lang Hamesd693caf2012-06-19 22:51:23 +00006270
6271 // fold (fsub x, (fmul y, z)) -> (fma (fneg y), z, x)
6272 // Note: Commutes FSUB operands.
Stephen Lin75d13062013-07-10 20:47:39 +00006273 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse())
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006274 return DAG.getNode(ISD::FMA, dl, VT,
6275 DAG.getNode(ISD::FNEG, dl, VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006276 N1.getOperand(0)),
6277 N1.getOperand(1), N0);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006278
Stephen Linb4940152013-07-09 00:44:49 +00006279 // fold (fsub (fneg (fmul, x, y)), z) -> (fma (fneg x), y, (fneg z))
Stephen Lin155615d2013-07-08 00:37:03 +00006280 if (N0.getOpcode() == ISD::FNEG &&
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006281 N0.getOperand(0).getOpcode() == ISD::FMUL &&
6282 N0->hasOneUse() && N0.getOperand(0).hasOneUse()) {
6283 SDValue N00 = N0.getOperand(0).getOperand(0);
6284 SDValue N01 = N0.getOperand(0).getOperand(1);
6285 return DAG.getNode(ISD::FMA, dl, VT,
6286 DAG.getNode(ISD::FNEG, dl, VT, N00), N01,
6287 DAG.getNode(ISD::FNEG, dl, VT, N1));
6288 }
Lang Hamesd693caf2012-06-19 22:51:23 +00006289 }
6290
Dan Gohman475871a2008-07-27 21:46:04 +00006291 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006292}
6293
Dan Gohman475871a2008-07-27 21:46:04 +00006294SDValue DAGCombiner::visitFMUL(SDNode *N) {
6295 SDValue N0 = N->getOperand(0);
6296 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00006297 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6298 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006299 EVT VT = N->getValueType(0);
Owen Andersonafd3d562012-03-06 00:29:31 +00006300 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner01b3d732005-09-28 22:28:18 +00006301
Dan Gohman7f321562007-06-25 16:23:39 +00006302 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006303 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006304 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006305 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006306 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006307
Nate Begeman11af4ea2005-10-17 20:40:11 +00006308 // fold (fmul c1, c2) -> c1*c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006309 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006310 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00006311 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00006312 if (N0CFP && !N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006313 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N1, N0);
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006314 // fold (fmul A, 0) -> 0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006315 if (DAG.getTarget().Options.UnsafeFPMath &&
6316 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00006317 return N1;
Dan Gohman77b81fe2009-06-04 17:12:12 +00006318 // fold (fmul A, 0) -> 0, vector edition.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006319 if (DAG.getTarget().Options.UnsafeFPMath &&
6320 ISD::isBuildVectorAllZeros(N1.getNode()))
Dan Gohman77b81fe2009-06-04 17:12:12 +00006321 return N1;
Owen Anderson363e4b92012-05-02 21:32:35 +00006322 // fold (fmul A, 1.0) -> A
6323 if (N1CFP && N1CFP->isExactlyValue(1.0))
6324 return N0;
Nate Begeman11af4ea2005-10-17 20:40:11 +00006325 // fold (fmul X, 2.0) -> (fadd X, X)
6326 if (N1CFP && N1CFP->isExactlyValue(+2.0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006327 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N0);
Dan Gohmaneb1fedc2009-08-10 16:50:32 +00006328 // fold (fmul X, -1.0) -> (fneg X)
Chris Lattner29446522007-05-14 22:04:50 +00006329 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman760f86f2009-01-22 21:58:43 +00006330 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006331 return DAG.getNode(ISD::FNEG, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006332
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006333 // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y)
Owen Andersonafd3d562012-03-06 00:29:31 +00006334 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006335 &DAG.getTarget().Options)) {
Stephen Lin155615d2013-07-08 00:37:03 +00006336 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006337 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00006338 // Both can be negated for free, check to see if at least one is cheaper
6339 // negated.
6340 if (LHSNeg == 2 || RHSNeg == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006341 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00006342 GetNegatedExpression(N0, DAG, LegalOperations),
6343 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00006344 }
6345 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006346
Chris Lattnerddae4bd2007-01-08 23:04:05 +00006347 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006348 if (DAG.getTarget().Options.UnsafeFPMath &&
6349 N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006350 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006351 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0.getOperand(0),
6352 DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Dale Johannesende064702009-02-06 21:50:26 +00006353 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006354
Dan Gohman475871a2008-07-27 21:46:04 +00006355 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006356}
6357
Owen Anderson062c0a52012-05-02 22:17:40 +00006358SDValue DAGCombiner::visitFMA(SDNode *N) {
6359 SDValue N0 = N->getOperand(0);
6360 SDValue N1 = N->getOperand(1);
6361 SDValue N2 = N->getOperand(2);
6362 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6363 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
6364 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006365 SDLoc dl(N);
Owen Anderson062c0a52012-05-02 22:17:40 +00006366
Owen Anderson607ebde2012-11-01 02:00:53 +00006367 if (DAG.getTarget().Options.UnsafeFPMath) {
6368 if (N0CFP && N0CFP->isZero())
6369 return N2;
6370 if (N1CFP && N1CFP->isZero())
6371 return N2;
6372 }
Owen Anderson062c0a52012-05-02 22:17:40 +00006373 if (N0CFP && N0CFP->isExactlyValue(1.0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006374 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N2);
Owen Anderson062c0a52012-05-02 22:17:40 +00006375 if (N1CFP && N1CFP->isExactlyValue(1.0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006376 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N2);
Owen Anderson062c0a52012-05-02 22:17:40 +00006377
Owen Anderson85ef6f42012-05-30 18:50:39 +00006378 // Canonicalize (fma c, x, y) -> (fma x, c, y)
Owen Andersonf917d202012-05-30 18:54:50 +00006379 if (N0CFP && !N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006380 return DAG.getNode(ISD::FMA, SDLoc(N), VT, N1, N0, N2);
Owen Anderson85ef6f42012-05-30 18:50:39 +00006381
Owen Anderson58d57292012-09-01 06:04:27 +00006382 // (fma x, c1, (fmul x, c2)) -> (fmul x, c1+c2)
6383 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6384 N2.getOpcode() == ISD::FMUL &&
6385 N0 == N2.getOperand(0) &&
6386 N2.getOperand(1).getOpcode() == ISD::ConstantFP) {
6387 return DAG.getNode(ISD::FMUL, dl, VT, N0,
6388 DAG.getNode(ISD::FADD, dl, VT, N1, N2.getOperand(1)));
6389 }
6390
6391
6392 // (fma (fmul x, c1), c2, y) -> (fma x, c1*c2, y)
6393 if (DAG.getTarget().Options.UnsafeFPMath &&
6394 N0.getOpcode() == ISD::FMUL && N1CFP &&
6395 N0.getOperand(1).getOpcode() == ISD::ConstantFP) {
6396 return DAG.getNode(ISD::FMA, dl, VT,
6397 N0.getOperand(0),
6398 DAG.getNode(ISD::FMUL, dl, VT, N1, N0.getOperand(1)),
6399 N2);
6400 }
6401
6402 // (fma x, 1, y) -> (fadd x, y)
6403 // (fma x, -1, y) -> (fadd (fneg x), y)
6404 if (N1CFP) {
6405 if (N1CFP->isExactlyValue(1.0))
6406 return DAG.getNode(ISD::FADD, dl, VT, N0, N2);
6407
6408 if (N1CFP->isExactlyValue(-1.0) &&
6409 (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))) {
6410 SDValue RHSNeg = DAG.getNode(ISD::FNEG, dl, VT, N0);
6411 AddToWorkList(RHSNeg.getNode());
6412 return DAG.getNode(ISD::FADD, dl, VT, N2, RHSNeg);
6413 }
6414 }
6415
6416 // (fma x, c, x) -> (fmul x, (c+1))
Stephen Linb4940152013-07-09 00:44:49 +00006417 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && N0 == N2)
6418 return DAG.getNode(ISD::FMUL, dl, VT, N0,
Owen Anderson58d57292012-09-01 06:04:27 +00006419 DAG.getNode(ISD::FADD, dl, VT,
6420 N1, DAG.getConstantFP(1.0, VT)));
Owen Anderson58d57292012-09-01 06:04:27 +00006421
6422 // (fma x, c, (fneg x)) -> (fmul x, (c-1))
6423 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
Stephen Linb4940152013-07-09 00:44:49 +00006424 N2.getOpcode() == ISD::FNEG && N2.getOperand(0) == N0)
6425 return DAG.getNode(ISD::FMUL, dl, VT, N0,
Owen Anderson58d57292012-09-01 06:04:27 +00006426 DAG.getNode(ISD::FADD, dl, VT,
6427 N1, DAG.getConstantFP(-1.0, VT)));
Owen Anderson58d57292012-09-01 06:04:27 +00006428
6429
Owen Anderson062c0a52012-05-02 22:17:40 +00006430 return SDValue();
6431}
6432
Dan Gohman475871a2008-07-27 21:46:04 +00006433SDValue DAGCombiner::visitFDIV(SDNode *N) {
6434 SDValue N0 = N->getOperand(0);
6435 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006436 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6437 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006438 EVT VT = N->getValueType(0);
Owen Andersonafd3d562012-03-06 00:29:31 +00006439 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner01b3d732005-09-28 22:28:18 +00006440
Dan Gohman7f321562007-06-25 16:23:39 +00006441 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006442 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006443 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006444 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006445 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006446
Nate Begemana148d982006-01-18 22:35:16 +00006447 // fold (fdiv c1, c2) -> c1/c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006448 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006449 return DAG.getNode(ISD::FDIV, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006450
Duncan Sands3ef3fcf2012-04-08 18:08:12 +00006451 // fold (fdiv X, c2) -> fmul X, 1/c2 if losing precision is acceptable.
Ulrich Weigande669c932012-10-29 18:35:49 +00006452 if (N1CFP && DAG.getTarget().Options.UnsafeFPMath) {
Duncan Sands961d6662012-04-07 20:04:00 +00006453 // Compute the reciprocal 1.0 / c2.
6454 APFloat N1APF = N1CFP->getValueAPF();
6455 APFloat Recip(N1APF.getSemantics(), 1); // 1.0
6456 APFloat::opStatus st = Recip.divide(N1APF, APFloat::rmNearestTiesToEven);
Duncan Sands507bb7a2012-04-10 20:35:27 +00006457 // Only do the transform if the reciprocal is a legal fp immediate that
6458 // isn't too nasty (eg NaN, denormal, ...).
6459 if ((st == APFloat::opOK || st == APFloat::opInexact) && // Not too nasty
Anton Korobeynikov999821c2012-04-10 13:22:49 +00006460 (!LegalOperations ||
6461 // FIXME: custom lowering of ConstantFP might fail (see e.g. ARM
6462 // backend)... we should handle this gracefully after Legalize.
6463 // TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT) ||
6464 TLI.isOperationLegal(llvm::ISD::ConstantFP, VT) ||
6465 TLI.isFPImmLegal(Recip, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006466 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0,
Duncan Sands961d6662012-04-07 20:04:00 +00006467 DAG.getConstantFP(Recip, VT));
6468 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006469
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006470 // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y)
Owen Andersonafd3d562012-03-06 00:29:31 +00006471 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006472 &DAG.getTarget().Options)) {
Owen Andersonafd3d562012-03-06 00:29:31 +00006473 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006474 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00006475 // Both can be negated for free, check to see if at least one is cheaper
6476 // negated.
6477 if (LHSNeg == 2 || RHSNeg == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006478 return DAG.getNode(ISD::FDIV, SDLoc(N), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00006479 GetNegatedExpression(N0, DAG, LegalOperations),
6480 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00006481 }
6482 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006483
Dan Gohman475871a2008-07-27 21:46:04 +00006484 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006485}
6486
Dan Gohman475871a2008-07-27 21:46:04 +00006487SDValue DAGCombiner::visitFREM(SDNode *N) {
6488 SDValue N0 = N->getOperand(0);
6489 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006490 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6491 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006492 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00006493
Nate Begemana148d982006-01-18 22:35:16 +00006494 // fold (frem c1, c2) -> fmod(c1,c2)
Ulrich Weigande669c932012-10-29 18:35:49 +00006495 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006496 return DAG.getNode(ISD::FREM, SDLoc(N), VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00006497
Dan Gohman475871a2008-07-27 21:46:04 +00006498 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006499}
6500
Dan Gohman475871a2008-07-27 21:46:04 +00006501SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
6502 SDValue N0 = N->getOperand(0);
6503 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00006504 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6505 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006506 EVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00006507
Ulrich Weigande669c932012-10-29 18:35:49 +00006508 if (N0CFP && N1CFP) // Constant fold
Andrew Trickac6d9be2013-05-25 02:42:55 +00006509 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006510
Chris Lattner12d83032006-03-05 05:30:57 +00006511 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00006512 const APFloat& V = N1CFP->getValueAPF();
Sylvestre Ledru94c22712012-09-27 10:14:43 +00006513 // copysign(x, c1) -> fabs(x) iff ispos(c1)
6514 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman760f86f2009-01-22 21:58:43 +00006515 if (!V.isNegative()) {
6516 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006517 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Dan Gohman760f86f2009-01-22 21:58:43 +00006518 } else {
6519 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006520 return DAG.getNode(ISD::FNEG, SDLoc(N), VT,
6521 DAG.getNode(ISD::FABS, SDLoc(N0), VT, N0));
Dan Gohman760f86f2009-01-22 21:58:43 +00006522 }
Chris Lattner12d83032006-03-05 05:30:57 +00006523 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006524
Chris Lattner12d83032006-03-05 05:30:57 +00006525 // copysign(fabs(x), y) -> copysign(x, y)
6526 // copysign(fneg(x), y) -> copysign(x, y)
6527 // copysign(copysign(x,z), y) -> copysign(x, y)
6528 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
6529 N0.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006530 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006531 N0.getOperand(0), N1);
Chris Lattner12d83032006-03-05 05:30:57 +00006532
6533 // copysign(x, abs(y)) -> abs(x)
6534 if (N1.getOpcode() == ISD::FABS)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006535 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006536
Chris Lattner12d83032006-03-05 05:30:57 +00006537 // copysign(x, copysign(y,z)) -> copysign(x, z)
6538 if (N1.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006539 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006540 N0, N1.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006541
Chris Lattner12d83032006-03-05 05:30:57 +00006542 // copysign(x, fp_extend(y)) -> copysign(x, y)
6543 // copysign(x, fp_round(y)) -> copysign(x, y)
6544 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006545 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006546 N0, N1.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00006547
Dan Gohman475871a2008-07-27 21:46:04 +00006548 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00006549}
6550
Dan Gohman475871a2008-07-27 21:46:04 +00006551SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
6552 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00006553 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006554 EVT VT = N->getValueType(0);
6555 EVT OpVT = N0.getValueType();
Chris Lattnercda88752008-06-26 00:16:49 +00006556
Nate Begeman1d4d4142005-09-01 00:19:25 +00006557 // fold (sint_to_fp c1) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006558 if (N0C &&
Stuart Hastings7e334182011-03-02 19:36:30 +00006559 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00006560 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00006561 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006562 return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006563
Chris Lattnercda88752008-06-26 00:16:49 +00006564 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
6565 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006566 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
6567 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006568 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00006569 if (DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006570 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00006571 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00006572
Nadav Rotemed1a3352012-07-23 07:59:50 +00006573 // The next optimizations are desireable only if SELECT_CC can be lowered.
6574 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6575 // having to say they don't support SELECT_CC on every type the DAG knows
6576 // about, since there is no way to mark an opcode illegal at all value types
6577 // (See also visitSELECT)
6578 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6579 // fold (sint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
6580 if (N0.getOpcode() == ISD::SETCC && N0.getValueType() == MVT::i1 &&
6581 !VT.isVector() &&
6582 (!LegalOperations ||
6583 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6584 SDValue Ops[] =
6585 { N0.getOperand(0), N0.getOperand(1),
6586 DAG.getConstantFP(-1.0, VT) , DAG.getConstantFP(0.0, VT),
6587 N0.getOperand(2) };
Andrew Trickac6d9be2013-05-25 02:42:55 +00006588 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotemed1a3352012-07-23 07:59:50 +00006589 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006590
Nadav Rotemed1a3352012-07-23 07:59:50 +00006591 // fold (sint_to_fp (zext (setcc x, y, cc))) ->
6592 // (select_cc x, y, 1.0, 0.0,, cc)
6593 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
6594 N0.getOperand(0).getOpcode() == ISD::SETCC &&!VT.isVector() &&
6595 (!LegalOperations ||
6596 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6597 SDValue Ops[] =
6598 { N0.getOperand(0).getOperand(0), N0.getOperand(0).getOperand(1),
6599 DAG.getConstantFP(1.0, VT) , DAG.getConstantFP(0.0, VT),
6600 N0.getOperand(0).getOperand(2) };
Andrew Trickac6d9be2013-05-25 02:42:55 +00006601 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotemed1a3352012-07-23 07:59:50 +00006602 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006603 }
6604
Dan Gohman475871a2008-07-27 21:46:04 +00006605 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006606}
6607
Dan Gohman475871a2008-07-27 21:46:04 +00006608SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
6609 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00006610 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006611 EVT VT = N->getValueType(0);
6612 EVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00006613
Nate Begeman1d4d4142005-09-01 00:19:25 +00006614 // fold (uint_to_fp c1) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006615 if (N0C &&
Stuart Hastings7e334182011-03-02 19:36:30 +00006616 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00006617 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00006618 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006619 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006620
Chris Lattnercda88752008-06-26 00:16:49 +00006621 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
6622 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006623 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
6624 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006625 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00006626 if (DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006627 return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00006628 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006629
Nadav Rotemed1a3352012-07-23 07:59:50 +00006630 // The next optimizations are desireable only if SELECT_CC can be lowered.
6631 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6632 // having to say they don't support SELECT_CC on every type the DAG knows
6633 // about, since there is no way to mark an opcode illegal at all value types
6634 // (See also visitSELECT)
6635 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6636 // fold (uint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
Owen Andersond9bf71f2012-07-09 20:31:12 +00006637
Nadav Rotemed1a3352012-07-23 07:59:50 +00006638 if (N0.getOpcode() == ISD::SETCC && !VT.isVector() &&
6639 (!LegalOperations ||
6640 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6641 SDValue Ops[] =
6642 { N0.getOperand(0), N0.getOperand(1),
6643 DAG.getConstantFP(1.0, VT), DAG.getConstantFP(0.0, VT),
6644 N0.getOperand(2) };
Andrew Trickac6d9be2013-05-25 02:42:55 +00006645 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotemed1a3352012-07-23 07:59:50 +00006646 }
6647 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006648
Dan Gohman475871a2008-07-27 21:46:04 +00006649 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006650}
6651
Dan Gohman475871a2008-07-27 21:46:04 +00006652SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
6653 SDValue N0 = N->getOperand(0);
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_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00006658 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006659 return DAG.getNode(ISD::FP_TO_SINT, SDLoc(N), VT, N0);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006660
Dan Gohman475871a2008-07-27 21:46:04 +00006661 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006662}
6663
Dan Gohman475871a2008-07-27 21:46:04 +00006664SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
6665 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006666 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006667 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006668
Nate Begeman1d4d4142005-09-01 00:19:25 +00006669 // fold (fp_to_uint c1fp) -> c1
Ulrich Weigande669c932012-10-29 18:35:49 +00006670 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006671 return DAG.getNode(ISD::FP_TO_UINT, SDLoc(N), VT, N0);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006672
Dan Gohman475871a2008-07-27 21:46:04 +00006673 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006674}
6675
Dan Gohman475871a2008-07-27 21:46:04 +00006676SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
6677 SDValue N0 = N->getOperand(0);
6678 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006679 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006680 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006681
Nate Begeman1d4d4142005-09-01 00:19:25 +00006682 // fold (fp_round c1fp) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006683 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006684 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006685
Chris Lattner79dbea52006-03-13 06:26:26 +00006686 // fold (fp_round (fp_extend x)) -> x
6687 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
6688 return N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006689
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00006690 // fold (fp_round (fp_round x)) -> (fp_round x)
6691 if (N0.getOpcode() == ISD::FP_ROUND) {
6692 // This is a value preserving truncation if both round's are.
6693 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006694 N0.getNode()->getConstantOperandVal(1) == 1;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006695 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0.getOperand(0),
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00006696 DAG.getIntPtrConstant(IsTrunc));
6697 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006698
Chris Lattner79dbea52006-03-13 06:26:26 +00006699 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00006700 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006701 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, SDLoc(N0), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006702 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00006703 AddToWorkList(Tmp.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006704 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006705 Tmp, N0.getOperand(1));
Chris Lattner79dbea52006-03-13 06:26:26 +00006706 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006707
Dan Gohman475871a2008-07-27 21:46:04 +00006708 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006709}
6710
Dan Gohman475871a2008-07-27 21:46:04 +00006711SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
6712 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006713 EVT VT = N->getValueType(0);
6714 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00006715 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006716
Nate Begeman1d4d4142005-09-01 00:19:25 +00006717 // fold (fp_round_inreg c1fp) -> c1fp
Chris Lattner2392ae72010-04-15 04:48:01 +00006718 if (N0CFP && isTypeLegal(EVT)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00006719 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006720 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006721 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00006722
Dan Gohman475871a2008-07-27 21:46:04 +00006723 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006724}
6725
Dan Gohman475871a2008-07-27 21:46:04 +00006726SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
6727 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006728 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006729 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006730
Chris Lattner5938bef2007-12-29 06:55:23 +00006731 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Scott Michelfdc40a02009-02-17 22:15:04 +00006732 if (N->hasOneUse() &&
Dan Gohmane7852d02009-01-26 04:35:06 +00006733 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman475871a2008-07-27 21:46:04 +00006734 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00006735
Nate Begeman1d4d4142005-09-01 00:19:25 +00006736 // fold (fp_extend c1fp) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006737 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006738 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00006739
6740 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
6741 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00006742 if (N0.getOpcode() == ISD::FP_ROUND
6743 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00006744 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00006745 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00006746 if (VT.bitsLT(In.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006747 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006748 In, N0.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006749 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, In);
Chris Lattner0bd48932008-01-17 07:00:52 +00006750 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006751
Chris Lattner0bd48932008-01-17 07:00:52 +00006752 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Hal Finkel03c8f8f2013-10-04 22:18:12 +00006753 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00006754 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00006755 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00006756 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006757 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006758 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00006759 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00006760 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00006761 LN0->isVolatile(), LN0->isNonTemporal(),
6762 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00006763 CombineTo(N, ExtLoad);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006764 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00006765 DAG.getNode(ISD::FP_ROUND, SDLoc(N0),
Bill Wendling0225a1d2009-01-30 23:15:49 +00006766 N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00006767 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00006768 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00006769 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00006770
Dan Gohman475871a2008-07-27 21:46:04 +00006771 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006772}
6773
Dan Gohman475871a2008-07-27 21:46:04 +00006774SDValue DAGCombiner::visitFNEG(SDNode *N) {
6775 SDValue N0 = N->getOperand(0);
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006776 EVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00006777
Craig Topperdd201ff2012-09-11 01:45:21 +00006778 if (VT.isVector()) {
6779 SDValue FoldedVOp = SimplifyVUnaryOp(N);
6780 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper956342b2012-09-09 22:58:45 +00006781 }
6782
Owen Andersonafd3d562012-03-06 00:29:31 +00006783 if (isNegatibleForFree(N0, LegalOperations, DAG.getTargetLoweringInfo(),
6784 &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00006785 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohman23ff1822007-07-02 15:48:56 +00006786
Chris Lattner3bd39d42008-01-27 17:42:27 +00006787 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
6788 // constant pool values.
Owen Anderson29f60f32012-04-02 22:10:29 +00006789 if (!TLI.isFNegFree(VT) && N0.getOpcode() == ISD::BITCAST &&
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006790 !VT.isVector() &&
6791 N0.getNode()->hasOneUse() &&
6792 N0.getOperand(0).getValueType().isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006793 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006794 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006795 if (IntVT.isInteger() && !IntVT.isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006796 Int = DAG.getNode(ISD::XOR, SDLoc(N0), IntVT, Int,
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006797 DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00006798 AddToWorkList(Int.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006799 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006800 VT, Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00006801 }
6802 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006803
Owen Anderson58d57292012-09-01 06:04:27 +00006804 // (fneg (fmul c, x)) -> (fmul -c, x)
6805 if (N0.getOpcode() == ISD::FMUL) {
6806 ConstantFPSDNode *CFP1 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
Stephen Linb4940152013-07-09 00:44:49 +00006807 if (CFP1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006808 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson58d57292012-09-01 06:04:27 +00006809 N0.getOperand(0),
Andrew Trickac6d9be2013-05-25 02:42:55 +00006810 DAG.getNode(ISD::FNEG, SDLoc(N), VT,
Owen Anderson58d57292012-09-01 06:04:27 +00006811 N0.getOperand(1)));
Owen Anderson58d57292012-09-01 06:04:27 +00006812 }
6813
Dan Gohman475871a2008-07-27 21:46:04 +00006814 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006815}
6816
Owen Anderson7c626d32012-08-13 23:32:49 +00006817SDValue DAGCombiner::visitFCEIL(SDNode *N) {
6818 SDValue N0 = N->getOperand(0);
6819 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6820 EVT VT = N->getValueType(0);
6821
6822 // fold (fceil c1) -> fceil(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006823 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006824 return DAG.getNode(ISD::FCEIL, SDLoc(N), VT, N0);
Owen Anderson7c626d32012-08-13 23:32:49 +00006825
6826 return SDValue();
6827}
6828
6829SDValue DAGCombiner::visitFTRUNC(SDNode *N) {
6830 SDValue N0 = N->getOperand(0);
6831 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6832 EVT VT = N->getValueType(0);
6833
6834 // fold (ftrunc c1) -> ftrunc(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006835 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006836 return DAG.getNode(ISD::FTRUNC, SDLoc(N), VT, N0);
Owen Anderson7c626d32012-08-13 23:32:49 +00006837
6838 return SDValue();
6839}
6840
6841SDValue DAGCombiner::visitFFLOOR(SDNode *N) {
6842 SDValue N0 = N->getOperand(0);
6843 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6844 EVT VT = N->getValueType(0);
6845
6846 // fold (ffloor c1) -> ffloor(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006847 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006848 return DAG.getNode(ISD::FFLOOR, SDLoc(N), VT, N0);
Owen Anderson7c626d32012-08-13 23:32:49 +00006849
6850 return SDValue();
6851}
6852
Dan Gohman475871a2008-07-27 21:46:04 +00006853SDValue DAGCombiner::visitFABS(SDNode *N) {
6854 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006855 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006856 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006857
Craig Topperdd201ff2012-09-11 01:45:21 +00006858 if (VT.isVector()) {
6859 SDValue FoldedVOp = SimplifyVUnaryOp(N);
6860 if (FoldedVOp.getNode()) return FoldedVOp;
6861 }
6862
Nate Begeman1d4d4142005-09-01 00:19:25 +00006863 // fold (fabs c1) -> fabs(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006864 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006865 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006866 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00006867 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00006868 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006869 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00006870 // fold (fabs (fcopysign x, y)) -> (fabs x)
6871 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006872 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00006873
Chris Lattner3bd39d42008-01-27 17:42:27 +00006874 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
6875 // constant pool values.
Stephen Lin155615d2013-07-08 00:37:03 +00006876 if (!TLI.isFAbsFree(VT) &&
Owen Anderson29f60f32012-04-02 22:10:29 +00006877 N0.getOpcode() == ISD::BITCAST && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00006878 N0.getOperand(0).getValueType().isInteger() &&
6879 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006880 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006881 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006882 if (IntVT.isInteger() && !IntVT.isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006883 Int = DAG.getNode(ISD::AND, SDLoc(N0), IntVT, Int,
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006884 DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00006885 AddToWorkList(Int.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006886 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00006887 N->getValueType(0), Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00006888 }
6889 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006890
Dan Gohman475871a2008-07-27 21:46:04 +00006891 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006892}
6893
Dan Gohman475871a2008-07-27 21:46:04 +00006894SDValue DAGCombiner::visitBRCOND(SDNode *N) {
6895 SDValue Chain = N->getOperand(0);
6896 SDValue N1 = N->getOperand(1);
6897 SDValue N2 = N->getOperand(2);
Scott Michelfdc40a02009-02-17 22:15:04 +00006898
Dan Gohmane0f06c72009-11-17 00:47:23 +00006899 // If N is a constant we could fold this into a fallthrough or unconditional
6900 // branch. However that doesn't happen very often in normal code, because
6901 // Instcombine/SimplifyCFG should have handled the available opportunities.
6902 // If we did this folding here, it would be necessary to update the
6903 // MachineBasicBlock CFG, which is awkward.
6904
Nate Begeman750ac1b2006-02-01 07:19:44 +00006905 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
6906 // on the target.
Scott Michelfdc40a02009-02-17 22:15:04 +00006907 if (N1.getOpcode() == ISD::SETCC &&
Tom Stellard3ef53832013-03-08 15:36:57 +00006908 TLI.isOperationLegalOrCustom(ISD::BR_CC,
6909 N1.getOperand(0).getValueType())) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006910 return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00006911 Chain, N1.getOperand(2),
Nate Begeman750ac1b2006-02-01 07:19:44 +00006912 N1.getOperand(0), N1.getOperand(1), N2);
6913 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00006914
Evan Cheng2a135ae2010-10-04 22:41:01 +00006915 if ((N1.hasOneUse() && N1.getOpcode() == ISD::SRL) ||
6916 ((N1.getOpcode() == ISD::TRUNCATE && N1.hasOneUse()) &&
6917 (N1.getOperand(0).hasOneUse() &&
6918 N1.getOperand(0).getOpcode() == ISD::SRL))) {
6919 SDNode *Trunc = 0;
6920 if (N1.getOpcode() == ISD::TRUNCATE) {
6921 // Look pass the truncate.
6922 Trunc = N1.getNode();
6923 N1 = N1.getOperand(0);
6924 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006925
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006926 // Match this pattern so that we can generate simpler code:
6927 //
6928 // %a = ...
6929 // %b = and i32 %a, 2
6930 // %c = srl i32 %b, 1
6931 // brcond i32 %c ...
6932 //
6933 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006934 //
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006935 // %a = ...
Evan Chengd40d03e2010-01-06 19:38:29 +00006936 // %b = and i32 %a, 2
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006937 // %c = setcc eq %b, 0
6938 // brcond %c ...
6939 //
6940 // This applies only when the AND constant value has one bit set and the
6941 // SRL constant is equal to the log2 of the AND constant. The back-end is
6942 // smart enough to convert the result into a TEST/JMP sequence.
6943 SDValue Op0 = N1.getOperand(0);
6944 SDValue Op1 = N1.getOperand(1);
6945
6946 if (Op0.getOpcode() == ISD::AND &&
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006947 Op1.getOpcode() == ISD::Constant) {
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006948 SDValue AndOp1 = Op0.getOperand(1);
6949
6950 if (AndOp1.getOpcode() == ISD::Constant) {
6951 const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue();
6952
6953 if (AndConst.isPowerOf2() &&
6954 cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) {
6955 SDValue SetCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00006956 DAG.getSetCC(SDLoc(N),
Matt Arsenault225ed702013-05-18 00:21:46 +00006957 getSetCCResultType(Op0.getValueType()),
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006958 Op0, DAG.getConstant(0, Op0.getValueType()),
6959 ISD::SETNE);
6960
Andrew Trickac6d9be2013-05-25 02:42:55 +00006961 SDValue NewBRCond = DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Chengd40d03e2010-01-06 19:38:29 +00006962 MVT::Other, Chain, SetCC, N2);
6963 // Don't add the new BRCond into the worklist or else SimplifySelectCC
6964 // will convert it back to (X & C1) >> C2.
6965 CombineTo(N, NewBRCond, false);
6966 // Truncate is dead.
6967 if (Trunc) {
6968 removeFromWorkList(Trunc);
6969 DAG.DeleteNode(Trunc);
6970 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006971 // Replace the uses of SRL with SETCC
Evan Cheng2c755ba2010-02-27 07:36:59 +00006972 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006973 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006974 removeFromWorkList(N1.getNode());
6975 DAG.DeleteNode(N1.getNode());
Evan Chengd40d03e2010-01-06 19:38:29 +00006976 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006977 }
6978 }
6979 }
Evan Cheng2a135ae2010-10-04 22:41:01 +00006980
6981 if (Trunc)
6982 // Restore N1 if the above transformation doesn't match.
6983 N1 = N->getOperand(1);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006984 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006985
Evan Cheng2c755ba2010-02-27 07:36:59 +00006986 // Transform br(xor(x, y)) -> br(x != y)
6987 // Transform br(xor(xor(x,y), 1)) -> br (x == y)
6988 if (N1.hasOneUse() && N1.getOpcode() == ISD::XOR) {
6989 SDNode *TheXor = N1.getNode();
6990 SDValue Op0 = TheXor->getOperand(0);
6991 SDValue Op1 = TheXor->getOperand(1);
6992 if (Op0.getOpcode() == Op1.getOpcode()) {
6993 // Avoid missing important xor optimizations.
6994 SDValue Tmp = visitXOR(TheXor);
Evan Cheng78ec0252013-01-09 20:56:40 +00006995 if (Tmp.getNode()) {
6996 if (Tmp.getNode() != TheXor) {
6997 DEBUG(dbgs() << "\nReplacing.8 ";
6998 TheXor->dump(&DAG);
6999 dbgs() << "\nWith: ";
7000 Tmp.getNode()->dump(&DAG);
7001 dbgs() << '\n');
7002 WorkListRemover DeadNodes(*this);
7003 DAG.ReplaceAllUsesOfValueWith(N1, Tmp);
7004 removeFromWorkList(TheXor);
7005 DAG.DeleteNode(TheXor);
Andrew Trickac6d9be2013-05-25 02:42:55 +00007006 return DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Cheng78ec0252013-01-09 20:56:40 +00007007 MVT::Other, Chain, Tmp, N2);
7008 }
7009
Benjamin Kramer0b68b752013-03-30 21:28:18 +00007010 // visitXOR has changed XOR's operands or replaced the XOR completely,
7011 // bail out.
7012 return SDValue(N, 0);
Evan Cheng2c755ba2010-02-27 07:36:59 +00007013 }
7014 }
7015
7016 if (Op0.getOpcode() != ISD::SETCC && Op1.getOpcode() != ISD::SETCC) {
7017 bool Equal = false;
7018 if (ConstantSDNode *RHSCI = dyn_cast<ConstantSDNode>(Op0))
7019 if (RHSCI->getAPIntValue() == 1 && Op0.hasOneUse() &&
7020 Op0.getOpcode() == ISD::XOR) {
7021 TheXor = Op0.getNode();
7022 Equal = true;
7023 }
7024
Evan Cheng2a135ae2010-10-04 22:41:01 +00007025 EVT SetCCVT = N1.getValueType();
Evan Cheng2c755ba2010-02-27 07:36:59 +00007026 if (LegalTypes)
Matt Arsenault225ed702013-05-18 00:21:46 +00007027 SetCCVT = getSetCCResultType(SetCCVT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00007028 SDValue SetCC = DAG.getSetCC(SDLoc(TheXor),
Evan Cheng2c755ba2010-02-27 07:36:59 +00007029 SetCCVT,
7030 Op0, Op1,
7031 Equal ? ISD::SETEQ : ISD::SETNE);
7032 // Replace the uses of XOR with SETCC
7033 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007034 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Evan Cheng2a135ae2010-10-04 22:41:01 +00007035 removeFromWorkList(N1.getNode());
7036 DAG.DeleteNode(N1.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00007037 return DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Cheng2c755ba2010-02-27 07:36:59 +00007038 MVT::Other, Chain, SetCC, N2);
7039 }
7040 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00007041
Dan Gohman475871a2008-07-27 21:46:04 +00007042 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00007043}
7044
Chris Lattner3ea0b472005-10-05 06:47:48 +00007045// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
7046//
Dan Gohman475871a2008-07-27 21:46:04 +00007047SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00007048 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00007049 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Scott Michelfdc40a02009-02-17 22:15:04 +00007050
Dan Gohmane0f06c72009-11-17 00:47:23 +00007051 // If N is a constant we could fold this into a fallthrough or unconditional
7052 // branch. However that doesn't happen very often in normal code, because
7053 // Instcombine/SimplifyCFG should have handled the available opportunities.
7054 // If we did this folding here, it would be necessary to update the
7055 // MachineBasicBlock CFG, which is awkward.
7056
Duncan Sands8eab8a22008-06-09 11:32:28 +00007057 // Use SimplifySetCC to simplify SETCC's.
Matt Arsenault225ed702013-05-18 00:21:46 +00007058 SDValue Simp = SimplifySetCC(getSetCCResultType(CondLHS.getValueType()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00007059 CondLHS, CondRHS, CC->get(), SDLoc(N),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00007060 false);
Gabor Greifba36cb52008-08-28 21:40:38 +00007061 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00007062
Nate Begemane17daeb2005-10-05 21:43:42 +00007063 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00007064 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Andrew Trickac6d9be2013-05-25 02:42:55 +00007065 return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00007066 N->getOperand(0), Simp.getOperand(2),
7067 Simp.getOperand(0), Simp.getOperand(1),
7068 N->getOperand(4));
7069
Dan Gohman475871a2008-07-27 21:46:04 +00007070 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00007071}
7072
Evan Chengc4b527a2012-01-13 01:37:24 +00007073/// canFoldInAddressingMode - Return true if 'Use' is a load or a store that
7074/// uses N as its base pointer and that N may be folded in the load / store
Evan Cheng03be3622012-03-06 23:33:32 +00007075/// addressing mode.
Evan Chengc4b527a2012-01-13 01:37:24 +00007076static bool canFoldInAddressingMode(SDNode *N, SDNode *Use,
7077 SelectionDAG &DAG,
7078 const TargetLowering &TLI) {
7079 EVT VT;
7080 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Use)) {
7081 if (LD->isIndexed() || LD->getBasePtr().getNode() != N)
7082 return false;
7083 VT = Use->getValueType(0);
7084 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(Use)) {
7085 if (ST->isIndexed() || ST->getBasePtr().getNode() != N)
7086 return false;
7087 VT = ST->getValue().getValueType();
7088 } else
7089 return false;
7090
Chandler Carruth56d433d2013-01-07 15:14:13 +00007091 TargetLowering::AddrMode AM;
Evan Chengc4b527a2012-01-13 01:37:24 +00007092 if (N->getOpcode() == ISD::ADD) {
7093 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
7094 if (Offset)
Evan Cheng03be3622012-03-06 23:33:32 +00007095 // [reg +/- imm]
Evan Chengc4b527a2012-01-13 01:37:24 +00007096 AM.BaseOffs = Offset->getSExtValue();
7097 else
Evan Cheng03be3622012-03-06 23:33:32 +00007098 // [reg +/- reg]
7099 AM.Scale = 1;
Evan Chengc4b527a2012-01-13 01:37:24 +00007100 } else if (N->getOpcode() == ISD::SUB) {
7101 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
7102 if (Offset)
Evan Cheng03be3622012-03-06 23:33:32 +00007103 // [reg +/- imm]
Evan Chengc4b527a2012-01-13 01:37:24 +00007104 AM.BaseOffs = -Offset->getSExtValue();
7105 else
Evan Cheng03be3622012-03-06 23:33:32 +00007106 // [reg +/- reg]
7107 AM.Scale = 1;
Evan Chengc4b527a2012-01-13 01:37:24 +00007108 } else
7109 return false;
7110
7111 return TLI.isLegalAddressingMode(AM, VT.getTypeForEVT(*DAG.getContext()));
7112}
7113
Duncan Sandsec87aa82008-06-15 20:12:31 +00007114/// CombineToPreIndexedLoadStore - Try turning a load / store into a
7115/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00007116/// and it has other uses besides the load / store. After the
7117/// transformation, the new indexed load / store has effectively folded
7118/// the add / subtract in and all of its other uses are redirected to the
7119/// new load / store.
7120bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00007121 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00007122 return false;
7123
7124 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007125 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00007126 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00007127 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007128 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007129 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007130 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00007131 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00007132 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
7133 return false;
7134 Ptr = LD->getBasePtr();
7135 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007136 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007137 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007138 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007139 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
7140 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
7141 return false;
7142 Ptr = ST->getBasePtr();
7143 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007144 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00007145 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007146 }
Chris Lattner448f2192006-11-11 00:39:41 +00007147
Chris Lattner9f1794e2006-11-11 00:56:29 +00007148 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
7149 // out. There is no reason to make this a preinc/predec.
7150 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00007151 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00007152 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00007153
Chris Lattner9f1794e2006-11-11 00:56:29 +00007154 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00007155 SDValue BasePtr;
7156 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007157 ISD::MemIndexedMode AM = ISD::UNINDEXED;
7158 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
7159 return false;
Hal Finkel089a5f82013-02-08 21:35:47 +00007160
7161 // Backends without true r+i pre-indexed forms may need to pass a
7162 // constant base with a variable offset so that constant coercion
7163 // will work with the patterns in canonical form.
7164 bool Swapped = false;
7165 if (isa<ConstantSDNode>(BasePtr)) {
7166 std::swap(BasePtr, Offset);
7167 Swapped = true;
7168 }
7169
Evan Chenga7d4a042007-05-03 23:52:19 +00007170 // Don't create a indexed load / store with zero offset.
7171 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00007172 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00007173 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007174
Chris Lattner41e53fd2006-11-11 01:00:15 +00007175 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00007176 // 1) The new base ptr is a frame index.
7177 // 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 +00007178 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00007179 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00007180 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00007181 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00007182
Chris Lattner41e53fd2006-11-11 01:00:15 +00007183 // Check #1. Preinc'ing a frame index would require copying the stack pointer
7184 // (plus the implicit offset) to a register to preinc anyway.
Evan Chengcaab1292009-05-06 18:25:01 +00007185 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
Chris Lattner41e53fd2006-11-11 01:00:15 +00007186 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007187
Chris Lattner41e53fd2006-11-11 01:00:15 +00007188 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00007189 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00007190 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007191 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007192 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00007193 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007194
Hal Finkel089a5f82013-02-08 21:35:47 +00007195 // If the offset is a constant, there may be other adds of constants that
7196 // can be folded with this one. We should do this to avoid having to keep
7197 // a copy of the original base pointer.
7198 SmallVector<SDNode *, 16> OtherUses;
7199 if (isa<ConstantSDNode>(Offset))
7200 for (SDNode::use_iterator I = BasePtr.getNode()->use_begin(),
7201 E = BasePtr.getNode()->use_end(); I != E; ++I) {
7202 SDNode *Use = *I;
7203 if (Use == Ptr.getNode())
7204 continue;
7205
7206 if (Use->isPredecessorOf(N))
7207 continue;
7208
7209 if (Use->getOpcode() != ISD::ADD && Use->getOpcode() != ISD::SUB) {
7210 OtherUses.clear();
7211 break;
7212 }
7213
7214 SDValue Op0 = Use->getOperand(0), Op1 = Use->getOperand(1);
7215 if (Op1.getNode() == BasePtr.getNode())
7216 std::swap(Op0, Op1);
7217 assert(Op0.getNode() == BasePtr.getNode() &&
7218 "Use of ADD/SUB but not an operand");
7219
7220 if (!isa<ConstantSDNode>(Op1)) {
7221 OtherUses.clear();
7222 break;
7223 }
7224
7225 // FIXME: In some cases, we can be smarter about this.
7226 if (Op1.getValueType() != Offset.getValueType()) {
7227 OtherUses.clear();
7228 break;
7229 }
7230
7231 OtherUses.push_back(Use);
7232 }
7233
7234 if (Swapped)
7235 std::swap(BasePtr, Offset);
7236
Evan Chengc843abe2007-05-24 02:35:39 +00007237 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00007238 bool RealUse = false;
Lang Hames944520f2011-07-07 04:31:51 +00007239
7240 // Caches for hasPredecessorHelper
7241 SmallPtrSet<const SDNode *, 32> Visited;
7242 SmallVector<const SDNode *, 16> Worklist;
7243
Gabor Greifba36cb52008-08-28 21:40:38 +00007244 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
7245 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00007246 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007247 if (Use == N)
7248 continue;
Lang Hames944520f2011-07-07 04:31:51 +00007249 if (N->hasPredecessorHelper(Use, Visited, Worklist))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007250 return false;
7251
Evan Chengc4b527a2012-01-13 01:37:24 +00007252 // If Ptr may be folded in addressing mode of other use, then it's
7253 // not profitable to do this transformation.
7254 if (!canFoldInAddressingMode(Ptr.getNode(), Use, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007255 RealUse = true;
7256 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007257
Chris Lattner9f1794e2006-11-11 00:56:29 +00007258 if (!RealUse)
7259 return false;
7260
Dan Gohman475871a2008-07-27 21:46:04 +00007261 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007262 if (isLoad)
Andrew Trickac6d9be2013-05-25 02:42:55 +00007263 Result = DAG.getIndexedLoad(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007264 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007265 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00007266 Result = DAG.getIndexedStore(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007267 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007268 ++PreIndexedNodes;
7269 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00007270 DEBUG(dbgs() << "\nReplacing.4 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007271 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007272 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007273 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007274 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007275 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007276 if (isLoad) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007277 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
7278 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007279 } else {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007280 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007281 }
7282
Chris Lattner9f1794e2006-11-11 00:56:29 +00007283 // Finally, since the node is now dead, remove it from the graph.
7284 DAG.DeleteNode(N);
7285
Hal Finkel089a5f82013-02-08 21:35:47 +00007286 if (Swapped)
7287 std::swap(BasePtr, Offset);
7288
7289 // Replace other uses of BasePtr that can be updated to use Ptr
7290 for (unsigned i = 0, e = OtherUses.size(); i != e; ++i) {
7291 unsigned OffsetIdx = 1;
7292 if (OtherUses[i]->getOperand(OffsetIdx).getNode() == BasePtr.getNode())
7293 OffsetIdx = 0;
7294 assert(OtherUses[i]->getOperand(!OffsetIdx).getNode() ==
7295 BasePtr.getNode() && "Expected BasePtr operand");
7296
Silviu Baranga730a5702013-04-26 15:52:24 +00007297 // We need to replace ptr0 in the following expression:
7298 // x0 * offset0 + y0 * ptr0 = t0
7299 // knowing that
7300 // x1 * offset1 + y1 * ptr0 = t1 (the indexed load/store)
Stephen Lin155615d2013-07-08 00:37:03 +00007301 //
Silviu Baranga730a5702013-04-26 15:52:24 +00007302 // where x0, x1, y0 and y1 in {-1, 1} are given by the types of the
7303 // indexed load/store and the expresion that needs to be re-written.
7304 //
7305 // Therefore, we have:
7306 // t0 = (x0 * offset0 - x1 * y0 * y1 *offset1) + (y0 * y1) * t1
Hal Finkel089a5f82013-02-08 21:35:47 +00007307
7308 ConstantSDNode *CN =
7309 cast<ConstantSDNode>(OtherUses[i]->getOperand(OffsetIdx));
Silviu Baranga730a5702013-04-26 15:52:24 +00007310 int X0, X1, Y0, Y1;
7311 APInt Offset0 = CN->getAPIntValue();
7312 APInt Offset1 = cast<ConstantSDNode>(Offset)->getAPIntValue();
Hal Finkel089a5f82013-02-08 21:35:47 +00007313
Silviu Baranga730a5702013-04-26 15:52:24 +00007314 X0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 1) ? -1 : 1;
7315 Y0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 0) ? -1 : 1;
7316 X1 = (AM == ISD::PRE_DEC && !Swapped) ? -1 : 1;
7317 Y1 = (AM == ISD::PRE_DEC && Swapped) ? -1 : 1;
Hal Finkel089a5f82013-02-08 21:35:47 +00007318
Silviu Baranga730a5702013-04-26 15:52:24 +00007319 unsigned Opcode = (Y0 * Y1 < 0) ? ISD::SUB : ISD::ADD;
7320
7321 APInt CNV = Offset0;
7322 if (X0 < 0) CNV = -CNV;
7323 if (X1 * Y0 * Y1 < 0) CNV = CNV + Offset1;
7324 else CNV = CNV - Offset1;
7325
7326 // We can now generate the new expression.
7327 SDValue NewOp1 = DAG.getConstant(CNV, CN->getValueType(0));
7328 SDValue NewOp2 = Result.getValue(isLoad ? 1 : 0);
7329
7330 SDValue NewUse = DAG.getNode(Opcode,
Andrew Trickac6d9be2013-05-25 02:42:55 +00007331 SDLoc(OtherUses[i]),
Hal Finkel089a5f82013-02-08 21:35:47 +00007332 OtherUses[i]->getValueType(0), NewOp1, NewOp2);
7333 DAG.ReplaceAllUsesOfValueWith(SDValue(OtherUses[i], 0), NewUse);
7334 removeFromWorkList(OtherUses[i]);
7335 DAG.DeleteNode(OtherUses[i]);
7336 }
7337
Chris Lattner9f1794e2006-11-11 00:56:29 +00007338 // Replace the uses of Ptr with uses of the updated base value.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007339 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0));
Gabor Greifba36cb52008-08-28 21:40:38 +00007340 removeFromWorkList(Ptr.getNode());
7341 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00007342
7343 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00007344}
7345
Duncan Sandsec87aa82008-06-15 20:12:31 +00007346/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00007347/// add / sub of the base pointer node into a post-indexed load / store.
7348/// The transformation folded the add / subtract into the new indexed
7349/// load / store effectively and all of its uses are redirected to the
7350/// new load / store.
7351bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00007352 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00007353 return false;
7354
7355 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007356 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00007357 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00007358 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007359 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007360 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007361 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007362 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
7363 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
7364 return false;
7365 Ptr = LD->getBasePtr();
7366 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007367 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007368 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007369 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007370 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
7371 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
7372 return false;
7373 Ptr = ST->getBasePtr();
7374 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007375 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00007376 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007377 }
Chris Lattner448f2192006-11-11 00:39:41 +00007378
Gabor Greifba36cb52008-08-28 21:40:38 +00007379 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00007380 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007381
Gabor Greifba36cb52008-08-28 21:40:38 +00007382 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
7383 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00007384 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007385 if (Op == N ||
7386 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
7387 continue;
7388
Dan Gohman475871a2008-07-27 21:46:04 +00007389 SDValue BasePtr;
7390 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007391 ISD::MemIndexedMode AM = ISD::UNINDEXED;
7392 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
Evan Chenga7d4a042007-05-03 23:52:19 +00007393 // Don't create a indexed load / store with zero offset.
7394 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00007395 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00007396 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00007397
Chris Lattner9f1794e2006-11-11 00:56:29 +00007398 // Try turning it into a post-indexed load / store except when
Evan Chengc4b527a2012-01-13 01:37:24 +00007399 // 1) All uses are load / store ops that use it as base ptr (and
7400 // it may be folded as addressing mmode).
Chris Lattner9f1794e2006-11-11 00:56:29 +00007401 // 2) Op must be independent of N, i.e. Op is neither a predecessor
7402 // nor a successor of N. Otherwise, if Op is folded that would
7403 // create a cycle.
7404
Evan Chengcaab1292009-05-06 18:25:01 +00007405 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
7406 continue;
7407
Chris Lattner9f1794e2006-11-11 00:56:29 +00007408 // Check for #1.
7409 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00007410 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
7411 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00007412 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00007413 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00007414 continue;
7415
Chris Lattner9f1794e2006-11-11 00:56:29 +00007416 // If all the uses are load / store addresses, then don't do the
7417 // transformation.
7418 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
7419 bool RealUse = false;
7420 for (SDNode::use_iterator III = Use->use_begin(),
7421 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00007422 SDNode *UseUse = *III;
Stephen Lin155615d2013-07-08 00:37:03 +00007423 if (!canFoldInAddressingMode(Use, UseUse, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007424 RealUse = true;
7425 }
Chris Lattner448f2192006-11-11 00:39:41 +00007426
Chris Lattner9f1794e2006-11-11 00:56:29 +00007427 if (!RealUse) {
7428 TryNext = true;
7429 break;
Chris Lattner448f2192006-11-11 00:39:41 +00007430 }
7431 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007432 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007433
Chris Lattner9f1794e2006-11-11 00:56:29 +00007434 if (TryNext)
7435 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00007436
Chris Lattner9f1794e2006-11-11 00:56:29 +00007437 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00007438 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00007439 SDValue Result = isLoad
Andrew Trickac6d9be2013-05-25 02:42:55 +00007440 ? DAG.getIndexedLoad(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007441 BasePtr, Offset, AM)
Andrew Trickac6d9be2013-05-25 02:42:55 +00007442 : DAG.getIndexedStore(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007443 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007444 ++PostIndexedNodes;
7445 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00007446 DEBUG(dbgs() << "\nReplacing.5 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007447 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007448 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007449 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007450 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007451 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007452 if (isLoad) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007453 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
7454 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007455 } else {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007456 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattner448f2192006-11-11 00:39:41 +00007457 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007458
Chris Lattner9f1794e2006-11-11 00:56:29 +00007459 // Finally, since the node is now dead, remove it from the graph.
7460 DAG.DeleteNode(N);
7461
7462 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00007463 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007464 Result.getValue(isLoad ? 1 : 0));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007465 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007466 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007467 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00007468 }
7469 }
7470 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007471
Chris Lattner448f2192006-11-11 00:39:41 +00007472 return false;
7473}
7474
Dan Gohman475871a2008-07-27 21:46:04 +00007475SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00007476 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007477 SDValue Chain = LD->getChain();
7478 SDValue Ptr = LD->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00007479
Evan Cheng45a7ca92007-05-01 00:38:21 +00007480 // If load is not volatile and there are no uses of the loaded value (and
7481 // the updated indexed value in case of indexed loads), change uses of the
7482 // chain value into uses of the chain input (i.e. delete the dead load).
7483 if (!LD->isVolatile()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007484 if (N->getValueType(1) == MVT::Other) {
Evan Cheng498f5592007-05-01 08:53:39 +00007485 // Unindexed loads.
Craig Topper704e1a02012-01-07 18:31:09 +00007486 if (!N->hasAnyUseOfValue(0)) {
Evan Cheng02c42852008-01-16 23:11:54 +00007487 // It's not safe to use the two value CombineTo variant here. e.g.
7488 // v1, chain2 = load chain1, loc
7489 // v2, chain3 = load chain2, loc
7490 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00007491 // Now we replace use of chain2 with chain1. This makes the second load
7492 // isomorphic to the one we are deleting, and thus makes this load live.
David Greenef1090292010-01-05 01:25:00 +00007493 DEBUG(dbgs() << "\nReplacing.6 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007494 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007495 dbgs() << "\nWith chain: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007496 Chain.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007497 dbgs() << "\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007498 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007499 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain);
Bill Wendlingc0debad2009-01-30 23:27:35 +00007500
Chris Lattner125991a2008-01-24 07:57:06 +00007501 if (N->use_empty()) {
7502 removeFromWorkList(N);
7503 DAG.DeleteNode(N);
7504 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007505
Dan Gohman475871a2008-07-27 21:46:04 +00007506 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00007507 }
Evan Cheng498f5592007-05-01 08:53:39 +00007508 } else {
7509 // Indexed loads.
Owen Anderson825b72b2009-08-11 20:47:22 +00007510 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
Craig Topper704e1a02012-01-07 18:31:09 +00007511 if (!N->hasAnyUseOfValue(0) && !N->hasAnyUseOfValue(1)) {
Dale Johannesene8d72302009-02-06 23:05:02 +00007512 SDValue Undef = DAG.getUNDEF(N->getValueType(0));
Evan Cheng2c755ba2010-02-27 07:36:59 +00007513 DEBUG(dbgs() << "\nReplacing.7 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007514 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007515 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007516 Undef.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007517 dbgs() << " and 2 other values\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007518 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007519 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef);
Dan Gohman475871a2008-07-27 21:46:04 +00007520 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007521 DAG.getUNDEF(N->getValueType(1)));
7522 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain);
Evan Cheng02c42852008-01-16 23:11:54 +00007523 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00007524 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007525 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00007526 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00007527 }
7528 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007529
Chris Lattner01a22022005-10-10 22:04:48 +00007530 // If this load is directly stored, replace the load value with the stored
7531 // value.
7532 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007533 // TODO: Handle TRUNCSTORE/LOADEXT
Evan Cheng9ef82ce2011-03-11 00:48:56 +00007534 if (ISD::isNormalLoad(N) && !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007535 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00007536 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
7537 if (PrevST->getBasePtr() == Ptr &&
7538 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007539 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00007540 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007541 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007542
Evan Cheng255f20f2010-04-01 06:04:33 +00007543 // Try to infer better alignment information than the load already has.
7544 if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00007545 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
Owen Andersonb48783b2013-02-05 19:24:39 +00007546 if (Align > LD->getMemOperand()->getBaseAlignment()) {
7547 SDValue NewLoad =
Andrew Trickac6d9be2013-05-25 02:42:55 +00007548 DAG.getExtLoad(LD->getExtensionType(), SDLoc(N),
Evan Chenged1c0c72011-11-28 22:37:34 +00007549 LD->getValueType(0),
7550 Chain, Ptr, LD->getPointerInfo(),
7551 LD->getMemoryVT(),
7552 LD->isVolatile(), LD->isNonTemporal(), Align);
Owen Andersonb48783b2013-02-05 19:24:39 +00007553 return CombineTo(N, NewLoad, SDValue(NewLoad.getNode(), 1), true);
7554 }
Evan Cheng255f20f2010-04-01 06:04:33 +00007555 }
7556 }
7557
Hal Finkel253acef2013-08-29 03:29:55 +00007558 bool UseAA = CombinerAA.getNumOccurrences() > 0 ? CombinerAA :
7559 TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA();
7560 if (UseAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00007561 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007562 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00007563
Jim Laskey6ff23e52006-10-04 16:53:27 +00007564 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00007565 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00007566 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007567
Jim Laskey279f0532006-09-25 16:29:54 +00007568 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007569 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007570 ReplLoad = DAG.getLoad(N->getValueType(0), SDLoc(LD),
Chris Lattnerfa459012010-09-21 16:08:50 +00007571 BetterChain, Ptr, LD->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00007572 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007573 LD->isInvariant(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007574 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007575 ReplLoad = DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD),
Stuart Hastingsa9011292011-02-16 16:23:55 +00007576 LD->getValueType(0),
Chris Lattnerfa459012010-09-21 16:08:50 +00007577 BetterChain, Ptr, LD->getPointerInfo(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007578 LD->getMemoryVT(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007579 LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00007580 LD->isNonTemporal(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00007581 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007582 }
Jim Laskey279f0532006-09-25 16:29:54 +00007583
Jim Laskey6ff23e52006-10-04 16:53:27 +00007584 // Create token factor to keep old chain connected.
Andrew Trickac6d9be2013-05-25 02:42:55 +00007585 SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson825b72b2009-08-11 20:47:22 +00007586 MVT::Other, Chain, ReplLoad.getValue(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007587
Nate Begemanb6aef5c2009-09-15 00:18:30 +00007588 // Make sure the new and old chains are cleaned up.
7589 AddToWorkList(Token.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007590
Jim Laskey274062c2006-10-13 23:32:28 +00007591 // Replace uses with load result and token factor. Don't add users
7592 // to work list.
7593 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00007594 }
7595 }
7596
Evan Cheng7fc033a2006-11-03 03:06:21 +00007597 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00007598 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00007599 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00007600
Quentin Colombetc34693f2013-10-11 18:01:14 +00007601 // Try to slice up N to more direct loads if the slices are mapped to
7602 // different register banks or pairing can take place.
7603 if (SliceUpLoad(N))
7604 return SDValue(N, 0);
7605
Dan Gohman475871a2008-07-27 21:46:04 +00007606 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00007607}
7608
Quentin Colombetc34693f2013-10-11 18:01:14 +00007609namespace {
7610/// \brief Helper structure used to slice a load in smaller loads.
7611/// Basically a slice is obtained from the following sequence:
7612/// Origin = load Ty1, Base
7613/// Shift = srl Ty1 Origin, CstTy Amount
7614/// Inst = trunc Shift to Ty2
7615///
7616/// Then, it will be rewriten into:
7617/// Slice = load SliceTy, Base + SliceOffset
7618/// [Inst = zext Slice to Ty2], only if SliceTy <> Ty2
7619///
7620/// SliceTy is deduced from the number of bits that are actually used to
7621/// build Inst.
7622struct LoadedSlice {
7623 /// \brief Helper structure used to compute the cost of a slice.
7624 struct Cost {
7625 /// Are we optimizing for code size.
7626 bool ForCodeSize;
7627 /// Various cost.
7628 unsigned Loads;
7629 unsigned Truncates;
7630 unsigned CrossRegisterBanksCopies;
7631 unsigned ZExts;
7632 unsigned Shift;
7633
7634 Cost(bool ForCodeSize = false)
7635 : ForCodeSize(ForCodeSize), Loads(0), Truncates(0),
7636 CrossRegisterBanksCopies(0), ZExts(0), Shift(0) {}
7637
7638 /// \brief Get the cost of one isolated slice.
7639 Cost(const LoadedSlice &LS, bool ForCodeSize = false)
7640 : ForCodeSize(ForCodeSize), Loads(1), Truncates(0),
7641 CrossRegisterBanksCopies(0), ZExts(0), Shift(0) {
7642 EVT TruncType = LS.Inst->getValueType(0);
7643 EVT LoadedType = LS.getLoadedType();
7644 if (TruncType != LoadedType &&
7645 !LS.DAG->getTargetLoweringInfo().isZExtFree(LoadedType, TruncType))
7646 ZExts = 1;
7647 }
7648
7649 /// \brief Account for slicing gain in the current cost.
7650 /// Slicing provide a few gains like removing a shift or a
7651 /// truncate. This method allows to grow the cost of the original
7652 /// load with the gain from this slice.
7653 void addSliceGain(const LoadedSlice &LS) {
7654 // Each slice saves a truncate.
7655 const TargetLowering &TLI = LS.DAG->getTargetLoweringInfo();
7656 if (!TLI.isTruncateFree(LS.Inst->getValueType(0),
7657 LS.Inst->getOperand(0).getValueType()))
7658 ++Truncates;
7659 // If there is a shift amount, this slice gets rid of it.
7660 if (LS.Shift)
7661 ++Shift;
7662 // If this slice can merge a cross register bank copy, account for it.
7663 if (LS.canMergeExpensiveCrossRegisterBankCopy())
7664 ++CrossRegisterBanksCopies;
7665 }
7666
7667 Cost &operator+=(const Cost &RHS) {
7668 Loads += RHS.Loads;
7669 Truncates += RHS.Truncates;
7670 CrossRegisterBanksCopies += RHS.CrossRegisterBanksCopies;
7671 ZExts += RHS.ZExts;
7672 Shift += RHS.Shift;
7673 return *this;
7674 }
7675
7676 bool operator==(const Cost &RHS) const {
7677 return Loads == RHS.Loads && Truncates == RHS.Truncates &&
7678 CrossRegisterBanksCopies == RHS.CrossRegisterBanksCopies &&
7679 ZExts == RHS.ZExts && Shift == RHS.Shift;
7680 }
7681
7682 bool operator!=(const Cost &RHS) const { return !(*this == RHS); }
7683
7684 bool operator<(const Cost &RHS) const {
7685 // Assume cross register banks copies are as expensive as loads.
7686 // FIXME: Do we want some more target hooks?
7687 unsigned ExpensiveOpsLHS = Loads + CrossRegisterBanksCopies;
7688 unsigned ExpensiveOpsRHS = RHS.Loads + RHS.CrossRegisterBanksCopies;
7689 // Unless we are optimizing for code size, consider the
7690 // expensive operation first.
7691 if (!ForCodeSize && ExpensiveOpsLHS != ExpensiveOpsRHS)
7692 return ExpensiveOpsLHS < ExpensiveOpsRHS;
7693 return (Truncates + ZExts + Shift + ExpensiveOpsLHS) <
7694 (RHS.Truncates + RHS.ZExts + RHS.Shift + ExpensiveOpsRHS);
7695 }
7696
7697 bool operator>(const Cost &RHS) const { return RHS < *this; }
7698
7699 bool operator<=(const Cost &RHS) const { return !(RHS < *this); }
7700
7701 bool operator>=(const Cost &RHS) const { return !(*this < RHS); }
7702 };
7703 // The last instruction that represent the slice. This should be a
7704 // truncate instruction.
7705 SDNode *Inst;
7706 // The original load instruction.
7707 LoadSDNode *Origin;
7708 // The right shift amount in bits from the original load.
7709 unsigned Shift;
7710 // The DAG from which Origin came from.
7711 // This is used to get some contextual information about legal types, etc.
7712 SelectionDAG *DAG;
7713
7714 LoadedSlice(SDNode *Inst = NULL, LoadSDNode *Origin = NULL,
7715 unsigned Shift = 0, SelectionDAG *DAG = NULL)
7716 : Inst(Inst), Origin(Origin), Shift(Shift), DAG(DAG) {}
7717
7718 LoadedSlice(const LoadedSlice &LS)
7719 : Inst(LS.Inst), Origin(LS.Origin), Shift(LS.Shift), DAG(LS.DAG) {}
7720
7721 /// \brief Get the bits used in a chunk of bits \p BitWidth large.
7722 /// \return Result is \p BitWidth and has used bits set to 1 and
7723 /// not used bits set to 0.
7724 APInt getUsedBits() const {
7725 // Reproduce the trunc(lshr) sequence:
7726 // - Start from the truncated value.
7727 // - Zero extend to the desired bit width.
7728 // - Shift left.
7729 assert(Origin && "No original load to compare against.");
7730 unsigned BitWidth = Origin->getValueSizeInBits(0);
7731 assert(Inst && "This slice is not bound to an instruction");
7732 assert(Inst->getValueSizeInBits(0) <= BitWidth &&
7733 "Extracted slice is bigger than the whole type!");
7734 APInt UsedBits(Inst->getValueSizeInBits(0), 0);
7735 UsedBits.setAllBits();
7736 UsedBits = UsedBits.zext(BitWidth);
7737 UsedBits <<= Shift;
7738 return UsedBits;
7739 }
7740
7741 /// \brief Get the size of the slice to be loaded in bytes.
7742 unsigned getLoadedSize() const {
7743 unsigned SliceSize = getUsedBits().countPopulation();
7744 assert(!(SliceSize & 0x7) && "Size is not a multiple of a byte.");
7745 return SliceSize / 8;
7746 }
7747
7748 /// \brief Get the type that will be loaded for this slice.
7749 /// Note: This may not be the final type for the slice.
7750 EVT getLoadedType() const {
7751 assert(DAG && "Missing context");
7752 LLVMContext &Ctxt = *DAG->getContext();
7753 return EVT::getIntegerVT(Ctxt, getLoadedSize() * 8);
7754 }
7755
7756 /// \brief Get the alignment of the load used for this slice.
7757 unsigned getAlignment() const {
7758 unsigned Alignment = Origin->getAlignment();
7759 unsigned Offset = getOffsetFromBase();
7760 if (Offset != 0)
7761 Alignment = MinAlign(Alignment, Alignment + Offset);
7762 return Alignment;
7763 }
7764
7765 /// \brief Check if this slice can be rewritten with legal operations.
7766 bool isLegal() const {
7767 // An invalid slice is not legal.
7768 if (!Origin || !Inst || !DAG)
7769 return false;
7770
7771 // Offsets are for indexed load only, we do not handle that.
7772 if (Origin->getOffset().getOpcode() != ISD::UNDEF)
7773 return false;
7774
7775 const TargetLowering &TLI = DAG->getTargetLoweringInfo();
7776
7777 // Check that the type is legal.
7778 EVT SliceType = getLoadedType();
7779 if (!TLI.isTypeLegal(SliceType))
7780 return false;
7781
7782 // Check that the load is legal for this type.
7783 if (!TLI.isOperationLegal(ISD::LOAD, SliceType))
7784 return false;
7785
7786 // Check that the offset can be computed.
7787 // 1. Check its type.
7788 EVT PtrType = Origin->getBasePtr().getValueType();
7789 if (PtrType == MVT::Untyped || PtrType.isExtended())
7790 return false;
7791
7792 // 2. Check that it fits in the immediate.
7793 if (!TLI.isLegalAddImmediate(getOffsetFromBase()))
7794 return false;
7795
7796 // 3. Check that the computation is legal.
7797 if (!TLI.isOperationLegal(ISD::ADD, PtrType))
7798 return false;
7799
7800 // Check that the zext is legal if it needs one.
7801 EVT TruncateType = Inst->getValueType(0);
7802 if (TruncateType != SliceType &&
7803 !TLI.isOperationLegal(ISD::ZERO_EXTEND, TruncateType))
7804 return false;
7805
7806 return true;
7807 }
7808
7809 /// \brief Get the offset in bytes of this slice in the original chunk of
7810 /// bits.
7811 /// \pre DAG != NULL.
7812 uint64_t getOffsetFromBase() const {
7813 assert(DAG && "Missing context.");
7814 bool IsBigEndian =
7815 DAG->getTargetLoweringInfo().getDataLayout()->isBigEndian();
7816 assert(!(Shift & 0x7) && "Shifts not aligned on Bytes are not supported.");
7817 uint64_t Offset = Shift / 8;
7818 unsigned TySizeInBytes = Origin->getValueSizeInBits(0) / 8;
7819 assert(!(Origin->getValueSizeInBits(0) & 0x7) &&
7820 "The size of the original loaded type is not a multiple of a"
7821 " byte.");
7822 // If Offset is bigger than TySizeInBytes, it means we are loading all
7823 // zeros. This should have been optimized before in the process.
7824 assert(TySizeInBytes > Offset &&
7825 "Invalid shift amount for given loaded size");
7826 if (IsBigEndian)
7827 Offset = TySizeInBytes - Offset - getLoadedSize();
7828 return Offset;
7829 }
7830
7831 /// \brief Generate the sequence of instructions to load the slice
7832 /// represented by this object and redirect the uses of this slice to
7833 /// this new sequence of instructions.
7834 /// \pre this->Inst && this->Origin are valid Instructions and this
7835 /// object passed the legal check: LoadedSlice::isLegal returned true.
7836 /// \return The last instruction of the sequence used to load the slice.
7837 SDValue loadSlice() const {
7838 assert(Inst && Origin && "Unable to replace a non-existing slice.");
7839 const SDValue &OldBaseAddr = Origin->getBasePtr();
7840 SDValue BaseAddr = OldBaseAddr;
7841 // Get the offset in that chunk of bytes w.r.t. the endianess.
7842 int64_t Offset = static_cast<int64_t>(getOffsetFromBase());
7843 assert(Offset >= 0 && "Offset too big to fit in int64_t!");
7844 if (Offset) {
7845 // BaseAddr = BaseAddr + Offset.
7846 EVT ArithType = BaseAddr.getValueType();
7847 BaseAddr = DAG->getNode(ISD::ADD, SDLoc(Origin), ArithType, BaseAddr,
7848 DAG->getConstant(Offset, ArithType));
7849 }
7850
7851 // Create the type of the loaded slice according to its size.
7852 EVT SliceType = getLoadedType();
7853
7854 // Create the load for the slice.
7855 SDValue LastInst = DAG->getLoad(
7856 SliceType, SDLoc(Origin), Origin->getChain(), BaseAddr,
7857 Origin->getPointerInfo().getWithOffset(Offset), Origin->isVolatile(),
7858 Origin->isNonTemporal(), Origin->isInvariant(), getAlignment());
7859 // If the final type is not the same as the loaded type, this means that
7860 // we have to pad with zero. Create a zero extend for that.
7861 EVT FinalType = Inst->getValueType(0);
7862 if (SliceType != FinalType)
7863 LastInst =
7864 DAG->getNode(ISD::ZERO_EXTEND, SDLoc(LastInst), FinalType, LastInst);
7865 return LastInst;
7866 }
7867
7868 /// \brief Check if this slice can be merged with an expensive cross register
7869 /// bank copy. E.g.,
7870 /// i = load i32
7871 /// f = bitcast i32 i to float
7872 bool canMergeExpensiveCrossRegisterBankCopy() const {
7873 if (!Inst || !Inst->hasOneUse())
7874 return false;
7875 SDNode *Use = *Inst->use_begin();
7876 if (Use->getOpcode() != ISD::BITCAST)
7877 return false;
7878 assert(DAG && "Missing context");
7879 const TargetLowering &TLI = DAG->getTargetLoweringInfo();
7880 EVT ResVT = Use->getValueType(0);
7881 const TargetRegisterClass *ResRC = TLI.getRegClassFor(ResVT.getSimpleVT());
7882 const TargetRegisterClass *ArgRC =
7883 TLI.getRegClassFor(Use->getOperand(0).getValueType().getSimpleVT());
7884 if (ArgRC == ResRC || !TLI.isOperationLegal(ISD::LOAD, ResVT))
7885 return false;
7886
7887 // At this point, we know that we perform a cross-register-bank copy.
7888 // Check if it is expensive.
7889 const TargetRegisterInfo *TRI = TLI.getTargetMachine().getRegisterInfo();
7890 // Assume bitcasts are cheap, unless both register classes do not
7891 // explicitly share a common sub class.
7892 if (!TRI || TRI->getCommonSubClass(ArgRC, ResRC))
7893 return false;
7894
7895 // Check if it will be merged with the load.
7896 // 1. Check the alignment constraint.
7897 unsigned RequiredAlignment = TLI.getDataLayout()->getABITypeAlignment(
7898 ResVT.getTypeForEVT(*DAG->getContext()));
7899
7900 if (RequiredAlignment > getAlignment())
7901 return false;
7902
7903 // 2. Check that the load is a legal operation for that type.
7904 if (!TLI.isOperationLegal(ISD::LOAD, ResVT))
7905 return false;
7906
7907 // 3. Check that we do not have a zext in the way.
7908 if (Inst->getValueType(0) != getLoadedType())
7909 return false;
7910
7911 return true;
7912 }
7913};
7914}
7915
7916/// \brief Sorts LoadedSlice according to their offset.
7917struct LoadedSliceSorter {
7918 bool operator()(const LoadedSlice &LHS, const LoadedSlice &RHS) {
7919 assert(LHS.Origin == RHS.Origin && "Different bases not implemented.");
7920 return LHS.getOffsetFromBase() < RHS.getOffsetFromBase();
7921 }
7922};
7923
7924/// \brief Check that all bits set in \p UsedBits form a dense region, i.e.,
7925/// \p UsedBits looks like 0..0 1..1 0..0.
7926static bool areUsedBitsDense(const APInt &UsedBits) {
7927 // If all the bits are one, this is dense!
7928 if (UsedBits.isAllOnesValue())
7929 return true;
7930
7931 // Get rid of the unused bits on the right.
7932 APInt NarrowedUsedBits = UsedBits.lshr(UsedBits.countTrailingZeros());
7933 // Get rid of the unused bits on the left.
7934 if (NarrowedUsedBits.countLeadingZeros())
7935 NarrowedUsedBits = NarrowedUsedBits.trunc(NarrowedUsedBits.getActiveBits());
7936 // Check that the chunk of bits is completely used.
7937 return NarrowedUsedBits.isAllOnesValue();
7938}
7939
7940/// \brief Check whether or not \p First and \p Second are next to each other
7941/// in memory. This means that there is no hole between the bits loaded
7942/// by \p First and the bits loaded by \p Second.
7943static bool areSlicesNextToEachOther(const LoadedSlice &First,
7944 const LoadedSlice &Second) {
7945 assert(First.Origin == Second.Origin && First.Origin &&
7946 "Unable to match different memory origins.");
7947 APInt UsedBits = First.getUsedBits();
7948 assert((UsedBits & Second.getUsedBits()) == 0 &&
7949 "Slices are not supposed to overlap.");
7950 UsedBits |= Second.getUsedBits();
7951 return areUsedBitsDense(UsedBits);
7952}
7953
7954/// \brief Adjust the \p GlobalLSCost according to the target
7955/// paring capabilities and the layout of the slices.
7956/// \pre \p GlobalLSCost should account for at least as many loads as
7957/// there is in the slices in \p LoadedSlices.
7958static void adjustCostForPairing(SmallVectorImpl<LoadedSlice> &LoadedSlices,
7959 LoadedSlice::Cost &GlobalLSCost) {
7960 unsigned NumberOfSlices = LoadedSlices.size();
7961 // If there is less than 2 elements, no pairing is possible.
7962 if (NumberOfSlices < 2)
7963 return;
7964
7965 // Sort the slices so that elements that are likely to be next to each
7966 // other in memory are next to each other in the list.
7967 std::sort(LoadedSlices.begin(), LoadedSlices.end(), LoadedSliceSorter());
7968 const TargetLowering &TLI = LoadedSlices[0].DAG->getTargetLoweringInfo();
7969 // First (resp. Second) is the first (resp. Second) potentially candidate
7970 // to be placed in a paired load.
7971 const LoadedSlice *First = NULL;
7972 const LoadedSlice *Second = NULL;
7973 for (unsigned CurrSlice = 0; CurrSlice < NumberOfSlices; ++CurrSlice,
7974 // Set the beginning of the pair.
7975 First = Second) {
7976
7977 Second = &LoadedSlices[CurrSlice];
7978
7979 // If First is NULL, it means we start a new pair.
7980 // Get to the next slice.
7981 if (!First)
7982 continue;
7983
7984 EVT LoadedType = First->getLoadedType();
7985
7986 // If the types of the slices are different, we cannot pair them.
7987 if (LoadedType != Second->getLoadedType())
7988 continue;
7989
7990 // Check if the target supplies paired loads for this type.
7991 unsigned RequiredAlignment = 0;
7992 if (!TLI.hasPairedLoad(LoadedType, RequiredAlignment)) {
7993 // move to the next pair, this type is hopeless.
7994 Second = NULL;
7995 continue;
7996 }
7997 // Check if we meet the alignment requirement.
7998 if (RequiredAlignment > First->getAlignment())
7999 continue;
8000
8001 // Check that both loads are next to each other in memory.
8002 if (!areSlicesNextToEachOther(*First, *Second))
8003 continue;
8004
8005 assert(GlobalLSCost.Loads > 0 && "We save more loads than we created!");
8006 --GlobalLSCost.Loads;
8007 // Move to the next pair.
8008 Second = NULL;
8009 }
8010}
8011
8012/// \brief Check the profitability of all involved LoadedSlice.
8013/// Currently, it is considered profitable if there is exactly two
8014/// involved slices (1) which are (2) next to each other in memory, and
8015/// whose cost (\see LoadedSlice::Cost) is smaller than the original load (3).
8016///
8017/// Note: The order of the elements in \p LoadedSlices may be modified, but not
8018/// the elements themselves.
8019///
8020/// FIXME: When the cost model will be mature enough, we can relax
8021/// constraints (1) and (2).
8022static bool isSlicingProfitable(SmallVectorImpl<LoadedSlice> &LoadedSlices,
8023 const APInt &UsedBits, bool ForCodeSize) {
8024 unsigned NumberOfSlices = LoadedSlices.size();
8025 if (StressLoadSlicing)
8026 return NumberOfSlices > 1;
8027
8028 // Check (1).
8029 if (NumberOfSlices != 2)
8030 return false;
8031
8032 // Check (2).
8033 if (!areUsedBitsDense(UsedBits))
8034 return false;
8035
8036 // Check (3).
8037 LoadedSlice::Cost OrigCost(ForCodeSize), GlobalSlicingCost(ForCodeSize);
8038 // The original code has one big load.
8039 OrigCost.Loads = 1;
8040 for (unsigned CurrSlice = 0; CurrSlice < NumberOfSlices; ++CurrSlice) {
8041 const LoadedSlice &LS = LoadedSlices[CurrSlice];
8042 // Accumulate the cost of all the slices.
8043 LoadedSlice::Cost SliceCost(LS, ForCodeSize);
8044 GlobalSlicingCost += SliceCost;
8045
8046 // Account as cost in the original configuration the gain obtained
8047 // with the current slices.
8048 OrigCost.addSliceGain(LS);
8049 }
8050
8051 // If the target supports paired load, adjust the cost accordingly.
8052 adjustCostForPairing(LoadedSlices, GlobalSlicingCost);
8053 return OrigCost > GlobalSlicingCost;
8054}
8055
8056/// \brief If the given load, \p LI, is used only by trunc or trunc(lshr)
8057/// operations, split it in the various pieces being extracted.
8058///
8059/// This sort of thing is introduced by SROA.
8060/// This slicing takes care not to insert overlapping loads.
8061/// \pre LI is a simple load (i.e., not an atomic or volatile load).
8062bool DAGCombiner::SliceUpLoad(SDNode *N) {
8063 if (Level < AfterLegalizeDAG)
8064 return false;
8065
8066 LoadSDNode *LD = cast<LoadSDNode>(N);
8067 if (LD->isVolatile() || !ISD::isNormalLoad(LD) ||
8068 !LD->getValueType(0).isInteger())
8069 return false;
8070
8071 // Keep track of already used bits to detect overlapping values.
8072 // In that case, we will just abort the transformation.
8073 APInt UsedBits(LD->getValueSizeInBits(0), 0);
8074
8075 SmallVector<LoadedSlice, 4> LoadedSlices;
8076
8077 // Check if this load is used as several smaller chunks of bits.
8078 // Basically, look for uses in trunc or trunc(lshr) and record a new chain
8079 // of computation for each trunc.
8080 for (SDNode::use_iterator UI = LD->use_begin(), UIEnd = LD->use_end();
8081 UI != UIEnd; ++UI) {
8082 // Skip the uses of the chain.
8083 if (UI.getUse().getResNo() != 0)
8084 continue;
8085
8086 SDNode *User = *UI;
8087 unsigned Shift = 0;
8088
8089 // Check if this is a trunc(lshr).
8090 if (User->getOpcode() == ISD::SRL && User->hasOneUse() &&
8091 isa<ConstantSDNode>(User->getOperand(1))) {
8092 Shift = cast<ConstantSDNode>(User->getOperand(1))->getZExtValue();
8093 User = *User->use_begin();
8094 }
8095
8096 // At this point, User is a Truncate, iff we encountered, trunc or
8097 // trunc(lshr).
8098 if (User->getOpcode() != ISD::TRUNCATE)
8099 return false;
8100
8101 // The width of the type must be a power of 2 and greater than 8-bits.
8102 // Otherwise the load cannot be represented in LLVM IR.
8103 // Moreover, if we shifted with a non 8-bits multiple, the slice
8104 // will be accross several bytes. We do not support that.
8105 unsigned Width = User->getValueSizeInBits(0);
8106 if (Width < 8 || !isPowerOf2_32(Width) || (Shift & 0x7))
8107 return 0;
8108
8109 // Build the slice for this chain of computations.
8110 LoadedSlice LS(User, LD, Shift, &DAG);
8111 APInt CurrentUsedBits = LS.getUsedBits();
8112
8113 // Check if this slice overlaps with another.
8114 if ((CurrentUsedBits & UsedBits) != 0)
8115 return false;
8116 // Update the bits used globally.
8117 UsedBits |= CurrentUsedBits;
8118
8119 // Check if the new slice would be legal.
8120 if (!LS.isLegal())
8121 return false;
8122
8123 // Record the slice.
8124 LoadedSlices.push_back(LS);
8125 }
8126
8127 // Abort slicing if it does not seem to be profitable.
8128 if (!isSlicingProfitable(LoadedSlices, UsedBits, ForCodeSize))
8129 return false;
8130
8131 ++SlicedLoads;
8132
8133 // Rewrite each chain to use an independent load.
8134 // By construction, each chain can be represented by a unique load.
8135
8136 // Prepare the argument for the new token factor for all the slices.
8137 SmallVector<SDValue, 8> ArgChains;
8138 for (SmallVectorImpl<LoadedSlice>::const_iterator
8139 LSIt = LoadedSlices.begin(),
8140 LSItEnd = LoadedSlices.end();
8141 LSIt != LSItEnd; ++LSIt) {
8142 SDValue SliceInst = LSIt->loadSlice();
8143 CombineTo(LSIt->Inst, SliceInst, true);
8144 if (SliceInst.getNode()->getOpcode() != ISD::LOAD)
8145 SliceInst = SliceInst.getOperand(0);
8146 assert(SliceInst->getOpcode() == ISD::LOAD &&
8147 "It takes more than a zext to get to the loaded slice!!");
8148 ArgChains.push_back(SliceInst.getValue(1));
8149 }
8150
8151 SDValue Chain = DAG.getNode(ISD::TokenFactor, SDLoc(LD), MVT::Other,
8152 &ArgChains[0], ArgChains.size());
8153 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain);
8154 return true;
8155}
8156
Chris Lattner2392ae72010-04-15 04:48:01 +00008157/// CheckForMaskedLoad - Check to see if V is (and load (ptr), imm), where the
8158/// load is having specific bytes cleared out. If so, return the byte size
8159/// being masked out and the shift amount.
8160static std::pair<unsigned, unsigned>
8161CheckForMaskedLoad(SDValue V, SDValue Ptr, SDValue Chain) {
8162 std::pair<unsigned, unsigned> Result(0, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008163
Chris Lattner2392ae72010-04-15 04:48:01 +00008164 // Check for the structure we're looking for.
8165 if (V->getOpcode() != ISD::AND ||
8166 !isa<ConstantSDNode>(V->getOperand(1)) ||
8167 !ISD::isNormalLoad(V->getOperand(0).getNode()))
8168 return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008169
Chris Lattnere6987582010-04-15 06:10:49 +00008170 // Check the chain and pointer.
Chris Lattner2392ae72010-04-15 04:48:01 +00008171 LoadSDNode *LD = cast<LoadSDNode>(V->getOperand(0));
Chris Lattnere6987582010-04-15 06:10:49 +00008172 if (LD->getBasePtr() != Ptr) return Result; // Not from same pointer.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008173
Chris Lattnere6987582010-04-15 06:10:49 +00008174 // The store should be chained directly to the load or be an operand of a
8175 // tokenfactor.
8176 if (LD == Chain.getNode())
8177 ; // ok.
8178 else if (Chain->getOpcode() != ISD::TokenFactor)
8179 return Result; // Fail.
8180 else {
8181 bool isOk = false;
8182 for (unsigned i = 0, e = Chain->getNumOperands(); i != e; ++i)
8183 if (Chain->getOperand(i).getNode() == LD) {
8184 isOk = true;
8185 break;
8186 }
8187 if (!isOk) return Result;
8188 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008189
Chris Lattner2392ae72010-04-15 04:48:01 +00008190 // This only handles simple types.
8191 if (V.getValueType() != MVT::i16 &&
8192 V.getValueType() != MVT::i32 &&
8193 V.getValueType() != MVT::i64)
8194 return Result;
8195
8196 // Check the constant mask. Invert it so that the bits being masked out are
8197 // 0 and the bits being kept are 1. Use getSExtValue so that leading bits
8198 // follow the sign bit for uniformity.
8199 uint64_t NotMask = ~cast<ConstantSDNode>(V->getOperand(1))->getSExtValue();
Michael J. Spencerc6af2432013-05-24 22:23:49 +00008200 unsigned NotMaskLZ = countLeadingZeros(NotMask);
Chris Lattner2392ae72010-04-15 04:48:01 +00008201 if (NotMaskLZ & 7) return Result; // Must be multiple of a byte.
Michael J. Spencerc6af2432013-05-24 22:23:49 +00008202 unsigned NotMaskTZ = countTrailingZeros(NotMask);
Chris Lattner2392ae72010-04-15 04:48:01 +00008203 if (NotMaskTZ & 7) return Result; // Must be multiple of a byte.
8204 if (NotMaskLZ == 64) return Result; // All zero mask.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008205
Chris Lattner2392ae72010-04-15 04:48:01 +00008206 // See if we have a continuous run of bits. If so, we have 0*1+0*
8207 if (CountTrailingOnes_64(NotMask >> NotMaskTZ)+NotMaskTZ+NotMaskLZ != 64)
8208 return Result;
8209
8210 // Adjust NotMaskLZ down to be from the actual size of the int instead of i64.
8211 if (V.getValueType() != MVT::i64 && NotMaskLZ)
8212 NotMaskLZ -= 64-V.getValueSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008213
Chris Lattner2392ae72010-04-15 04:48:01 +00008214 unsigned MaskedBytes = (V.getValueSizeInBits()-NotMaskLZ-NotMaskTZ)/8;
8215 switch (MaskedBytes) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008216 case 1:
8217 case 2:
Chris Lattner2392ae72010-04-15 04:48:01 +00008218 case 4: break;
8219 default: return Result; // All one mask, or 5-byte mask.
8220 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008221
Chris Lattner2392ae72010-04-15 04:48:01 +00008222 // Verify that the first bit starts at a multiple of mask so that the access
8223 // is aligned the same as the access width.
8224 if (NotMaskTZ && NotMaskTZ/8 % MaskedBytes) return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008225
Chris Lattner2392ae72010-04-15 04:48:01 +00008226 Result.first = MaskedBytes;
8227 Result.second = NotMaskTZ/8;
8228 return Result;
8229}
8230
8231
8232/// ShrinkLoadReplaceStoreWithStore - Check to see if IVal is something that
8233/// provides a value as specified by MaskInfo. If so, replace the specified
8234/// store with a narrower store of truncated IVal.
8235static SDNode *
8236ShrinkLoadReplaceStoreWithStore(const std::pair<unsigned, unsigned> &MaskInfo,
8237 SDValue IVal, StoreSDNode *St,
8238 DAGCombiner *DC) {
8239 unsigned NumBytes = MaskInfo.first;
8240 unsigned ByteShift = MaskInfo.second;
8241 SelectionDAG &DAG = DC->getDAG();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008242
Chris Lattner2392ae72010-04-15 04:48:01 +00008243 // Check to see if IVal is all zeros in the part being masked in by the 'or'
8244 // that uses this. If not, this is not a replacement.
8245 APInt Mask = ~APInt::getBitsSet(IVal.getValueSizeInBits(),
8246 ByteShift*8, (ByteShift+NumBytes)*8);
8247 if (!DAG.MaskedValueIsZero(IVal, Mask)) return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008248
Chris Lattner2392ae72010-04-15 04:48:01 +00008249 // Check that it is legal on the target to do this. It is legal if the new
8250 // VT we're shrinking to (i8/i16/i32) is legal or we're still before type
8251 // legalization.
8252 MVT VT = MVT::getIntegerVT(NumBytes*8);
8253 if (!DC->isTypeLegal(VT))
8254 return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008255
Chris Lattner2392ae72010-04-15 04:48:01 +00008256 // Okay, we can do this! Replace the 'St' store with a store of IVal that is
8257 // shifted by ByteShift and truncated down to NumBytes.
8258 if (ByteShift)
Andrew Trickac6d9be2013-05-25 02:42:55 +00008259 IVal = DAG.getNode(ISD::SRL, SDLoc(IVal), IVal.getValueType(), IVal,
Owen Anderson95771af2011-02-25 21:41:48 +00008260 DAG.getConstant(ByteShift*8,
8261 DC->getShiftAmountTy(IVal.getValueType())));
Chris Lattner2392ae72010-04-15 04:48:01 +00008262
8263 // Figure out the offset for the store and the alignment of the access.
8264 unsigned StOffset;
8265 unsigned NewAlign = St->getAlignment();
8266
8267 if (DAG.getTargetLoweringInfo().isLittleEndian())
8268 StOffset = ByteShift;
8269 else
8270 StOffset = IVal.getValueType().getStoreSize() - ByteShift - NumBytes;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008271
Chris Lattner2392ae72010-04-15 04:48:01 +00008272 SDValue Ptr = St->getBasePtr();
8273 if (StOffset) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008274 Ptr = DAG.getNode(ISD::ADD, SDLoc(IVal), Ptr.getValueType(),
Chris Lattner2392ae72010-04-15 04:48:01 +00008275 Ptr, DAG.getConstant(StOffset, Ptr.getValueType()));
8276 NewAlign = MinAlign(NewAlign, StOffset);
8277 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008278
Chris Lattner2392ae72010-04-15 04:48:01 +00008279 // Truncate down to the new size.
Andrew Trickac6d9be2013-05-25 02:42:55 +00008280 IVal = DAG.getNode(ISD::TRUNCATE, SDLoc(IVal), VT, IVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008281
Chris Lattner2392ae72010-04-15 04:48:01 +00008282 ++OpsNarrowed;
Andrew Trickac6d9be2013-05-25 02:42:55 +00008283 return DAG.getStore(St->getChain(), SDLoc(St), IVal, Ptr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008284 St->getPointerInfo().getWithOffset(StOffset),
Chris Lattner2392ae72010-04-15 04:48:01 +00008285 false, false, NewAlign).getNode();
8286}
8287
Evan Cheng8b944d32009-05-28 00:35:15 +00008288
8289/// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is
8290/// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some
8291/// of the loaded bits, try narrowing the load and store if it would end up
8292/// being a win for performance or code size.
8293SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) {
8294 StoreSDNode *ST = cast<StoreSDNode>(N);
Evan Chengcdcecc02009-05-28 18:41:02 +00008295 if (ST->isVolatile())
8296 return SDValue();
8297
Evan Cheng8b944d32009-05-28 00:35:15 +00008298 SDValue Chain = ST->getChain();
8299 SDValue Value = ST->getValue();
8300 SDValue Ptr = ST->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +00008301 EVT VT = Value.getValueType();
Evan Cheng8b944d32009-05-28 00:35:15 +00008302
8303 if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse())
Evan Chengcdcecc02009-05-28 18:41:02 +00008304 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00008305
8306 unsigned Opc = Value.getOpcode();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008307
Chris Lattner2392ae72010-04-15 04:48:01 +00008308 // If this is "store (or X, Y), P" and X is "(and (load P), cst)", where cst
8309 // is a byte mask indicating a consecutive number of bytes, check to see if
8310 // Y is known to provide just those bytes. If so, we try to replace the
8311 // load + replace + store sequence with a single (narrower) store, which makes
8312 // the load dead.
8313 if (Opc == ISD::OR) {
8314 std::pair<unsigned, unsigned> MaskedLoad;
8315 MaskedLoad = CheckForMaskedLoad(Value.getOperand(0), Ptr, Chain);
8316 if (MaskedLoad.first)
8317 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
8318 Value.getOperand(1), ST,this))
8319 return SDValue(NewST, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008320
Chris Lattner2392ae72010-04-15 04:48:01 +00008321 // Or is commutative, so try swapping X and Y.
8322 MaskedLoad = CheckForMaskedLoad(Value.getOperand(1), Ptr, Chain);
8323 if (MaskedLoad.first)
8324 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
8325 Value.getOperand(0), ST,this))
8326 return SDValue(NewST, 0);
8327 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008328
Evan Cheng8b944d32009-05-28 00:35:15 +00008329 if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) ||
8330 Value.getOperand(1).getOpcode() != ISD::Constant)
Evan Chengcdcecc02009-05-28 18:41:02 +00008331 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00008332
8333 SDValue N0 = Value.getOperand(0);
Dan Gohman24bde5b2010-09-02 21:18:42 +00008334 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
8335 Chain == SDValue(N0.getNode(), 1)) {
Evan Cheng8b944d32009-05-28 00:35:15 +00008336 LoadSDNode *LD = cast<LoadSDNode>(N0);
Chris Lattnerfa459012010-09-21 16:08:50 +00008337 if (LD->getBasePtr() != Ptr ||
8338 LD->getPointerInfo().getAddrSpace() !=
8339 ST->getPointerInfo().getAddrSpace())
Evan Chengcdcecc02009-05-28 18:41:02 +00008340 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00008341
8342 // Find the type to narrow it the load / op / store to.
8343 SDValue N1 = Value.getOperand(1);
8344 unsigned BitWidth = N1.getValueSizeInBits();
8345 APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue();
8346 if (Opc == ISD::AND)
8347 Imm ^= APInt::getAllOnesValue(BitWidth);
Evan Chengd3c76bb2009-05-28 23:52:18 +00008348 if (Imm == 0 || Imm.isAllOnesValue())
8349 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00008350 unsigned ShAmt = Imm.countTrailingZeros();
8351 unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1;
8352 unsigned NewBW = NextPowerOf2(MSB - ShAmt);
Owen Anderson23b9b192009-08-12 00:36:31 +00008353 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00008354 while (NewBW < BitWidth &&
Evan Chengcdcecc02009-05-28 18:41:02 +00008355 !(TLI.isOperationLegalOrCustom(Opc, NewVT) &&
Evan Cheng8b944d32009-05-28 00:35:15 +00008356 TLI.isNarrowingProfitable(VT, NewVT))) {
8357 NewBW = NextPowerOf2(NewBW);
Owen Anderson23b9b192009-08-12 00:36:31 +00008358 NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00008359 }
Evan Chengcdcecc02009-05-28 18:41:02 +00008360 if (NewBW >= BitWidth)
8361 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00008362
8363 // If the lsb changed does not start at the type bitwidth boundary,
8364 // start at the previous one.
8365 if (ShAmt % NewBW)
8366 ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW;
Manman Ren981b9632012-12-12 01:13:50 +00008367 APInt Mask = APInt::getBitsSet(BitWidth, ShAmt,
8368 std::min(BitWidth, ShAmt + NewBW));
Evan Cheng8b944d32009-05-28 00:35:15 +00008369 if ((Imm & Mask) == Imm) {
8370 APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW);
8371 if (Opc == ISD::AND)
8372 NewImm ^= APInt::getAllOnesValue(NewBW);
8373 uint64_t PtrOff = ShAmt / 8;
8374 // For big endian targets, we need to adjust the offset to the pointer to
8375 // load the correct bytes.
8376 if (TLI.isBigEndian())
Evan Chengcdcecc02009-05-28 18:41:02 +00008377 PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff;
Evan Cheng8b944d32009-05-28 00:35:15 +00008378
8379 unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008380 Type *NewVTTy = NewVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00008381 if (NewAlign < TLI.getDataLayout()->getABITypeAlignment(NewVTTy))
Evan Chengcdcecc02009-05-28 18:41:02 +00008382 return SDValue();
8383
Andrew Trickac6d9be2013-05-25 02:42:55 +00008384 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(LD),
Evan Cheng8b944d32009-05-28 00:35:15 +00008385 Ptr.getValueType(), Ptr,
8386 DAG.getConstant(PtrOff, Ptr.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00008387 SDValue NewLD = DAG.getLoad(NewVT, SDLoc(N0),
Evan Cheng8b944d32009-05-28 00:35:15 +00008388 LD->getChain(), NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00008389 LD->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00008390 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008391 LD->isInvariant(), NewAlign);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008392 SDValue NewVal = DAG.getNode(Opc, SDLoc(Value), NewVT, NewLD,
Evan Cheng8b944d32009-05-28 00:35:15 +00008393 DAG.getConstant(NewImm, NewVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00008394 SDValue NewST = DAG.getStore(Chain, SDLoc(N),
Evan Cheng8b944d32009-05-28 00:35:15 +00008395 NewVal, NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00008396 ST->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00008397 false, false, NewAlign);
Evan Cheng8b944d32009-05-28 00:35:15 +00008398
8399 AddToWorkList(NewPtr.getNode());
8400 AddToWorkList(NewLD.getNode());
8401 AddToWorkList(NewVal.getNode());
8402 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00008403 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1));
Evan Cheng8b944d32009-05-28 00:35:15 +00008404 ++OpsNarrowed;
8405 return NewST;
8406 }
8407 }
8408
Evan Chengcdcecc02009-05-28 18:41:02 +00008409 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00008410}
8411
Evan Cheng31959b12011-02-02 01:06:55 +00008412/// TransformFPLoadStorePair - For a given floating point load / store pair,
8413/// if the load value isn't used by any other operations, then consider
8414/// transforming the pair to integer load / store operations if the target
8415/// deems the transformation profitable.
8416SDValue DAGCombiner::TransformFPLoadStorePair(SDNode *N) {
8417 StoreSDNode *ST = cast<StoreSDNode>(N);
8418 SDValue Chain = ST->getChain();
8419 SDValue Value = ST->getValue();
8420 if (ISD::isNormalStore(ST) && ISD::isNormalLoad(Value.getNode()) &&
8421 Value.hasOneUse() &&
8422 Chain == SDValue(Value.getNode(), 1)) {
8423 LoadSDNode *LD = cast<LoadSDNode>(Value);
8424 EVT VT = LD->getMemoryVT();
8425 if (!VT.isFloatingPoint() ||
8426 VT != ST->getMemoryVT() ||
8427 LD->isNonTemporal() ||
8428 ST->isNonTemporal() ||
8429 LD->getPointerInfo().getAddrSpace() != 0 ||
8430 ST->getPointerInfo().getAddrSpace() != 0)
8431 return SDValue();
8432
8433 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
8434 if (!TLI.isOperationLegal(ISD::LOAD, IntVT) ||
8435 !TLI.isOperationLegal(ISD::STORE, IntVT) ||
8436 !TLI.isDesirableToTransformToIntegerOp(ISD::LOAD, VT) ||
8437 !TLI.isDesirableToTransformToIntegerOp(ISD::STORE, VT))
8438 return SDValue();
8439
8440 unsigned LDAlign = LD->getAlignment();
8441 unsigned STAlign = ST->getAlignment();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008442 Type *IntVTTy = IntVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00008443 unsigned ABIAlign = TLI.getDataLayout()->getABITypeAlignment(IntVTTy);
Evan Cheng31959b12011-02-02 01:06:55 +00008444 if (LDAlign < ABIAlign || STAlign < ABIAlign)
8445 return SDValue();
8446
Andrew Trickac6d9be2013-05-25 02:42:55 +00008447 SDValue NewLD = DAG.getLoad(IntVT, SDLoc(Value),
Evan Cheng31959b12011-02-02 01:06:55 +00008448 LD->getChain(), LD->getBasePtr(),
8449 LD->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008450 false, false, false, LDAlign);
Evan Cheng31959b12011-02-02 01:06:55 +00008451
Andrew Trickac6d9be2013-05-25 02:42:55 +00008452 SDValue NewST = DAG.getStore(NewLD.getValue(1), SDLoc(N),
Evan Cheng31959b12011-02-02 01:06:55 +00008453 NewLD, ST->getBasePtr(),
8454 ST->getPointerInfo(),
8455 false, false, STAlign);
8456
8457 AddToWorkList(NewLD.getNode());
8458 AddToWorkList(NewST.getNode());
8459 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00008460 DAG.ReplaceAllUsesOfValueWith(Value.getValue(1), NewLD.getValue(1));
Evan Cheng31959b12011-02-02 01:06:55 +00008461 ++LdStFP2Int;
8462 return NewST;
8463 }
8464
8465 return SDValue();
8466}
8467
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008468/// Helper struct to parse and store a memory address as base + index + offset.
8469/// We ignore sign extensions when it is safe to do so.
8470/// The following two expressions are not equivalent. To differentiate we need
8471/// to store whether there was a sign extension involved in the index
8472/// computation.
8473/// (load (i64 add (i64 copyfromreg %c)
8474/// (i64 signextend (add (i8 load %index)
8475/// (i8 1))))
8476/// vs
8477///
8478/// (load (i64 add (i64 copyfromreg %c)
8479/// (i64 signextend (i32 add (i32 signextend (i8 load %index))
8480/// (i32 1)))))
8481struct BaseIndexOffset {
8482 SDValue Base;
8483 SDValue Index;
8484 int64_t Offset;
8485 bool IsIndexSignExt;
8486
8487 BaseIndexOffset() : Offset(0), IsIndexSignExt(false) {}
8488
8489 BaseIndexOffset(SDValue Base, SDValue Index, int64_t Offset,
8490 bool IsIndexSignExt) :
8491 Base(Base), Index(Index), Offset(Offset), IsIndexSignExt(IsIndexSignExt) {}
8492
8493 bool equalBaseIndex(const BaseIndexOffset &Other) {
8494 return Other.Base == Base && Other.Index == Index &&
8495 Other.IsIndexSignExt == IsIndexSignExt;
Nadav Rotemc653de62012-10-03 16:11:15 +00008496 }
8497
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008498 /// Parses tree in Ptr for base, index, offset addresses.
8499 static BaseIndexOffset match(SDValue Ptr) {
8500 bool IsIndexSignExt = false;
8501
Juergen Ributzka915e9362013-08-21 21:53:38 +00008502 // We only can pattern match BASE + INDEX + OFFSET. If Ptr is not an ADD
8503 // instruction, then it could be just the BASE or everything else we don't
8504 // know how to handle. Just use Ptr as BASE and give up.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008505 if (Ptr->getOpcode() != ISD::ADD)
8506 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
8507
Juergen Ributzka915e9362013-08-21 21:53:38 +00008508 // We know that we have at least an ADD instruction. Try to pattern match
8509 // the simple case of BASE + OFFSET.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008510 if (isa<ConstantSDNode>(Ptr->getOperand(1))) {
8511 int64_t Offset = cast<ConstantSDNode>(Ptr->getOperand(1))->getSExtValue();
8512 return BaseIndexOffset(Ptr->getOperand(0), SDValue(), Offset,
8513 IsIndexSignExt);
8514 }
8515
Juergen Ributzka915e9362013-08-21 21:53:38 +00008516 // Inside a loop the current BASE pointer is calculated using an ADD and a
Juergen Ributzka2b884bc2013-08-28 22:33:58 +00008517 // MUL instruction. In this case Ptr is the actual BASE pointer.
Juergen Ributzka915e9362013-08-21 21:53:38 +00008518 // (i64 add (i64 %array_ptr)
8519 // (i64 mul (i64 %induction_var)
8520 // (i64 %element_size)))
Juergen Ributzka2b884bc2013-08-28 22:33:58 +00008521 if (Ptr->getOperand(1)->getOpcode() == ISD::MUL)
Juergen Ributzka915e9362013-08-21 21:53:38 +00008522 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
Juergen Ributzka915e9362013-08-21 21:53:38 +00008523
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008524 // Look at Base + Index + Offset cases.
8525 SDValue Base = Ptr->getOperand(0);
8526 SDValue IndexOffset = Ptr->getOperand(1);
8527
8528 // Skip signextends.
8529 if (IndexOffset->getOpcode() == ISD::SIGN_EXTEND) {
8530 IndexOffset = IndexOffset->getOperand(0);
8531 IsIndexSignExt = true;
8532 }
8533
8534 // Either the case of Base + Index (no offset) or something else.
8535 if (IndexOffset->getOpcode() != ISD::ADD)
8536 return BaseIndexOffset(Base, IndexOffset, 0, IsIndexSignExt);
8537
8538 // Now we have the case of Base + Index + offset.
8539 SDValue Index = IndexOffset->getOperand(0);
8540 SDValue Offset = IndexOffset->getOperand(1);
8541
8542 if (!isa<ConstantSDNode>(Offset))
8543 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
8544
8545 // Ignore signextends.
8546 if (Index->getOpcode() == ISD::SIGN_EXTEND) {
8547 Index = Index->getOperand(0);
8548 IsIndexSignExt = true;
8549 } else IsIndexSignExt = false;
8550
8551 int64_t Off = cast<ConstantSDNode>(Offset)->getSExtValue();
8552 return BaseIndexOffset(Base, Index, Off, IsIndexSignExt);
8553 }
8554};
Nadav Rotemc653de62012-10-03 16:11:15 +00008555
8556/// Holds a pointer to an LSBaseSDNode as well as information on where it
8557/// is located in a sequence of memory operations connected by a chain.
8558struct MemOpLink {
8559 MemOpLink (LSBaseSDNode *N, int64_t Offset, unsigned Seq):
8560 MemNode(N), OffsetFromBase(Offset), SequenceNum(Seq) { }
8561 // Ptr to the mem node.
8562 LSBaseSDNode *MemNode;
8563 // Offset from the base ptr.
8564 int64_t OffsetFromBase;
8565 // What is the sequence number of this mem node.
8566 // Lowest mem operand in the DAG starts at zero.
8567 unsigned SequenceNum;
8568};
8569
8570/// Sorts store nodes in a link according to their offset from a shared
8571// base ptr.
8572struct ConsecutiveMemoryChainSorter {
8573 bool operator()(MemOpLink LHS, MemOpLink RHS) {
8574 return LHS.OffsetFromBase < RHS.OffsetFromBase;
8575 }
8576};
8577
8578bool DAGCombiner::MergeConsecutiveStores(StoreSDNode* St) {
8579 EVT MemVT = St->getMemoryVT();
8580 int64_t ElementSizeBytes = MemVT.getSizeInBits()/8;
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008581 bool NoVectors = DAG.getMachineFunction().getFunction()->getAttributes().
8582 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Nadav Rotemc653de62012-10-03 16:11:15 +00008583
8584 // Don't merge vectors into wider inputs.
8585 if (MemVT.isVector() || !MemVT.isSimple())
8586 return false;
8587
8588 // Perform an early exit check. Do not bother looking at stored values that
8589 // are not constants or loads.
8590 SDValue StoredVal = St->getValue();
8591 bool IsLoadSrc = isa<LoadSDNode>(StoredVal);
8592 if (!isa<ConstantSDNode>(StoredVal) && !isa<ConstantFPSDNode>(StoredVal) &&
8593 !IsLoadSrc)
8594 return false;
8595
8596 // Only look at ends of store sequences.
8597 SDValue Chain = SDValue(St, 1);
8598 if (Chain->hasOneUse() && Chain->use_begin()->getOpcode() == ISD::STORE)
8599 return false;
8600
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008601 // This holds the base pointer, index, and the offset in bytes from the base
8602 // pointer.
8603 BaseIndexOffset BasePtr = BaseIndexOffset::match(St->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00008604
8605 // We must have a base and an offset.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008606 if (!BasePtr.Base.getNode())
Nadav Rotemc653de62012-10-03 16:11:15 +00008607 return false;
8608
8609 // Do not handle stores to undef base pointers.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008610 if (BasePtr.Base.getOpcode() == ISD::UNDEF)
Nadav Rotemc653de62012-10-03 16:11:15 +00008611 return false;
8612
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008613 // Save the LoadSDNodes that we find in the chain.
8614 // We need to make sure that these nodes do not interfere with
8615 // any of the store nodes.
8616 SmallVector<LSBaseSDNode*, 8> AliasLoadNodes;
8617
8618 // Save the StoreSDNodes that we find in the chain.
Nadav Rotemc653de62012-10-03 16:11:15 +00008619 SmallVector<MemOpLink, 8> StoreNodes;
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008620
Nadav Rotemc653de62012-10-03 16:11:15 +00008621 // Walk up the chain and look for nodes with offsets from the same
8622 // base pointer. Stop when reaching an instruction with a different kind
8623 // or instruction which has a different base pointer.
8624 unsigned Seq = 0;
8625 StoreSDNode *Index = St;
8626 while (Index) {
8627 // If the chain has more than one use, then we can't reorder the mem ops.
8628 if (Index != St && !SDValue(Index, 1)->hasOneUse())
8629 break;
8630
8631 // Find the base pointer and offset for this memory node.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008632 BaseIndexOffset Ptr = BaseIndexOffset::match(Index->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00008633
8634 // Check that the base pointer is the same as the original one.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008635 if (!Ptr.equalBaseIndex(BasePtr))
Nadav Rotemc653de62012-10-03 16:11:15 +00008636 break;
8637
8638 // Check that the alignment is the same.
8639 if (Index->getAlignment() != St->getAlignment())
8640 break;
8641
8642 // The memory operands must not be volatile.
8643 if (Index->isVolatile() || Index->isIndexed())
8644 break;
8645
8646 // No truncation.
8647 if (StoreSDNode *St = dyn_cast<StoreSDNode>(Index))
8648 if (St->isTruncatingStore())
8649 break;
8650
8651 // The stored memory type must be the same.
8652 if (Index->getMemoryVT() != MemVT)
8653 break;
8654
8655 // We do not allow unaligned stores because we want to prevent overriding
8656 // stores.
8657 if (Index->getAlignment()*8 != MemVT.getSizeInBits())
8658 break;
8659
8660 // We found a potential memory operand to merge.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008661 StoreNodes.push_back(MemOpLink(Index, Ptr.Offset, Seq++));
Nadav Rotemc653de62012-10-03 16:11:15 +00008662
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008663 // Find the next memory operand in the chain. If the next operand in the
8664 // chain is a store then move up and continue the scan with the next
8665 // memory operand. If the next operand is a load save it and use alias
8666 // information to check if it interferes with anything.
8667 SDNode *NextInChain = Index->getChain().getNode();
8668 while (1) {
Nadav Rotemdde785c2012-12-06 17:34:13 +00008669 if (StoreSDNode *STn = dyn_cast<StoreSDNode>(NextInChain)) {
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008670 // We found a store node. Use it for the next iteration.
Nadav Rotemdde785c2012-12-06 17:34:13 +00008671 Index = STn;
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008672 break;
8673 } else if (LoadSDNode *Ldn = dyn_cast<LoadSDNode>(NextInChain)) {
8674 // Save the load node for later. Continue the scan.
8675 AliasLoadNodes.push_back(Ldn);
8676 NextInChain = Ldn->getChain().getNode();
8677 continue;
8678 } else {
8679 Index = NULL;
8680 break;
8681 }
8682 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008683 }
8684
8685 // Check if there is anything to merge.
8686 if (StoreNodes.size() < 2)
8687 return false;
8688
8689 // Sort the memory operands according to their distance from the base pointer.
8690 std::sort(StoreNodes.begin(), StoreNodes.end(),
8691 ConsecutiveMemoryChainSorter());
8692
8693 // Scan the memory operations on the chain and find the first non-consecutive
8694 // store memory address.
8695 unsigned LastConsecutiveStore = 0;
8696 int64_t StartAddress = StoreNodes[0].OffsetFromBase;
Nadav Rotemdde785c2012-12-06 17:34:13 +00008697 for (unsigned i = 0, e = StoreNodes.size(); i < e; ++i) {
8698
8699 // Check that the addresses are consecutive starting from the second
8700 // element in the list of stores.
8701 if (i > 0) {
8702 int64_t CurrAddress = StoreNodes[i].OffsetFromBase;
8703 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
8704 break;
8705 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008706
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008707 bool Alias = false;
8708 // Check if this store interferes with any of the loads that we found.
8709 for (unsigned ld = 0, lde = AliasLoadNodes.size(); ld < lde; ++ld)
8710 if (isAlias(AliasLoadNodes[ld], StoreNodes[i].MemNode)) {
8711 Alias = true;
8712 break;
8713 }
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008714 // We found a load that alias with this store. Stop the sequence.
8715 if (Alias)
8716 break;
8717
Nadav Rotemc653de62012-10-03 16:11:15 +00008718 // Mark this node as useful.
8719 LastConsecutiveStore = i;
8720 }
8721
8722 // The node with the lowest store address.
8723 LSBaseSDNode *FirstInChain = StoreNodes[0].MemNode;
8724
8725 // Store the constants into memory as one consecutive store.
8726 if (!IsLoadSrc) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008727 unsigned LastLegalType = 0;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008728 unsigned LastLegalVectorType = 0;
8729 bool NonZero = false;
Nadav Rotemc653de62012-10-03 16:11:15 +00008730 for (unsigned i=0; i<LastConsecutiveStore+1; ++i) {
8731 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8732 SDValue StoredVal = St->getValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008733
8734 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(StoredVal)) {
Benjamin Kramerebd7eab2012-10-05 18:19:44 +00008735 NonZero |= !C->isNullValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008736 } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(StoredVal)) {
Benjamin Kramerebd7eab2012-10-05 18:19:44 +00008737 NonZero |= !C->getConstantFPValue()->isNullValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008738 } else {
8739 // Non constant.
Nadav Rotemc653de62012-10-03 16:11:15 +00008740 break;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008741 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008742
Nadav Rotemc653de62012-10-03 16:11:15 +00008743 // Find a legal type for the constant store.
8744 unsigned StoreBW = (i+1) * ElementSizeBytes * 8;
8745 EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8746 if (TLI.isTypeLegal(StoreTy))
8747 LastLegalType = i+1;
Arnold Schwaighofere7370182013-04-02 15:58:51 +00008748 // Or check whether a truncstore is legal.
8749 else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) ==
8750 TargetLowering::TypePromoteInteger) {
8751 EVT LegalizedStoredValueTy =
8752 TLI.getTypeToTransformTo(*DAG.getContext(), StoredVal.getValueType());
8753 if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy))
8754 LastLegalType = i+1;
8755 }
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008756
8757 // Find a legal type for the vector store.
8758 EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1);
8759 if (TLI.isTypeLegal(Ty))
8760 LastLegalVectorType = i + 1;
Nadav Rotemc653de62012-10-03 16:11:15 +00008761 }
8762
Bob Wilson99d8e762012-12-20 01:36:20 +00008763 // We only use vectors if the constant is known to be zero and the
8764 // function is not marked with the noimplicitfloat attribute.
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008765 if (NonZero || NoVectors)
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008766 LastLegalVectorType = 0;
8767
Nadav Rotemc653de62012-10-03 16:11:15 +00008768 // Check if we found a legal integer type to store.
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008769 if (LastLegalType == 0 && LastLegalVectorType == 0)
Nadav Rotemc653de62012-10-03 16:11:15 +00008770 return false;
8771
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008772 bool UseVector = (LastLegalVectorType > LastLegalType) && !NoVectors;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008773 unsigned NumElem = UseVector ? LastLegalVectorType : LastLegalType;
8774
8775 // Make sure we have something to merge.
8776 if (NumElem < 2)
8777 return false;
Nadav Rotemc653de62012-10-03 16:11:15 +00008778
8779 unsigned EarliestNodeUsed = 0;
8780 for (unsigned i=0; i < NumElem; ++i) {
8781 // Find a chain for the new wide-store operand. Notice that some
8782 // of the store nodes that we found may not be selected for inclusion
8783 // in the wide store. The chain we use needs to be the chain of the
8784 // earliest store node which is *used* and replaced by the wide store.
8785 if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum)
8786 EarliestNodeUsed = i;
8787 }
8788
8789 // The earliest Node in the DAG.
8790 LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode;
Andrew Trickac6d9be2013-05-25 02:42:55 +00008791 SDLoc DL(StoreNodes[0].MemNode);
Nadav Rotemc653de62012-10-03 16:11:15 +00008792
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008793 SDValue StoredVal;
8794 if (UseVector) {
8795 // Find a legal type for the vector store.
8796 EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem);
8797 assert(TLI.isTypeLegal(Ty) && "Illegal vector store");
8798 StoredVal = DAG.getConstant(0, Ty);
8799 } else {
8800 unsigned StoreBW = NumElem * ElementSizeBytes * 8;
8801 APInt StoreInt(StoreBW, 0);
8802
8803 // Construct a single integer constant which is made of the smaller
8804 // constant inputs.
8805 bool IsLE = TLI.isLittleEndian();
8806 for (unsigned i = 0; i < NumElem ; ++i) {
8807 unsigned Idx = IsLE ?(NumElem - 1 - i) : i;
8808 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[Idx].MemNode);
8809 SDValue Val = St->getValue();
8810 StoreInt<<=ElementSizeBytes*8;
8811 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val)) {
8812 StoreInt|=C->getAPIntValue().zext(StoreBW);
8813 } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Val)) {
8814 StoreInt|= C->getValueAPF().bitcastToAPInt().zext(StoreBW);
8815 } else {
8816 assert(false && "Invalid constant element type");
8817 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008818 }
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008819
8820 // Create the new Load and Store operations.
8821 EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8822 StoredVal = DAG.getConstant(StoreInt, StoreTy);
Nadav Rotemc653de62012-10-03 16:11:15 +00008823 }
8824
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008825 SDValue NewStore = DAG.getStore(EarliestOp->getChain(), DL, StoredVal,
Nadav Rotemc653de62012-10-03 16:11:15 +00008826 FirstInChain->getBasePtr(),
8827 FirstInChain->getPointerInfo(),
8828 false, false,
8829 FirstInChain->getAlignment());
8830
8831 // Replace the first store with the new store
8832 CombineTo(EarliestOp, NewStore);
8833 // Erase all other stores.
8834 for (unsigned i = 0; i < NumElem ; ++i) {
8835 if (StoreNodes[i].MemNode == EarliestOp)
8836 continue;
8837 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
Rafael Espindola8e2b8ae2012-11-14 05:08:56 +00008838 // ReplaceAllUsesWith will replace all uses that existed when it was
8839 // called, but graph optimizations may cause new ones to appear. For
8840 // example, the case in pr14333 looks like
8841 //
8842 // St's chain -> St -> another store -> X
8843 //
8844 // And the only difference from St to the other store is the chain.
8845 // When we change it's chain to be St's chain they become identical,
8846 // get CSEed and the net result is that X is now a use of St.
8847 // Since we know that St is redundant, just iterate.
8848 while (!St->use_empty())
8849 DAG.ReplaceAllUsesWith(SDValue(St, 0), St->getChain());
Nadav Rotemc653de62012-10-03 16:11:15 +00008850 removeFromWorkList(St);
8851 DAG.DeleteNode(St);
8852 }
8853
8854 return true;
8855 }
8856
8857 // Below we handle the case of multiple consecutive stores that
8858 // come from multiple consecutive loads. We merge them into a single
8859 // wide load and a single wide store.
8860
8861 // Look for load nodes which are used by the stored values.
8862 SmallVector<MemOpLink, 8> LoadNodes;
8863
8864 // Find acceptable loads. Loads need to have the same chain (token factor),
8865 // must not be zext, volatile, indexed, and they must be consecutive.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008866 BaseIndexOffset LdBasePtr;
Nadav Rotemc653de62012-10-03 16:11:15 +00008867 for (unsigned i=0; i<LastConsecutiveStore+1; ++i) {
8868 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8869 LoadSDNode *Ld = dyn_cast<LoadSDNode>(St->getValue());
8870 if (!Ld) break;
8871
8872 // Loads must only have one use.
8873 if (!Ld->hasNUsesOfValue(1, 0))
8874 break;
8875
8876 // Check that the alignment is the same as the stores.
8877 if (Ld->getAlignment() != St->getAlignment())
8878 break;
8879
8880 // The memory operands must not be volatile.
8881 if (Ld->isVolatile() || Ld->isIndexed())
8882 break;
8883
8884 // We do not accept ext loads.
8885 if (Ld->getExtensionType() != ISD::NON_EXTLOAD)
8886 break;
8887
8888 // The stored memory type must be the same.
8889 if (Ld->getMemoryVT() != MemVT)
8890 break;
8891
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008892 BaseIndexOffset LdPtr = BaseIndexOffset::match(Ld->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00008893 // If this is not the first ptr that we check.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008894 if (LdBasePtr.Base.getNode()) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008895 // The base ptr must be the same.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008896 if (!LdPtr.equalBaseIndex(LdBasePtr))
Nadav Rotemc653de62012-10-03 16:11:15 +00008897 break;
8898 } else {
8899 // Check that all other base pointers are the same as this one.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008900 LdBasePtr = LdPtr;
Nadav Rotemc653de62012-10-03 16:11:15 +00008901 }
8902
8903 // We found a potential memory operand to merge.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008904 LoadNodes.push_back(MemOpLink(Ld, LdPtr.Offset, 0));
Nadav Rotemc653de62012-10-03 16:11:15 +00008905 }
8906
8907 if (LoadNodes.size() < 2)
8908 return false;
8909
8910 // Scan the memory operations on the chain and find the first non-consecutive
8911 // load memory address. These variables hold the index in the store node
8912 // array.
8913 unsigned LastConsecutiveLoad = 0;
8914 // This variable refers to the size and not index in the array.
8915 unsigned LastLegalVectorType = 0;
8916 unsigned LastLegalIntegerType = 0;
8917 StartAddress = LoadNodes[0].OffsetFromBase;
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008918 SDValue FirstChain = LoadNodes[0].MemNode->getChain();
8919 for (unsigned i = 1; i < LoadNodes.size(); ++i) {
8920 // All loads much share the same chain.
8921 if (LoadNodes[i].MemNode->getChain() != FirstChain)
8922 break;
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008923
Nadav Rotemc653de62012-10-03 16:11:15 +00008924 int64_t CurrAddress = LoadNodes[i].OffsetFromBase;
8925 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
8926 break;
8927 LastConsecutiveLoad = i;
8928
8929 // Find a legal type for the vector store.
8930 EVT StoreTy = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1);
8931 if (TLI.isTypeLegal(StoreTy))
8932 LastLegalVectorType = i + 1;
8933
8934 // Find a legal type for the integer store.
8935 unsigned StoreBW = (i+1) * ElementSizeBytes * 8;
8936 StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8937 if (TLI.isTypeLegal(StoreTy))
8938 LastLegalIntegerType = i + 1;
Arnold Schwaighofere7370182013-04-02 15:58:51 +00008939 // Or check whether a truncstore and extload is legal.
8940 else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) ==
8941 TargetLowering::TypePromoteInteger) {
8942 EVT LegalizedStoredValueTy =
8943 TLI.getTypeToTransformTo(*DAG.getContext(), StoreTy);
8944 if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy) &&
8945 TLI.isLoadExtLegal(ISD::ZEXTLOAD, StoreTy) &&
8946 TLI.isLoadExtLegal(ISD::SEXTLOAD, StoreTy) &&
8947 TLI.isLoadExtLegal(ISD::EXTLOAD, StoreTy))
8948 LastLegalIntegerType = i+1;
8949 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008950 }
8951
8952 // Only use vector types if the vector type is larger than the integer type.
8953 // If they are the same, use integers.
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008954 bool UseVectorTy = LastLegalVectorType > LastLegalIntegerType && !NoVectors;
Nadav Rotemc653de62012-10-03 16:11:15 +00008955 unsigned LastLegalType = std::max(LastLegalVectorType, LastLegalIntegerType);
8956
8957 // We add +1 here because the LastXXX variables refer to location while
8958 // the NumElem refers to array/index size.
8959 unsigned NumElem = std::min(LastConsecutiveStore, LastConsecutiveLoad) + 1;
8960 NumElem = std::min(LastLegalType, NumElem);
8961
8962 if (NumElem < 2)
8963 return false;
8964
8965 // The earliest Node in the DAG.
8966 unsigned EarliestNodeUsed = 0;
8967 LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode;
8968 for (unsigned i=1; i<NumElem; ++i) {
8969 // Find a chain for the new wide-store operand. Notice that some
8970 // of the store nodes that we found may not be selected for inclusion
8971 // in the wide store. The chain we use needs to be the chain of the
8972 // earliest store node which is *used* and replaced by the wide store.
8973 if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum)
8974 EarliestNodeUsed = i;
8975 }
8976
8977 // Find if it is better to use vectors or integers to load and store
8978 // to memory.
8979 EVT JointMemOpVT;
8980 if (UseVectorTy) {
8981 JointMemOpVT = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem);
8982 } else {
8983 unsigned StoreBW = NumElem * ElementSizeBytes * 8;
8984 JointMemOpVT = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8985 }
8986
Andrew Trickac6d9be2013-05-25 02:42:55 +00008987 SDLoc LoadDL(LoadNodes[0].MemNode);
8988 SDLoc StoreDL(StoreNodes[0].MemNode);
Nadav Rotemc653de62012-10-03 16:11:15 +00008989
8990 LoadSDNode *FirstLoad = cast<LoadSDNode>(LoadNodes[0].MemNode);
8991 SDValue NewLoad = DAG.getLoad(JointMemOpVT, LoadDL,
8992 FirstLoad->getChain(),
8993 FirstLoad->getBasePtr(),
8994 FirstLoad->getPointerInfo(),
8995 false, false, false,
8996 FirstLoad->getAlignment());
8997
8998 SDValue NewStore = DAG.getStore(EarliestOp->getChain(), StoreDL, NewLoad,
8999 FirstInChain->getBasePtr(),
9000 FirstInChain->getPointerInfo(), false, false,
9001 FirstInChain->getAlignment());
9002
Nadav Rotem2e7d3812012-10-03 19:30:31 +00009003 // Replace one of the loads with the new load.
9004 LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[0].MemNode);
9005 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1),
9006 SDValue(NewLoad.getNode(), 1));
9007
9008 // Remove the rest of the load chains.
9009 for (unsigned i = 1; i < NumElem ; ++i) {
Nadav Rotemc653de62012-10-03 16:11:15 +00009010 // Replace all chain users of the old load nodes with the chain of the new
9011 // load node.
9012 LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[i].MemNode);
Nadav Rotem2e7d3812012-10-03 19:30:31 +00009013 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), Ld->getChain());
9014 }
Nadav Rotemc653de62012-10-03 16:11:15 +00009015
Nadav Rotem2e7d3812012-10-03 19:30:31 +00009016 // Replace the first store with the new store.
9017 CombineTo(EarliestOp, NewStore);
9018 // Erase all other stores.
9019 for (unsigned i = 0; i < NumElem ; ++i) {
Nadav Rotemc653de62012-10-03 16:11:15 +00009020 // Remove all Store nodes.
9021 if (StoreNodes[i].MemNode == EarliestOp)
9022 continue;
9023 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
9024 DAG.ReplaceAllUsesOfValueWith(SDValue(St, 0), St->getChain());
9025 removeFromWorkList(St);
9026 DAG.DeleteNode(St);
9027 }
9028
9029 return true;
9030}
9031
Dan Gohman475871a2008-07-27 21:46:04 +00009032SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00009033 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00009034 SDValue Chain = ST->getChain();
9035 SDValue Value = ST->getValue();
9036 SDValue Ptr = ST->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00009037
Evan Cheng59d5b682007-05-07 21:27:48 +00009038 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00009039 // resultant store does not need a higher alignment than the original.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009040 if (Value.getOpcode() == ISD::BITCAST && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00009041 ST->isUnindexed()) {
Dan Gohman1ba519b2009-02-20 23:29:13 +00009042 unsigned OrigAlign = ST->getAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009043 EVT SVT = Value.getOperand(0).getValueType();
Micah Villmow3574eca2012-10-08 16:38:25 +00009044 unsigned Align = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00009045 getABITypeAlignment(SVT.getTypeForEVT(*DAG.getContext()));
Duncan Sandsd4b9c172008-06-13 19:07:40 +00009046 if (Align <= OrigAlign &&
Duncan Sands25cf2272008-11-24 14:53:14 +00009047 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohmanf560ffa2009-01-28 17:46:25 +00009048 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00009049 return DAG.getStore(Chain, SDLoc(N), Value.getOperand(0),
Chris Lattner6229d0a2010-09-21 18:41:36 +00009050 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00009051 ST->isNonTemporal(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00009052 }
Owen Andersona34d9362011-04-14 17:30:49 +00009053
Chris Lattnerb3452ea2011-04-09 02:32:02 +00009054 // Turn 'store undef, Ptr' -> nothing.
9055 if (Value.getOpcode() == ISD::UNDEF && ST->isUnindexed())
9056 return Chain;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00009057
Nate Begeman2cbba892006-12-11 02:23:46 +00009058 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00009059 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00009060 // NOTE: If the original store is volatile, this transform must not increase
9061 // the number of stores. For example, on x86-32 an f64 can be stored in one
9062 // processor operation but an i64 (which is not legal) requires two. So the
9063 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00009064 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00009065 SDValue Tmp;
Craig Topper0ff11902013-08-15 02:44:19 +00009066 switch (CFP->getSimpleValueType(0).SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009067 default: llvm_unreachable("Unknown FP type");
Pete Cooper438c0402012-06-21 18:00:39 +00009068 case MVT::f16: // We don't do this for these yet.
9069 case MVT::f80:
Owen Anderson825b72b2009-08-11 20:47:22 +00009070 case MVT::f128:
9071 case MVT::ppcf128:
Dale Johannesenc7b21d52007-09-18 18:36:59 +00009072 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00009073 case MVT::f32:
Chris Lattner2392ae72010-04-15 04:48:01 +00009074 if ((isTypeLegal(MVT::i32) && !LegalOperations && !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009075 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00009076 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Owen Anderson825b72b2009-08-11 20:47:22 +00009077 bitcastToAPInt().getZExtValue(), MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00009078 return DAG.getStore(Chain, SDLoc(N), Tmp,
Chris Lattner6229d0a2010-09-21 18:41:36 +00009079 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00009080 ST->isNonTemporal(), ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00009081 }
9082 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00009083 case MVT::f64:
Chris Lattner2392ae72010-04-15 04:48:01 +00009084 if ((TLI.isTypeLegal(MVT::i64) && !LegalOperations &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00009085 !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009086 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen7111b022008-10-09 18:53:47 +00009087 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +00009088 getZExtValue(), MVT::i64);
Andrew Trickac6d9be2013-05-25 02:42:55 +00009089 return DAG.getStore(Chain, SDLoc(N), Tmp,
Chris Lattner6229d0a2010-09-21 18:41:36 +00009090 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00009091 ST->isNonTemporal(), ST->getAlignment());
Chris Lattnerb3452ea2011-04-09 02:32:02 +00009092 }
Owen Andersona34d9362011-04-14 17:30:49 +00009093
Chris Lattnerb3452ea2011-04-09 02:32:02 +00009094 if (!ST->isVolatile() &&
9095 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00009096 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00009097 // argument passing. Since this is so common, custom legalize the
9098 // 64-bit integer store into two 32-bit stores.
Dale Johannesen7111b022008-10-09 18:53:47 +00009099 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009100 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
9101 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00009102 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00009103
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00009104 unsigned Alignment = ST->getAlignment();
9105 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00009106 bool isNonTemporal = ST->isNonTemporal();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00009107
Andrew Trickac6d9be2013-05-25 02:42:55 +00009108 SDValue St0 = DAG.getStore(Chain, SDLoc(ST), Lo,
Chris Lattner6229d0a2010-09-21 18:41:36 +00009109 Ptr, ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00009110 isVolatile, isNonTemporal,
9111 ST->getAlignment());
Andrew Trickac6d9be2013-05-25 02:42:55 +00009112 Ptr = DAG.getNode(ISD::ADD, SDLoc(N), Ptr.getValueType(), Ptr,
Chris Lattner62be1a72006-12-12 04:16:14 +00009113 DAG.getConstant(4, Ptr.getValueType()));
Duncan Sandsdc846502007-10-28 12:59:45 +00009114 Alignment = MinAlign(Alignment, 4U);
Andrew Trickac6d9be2013-05-25 02:42:55 +00009115 SDValue St1 = DAG.getStore(Chain, SDLoc(ST), Hi,
Chris Lattner6229d0a2010-09-21 18:41:36 +00009116 Ptr, ST->getPointerInfo().getWithOffset(4),
9117 isVolatile, isNonTemporal,
David Greene1e559442010-02-15 17:00:31 +00009118 Alignment);
Andrew Trickac6d9be2013-05-25 02:42:55 +00009119 return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other,
Bill Wendlingc144a572009-01-30 23:36:47 +00009120 St0, St1);
Chris Lattner62be1a72006-12-12 04:16:14 +00009121 }
Bill Wendlingc144a572009-01-30 23:36:47 +00009122
Chris Lattner62be1a72006-12-12 04:16:14 +00009123 break;
Evan Cheng25ece662006-12-11 17:25:19 +00009124 }
Nate Begeman2cbba892006-12-11 02:23:46 +00009125 }
Nate Begeman2cbba892006-12-11 02:23:46 +00009126 }
9127
Evan Cheng255f20f2010-04-01 06:04:33 +00009128 // Try to infer better alignment information than the store already has.
9129 if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00009130 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
9131 if (Align > ST->getAlignment())
Andrew Trickac6d9be2013-05-25 02:42:55 +00009132 return DAG.getTruncStore(Chain, SDLoc(N), Value,
Evan Chenged1c0c72011-11-28 22:37:34 +00009133 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
9134 ST->isVolatile(), ST->isNonTemporal(), Align);
Evan Cheng255f20f2010-04-01 06:04:33 +00009135 }
9136 }
9137
Evan Cheng31959b12011-02-02 01:06:55 +00009138 // Try transforming a pair floating point load / store ops to integer
9139 // load / store ops.
9140 SDValue NewST = TransformFPLoadStorePair(N);
9141 if (NewST.getNode())
9142 return NewST;
9143
Hal Finkel253acef2013-08-29 03:29:55 +00009144 bool UseAA = CombinerAA.getNumOccurrences() > 0 ? CombinerAA :
9145 TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA();
9146 if (UseAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00009147 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009148 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00009149
Jim Laskey6ff23e52006-10-04 16:53:27 +00009150 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00009151 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00009152 SDValue ReplStore;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009153
9154 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00009155 if (ST->isTruncatingStore()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009156 ReplStore = DAG.getTruncStore(BetterChain, SDLoc(N), Value, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00009157 ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00009158 ST->getMemoryVT(), ST->isVolatile(),
9159 ST->isNonTemporal(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00009160 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009161 ReplStore = DAG.getStore(BetterChain, SDLoc(N), Value, Ptr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00009162 ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00009163 ST->isVolatile(), ST->isNonTemporal(),
9164 ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00009165 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009166
Jim Laskey279f0532006-09-25 16:29:54 +00009167 // Create token to keep both nodes around.
Andrew Trickac6d9be2013-05-25 02:42:55 +00009168 SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson825b72b2009-08-11 20:47:22 +00009169 MVT::Other, Chain, ReplStore);
Bill Wendlingc144a572009-01-30 23:36:47 +00009170
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009171 // Make sure the new and old chains are cleaned up.
9172 AddToWorkList(Token.getNode());
9173
Jim Laskey274062c2006-10-13 23:32:28 +00009174 // Don't add users to work list.
9175 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00009176 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00009177 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009178
Evan Cheng33dbedc2006-11-05 09:31:14 +00009179 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00009180 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00009181 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00009182
Chris Lattner3c872852007-12-29 06:26:16 +00009183 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00009184 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Nadav Rotembaff46f2011-06-15 11:19:12 +00009185 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00009186 // See if we can simplify the input to this truncstore with knowledge that
9187 // only the low bits are being used. For example:
9188 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Scott Michelfdc40a02009-02-17 22:15:04 +00009189 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00009190 GetDemandedBits(Value,
Nadav Rotembaff46f2011-06-15 11:19:12 +00009191 APInt::getLowBitsSet(
9192 Value.getValueType().getScalarType().getSizeInBits(),
9193 ST->getMemoryVT().getScalarType().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00009194 AddToWorkList(Value.getNode());
9195 if (Shorter.getNode())
Andrew Trickac6d9be2013-05-25 02:42:55 +00009196 return DAG.getTruncStore(Chain, SDLoc(N), Shorter,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00009197 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00009198 ST->isVolatile(), ST->isNonTemporal(),
9199 ST->getAlignment());
Scott Michelfdc40a02009-02-17 22:15:04 +00009200
Chris Lattnere33544c2007-10-13 06:58:48 +00009201 // Otherwise, see if we can simplify the operation with
9202 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00009203 if (SimplifyDemandedBits(Value,
Eric Christopher503a64d2010-12-09 04:48:06 +00009204 APInt::getLowBitsSet(
9205 Value.getValueType().getScalarType().getSizeInBits(),
9206 ST->getMemoryVT().getScalarType().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00009207 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00009208 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009209
Chris Lattner3c872852007-12-29 06:26:16 +00009210 // If this is a load followed by a store to the same location, then the store
9211 // is dead/noop.
9212 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009213 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00009214 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00009215 // There can't be any side effects between the load and store, such as
9216 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00009217 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00009218 // The store is dead, remove it.
9219 return Chain;
9220 }
9221 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00009222
Chris Lattnerddf89562008-01-17 19:59:44 +00009223 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
9224 // truncating store. We can do this even if this is already a truncstore.
9225 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00009226 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00009227 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009228 ST->getMemoryVT())) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009229 return DAG.getTruncStore(Chain, SDLoc(N), Value.getOperand(0),
Chris Lattnerda2d8e12010-09-21 17:42:31 +00009230 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00009231 ST->isVolatile(), ST->isNonTemporal(),
9232 ST->getAlignment());
Chris Lattnerddf89562008-01-17 19:59:44 +00009233 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00009234
Nadav Rotemc653de62012-10-03 16:11:15 +00009235 // Only perform this optimization before the types are legal, because we
Nadav Rotemea2c50c2012-10-04 22:35:15 +00009236 // don't want to perform this optimization on every DAGCombine invocation.
Nadav Rotema569a802012-12-02 17:14:09 +00009237 if (!LegalTypes) {
9238 bool EverChanged = false;
9239
9240 do {
9241 // There can be multiple store sequences on the same chain.
9242 // Keep trying to merge store sequences until we are unable to do so
9243 // or until we merge the last store on the chain.
9244 bool Changed = MergeConsecutiveStores(ST);
9245 EverChanged |= Changed;
9246 if (!Changed) break;
9247 } while (ST->getOpcode() != ISD::DELETED_NODE);
9248
9249 if (EverChanged)
9250 return SDValue(N, 0);
9251 }
Nadav Rotemc653de62012-10-03 16:11:15 +00009252
Evan Cheng8b944d32009-05-28 00:35:15 +00009253 return ReduceLoadOpStoreWidth(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00009254}
9255
Dan Gohman475871a2008-07-27 21:46:04 +00009256SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
9257 SDValue InVec = N->getOperand(0);
9258 SDValue InVal = N->getOperand(1);
9259 SDValue EltNo = N->getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00009260 SDLoc dl(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00009261
Bob Wilson492fd452010-05-19 23:42:58 +00009262 // If the inserted element is an UNDEF, just use the input vector.
9263 if (InVal.getOpcode() == ISD::UNDEF)
9264 return InVec;
9265
Nadav Rotem609d54e2011-02-12 14:40:33 +00009266 EVT VT = InVec.getValueType();
9267
Owen Anderson95771af2011-02-25 21:41:48 +00009268 // If we can't generate a legal BUILD_VECTOR, exit
Nadav Rotem609d54e2011-02-12 14:40:33 +00009269 if (LegalOperations && !TLI.isOperationLegal(ISD::BUILD_VECTOR, VT))
9270 return SDValue();
9271
Eli Friedman9db817f2011-09-09 21:04:06 +00009272 // Check that we know which element is being inserted
9273 if (!isa<ConstantSDNode>(EltNo))
9274 return SDValue();
9275 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009276
Eli Friedman9db817f2011-09-09 21:04:06 +00009277 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
9278 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
9279 // vector elements.
9280 SmallVector<SDValue, 8> Ops;
Quentin Colombet75c94332013-07-30 00:24:09 +00009281 // Do not combine these two vectors if the output vector will not replace
9282 // the input vector.
9283 if (InVec.getOpcode() == ISD::BUILD_VECTOR && InVec.hasOneUse()) {
Eli Friedman9db817f2011-09-09 21:04:06 +00009284 Ops.append(InVec.getNode()->op_begin(),
9285 InVec.getNode()->op_end());
9286 } else if (InVec.getOpcode() == ISD::UNDEF) {
9287 unsigned NElts = VT.getVectorNumElements();
9288 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
9289 } else {
9290 return SDValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00009291 }
Eli Friedman9db817f2011-09-09 21:04:06 +00009292
9293 // Insert the element
9294 if (Elt < Ops.size()) {
9295 // All the operands of BUILD_VECTOR must have the same type;
9296 // we enforce that here.
9297 EVT OpVT = Ops[0].getValueType();
9298 if (InVal.getValueType() != OpVT)
9299 InVal = OpVT.bitsGT(InVal.getValueType()) ?
9300 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
9301 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
9302 Ops[Elt] = InVal;
9303 }
9304
9305 // Return the new vector
9306 return DAG.getNode(ISD::BUILD_VECTOR, dl,
9307 VT, &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00009308}
9309
Dan Gohman475871a2008-07-27 21:46:04 +00009310SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00009311 // (vextract (scalar_to_vector val, 0) -> val
9312 SDValue InVec = N->getOperand(0);
Nadav Rotemba05c912012-01-17 21:44:01 +00009313 EVT VT = InVec.getValueType();
9314 EVT NVT = N->getValueType(0);
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00009315
Duncan Sandsc356f332011-05-09 08:03:33 +00009316 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
9317 // Check if the result type doesn't match the inserted element type. A
9318 // SCALAR_TO_VECTOR may truncate the inserted element and the
9319 // EXTRACT_VECTOR_ELT may widen the extracted vector.
9320 SDValue InOp = InVec.getOperand(0);
Duncan Sandsc356f332011-05-09 08:03:33 +00009321 if (InOp.getValueType() != NVT) {
9322 assert(InOp.getValueType().isInteger() && NVT.isInteger());
Andrew Trickac6d9be2013-05-25 02:42:55 +00009323 return DAG.getSExtOrTrunc(InOp, SDLoc(InVec), NVT);
Duncan Sandsc356f332011-05-09 08:03:33 +00009324 }
9325 return InOp;
9326 }
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009327
Nadav Rotemba05c912012-01-17 21:44:01 +00009328 SDValue EltNo = N->getOperand(1);
9329 bool ConstEltNo = isa<ConstantSDNode>(EltNo);
9330
9331 // Transform: (EXTRACT_VECTOR_ELT( VECTOR_SHUFFLE )) -> EXTRACT_VECTOR_ELT.
9332 // We only perform this optimization before the op legalization phase because
Nadav Rotem6dfabb62012-09-20 08:53:31 +00009333 // we may introduce new vector instructions which are not backed by TD
9334 // patterns. For example on AVX, extracting elements from a wide vector
9335 // without using extract_subvector.
Nadav Rotemba05c912012-01-17 21:44:01 +00009336 if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE
9337 && ConstEltNo && !LegalOperations) {
9338 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
9339 int NumElem = VT.getVectorNumElements();
9340 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(InVec);
9341 // Find the new index to extract from.
9342 int OrigElt = SVOp->getMaskElt(Elt);
9343
9344 // Extracting an undef index is undef.
9345 if (OrigElt == -1)
9346 return DAG.getUNDEF(NVT);
9347
9348 // Select the right vector half to extract from.
9349 if (OrigElt < NumElem) {
9350 InVec = InVec->getOperand(0);
9351 } else {
9352 InVec = InVec->getOperand(1);
9353 OrigElt -= NumElem;
9354 }
9355
Tom Stellard425b76c2013-08-05 22:22:01 +00009356 EVT IndexTy = TLI.getVectorIdxTy();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009357 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N), NVT,
Jim Grosbacha249f7d2012-05-08 20:56:07 +00009358 InVec, DAG.getConstant(OrigElt, IndexTy));
Nadav Rotemba05c912012-01-17 21:44:01 +00009359 }
9360
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009361 // Perform only after legalization to ensure build_vector / vector_shuffle
9362 // optimizations have already been done.
Duncan Sands25cf2272008-11-24 14:53:14 +00009363 if (!LegalOperations) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009364
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00009365 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
9366 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
9367 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Evan Cheng513da432007-10-06 08:19:55 +00009368
Nadav Rotemba05c912012-01-17 21:44:01 +00009369 if (ConstEltNo) {
Eric Christophercaebdd42010-11-03 09:36:40 +00009370 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00009371 bool NewLoad = false;
Mon P Wanga60b5232008-12-11 00:26:16 +00009372 bool BCNumEltsChanged = false;
Owen Andersone50ed302009-08-10 22:56:29 +00009373 EVT ExtVT = VT.getVectorElementType();
9374 EVT LVT = ExtVT;
Bill Wendlingc144a572009-01-30 23:36:47 +00009375
Evan Cheng84387ea2012-03-13 22:00:52 +00009376 // If the result of load has to be truncated, then it's not necessarily
9377 // profitable.
Evan Chenga03d3662012-03-13 22:16:11 +00009378 if (NVT.bitsLT(LVT) && !TLI.isTruncateFree(LVT, NVT))
Evan Cheng84387ea2012-03-13 22:00:52 +00009379 return SDValue();
9380
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009381 if (InVec.getOpcode() == ISD::BITCAST) {
Eli Friedmand6e25602011-12-26 22:49:32 +00009382 // Don't duplicate a load with other uses.
9383 if (!InVec.hasOneUse())
9384 return SDValue();
9385
Owen Andersone50ed302009-08-10 22:56:29 +00009386 EVT BCVT = InVec.getOperand(0).getValueType();
9387 if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00009388 return SDValue();
Mon P Wanga60b5232008-12-11 00:26:16 +00009389 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
9390 BCNumEltsChanged = true;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009391 InVec = InVec.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00009392 ExtVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009393 NewLoad = true;
9394 }
Evan Cheng513da432007-10-06 08:19:55 +00009395
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009396 LoadSDNode *LN0 = NULL;
Nate Begeman5a5ca152009-04-29 05:20:52 +00009397 const ShuffleVectorSDNode *SVN = NULL;
Bill Wendlingc144a572009-01-30 23:36:47 +00009398 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009399 LN0 = cast<LoadSDNode>(InVec);
Bill Wendlingc144a572009-01-30 23:36:47 +00009400 } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Owen Andersone50ed302009-08-10 22:56:29 +00009401 InVec.getOperand(0).getValueType() == ExtVT &&
Bill Wendlingc144a572009-01-30 23:36:47 +00009402 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Eli Friedmand6e25602011-12-26 22:49:32 +00009403 // Don't duplicate a load with other uses.
9404 if (!InVec.hasOneUse())
9405 return SDValue();
9406
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009407 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
Nate Begeman5a5ca152009-04-29 05:20:52 +00009408 } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009409 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
9410 // =>
9411 // (load $addr+1*size)
Scott Michelfdc40a02009-02-17 22:15:04 +00009412
Eli Friedmand6e25602011-12-26 22:49:32 +00009413 // Don't duplicate a load with other uses.
9414 if (!InVec.hasOneUse())
9415 return SDValue();
9416
Mon P Wanga60b5232008-12-11 00:26:16 +00009417 // If the bit convert changed the number of elements, it is unsafe
9418 // to examine the mask.
9419 if (BCNumEltsChanged)
9420 return SDValue();
Nate Begeman5a5ca152009-04-29 05:20:52 +00009421
9422 // Select the input vector, guarding against out of range extract vector.
9423 unsigned NumElems = VT.getVectorNumElements();
Eric Christophercaebdd42010-11-03 09:36:40 +00009424 int Idx = (Elt > (int)NumElems) ? -1 : SVN->getMaskElt(Elt);
Nate Begeman5a5ca152009-04-29 05:20:52 +00009425 InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
9426
Eli Friedmand6e25602011-12-26 22:49:32 +00009427 if (InVec.getOpcode() == ISD::BITCAST) {
9428 // Don't duplicate a load with other uses.
9429 if (!InVec.hasOneUse())
9430 return SDValue();
9431
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009432 InVec = InVec.getOperand(0);
Eli Friedmand6e25602011-12-26 22:49:32 +00009433 }
Gabor Greifba36cb52008-08-28 21:40:38 +00009434 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009435 LN0 = cast<LoadSDNode>(InVec);
Ted Kremenekd0e88f32010-04-08 18:49:30 +00009436 Elt = (Idx < (int)NumElems) ? Idx : Idx - (int)NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00009437 }
9438 }
Bill Wendlingc144a572009-01-30 23:36:47 +00009439
Eli Friedmand6e25602011-12-26 22:49:32 +00009440 // Make sure we found a non-volatile load and the extractelement is
9441 // the only use.
Nadav Rotem42febc62011-05-11 14:40:50 +00009442 if (!LN0 || !LN0->hasNUsesOfValue(1,0) || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00009443 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009444
Eric Christopherd81f17a2010-11-03 20:44:42 +00009445 // If Idx was -1 above, Elt is going to be -1, so just return undef.
9446 if (Elt == -1)
Eli Friedmaned4b4272011-07-25 22:25:42 +00009447 return DAG.getUNDEF(LVT);
Eric Christopherd81f17a2010-11-03 20:44:42 +00009448
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009449 unsigned Align = LN0->getAlignment();
9450 if (NewLoad) {
9451 // Check the resultant load doesn't need a higher alignment than the
9452 // original load.
Bill Wendlingc144a572009-01-30 23:36:47 +00009453 unsigned NewAlign =
Micah Villmow3574eca2012-10-08 16:38:25 +00009454 TLI.getDataLayout()
Eric Christopher503a64d2010-12-09 04:48:06 +00009455 ->getABITypeAlignment(LVT.getTypeForEVT(*DAG.getContext()));
Bill Wendlingc144a572009-01-30 23:36:47 +00009456
Dan Gohmanf560ffa2009-01-28 17:46:25 +00009457 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00009458 return SDValue();
Bill Wendlingc144a572009-01-30 23:36:47 +00009459
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009460 Align = NewAlign;
9461 }
9462
Dan Gohman475871a2008-07-27 21:46:04 +00009463 SDValue NewPtr = LN0->getBasePtr();
Chris Lattnerfa459012010-09-21 16:08:50 +00009464 unsigned PtrOff = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009465
Eric Christopherd81f17a2010-11-03 20:44:42 +00009466 if (Elt) {
Chris Lattnerfa459012010-09-21 16:08:50 +00009467 PtrOff = LVT.getSizeInBits() * Elt / 8;
Owen Andersone50ed302009-08-10 22:56:29 +00009468 EVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009469 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00009470 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Andrew Trickac6d9be2013-05-25 02:42:55 +00009471 NewPtr = DAG.getNode(ISD::ADD, SDLoc(N), PtrType, NewPtr,
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009472 DAG.getConstant(PtrOff, PtrType));
9473 }
Bill Wendlingc144a572009-01-30 23:36:47 +00009474
Eli Friedman4db4add2011-11-16 23:50:22 +00009475 // The replacement we need to do here is a little tricky: we need to
9476 // replace an extractelement of a load with a load.
9477 // Use ReplaceAllUsesOfValuesWith to do the replacement.
Eli Friedmand6e25602011-12-26 22:49:32 +00009478 // Note that this replacement assumes that the extractvalue is the only
9479 // use of the load; that's okay because we don't want to perform this
9480 // transformation in other cases anyway.
Evan Cheng84387ea2012-03-13 22:00:52 +00009481 SDValue Load;
Evan Chenga03d3662012-03-13 22:16:11 +00009482 SDValue Chain;
Evan Cheng84387ea2012-03-13 22:00:52 +00009483 if (NVT.bitsGT(LVT)) {
9484 // If the result type of vextract is wider than the load, then issue an
9485 // extending load instead.
9486 ISD::LoadExtType ExtType = TLI.isLoadExtLegal(ISD::ZEXTLOAD, LVT)
9487 ? ISD::ZEXTLOAD : ISD::EXTLOAD;
Andrew Trickac6d9be2013-05-25 02:42:55 +00009488 Load = DAG.getExtLoad(ExtType, SDLoc(N), NVT, LN0->getChain(),
Evan Cheng84387ea2012-03-13 22:00:52 +00009489 NewPtr, LN0->getPointerInfo().getWithOffset(PtrOff),
9490 LVT, LN0->isVolatile(), LN0->isNonTemporal(),Align);
Evan Chenga03d3662012-03-13 22:16:11 +00009491 Chain = Load.getValue(1);
9492 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009493 Load = DAG.getLoad(LVT, SDLoc(N), LN0->getChain(), NewPtr,
Evan Cheng84387ea2012-03-13 22:00:52 +00009494 LN0->getPointerInfo().getWithOffset(PtrOff),
Stephen Lin155615d2013-07-08 00:37:03 +00009495 LN0->isVolatile(), LN0->isNonTemporal(),
Evan Cheng84387ea2012-03-13 22:00:52 +00009496 LN0->isInvariant(), Align);
Evan Chenga03d3662012-03-13 22:16:11 +00009497 Chain = Load.getValue(1);
9498 if (NVT.bitsLT(LVT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00009499 Load = DAG.getNode(ISD::TRUNCATE, SDLoc(N), NVT, Load);
Evan Chenga03d3662012-03-13 22:16:11 +00009500 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00009501 Load = DAG.getNode(ISD::BITCAST, SDLoc(N), NVT, Load);
Evan Chenga03d3662012-03-13 22:16:11 +00009502 }
Eli Friedman4db4add2011-11-16 23:50:22 +00009503 WorkListRemover DeadNodes(*this);
9504 SDValue From[] = { SDValue(N, 0), SDValue(LN0,1) };
Evan Chenga03d3662012-03-13 22:16:11 +00009505 SDValue To[] = { Load, Chain };
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00009506 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
Eli Friedman4db4add2011-11-16 23:50:22 +00009507 // Since we're explcitly calling ReplaceAllUses, add the new node to the
9508 // worklist explicitly as well.
9509 AddToWorkList(Load.getNode());
Craig Topper0c9da212012-03-20 05:28:39 +00009510 AddUsersToWorkList(Load.getNode()); // Add users too
Eli Friedman4db4add2011-11-16 23:50:22 +00009511 // Make sure to revisit this node to clean it up; it will usually be dead.
9512 AddToWorkList(N);
9513 return SDValue(N, 0);
Evan Cheng513da432007-10-06 08:19:55 +00009514 }
Bill Wendlingc144a572009-01-30 23:36:47 +00009515
Dan Gohman475871a2008-07-27 21:46:04 +00009516 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00009517}
Evan Cheng513da432007-10-06 08:19:55 +00009518
Michael Liaofac14ab2012-10-23 23:06:52 +00009519// Simplify (build_vec (ext )) to (bitcast (build_vec ))
9520SDValue DAGCombiner::reduceBuildVecExtToExtBuildVec(SDNode *N) {
9521 // We perform this optimization post type-legalization because
9522 // the type-legalizer often scalarizes integer-promoted vectors.
9523 // Performing this optimization before may create bit-casts which
9524 // will be type-legalized to complex code sequences.
9525 // We perform this optimization only before the operation legalizer because we
9526 // may introduce illegal operations.
9527 if (Level != AfterLegalizeVectorOps && Level != AfterLegalizeTypes)
9528 return SDValue();
9529
Dan Gohman7f321562007-06-25 16:23:39 +00009530 unsigned NumInScalars = N->getNumOperands();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009531 SDLoc dl(N);
Owen Andersone50ed302009-08-10 22:56:29 +00009532 EVT VT = N->getValueType(0);
Nadav Rotemb87bdac2012-07-15 08:38:23 +00009533
Nadav Rotemb00418a2011-10-29 21:23:04 +00009534 // Check to see if this is a BUILD_VECTOR of a bunch of values
9535 // which come from any_extend or zero_extend nodes. If so, we can create
9536 // a new BUILD_VECTOR using bit-casts which may enable other BUILD_VECTOR
Nadav Rotemf47368b2011-10-31 20:08:25 +00009537 // optimizations. We do not handle sign-extend because we can't fill the sign
9538 // using shuffles.
Nadav Rotemb00418a2011-10-29 21:23:04 +00009539 EVT SourceType = MVT::Other;
Craig Topperd3b58892012-01-17 09:09:48 +00009540 bool AllAnyExt = true;
Nadav Rotemb87bdac2012-07-15 08:38:23 +00009541
Craig Topperd3b58892012-01-17 09:09:48 +00009542 for (unsigned i = 0; i != NumInScalars; ++i) {
Nadav Rotemb00418a2011-10-29 21:23:04 +00009543 SDValue In = N->getOperand(i);
9544 // Ignore undef inputs.
9545 if (In.getOpcode() == ISD::UNDEF) continue;
9546
9547 bool AnyExt = In.getOpcode() == ISD::ANY_EXTEND;
9548 bool ZeroExt = In.getOpcode() == ISD::ZERO_EXTEND;
9549
Nadav Rotemf47368b2011-10-31 20:08:25 +00009550 // Abort if the element is not an extension.
Nadav Rotemb00418a2011-10-29 21:23:04 +00009551 if (!ZeroExt && !AnyExt) {
Nadav Rotemf47368b2011-10-31 20:08:25 +00009552 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00009553 break;
9554 }
9555
9556 // The input is a ZeroExt or AnyExt. Check the original type.
9557 EVT InTy = In.getOperand(0).getValueType();
9558
9559 // Check that all of the widened source types are the same.
9560 if (SourceType == MVT::Other)
Nadav Rotemf47368b2011-10-31 20:08:25 +00009561 // First time.
Nadav Rotemb00418a2011-10-29 21:23:04 +00009562 SourceType = InTy;
9563 else if (InTy != SourceType) {
9564 // Multiple income types. Abort.
Nadav Rotemf47368b2011-10-31 20:08:25 +00009565 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00009566 break;
9567 }
9568
9569 // Check if all of the extends are ANY_EXTENDs.
Craig Topperd3b58892012-01-17 09:09:48 +00009570 AllAnyExt &= AnyExt;
Nadav Rotemb00418a2011-10-29 21:23:04 +00009571 }
9572
Nadav Rotemf47368b2011-10-31 20:08:25 +00009573 // In order to have valid types, all of the inputs must be extended from the
9574 // same source type and all of the inputs must be any or zero extend.
9575 // Scalar sizes must be a power of two.
Michael Liaofac14ab2012-10-23 23:06:52 +00009576 EVT OutScalarTy = VT.getScalarType();
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009577 bool ValidTypes = SourceType != MVT::Other &&
Nadav Rotemf47368b2011-10-31 20:08:25 +00009578 isPowerOf2_32(OutScalarTy.getSizeInBits()) &&
9579 isPowerOf2_32(SourceType.getSizeInBits());
9580
Nadav Rotem6431ff92012-03-15 08:49:06 +00009581 // Create a new simpler BUILD_VECTOR sequence which other optimizations can
9582 // turn into a single shuffle instruction.
Michael Liaofac14ab2012-10-23 23:06:52 +00009583 if (!ValidTypes)
9584 return SDValue();
Nadav Rotemb00418a2011-10-29 21:23:04 +00009585
Michael Liaofac14ab2012-10-23 23:06:52 +00009586 bool isLE = TLI.isLittleEndian();
9587 unsigned ElemRatio = OutScalarTy.getSizeInBits()/SourceType.getSizeInBits();
9588 assert(ElemRatio > 1 && "Invalid element size ratio");
9589 SDValue Filler = AllAnyExt ? DAG.getUNDEF(SourceType):
9590 DAG.getConstant(0, SourceType);
Nadav Rotemb00418a2011-10-29 21:23:04 +00009591
Michael Liaofac14ab2012-10-23 23:06:52 +00009592 unsigned NewBVElems = ElemRatio * VT.getVectorNumElements();
9593 SmallVector<SDValue, 8> Ops(NewBVElems, Filler);
Nadav Rotemb00418a2011-10-29 21:23:04 +00009594
Michael Liaofac14ab2012-10-23 23:06:52 +00009595 // Populate the new build_vector
Jakub Staszakadf38912012-10-24 00:38:25 +00009596 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Michael Liaofac14ab2012-10-23 23:06:52 +00009597 SDValue Cast = N->getOperand(i);
9598 assert((Cast.getOpcode() == ISD::ANY_EXTEND ||
9599 Cast.getOpcode() == ISD::ZERO_EXTEND ||
9600 Cast.getOpcode() == ISD::UNDEF) && "Invalid cast opcode");
9601 SDValue In;
9602 if (Cast.getOpcode() == ISD::UNDEF)
9603 In = DAG.getUNDEF(SourceType);
9604 else
9605 In = Cast->getOperand(0);
9606 unsigned Index = isLE ? (i * ElemRatio) :
9607 (i * ElemRatio + (ElemRatio - 1));
Nadav Rotemb00418a2011-10-29 21:23:04 +00009608
Michael Liaofac14ab2012-10-23 23:06:52 +00009609 assert(Index < Ops.size() && "Invalid index");
9610 Ops[Index] = In;
Nadav Rotemb00418a2011-10-29 21:23:04 +00009611 }
Chris Lattnerca242442006-03-19 01:27:56 +00009612
Michael Liaofac14ab2012-10-23 23:06:52 +00009613 // The type of the new BUILD_VECTOR node.
9614 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), SourceType, NewBVElems);
9615 assert(VecVT.getSizeInBits() == VT.getSizeInBits() &&
9616 "Invalid vector size");
9617 // Check if the new vector type is legal.
9618 if (!isTypeLegal(VecVT)) return SDValue();
9619
9620 // Make the new BUILD_VECTOR.
9621 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], Ops.size());
9622
9623 // The new BUILD_VECTOR node has the potential to be further optimized.
9624 AddToWorkList(BV.getNode());
9625 // Bitcast to the desired type.
9626 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
9627}
9628
Michael Liao1a5cc712012-10-24 04:14:18 +00009629SDValue DAGCombiner::reduceBuildVecConvertToConvertBuildVec(SDNode *N) {
9630 EVT VT = N->getValueType(0);
9631
9632 unsigned NumInScalars = N->getNumOperands();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009633 SDLoc dl(N);
Michael Liao1a5cc712012-10-24 04:14:18 +00009634
9635 EVT SrcVT = MVT::Other;
9636 unsigned Opcode = ISD::DELETED_NODE;
9637 unsigned NumDefs = 0;
9638
9639 for (unsigned i = 0; i != NumInScalars; ++i) {
9640 SDValue In = N->getOperand(i);
9641 unsigned Opc = In.getOpcode();
9642
9643 if (Opc == ISD::UNDEF)
9644 continue;
9645
9646 // If all scalar values are floats and converted from integers.
9647 if (Opcode == ISD::DELETED_NODE &&
9648 (Opc == ISD::UINT_TO_FP || Opc == ISD::SINT_TO_FP)) {
9649 Opcode = Opc;
Michael Liao1a5cc712012-10-24 04:14:18 +00009650 }
Tom Stellardd40758b2013-01-02 22:13:01 +00009651
Michael Liao1a5cc712012-10-24 04:14:18 +00009652 if (Opc != Opcode)
9653 return SDValue();
9654
9655 EVT InVT = In.getOperand(0).getValueType();
9656
9657 // If all scalar values are typed differently, bail out. It's chosen to
9658 // simplify BUILD_VECTOR of integer types.
9659 if (SrcVT == MVT::Other)
9660 SrcVT = InVT;
9661 if (SrcVT != InVT)
9662 return SDValue();
9663 NumDefs++;
9664 }
9665
9666 // If the vector has just one element defined, it's not worth to fold it into
9667 // a vectorized one.
9668 if (NumDefs < 2)
9669 return SDValue();
9670
9671 assert((Opcode == ISD::UINT_TO_FP || Opcode == ISD::SINT_TO_FP)
9672 && "Should only handle conversion from integer to float.");
9673 assert(SrcVT != MVT::Other && "Cannot determine source type!");
9674
9675 EVT NVT = EVT::getVectorVT(*DAG.getContext(), SrcVT, NumInScalars);
Tom Stellardd40758b2013-01-02 22:13:01 +00009676
9677 if (!TLI.isOperationLegalOrCustom(Opcode, NVT))
9678 return SDValue();
9679
Michael Liao1a5cc712012-10-24 04:14:18 +00009680 SmallVector<SDValue, 8> Opnds;
9681 for (unsigned i = 0; i != NumInScalars; ++i) {
9682 SDValue In = N->getOperand(i);
9683
9684 if (In.getOpcode() == ISD::UNDEF)
9685 Opnds.push_back(DAG.getUNDEF(SrcVT));
9686 else
9687 Opnds.push_back(In.getOperand(0));
9688 }
9689 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT,
9690 &Opnds[0], Opnds.size());
9691 AddToWorkList(BV.getNode());
9692
9693 return DAG.getNode(Opcode, dl, VT, BV);
9694}
9695
Michael Liaofac14ab2012-10-23 23:06:52 +00009696SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
9697 unsigned NumInScalars = N->getNumOperands();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009698 SDLoc dl(N);
Michael Liaofac14ab2012-10-23 23:06:52 +00009699 EVT VT = N->getValueType(0);
9700
9701 // A vector built entirely of undefs is undef.
9702 if (ISD::allOperandsUndef(N))
9703 return DAG.getUNDEF(VT);
9704
9705 SDValue V = reduceBuildVecExtToExtBuildVec(N);
9706 if (V.getNode())
9707 return V;
9708
Michael Liao1a5cc712012-10-24 04:14:18 +00009709 V = reduceBuildVecConvertToConvertBuildVec(N);
9710 if (V.getNode())
9711 return V;
9712
Dan Gohman7f321562007-06-25 16:23:39 +00009713 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
9714 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
9715 // at most two distinct vectors, turn this into a shuffle node.
Duncan Sands00294ca2012-03-19 15:35:44 +00009716
9717 // May only combine to shuffle after legalize if shuffle is legal.
9718 if (LegalOperations &&
9719 !TLI.isOperationLegalOrCustom(ISD::VECTOR_SHUFFLE, VT))
9720 return SDValue();
9721
Dan Gohman475871a2008-07-27 21:46:04 +00009722 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00009723 for (unsigned i = 0; i != NumInScalars; ++i) {
9724 // Ignore undef inputs.
9725 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00009726
Dan Gohman7f321562007-06-25 16:23:39 +00009727 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00009728 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00009729 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00009730 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00009731 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009732 break;
9733 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009734
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009735 // We allow up to two distinct input vectors.
Dan Gohman475871a2008-07-27 21:46:04 +00009736 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009737 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
9738 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00009739
Gabor Greifba36cb52008-08-28 21:40:38 +00009740 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00009741 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00009742 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00009743 VecIn2 = ExtractedFromVec;
9744 } else {
9745 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009746 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009747 break;
9748 }
9749 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009750
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009751 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00009752 if (VecIn1.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00009753 SmallVector<int, 8> Mask;
Chris Lattnerd7648c82006-03-28 20:28:38 +00009754 for (unsigned i = 0; i != NumInScalars; ++i) {
9755 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Nate Begeman9008ca62009-04-27 18:41:29 +00009756 Mask.push_back(-1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009757 continue;
9758 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009759
Rafael Espindola15684b22009-04-24 12:40:33 +00009760 // If extracting from the first vector, just use the index directly.
Nate Begeman9008ca62009-04-27 18:41:29 +00009761 SDValue Extract = N->getOperand(i);
Mon P Wang93b74152009-03-17 06:33:10 +00009762 SDValue ExtVal = Extract.getOperand(1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009763 if (Extract.getOperand(0) == VecIn1) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00009764 unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue();
9765 if (ExtIndex > VT.getVectorNumElements())
9766 return SDValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009767
Nate Begeman5a5ca152009-04-29 05:20:52 +00009768 Mask.push_back(ExtIndex);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009769 continue;
9770 }
9771
9772 // Otherwise, use InIdx + VecSize
Mon P Wang93b74152009-03-17 06:33:10 +00009773 unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00009774 Mask.push_back(Idx+NumInScalars);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009775 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009776
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009777 // We can't generate a shuffle node with mismatched input and output types.
9778 // Attempt to transform a single input vector to the correct type.
9779 if ((VT != VecIn1.getValueType())) {
9780 // We don't support shuffeling between TWO values of different types.
9781 if (VecIn2.getNode() != 0)
9782 return SDValue();
9783
9784 // We only support widening of vectors which are half the size of the
9785 // output registers. For example XMM->YMM widening on X86 with AVX.
9786 if (VecIn1.getValueType().getSizeInBits()*2 != VT.getSizeInBits())
9787 return SDValue();
9788
James Molloy8cd08bf2012-09-10 14:01:21 +00009789 // If the input vector type has a different base type to the output
9790 // vector type, bail out.
9791 if (VecIn1.getValueType().getVectorElementType() !=
9792 VT.getVectorElementType())
9793 return SDValue();
9794
Stepan Dyatkovskiyfdeb9fe2012-08-22 09:33:55 +00009795 // Widen the input vector by adding undef values.
Michael Liaofac14ab2012-10-23 23:06:52 +00009796 VecIn1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
Stepan Dyatkovskiyfdeb9fe2012-08-22 09:33:55 +00009797 VecIn1, DAG.getUNDEF(VecIn1.getValueType()));
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009798 }
9799
9800 // If VecIn2 is unused then change it to undef.
9801 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
9802
Nadav Rotem6dfabb62012-09-20 08:53:31 +00009803 // Check that we were able to transform all incoming values to the same
9804 // type.
Nadav Rotem0877fdf2012-02-13 12:42:26 +00009805 if (VecIn2.getValueType() != VecIn1.getValueType() ||
9806 VecIn1.getValueType() != VT)
9807 return SDValue();
9808
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009809 // Only type-legal BUILD_VECTOR nodes are converted to shuffle nodes.
Nadav Rotem0877fdf2012-02-13 12:42:26 +00009810 if (!isTypeLegal(VT))
Duncan Sands25cf2272008-11-24 14:53:14 +00009811 return SDValue();
9812
Dan Gohman7f321562007-06-25 16:23:39 +00009813 // Return the new VECTOR_SHUFFLE node.
Nate Begeman9008ca62009-04-27 18:41:29 +00009814 SDValue Ops[2];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00009815 Ops[0] = VecIn1;
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009816 Ops[1] = VecIn2;
Michael Liaofac14ab2012-10-23 23:06:52 +00009817 return DAG.getVectorShuffle(VT, dl, Ops[0], Ops[1], &Mask[0]);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009818 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009819
Dan Gohman475871a2008-07-27 21:46:04 +00009820 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00009821}
9822
Dan Gohman475871a2008-07-27 21:46:04 +00009823SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00009824 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
9825 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
9826 // inputs come from at most two distinct vectors, turn this into a shuffle
9827 // node.
9828
9829 // If we only have one input vector, we don't need to do any concatenation.
Bill Wendlingc144a572009-01-30 23:36:47 +00009830 if (N->getNumOperands() == 1)
Dan Gohman7f321562007-06-25 16:23:39 +00009831 return N->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00009832
Nadav Rotemb7e230d2012-07-14 21:30:27 +00009833 // Check if all of the operands are undefs.
Nadav Rotemb87bdac2012-07-15 08:38:23 +00009834 if (ISD::allOperandsUndef(N))
Nadav Rotemb7e230d2012-07-14 21:30:27 +00009835 return DAG.getUNDEF(N->getValueType(0));
9836
Nadav Rotemb2ed5fa2013-05-01 19:18:51 +00009837 // Type legalization of vectors and DAG canonicalization of SHUFFLE_VECTOR
9838 // nodes often generate nop CONCAT_VECTOR nodes.
9839 // Scan the CONCAT_VECTOR operands and look for a CONCAT operations that
9840 // place the incoming vectors at the exact same location.
9841 SDValue SingleSource = SDValue();
9842 unsigned PartNumElem = N->getOperand(0).getValueType().getVectorNumElements();
9843
9844 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
9845 SDValue Op = N->getOperand(i);
9846
9847 if (Op.getOpcode() == ISD::UNDEF)
9848 continue;
9849
9850 // Check if this is the identity extract:
9851 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR)
9852 return SDValue();
9853
9854 // Find the single incoming vector for the extract_subvector.
9855 if (SingleSource.getNode()) {
9856 if (Op.getOperand(0) != SingleSource)
9857 return SDValue();
9858 } else {
9859 SingleSource = Op.getOperand(0);
Michael Kuperstein27202482013-05-06 08:06:13 +00009860
9861 // Check the source type is the same as the type of the result.
9862 // If not, this concat may extend the vector, so we can not
9863 // optimize it away.
9864 if (SingleSource.getValueType() != N->getValueType(0))
9865 return SDValue();
Nadav Rotemb2ed5fa2013-05-01 19:18:51 +00009866 }
9867
9868 unsigned IdentityIndex = i * PartNumElem;
9869 ConstantSDNode *CS = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9870 // The extract index must be constant.
9871 if (!CS)
9872 return SDValue();
Stephen Lin155615d2013-07-08 00:37:03 +00009873
Nadav Rotemb2ed5fa2013-05-01 19:18:51 +00009874 // Check that we are reading from the identity index.
9875 if (CS->getZExtValue() != IdentityIndex)
9876 return SDValue();
9877 }
9878
9879 if (SingleSource.getNode())
9880 return SingleSource;
Stephen Lin155615d2013-07-08 00:37:03 +00009881
Dan Gohman475871a2008-07-27 21:46:04 +00009882 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00009883}
9884
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00009885SDValue DAGCombiner::visitEXTRACT_SUBVECTOR(SDNode* N) {
9886 EVT NVT = N->getValueType(0);
9887 SDValue V = N->getOperand(0);
9888
Michael Liao13429e22012-10-17 20:48:33 +00009889 if (V->getOpcode() == ISD::CONCAT_VECTORS) {
9890 // Combine:
9891 // (extract_subvec (concat V1, V2, ...), i)
9892 // Into:
9893 // Vi if possible
Michael Liao9aecdb52012-10-19 03:17:00 +00009894 // Only operand 0 is checked as 'concat' assumes all inputs of the same type.
9895 if (V->getOperand(0).getValueType() != NVT)
9896 return SDValue();
Michael Liao13429e22012-10-17 20:48:33 +00009897 unsigned Idx = dyn_cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
9898 unsigned NumElems = NVT.getVectorNumElements();
9899 assert((Idx % NumElems) == 0 &&
9900 "IDX in concat is not a multiple of the result vector length.");
9901 return V->getOperand(Idx / NumElems);
9902 }
9903
Michael Liaob4f98ea2013-03-25 23:47:35 +00009904 // Skip bitcasting
9905 if (V->getOpcode() == ISD::BITCAST)
9906 V = V.getOperand(0);
9907
9908 if (V->getOpcode() == ISD::INSERT_SUBVECTOR) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009909 SDLoc dl(N);
Michael Liaob4f98ea2013-03-25 23:47:35 +00009910 // Handle only simple case where vector being inserted and vector
9911 // being extracted are of same type, and are half size of larger vectors.
9912 EVT BigVT = V->getOperand(0).getValueType();
9913 EVT SmallVT = V->getOperand(1).getValueType();
9914 if (!NVT.bitsEq(SmallVT) || NVT.getSizeInBits()*2 != BigVT.getSizeInBits())
9915 return SDValue();
9916
9917 // Only handle cases where both indexes are constants with the same type.
9918 ConstantSDNode *ExtIdx = dyn_cast<ConstantSDNode>(N->getOperand(1));
9919 ConstantSDNode *InsIdx = dyn_cast<ConstantSDNode>(V->getOperand(2));
9920
9921 if (InsIdx && ExtIdx &&
9922 InsIdx->getValueType(0).getSizeInBits() <= 64 &&
9923 ExtIdx->getValueType(0).getSizeInBits() <= 64) {
9924 // Combine:
9925 // (extract_subvec (insert_subvec V1, V2, InsIdx), ExtIdx)
9926 // Into:
9927 // indices are equal or bit offsets are equal => V1
9928 // otherwise => (extract_subvec V1, ExtIdx)
9929 if (InsIdx->getZExtValue() * SmallVT.getScalarType().getSizeInBits() ==
9930 ExtIdx->getZExtValue() * NVT.getScalarType().getSizeInBits())
9931 return DAG.getNode(ISD::BITCAST, dl, NVT, V->getOperand(1));
9932 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NVT,
9933 DAG.getNode(ISD::BITCAST, dl,
9934 N->getOperand(0).getValueType(),
9935 V->getOperand(0)), N->getOperand(1));
9936 }
9937 }
9938
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00009939 return SDValue();
9940}
9941
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009942// Tries to turn a shuffle of two CONCAT_VECTORS into a single concat.
9943static SDValue partitionShuffleOfConcats(SDNode *N, SelectionDAG &DAG) {
9944 EVT VT = N->getValueType(0);
9945 unsigned NumElts = VT.getVectorNumElements();
9946
9947 SDValue N0 = N->getOperand(0);
9948 SDValue N1 = N->getOperand(1);
9949 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9950
9951 SmallVector<SDValue, 4> Ops;
9952 EVT ConcatVT = N0.getOperand(0).getValueType();
9953 unsigned NumElemsPerConcat = ConcatVT.getVectorNumElements();
9954 unsigned NumConcats = NumElts / NumElemsPerConcat;
9955
9956 // Look at every vector that's inserted. We're looking for exact
9957 // subvector-sized copies from a concatenated vector
9958 for (unsigned I = 0; I != NumConcats; ++I) {
9959 // Make sure we're dealing with a copy.
9960 unsigned Begin = I * NumElemsPerConcat;
Hao Liu3778c042013-05-13 02:07:05 +00009961 bool AllUndef = true, NoUndef = true;
9962 for (unsigned J = Begin; J != Begin + NumElemsPerConcat; ++J) {
9963 if (SVN->getMaskElt(J) >= 0)
9964 AllUndef = false;
9965 else
9966 NoUndef = false;
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009967 }
9968
Hao Liu3778c042013-05-13 02:07:05 +00009969 if (NoUndef) {
Hao Liu3778c042013-05-13 02:07:05 +00009970 if (SVN->getMaskElt(Begin) % NumElemsPerConcat != 0)
9971 return SDValue();
9972
9973 for (unsigned J = 1; J != NumElemsPerConcat; ++J)
9974 if (SVN->getMaskElt(Begin + J - 1) + 1 != SVN->getMaskElt(Begin + J))
9975 return SDValue();
9976
9977 unsigned FirstElt = SVN->getMaskElt(Begin) / NumElemsPerConcat;
9978 if (FirstElt < N0.getNumOperands())
9979 Ops.push_back(N0.getOperand(FirstElt));
9980 else
9981 Ops.push_back(N1.getOperand(FirstElt - N0.getNumOperands()));
9982
9983 } else if (AllUndef) {
9984 Ops.push_back(DAG.getUNDEF(N0.getOperand(0).getValueType()));
9985 } else { // Mixed with general masks and undefs, can't do optimization.
9986 return SDValue();
9987 }
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009988 }
9989
Andrew Trickac6d9be2013-05-25 02:42:55 +00009990 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, Ops.data(),
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009991 Ops.size());
9992}
9993
Dan Gohman475871a2008-07-27 21:46:04 +00009994SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00009995 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00009996 unsigned NumElts = VT.getVectorNumElements();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00009997
Mon P Wangaeb06d22008-11-10 04:46:22 +00009998 SDValue N0 = N->getOperand(0);
Craig Topper481b79c2012-01-04 08:07:43 +00009999 SDValue N1 = N->getOperand(1);
Mon P Wangaeb06d22008-11-10 04:46:22 +000010000
Craig Topperae1bec52012-04-09 05:16:56 +000010001 assert(N0.getValueType() == VT && "Vector shuffle must be normalized in DAG");
Mon P Wangaeb06d22008-11-10 04:46:22 +000010002
Craig Topper481b79c2012-01-04 08:07:43 +000010003 // Canonicalize shuffle undef, undef -> undef
10004 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
10005 return DAG.getUNDEF(VT);
10006
10007 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
10008
10009 // Canonicalize shuffle v, v -> v, undef
10010 if (N0 == N1) {
10011 SmallVector<int, 8> NewMask;
10012 for (unsigned i = 0; i != NumElts; ++i) {
10013 int Idx = SVN->getMaskElt(i);
10014 if (Idx >= (int)NumElts) Idx -= NumElts;
10015 NewMask.push_back(Idx);
10016 }
Andrew Trickac6d9be2013-05-25 02:42:55 +000010017 return DAG.getVectorShuffle(VT, SDLoc(N), N0, DAG.getUNDEF(VT),
Craig Topper481b79c2012-01-04 08:07:43 +000010018 &NewMask[0]);
10019 }
10020
10021 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
10022 if (N0.getOpcode() == ISD::UNDEF) {
10023 SmallVector<int, 8> NewMask;
10024 for (unsigned i = 0; i != NumElts; ++i) {
10025 int Idx = SVN->getMaskElt(i);
Craig Topper4b206bd2012-04-09 05:55:33 +000010026 if (Idx >= 0) {
Craig Topper01d22aa2013-08-08 07:38:55 +000010027 if (Idx >= (int)NumElts)
Craig Topper4b206bd2012-04-09 05:55:33 +000010028 Idx -= NumElts;
Craig Topper01d22aa2013-08-08 07:38:55 +000010029 else
10030 Idx = -1; // remove reference to lhs
Craig Topper4b206bd2012-04-09 05:55:33 +000010031 }
10032 NewMask.push_back(Idx);
Craig Topper481b79c2012-01-04 08:07:43 +000010033 }
Andrew Trickac6d9be2013-05-25 02:42:55 +000010034 return DAG.getVectorShuffle(VT, SDLoc(N), N1, DAG.getUNDEF(VT),
Craig Topper481b79c2012-01-04 08:07:43 +000010035 &NewMask[0]);
10036 }
10037
10038 // Remove references to rhs if it is undef
10039 if (N1.getOpcode() == ISD::UNDEF) {
10040 bool Changed = false;
10041 SmallVector<int, 8> NewMask;
10042 for (unsigned i = 0; i != NumElts; ++i) {
10043 int Idx = SVN->getMaskElt(i);
10044 if (Idx >= (int)NumElts) {
10045 Idx = -1;
10046 Changed = true;
10047 }
10048 NewMask.push_back(Idx);
10049 }
10050 if (Changed)
Andrew Trickac6d9be2013-05-25 02:42:55 +000010051 return DAG.getVectorShuffle(VT, SDLoc(N), N0, N1, &NewMask[0]);
Craig Topper481b79c2012-01-04 08:07:43 +000010052 }
Evan Chenge7bec0d2006-07-20 22:44:41 +000010053
Bob Wilson0f1db1a2010-10-28 17:06:14 +000010054 // If it is a splat, check if the argument vector is another splat or a
10055 // build_vector with all scalar elements the same.
Bob Wilson0f1db1a2010-10-28 17:06:14 +000010056 if (SVN->isSplat() && SVN->getSplatIndex() < (int)NumElts) {
Gabor Greifba36cb52008-08-28 21:40:38 +000010057 SDNode *V = N0.getNode();
Evan Cheng917ec982006-07-21 08:25:53 +000010058
Dan Gohman7f321562007-06-25 16:23:39 +000010059 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +000010060 // not the number of vector elements, look through it. Be careful not to
10061 // look though conversions that change things like v4f32 to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010062 if (V->getOpcode() == ISD::BITCAST) {
Dan Gohman475871a2008-07-27 21:46:04 +000010063 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +000010064 if (ConvInput.getValueType().isVector() &&
10065 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +000010066 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +000010067 }
10068
Dan Gohman7f321562007-06-25 16:23:39 +000010069 if (V->getOpcode() == ISD::BUILD_VECTOR) {
Bob Wilson0f1db1a2010-10-28 17:06:14 +000010070 assert(V->getNumOperands() == NumElts &&
10071 "BUILD_VECTOR has wrong number of operands");
10072 SDValue Base;
10073 bool AllSame = true;
10074 for (unsigned i = 0; i != NumElts; ++i) {
10075 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
10076 Base = V->getOperand(i);
10077 break;
Evan Cheng917ec982006-07-21 08:25:53 +000010078 }
Evan Cheng917ec982006-07-21 08:25:53 +000010079 }
Bob Wilson0f1db1a2010-10-28 17:06:14 +000010080 // Splat of <u, u, u, u>, return <u, u, u, u>
10081 if (!Base.getNode())
10082 return N0;
10083 for (unsigned i = 0; i != NumElts; ++i) {
10084 if (V->getOperand(i) != Base) {
10085 AllSame = false;
10086 break;
10087 }
10088 }
10089 // Splat of <x, x, x, x>, return <x, x, x, x>
10090 if (AllSame)
10091 return N0;
Evan Cheng917ec982006-07-21 08:25:53 +000010092 }
10093 }
Nadav Rotem4ac90812012-04-01 19:31:22 +000010094
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +000010095 if (N0.getOpcode() == ISD::CONCAT_VECTORS &&
10096 Level < AfterLegalizeVectorOps &&
10097 (N1.getOpcode() == ISD::UNDEF ||
10098 (N1.getOpcode() == ISD::CONCAT_VECTORS &&
10099 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()))) {
10100 SDValue V = partitionShuffleOfConcats(N, DAG);
10101
10102 if (V.getNode())
10103 return V;
10104 }
10105
Nadav Rotem4ac90812012-04-01 19:31:22 +000010106 // If this shuffle node is simply a swizzle of another shuffle node,
Nadav Rotemd16c8d02012-04-07 21:19:08 +000010107 // and it reverses the swizzle of the previous shuffle then we can
10108 // optimize shuffle(shuffle(x, undef), undef) -> x.
Nadav Rotem4ac90812012-04-01 19:31:22 +000010109 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
10110 N1.getOpcode() == ISD::UNDEF) {
10111
Nadav Rotem4ac90812012-04-01 19:31:22 +000010112 ShuffleVectorSDNode *OtherSV = cast<ShuffleVectorSDNode>(N0);
10113
Nadav Rotemd16c8d02012-04-07 21:19:08 +000010114 // Shuffle nodes can only reverse shuffles with a single non-undef value.
10115 if (N0.getOperand(1).getOpcode() != ISD::UNDEF)
10116 return SDValue();
10117
Craig Topperae1bec52012-04-09 05:16:56 +000010118 // The incoming shuffle must be of the same type as the result of the
10119 // current shuffle.
10120 assert(OtherSV->getOperand(0).getValueType() == VT &&
10121 "Shuffle types don't match");
Nadav Rotem4ac90812012-04-01 19:31:22 +000010122
10123 for (unsigned i = 0; i != NumElts; ++i) {
10124 int Idx = SVN->getMaskElt(i);
Craig Topperae1bec52012-04-09 05:16:56 +000010125 assert(Idx < (int)NumElts && "Index references undef operand");
Nadav Rotem4ac90812012-04-01 19:31:22 +000010126 // Next, this index comes from the first value, which is the incoming
10127 // shuffle. Adopt the incoming index.
10128 if (Idx >= 0)
10129 Idx = OtherSV->getMaskElt(Idx);
10130
Nadav Rotemd16c8d02012-04-07 21:19:08 +000010131 // The combined shuffle must map each index to itself.
Craig Topperae1bec52012-04-09 05:16:56 +000010132 if (Idx >= 0 && (unsigned)Idx != i)
Nadav Rotemd16c8d02012-04-07 21:19:08 +000010133 return SDValue();
Nadav Rotem4ac90812012-04-01 19:31:22 +000010134 }
Nadav Rotemd16c8d02012-04-07 21:19:08 +000010135
10136 return OtherSV->getOperand(0);
Nadav Rotem4ac90812012-04-01 19:31:22 +000010137 }
10138
Dan Gohman475871a2008-07-27 21:46:04 +000010139 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +000010140}
10141
Evan Cheng44f1f092006-04-20 08:56:16 +000010142/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +000010143/// an AND to a vector_shuffle with the destination vector and a zero vector.
10144/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +000010145/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +000010146SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +000010147 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +000010148 SDLoc dl(N);
Dan Gohman475871a2008-07-27 21:46:04 +000010149 SDValue LHS = N->getOperand(0);
10150 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +000010151 if (N->getOpcode() == ISD::AND) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010152 if (RHS.getOpcode() == ISD::BITCAST)
Evan Cheng44f1f092006-04-20 08:56:16 +000010153 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +000010154 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Nate Begeman9008ca62009-04-27 18:41:29 +000010155 SmallVector<int, 8> Indices;
10156 unsigned NumElts = RHS.getNumOperands();
Evan Cheng44f1f092006-04-20 08:56:16 +000010157 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +000010158 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +000010159 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +000010160 return SDValue();
Craig Topperb7135e52012-04-09 05:59:53 +000010161
10162 if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Nate Begeman9008ca62009-04-27 18:41:29 +000010163 Indices.push_back(i);
Evan Cheng44f1f092006-04-20 08:56:16 +000010164 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Nate Begeman9008ca62009-04-27 18:41:29 +000010165 Indices.push_back(NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +000010166 else
Dan Gohman475871a2008-07-27 21:46:04 +000010167 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +000010168 }
10169
10170 // Let's see if the target supports this vector_shuffle.
Owen Andersone50ed302009-08-10 22:56:29 +000010171 EVT RVT = RHS.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +000010172 if (!TLI.isVectorClearMaskLegal(Indices, RVT))
Dan Gohman475871a2008-07-27 21:46:04 +000010173 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +000010174
Dan Gohman7f321562007-06-25 16:23:39 +000010175 // Return the new VECTOR_SHUFFLE node.
Dan Gohman8a55ce42009-09-23 21:02:20 +000010176 EVT EltVT = RVT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +000010177 SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(),
Dan Gohman8a55ce42009-09-23 21:02:20 +000010178 DAG.getConstant(0, EltVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +000010179 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Nate Begeman9008ca62009-04-27 18:41:29 +000010180 RVT, &ZeroOps[0], ZeroOps.size());
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010181 LHS = DAG.getNode(ISD::BITCAST, dl, RVT, LHS);
Nate Begeman9008ca62009-04-27 18:41:29 +000010182 SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010183 return DAG.getNode(ISD::BITCAST, dl, VT, Shuf);
Evan Cheng44f1f092006-04-20 08:56:16 +000010184 }
10185 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010186
Dan Gohman475871a2008-07-27 21:46:04 +000010187 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +000010188}
10189
Dan Gohman7f321562007-06-25 16:23:39 +000010190/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +000010191SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Bob Wilsond7273432010-12-17 23:06:49 +000010192 assert(N->getValueType(0).isVector() &&
10193 "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +000010194
Dan Gohman475871a2008-07-27 21:46:04 +000010195 SDValue LHS = N->getOperand(0);
10196 SDValue RHS = N->getOperand(1);
10197 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +000010198 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +000010199
Dan Gohman7f321562007-06-25 16:23:39 +000010200 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +000010201 // this operation.
Scott Michelfdc40a02009-02-17 22:15:04 +000010202 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
Dan Gohman7f321562007-06-25 16:23:39 +000010203 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +000010204 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +000010205 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +000010206 SDValue LHSOp = LHS.getOperand(i);
10207 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +000010208 // If these two elements can't be folded, bail out.
10209 if ((LHSOp.getOpcode() != ISD::UNDEF &&
10210 LHSOp.getOpcode() != ISD::Constant &&
10211 LHSOp.getOpcode() != ISD::ConstantFP) ||
10212 (RHSOp.getOpcode() != ISD::UNDEF &&
10213 RHSOp.getOpcode() != ISD::Constant &&
10214 RHSOp.getOpcode() != ISD::ConstantFP))
10215 break;
Bill Wendling836ca7d2009-01-30 23:59:18 +000010216
Evan Cheng7b336a82006-05-31 06:08:35 +000010217 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +000010218 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
10219 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +000010220 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +000010221 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +000010222 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +000010223 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +000010224 break;
10225 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010226
Bob Wilsond7273432010-12-17 23:06:49 +000010227 EVT VT = LHSOp.getValueType();
Bob Wilsondb2b18f2011-10-18 17:34:47 +000010228 EVT RVT = RHSOp.getValueType();
10229 if (RVT != VT) {
10230 // Integer BUILD_VECTOR operands may have types larger than the element
10231 // size (e.g., when the element type is not legal). Prior to type
10232 // legalization, the types may not match between the two BUILD_VECTORS.
10233 // Truncate one of the operands to make them match.
10234 if (RVT.getSizeInBits() > VT.getSizeInBits()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010235 RHSOp = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, RHSOp);
Bob Wilsondb2b18f2011-10-18 17:34:47 +000010236 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010237 LHSOp = DAG.getNode(ISD::TRUNCATE, SDLoc(N), RVT, LHSOp);
Bob Wilsondb2b18f2011-10-18 17:34:47 +000010238 VT = RVT;
10239 }
10240 }
Andrew Trickac6d9be2013-05-25 02:42:55 +000010241 SDValue FoldOp = DAG.getNode(N->getOpcode(), SDLoc(LHS), VT,
Evan Chenga0839882010-05-18 00:03:40 +000010242 LHSOp, RHSOp);
10243 if (FoldOp.getOpcode() != ISD::UNDEF &&
10244 FoldOp.getOpcode() != ISD::Constant &&
10245 FoldOp.getOpcode() != ISD::ConstantFP)
10246 break;
10247 Ops.push_back(FoldOp);
10248 AddToWorkList(FoldOp.getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +000010249 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010250
Bob Wilsond7273432010-12-17 23:06:49 +000010251 if (Ops.size() == LHS.getNumOperands())
Andrew Trickac6d9be2013-05-25 02:42:55 +000010252 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Bob Wilsond7273432010-12-17 23:06:49 +000010253 LHS.getValueType(), &Ops[0], Ops.size());
Chris Lattneredab1b92006-04-02 03:25:57 +000010254 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010255
Dan Gohman475871a2008-07-27 21:46:04 +000010256 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +000010257}
10258
Craig Topperdd201ff2012-09-11 01:45:21 +000010259/// SimplifyVUnaryOp - Visit a binary vector operation, like FABS/FNEG.
10260SDValue DAGCombiner::SimplifyVUnaryOp(SDNode *N) {
Craig Topperdd201ff2012-09-11 01:45:21 +000010261 assert(N->getValueType(0).isVector() &&
10262 "SimplifyVUnaryOp only works on vectors!");
10263
10264 SDValue N0 = N->getOperand(0);
10265
10266 if (N0.getOpcode() != ISD::BUILD_VECTOR)
10267 return SDValue();
10268
10269 // Operand is a BUILD_VECTOR node, see if we can constant fold it.
10270 SmallVector<SDValue, 8> Ops;
10271 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) {
10272 SDValue Op = N0.getOperand(i);
10273 if (Op.getOpcode() != ISD::UNDEF &&
10274 Op.getOpcode() != ISD::ConstantFP)
10275 break;
10276 EVT EltVT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +000010277 SDValue FoldOp = DAG.getNode(N->getOpcode(), SDLoc(N0), EltVT, Op);
Craig Topperdd201ff2012-09-11 01:45:21 +000010278 if (FoldOp.getOpcode() != ISD::UNDEF &&
10279 FoldOp.getOpcode() != ISD::ConstantFP)
10280 break;
10281 Ops.push_back(FoldOp);
10282 AddToWorkList(FoldOp.getNode());
10283 }
10284
10285 if (Ops.size() != N0.getNumOperands())
10286 return SDValue();
10287
Andrew Trickac6d9be2013-05-25 02:42:55 +000010288 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Craig Topperdd201ff2012-09-11 01:45:21 +000010289 N0.getValueType(), &Ops[0], Ops.size());
10290}
10291
Andrew Trickac6d9be2013-05-25 02:42:55 +000010292SDValue DAGCombiner::SimplifySelect(SDLoc DL, SDValue N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +000010293 SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +000010294 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
Scott Michelfdc40a02009-02-17 22:15:04 +000010295
Bill Wendling836ca7d2009-01-30 23:59:18 +000010296 SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +000010297 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Bill Wendling836ca7d2009-01-30 23:59:18 +000010298
Nate Begemanf845b452005-10-08 00:29:44 +000010299 // If we got a simplified select_cc node back from SimplifySelectCC, then
10300 // break it down into a new SETCC node, and a new SELECT node, and then return
10301 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +000010302 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +000010303 // Check to see if we got a select_cc back (to turn into setcc/select).
10304 // Otherwise, just return whatever node we got back, like fabs.
10305 if (SCC.getOpcode() == ISD::SELECT_CC) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010306 SDValue SETCC = DAG.getNode(ISD::SETCC, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +000010307 N0.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +000010308 SCC.getOperand(0), SCC.getOperand(1),
Bill Wendling836ca7d2009-01-30 23:59:18 +000010309 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +000010310 AddToWorkList(SETCC.getNode());
Matt Arsenaultb05e4772013-06-14 22:04:37 +000010311 return DAG.getSelect(SDLoc(SCC), SCC.getValueType(),
10312 SCC.getOperand(2), SCC.getOperand(3), SETCC);
Nate Begemanf845b452005-10-08 00:29:44 +000010313 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010314
Nate Begemanf845b452005-10-08 00:29:44 +000010315 return SCC;
10316 }
Dan Gohman475871a2008-07-27 21:46:04 +000010317 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +000010318}
10319
Chris Lattner40c62d52005-10-18 06:04:22 +000010320/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
10321/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +000010322/// select. Callers of this should assume that TheSelect is deleted if this
10323/// returns true. As such, they should return the appropriate thing (e.g. the
10324/// node) back to the top-level of the DAG combiner loop to avoid it being
10325/// looked at.
Scott Michelfdc40a02009-02-17 22:15:04 +000010326bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
Dan Gohman475871a2008-07-27 21:46:04 +000010327 SDValue RHS) {
Scott Michelfdc40a02009-02-17 22:15:04 +000010328
Nadav Rotemf94fdb62011-02-11 19:57:47 +000010329 // Cannot simplify select with vector condition
10330 if (TheSelect->getOperand(0).getValueType().isVector()) return false;
10331
Chris Lattner40c62d52005-10-18 06:04:22 +000010332 // If this is a select from two identical things, try to pull the operation
10333 // through the select.
Chris Lattner18061612010-09-21 15:46:59 +000010334 if (LHS.getOpcode() != RHS.getOpcode() ||
10335 !LHS.hasOneUse() || !RHS.hasOneUse())
10336 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010337
Chris Lattner18061612010-09-21 15:46:59 +000010338 // If this is a load and the token chain is identical, replace the select
10339 // of two loads with a load through a select of the address to load from.
10340 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
10341 // constants have been dropped into the constant pool.
10342 if (LHS.getOpcode() == ISD::LOAD) {
10343 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
10344 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010345
Chris Lattner18061612010-09-21 15:46:59 +000010346 // Token chains must be identical.
10347 if (LHS.getOperand(0) != RHS.getOperand(0) ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +000010348 // Do not let this transformation reduce the number of volatile loads.
Chris Lattner18061612010-09-21 15:46:59 +000010349 LLD->isVolatile() || RLD->isVolatile() ||
10350 // If this is an EXTLOAD, the VT's must match.
10351 LLD->getMemoryVT() != RLD->getMemoryVT() ||
Duncan Sandsdcfd3a72010-11-18 20:05:18 +000010352 // If this is an EXTLOAD, the kind of extension must match.
10353 (LLD->getExtensionType() != RLD->getExtensionType() &&
10354 // The only exception is if one of the extensions is anyext.
10355 LLD->getExtensionType() != ISD::EXTLOAD &&
10356 RLD->getExtensionType() != ISD::EXTLOAD) ||
Dan Gohman75832d72009-10-31 14:14:04 +000010357 // FIXME: this discards src value information. This is
10358 // over-conservative. It would be beneficial to be able to remember
Mon P Wangfe240b12010-01-11 20:12:49 +000010359 // both potential memory locations. Since we are discarding
10360 // src value info, don't do the transformation if the memory
10361 // locations are not in the default address space.
Chris Lattner18061612010-09-21 15:46:59 +000010362 LLD->getPointerInfo().getAddrSpace() != 0 ||
Pete Cooperb0fde6d2013-02-12 03:14:50 +000010363 RLD->getPointerInfo().getAddrSpace() != 0 ||
10364 !TLI.isOperationLegalOrCustom(TheSelect->getOpcode(),
10365 LLD->getBasePtr().getValueType()))
Chris Lattner18061612010-09-21 15:46:59 +000010366 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010367
Chris Lattnerf1658062010-09-21 15:58:55 +000010368 // Check that the select condition doesn't reach either load. If so,
10369 // folding this will induce a cycle into the DAG. If not, this is safe to
10370 // xform, so create a select of the addresses.
Chris Lattner18061612010-09-21 15:46:59 +000010371 SDValue Addr;
10372 if (TheSelect->getOpcode() == ISD::SELECT) {
Chris Lattnerf1658062010-09-21 15:58:55 +000010373 SDNode *CondNode = TheSelect->getOperand(0).getNode();
10374 if ((LLD->hasAnyUseOfValue(1) && LLD->isPredecessorOf(CondNode)) ||
10375 (RLD->hasAnyUseOfValue(1) && RLD->isPredecessorOf(CondNode)))
10376 return false;
Nadav Rotem1c5bf3f2012-10-18 18:06:48 +000010377 // The loads must not depend on one another.
10378 if (LLD->isPredecessorOf(RLD) ||
10379 RLD->isPredecessorOf(LLD))
10380 return false;
Matt Arsenaultb05e4772013-06-14 22:04:37 +000010381 Addr = DAG.getSelect(SDLoc(TheSelect),
10382 LLD->getBasePtr().getValueType(),
10383 TheSelect->getOperand(0), LLD->getBasePtr(),
10384 RLD->getBasePtr());
Chris Lattner18061612010-09-21 15:46:59 +000010385 } else { // Otherwise SELECT_CC
Chris Lattnerf1658062010-09-21 15:58:55 +000010386 SDNode *CondLHS = TheSelect->getOperand(0).getNode();
10387 SDNode *CondRHS = TheSelect->getOperand(1).getNode();
10388
10389 if ((LLD->hasAnyUseOfValue(1) &&
10390 (LLD->isPredecessorOf(CondLHS) || LLD->isPredecessorOf(CondRHS))) ||
Chris Lattner77d95212012-03-27 16:27:21 +000010391 (RLD->hasAnyUseOfValue(1) &&
10392 (RLD->isPredecessorOf(CondLHS) || RLD->isPredecessorOf(CondRHS))))
Chris Lattnerf1658062010-09-21 15:58:55 +000010393 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010394
Andrew Trickac6d9be2013-05-25 02:42:55 +000010395 Addr = DAG.getNode(ISD::SELECT_CC, SDLoc(TheSelect),
Chris Lattnerf1658062010-09-21 15:58:55 +000010396 LLD->getBasePtr().getValueType(),
10397 TheSelect->getOperand(0),
10398 TheSelect->getOperand(1),
10399 LLD->getBasePtr(), RLD->getBasePtr(),
10400 TheSelect->getOperand(4));
Chris Lattner18061612010-09-21 15:46:59 +000010401 }
10402
Chris Lattnerf1658062010-09-21 15:58:55 +000010403 SDValue Load;
10404 if (LLD->getExtensionType() == ISD::NON_EXTLOAD) {
10405 Load = DAG.getLoad(TheSelect->getValueType(0),
Andrew Trickac6d9be2013-05-25 02:42:55 +000010406 SDLoc(TheSelect),
Chris Lattnerf1658062010-09-21 15:58:55 +000010407 // FIXME: Discards pointer info.
10408 LLD->getChain(), Addr, MachinePointerInfo(),
10409 LLD->isVolatile(), LLD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010410 LLD->isInvariant(), LLD->getAlignment());
Chris Lattnerf1658062010-09-21 15:58:55 +000010411 } else {
Duncan Sandsb9064bb2010-11-18 21:16:28 +000010412 Load = DAG.getExtLoad(LLD->getExtensionType() == ISD::EXTLOAD ?
10413 RLD->getExtensionType() : LLD->getExtensionType(),
Andrew Trickac6d9be2013-05-25 02:42:55 +000010414 SDLoc(TheSelect),
Stuart Hastingsa9011292011-02-16 16:23:55 +000010415 TheSelect->getValueType(0),
Chris Lattnerf1658062010-09-21 15:58:55 +000010416 // FIXME: Discards pointer info.
10417 LLD->getChain(), Addr, MachinePointerInfo(),
10418 LLD->getMemoryVT(), LLD->isVolatile(),
10419 LLD->isNonTemporal(), LLD->getAlignment());
Chris Lattner40c62d52005-10-18 06:04:22 +000010420 }
Chris Lattnerf1658062010-09-21 15:58:55 +000010421
10422 // Users of the select now use the result of the load.
10423 CombineTo(TheSelect, Load);
10424
10425 // Users of the old loads now use the new load's chain. We know the
10426 // old-load value is dead now.
10427 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
10428 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
10429 return true;
Chris Lattner40c62d52005-10-18 06:04:22 +000010430 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010431
Chris Lattner40c62d52005-10-18 06:04:22 +000010432 return false;
10433}
10434
Chris Lattner600fec32009-03-11 05:08:08 +000010435/// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3
10436/// where 'cond' is the comparison specified by CC.
Andrew Trickac6d9be2013-05-25 02:42:55 +000010437SDValue DAGCombiner::SimplifySelectCC(SDLoc DL, SDValue N0, SDValue N1,
Dan Gohman475871a2008-07-27 21:46:04 +000010438 SDValue N2, SDValue N3,
10439 ISD::CondCode CC, bool NotExtCompare) {
Chris Lattner600fec32009-03-11 05:08:08 +000010440 // (x ? y : y) -> y.
10441 if (N2 == N3) return N2;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010442
Owen Andersone50ed302009-08-10 22:56:29 +000010443 EVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +000010444 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
10445 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
10446 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +000010447
10448 // Determine if the condition we're dealing with is constant
Matt Arsenault225ed702013-05-18 00:21:46 +000010449 SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +000010450 N0, N1, CC, DL, false);
Gabor Greifba36cb52008-08-28 21:40:38 +000010451 if (SCC.getNode()) AddToWorkList(SCC.getNode());
10452 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +000010453
10454 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +000010455 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +000010456 return N2;
10457 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +000010458 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +000010459 return N3;
Scott Michelfdc40a02009-02-17 22:15:04 +000010460
Nate Begemanf845b452005-10-08 00:29:44 +000010461 // Check to see if we can simplify the select into an fabs node
10462 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
10463 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +000010464 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +000010465 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
10466 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
10467 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
10468 N2 == N3.getOperand(0))
Bill Wendling836ca7d2009-01-30 23:59:18 +000010469 return DAG.getNode(ISD::FABS, DL, VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +000010470
Nate Begemanf845b452005-10-08 00:29:44 +000010471 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
10472 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
10473 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
10474 N2.getOperand(0) == N3)
Bill Wendling836ca7d2009-01-30 23:59:18 +000010475 return DAG.getNode(ISD::FABS, DL, VT, N3);
Nate Begemanf845b452005-10-08 00:29:44 +000010476 }
10477 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010478
Chris Lattner600fec32009-03-11 05:08:08 +000010479 // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)"
10480 // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0
10481 // in it. This is a win when the constant is not otherwise available because
10482 // it replaces two constant pool loads with one. We only do this if the FP
10483 // type is known to be legal, because if it isn't, then we are before legalize
10484 // types an we want the other legalization to happen first (e.g. to avoid
Mon P Wang0b7a7862009-03-14 00:25:19 +000010485 // messing with soft float) and if the ConstantFP is not legal, because if
10486 // it is legal, we may not need to store the FP constant in a constant pool.
Chris Lattner600fec32009-03-11 05:08:08 +000010487 if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2))
10488 if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) {
10489 if (TLI.isTypeLegal(N2.getValueType()) &&
Mon P Wang0b7a7862009-03-14 00:25:19 +000010490 (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) !=
10491 TargetLowering::Legal) &&
Chris Lattner600fec32009-03-11 05:08:08 +000010492 // If both constants have multiple uses, then we won't need to do an
10493 // extra load, they are likely around in registers for other users.
10494 (TV->hasOneUse() || FV->hasOneUse())) {
10495 Constant *Elts[] = {
10496 const_cast<ConstantFP*>(FV->getConstantFPValue()),
10497 const_cast<ConstantFP*>(TV->getConstantFPValue())
10498 };
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010499 Type *FPTy = Elts[0]->getType();
Micah Villmow3574eca2012-10-08 16:38:25 +000010500 const DataLayout &TD = *TLI.getDataLayout();
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010501
Chris Lattner600fec32009-03-11 05:08:08 +000010502 // Create a ConstantArray of the two constants.
Jay Foad26701082011-06-22 09:24:39 +000010503 Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts);
Chris Lattner600fec32009-03-11 05:08:08 +000010504 SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(),
10505 TD.getPrefTypeAlignment(FPTy));
Evan Cheng1606e8e2009-03-13 07:51:59 +000010506 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner600fec32009-03-11 05:08:08 +000010507
10508 // Get the offsets to the 0 and 1 element of the array so that we can
10509 // select between them.
10510 SDValue Zero = DAG.getIntPtrConstant(0);
Duncan Sands777d2302009-05-09 07:06:46 +000010511 unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType());
Chris Lattner600fec32009-03-11 05:08:08 +000010512 SDValue One = DAG.getIntPtrConstant(EltSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010513
Chris Lattner600fec32009-03-11 05:08:08 +000010514 SDValue Cond = DAG.getSetCC(DL,
Matt Arsenault225ed702013-05-18 00:21:46 +000010515 getSetCCResultType(N0.getValueType()),
Chris Lattner600fec32009-03-11 05:08:08 +000010516 N0, N1, CC);
Dan Gohman7b316c92011-09-22 23:01:29 +000010517 AddToWorkList(Cond.getNode());
Matt Arsenaultb05e4772013-06-14 22:04:37 +000010518 SDValue CstOffset = DAG.getSelect(DL, Zero.getValueType(),
10519 Cond, One, Zero);
Dan Gohman7b316c92011-09-22 23:01:29 +000010520 AddToWorkList(CstOffset.getNode());
Tom Stellardedd08f72013-08-26 15:06:10 +000010521 CPIdx = DAG.getNode(ISD::ADD, DL, CPIdx.getValueType(), CPIdx,
Chris Lattner600fec32009-03-11 05:08:08 +000010522 CstOffset);
Dan Gohman7b316c92011-09-22 23:01:29 +000010523 AddToWorkList(CPIdx.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +000010524 return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +000010525 MachinePointerInfo::getConstantPool(), false,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010526 false, false, Alignment);
Chris Lattner600fec32009-03-11 05:08:08 +000010527
10528 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010529 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010530
Nate Begemanf845b452005-10-08 00:29:44 +000010531 // Check to see if we can perform the "gzip trick", transforming
Bill Wendling836ca7d2009-01-30 23:59:18 +000010532 // (select_cc setlt X, 0, A, 0) -> (and (sra X, (sub size(X), 1), A)
Chris Lattnere3152e52006-09-20 06:41:35 +000010533 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +000010534 (N1C->isNullValue() || // (a < 0) ? b : 0
10535 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Owen Andersone50ed302009-08-10 22:56:29 +000010536 EVT XType = N0.getValueType();
10537 EVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +000010538 if (XType.bitsGE(AType)) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +000010539 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +000010540 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +000010541 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
10542 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +000010543 ShCtV = XType.getSizeInBits()-ShCtV-1;
Owen Anderson95771af2011-02-25 21:41:48 +000010544 SDValue ShCt = DAG.getConstant(ShCtV,
10545 getShiftAmountTy(N0.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +000010546 SDValue Shift = DAG.getNode(ISD::SRL, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +000010547 XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +000010548 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +000010549
Duncan Sands8e4eb092008-06-08 20:54:56 +000010550 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +000010551 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +000010552 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +000010553 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010554
10555 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +000010556 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010557
Andrew Trickac6d9be2013-05-25 02:42:55 +000010558 SDValue Shift = DAG.getNode(ISD::SRA, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +000010559 XType, N0,
10560 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +000010561 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +000010562 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +000010563
Duncan Sands8e4eb092008-06-08 20:54:56 +000010564 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +000010565 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +000010566 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +000010567 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010568
10569 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +000010570 }
10571 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010572
Owen Andersoned1088a2010-09-22 22:58:22 +000010573 // fold (select_cc seteq (and x, y), 0, 0, A) -> (and (shr (shl x)) A)
10574 // where y is has a single bit set.
10575 // A plaintext description would be, we can turn the SELECT_CC into an AND
10576 // when the condition can be materialized as an all-ones register. Any
10577 // single bit-test can be materialized as an all-ones register with
10578 // shift-left and shift-right-arith.
10579 if (CC == ISD::SETEQ && N0->getOpcode() == ISD::AND &&
10580 N0->getValueType(0) == VT &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010581 N1C && N1C->isNullValue() &&
Owen Andersoned1088a2010-09-22 22:58:22 +000010582 N2C && N2C->isNullValue()) {
10583 SDValue AndLHS = N0->getOperand(0);
10584 ConstantSDNode *ConstAndRHS = dyn_cast<ConstantSDNode>(N0->getOperand(1));
10585 if (ConstAndRHS && ConstAndRHS->getAPIntValue().countPopulation() == 1) {
10586 // Shift the tested bit over the sign bit.
10587 APInt AndMask = ConstAndRHS->getAPIntValue();
10588 SDValue ShlAmt =
Owen Anderson95771af2011-02-25 21:41:48 +000010589 DAG.getConstant(AndMask.countLeadingZeros(),
10590 getShiftAmountTy(AndLHS.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +000010591 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(N0), VT, AndLHS, ShlAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010592
Owen Andersoned1088a2010-09-22 22:58:22 +000010593 // Now arithmetic right shift it all the way over, so the result is either
10594 // all-ones, or zero.
10595 SDValue ShrAmt =
Owen Anderson95771af2011-02-25 21:41:48 +000010596 DAG.getConstant(AndMask.getBitWidth()-1,
10597 getShiftAmountTy(Shl.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +000010598 SDValue Shr = DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl, ShrAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010599
Owen Andersoned1088a2010-09-22 22:58:22 +000010600 return DAG.getNode(ISD::AND, DL, VT, Shr, N3);
10601 }
10602 }
10603
Nate Begeman07ed4172005-10-10 21:26:48 +000010604 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +000010605 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands28b77e92011-09-06 19:07:46 +000010606 TLI.getBooleanContents(N0.getValueType().isVector()) ==
10607 TargetLowering::ZeroOrOneBooleanContent) {
Scott Michelfdc40a02009-02-17 22:15:04 +000010608
Chris Lattner1eba01e2007-04-11 06:50:51 +000010609 // If the caller doesn't want us to simplify this into a zext of a compare,
10610 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +000010611 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +000010612 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000010613
Nate Begeman07ed4172005-10-10 21:26:48 +000010614 // Get a SetCC of the condition
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010615 // NOTE: Don't create a SETCC if it's not legal on this target.
10616 if (!LegalOperations ||
10617 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault225ed702013-05-18 00:21:46 +000010618 LegalTypes ? getSetCCResultType(N0.getValueType()) : MVT::i1)) {
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010619 SDValue Temp, SCC;
10620 // cast from setcc result type to select result type
10621 if (LegalTypes) {
Matt Arsenault225ed702013-05-18 00:21:46 +000010622 SCC = DAG.getSetCC(DL, getSetCCResultType(N0.getValueType()),
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010623 N0, N1, CC);
10624 if (N2.getValueType().bitsLT(SCC.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +000010625 Temp = DAG.getZeroExtendInReg(SCC, SDLoc(N2),
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010626 N2.getValueType());
10627 else
Andrew Trickac6d9be2013-05-25 02:42:55 +000010628 Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2),
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010629 N2.getValueType(), SCC);
10630 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010631 SCC = DAG.getSetCC(SDLoc(N0), MVT::i1, N0, N1, CC);
10632 Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2),
Bill Wendling836ca7d2009-01-30 23:59:18 +000010633 N2.getValueType(), SCC);
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010634 }
10635
10636 AddToWorkList(SCC.getNode());
10637 AddToWorkList(Temp.getNode());
10638
10639 if (N2C->getAPIntValue() == 1)
10640 return Temp;
10641
10642 // shl setcc result by log2 n2c
10643 return DAG.getNode(ISD::SHL, DL, N2.getValueType(), Temp,
10644 DAG.getConstant(N2C->getAPIntValue().logBase2(),
10645 getShiftAmountTy(Temp.getValueType())));
Nate Begemanb0d04a72006-02-18 02:40:58 +000010646 }
Nate Begeman07ed4172005-10-10 21:26:48 +000010647 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010648
Nate Begemanf845b452005-10-08 00:29:44 +000010649 // Check to see if this is the equivalent of setcc
10650 // FIXME: Turn all of these into setcc if setcc if setcc is legal
10651 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +000010652 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Owen Andersone50ed302009-08-10 22:56:29 +000010653 EVT XType = N0.getValueType();
Duncan Sands25cf2272008-11-24 14:53:14 +000010654 if (!LegalOperations ||
Matt Arsenault225ed702013-05-18 00:21:46 +000010655 TLI.isOperationLegal(ISD::SETCC, getSetCCResultType(XType))) {
10656 SDValue Res = DAG.getSetCC(DL, getSetCCResultType(XType), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +000010657 if (Res.getValueType() != VT)
Bill Wendling836ca7d2009-01-30 23:59:18 +000010658 Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res);
Nate Begemanf845b452005-10-08 00:29:44 +000010659 return Res;
10660 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010661
Bill Wendling836ca7d2009-01-30 23:59:18 +000010662 // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X))))
Scott Michelfdc40a02009-02-17 22:15:04 +000010663 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands25cf2272008-11-24 14:53:14 +000010664 (!LegalOperations ||
Duncan Sands184a8762008-06-14 17:48:34 +000010665 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010666 SDValue Ctlz = DAG.getNode(ISD::CTLZ, SDLoc(N0), XType, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +000010667 return DAG.getNode(ISD::SRL, DL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +000010668 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Owen Anderson95771af2011-02-25 21:41:48 +000010669 getShiftAmountTy(Ctlz.getValueType())));
Nate Begemanf845b452005-10-08 00:29:44 +000010670 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010671 // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1))
Scott Michelfdc40a02009-02-17 22:15:04 +000010672 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010673 SDValue NegN0 = DAG.getNode(ISD::SUB, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +000010674 XType, DAG.getConstant(0, XType), N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +000010675 SDValue NotN0 = DAG.getNOT(SDLoc(N0), N0, XType);
Bill Wendling836ca7d2009-01-30 23:59:18 +000010676 return DAG.getNode(ISD::SRL, DL, XType,
Bill Wendlingfc4b6772009-02-01 11:19:36 +000010677 DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +000010678 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +000010679 getShiftAmountTy(XType)));
Nate Begemanf845b452005-10-08 00:29:44 +000010680 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010681 // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1))
Nate Begemanf845b452005-10-08 00:29:44 +000010682 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010683 SDValue Sign = DAG.getNode(ISD::SRL, SDLoc(N0), XType, N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +000010684 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +000010685 getShiftAmountTy(N0.getValueType())));
Bill Wendling836ca7d2009-01-30 23:59:18 +000010686 return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType));
Nate Begemanf845b452005-10-08 00:29:44 +000010687 }
10688 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010689
Benjamin Kramercde51102010-07-08 12:09:56 +000010690 // Check to see if this is an integer abs.
10691 // select_cc setg[te] X, 0, X, -X ->
10692 // select_cc setgt X, -1, X, -X ->
10693 // select_cc setl[te] X, 0, -X, X ->
10694 // select_cc setlt X, 1, -X, X ->
Nate Begemanf845b452005-10-08 00:29:44 +000010695 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
Benjamin Kramercde51102010-07-08 12:09:56 +000010696 if (N1C) {
10697 ConstantSDNode *SubC = NULL;
10698 if (((N1C->isNullValue() && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
10699 (N1C->isAllOnesValue() && CC == ISD::SETGT)) &&
10700 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1))
10701 SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0));
10702 else if (((N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE)) ||
10703 (N1C->isOne() && CC == ISD::SETLT)) &&
10704 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1))
10705 SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0));
10706
Owen Andersone50ed302009-08-10 22:56:29 +000010707 EVT XType = N0.getValueType();
Benjamin Kramercde51102010-07-08 12:09:56 +000010708 if (SubC && SubC->isNullValue() && XType.isInteger()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010709 SDValue Shift = DAG.getNode(ISD::SRA, SDLoc(N0), XType,
Benjamin Kramercde51102010-07-08 12:09:56 +000010710 N0,
10711 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +000010712 getShiftAmountTy(N0.getValueType())));
Andrew Trickac6d9be2013-05-25 02:42:55 +000010713 SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N0),
Benjamin Kramercde51102010-07-08 12:09:56 +000010714 XType, N0, Shift);
10715 AddToWorkList(Shift.getNode());
10716 AddToWorkList(Add.getNode());
10717 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Nate Begemanf845b452005-10-08 00:29:44 +000010718 }
10719 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010720
Dan Gohman475871a2008-07-27 21:46:04 +000010721 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +000010722}
10723
Evan Chengfa1eb272007-02-08 22:13:59 +000010724/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Owen Andersone50ed302009-08-10 22:56:29 +000010725SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0,
Dan Gohman475871a2008-07-27 21:46:04 +000010726 SDValue N1, ISD::CondCode Cond,
Andrew Trickac6d9be2013-05-25 02:42:55 +000010727 SDLoc DL, bool foldBooleans) {
Scott Michelfdc40a02009-02-17 22:15:04 +000010728 TargetLowering::DAGCombinerInfo
Nadav Rotem444b4bf2012-12-27 06:47:41 +000010729 DagCombineInfo(DAG, Level, false, this);
Dale Johannesenff97d4f2009-02-03 00:47:48 +000010730 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL);
Nate Begeman452d7be2005-09-16 00:54:12 +000010731}
10732
Nate Begeman69575232005-10-20 02:15:44 +000010733/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
10734/// return a DAG expression to select that will generate the same value by
10735/// multiplying by a magic number. See:
10736/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +000010737SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +000010738 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +000010739 SDValue S = TLI.BuildSDIV(N, DAG, LegalOperations, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010740
Andrew Lenharth232c9102006-06-12 16:07:18 +000010741 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010742 ii != ee; ++ii)
10743 AddToWorkList(*ii);
10744 return S;
Nate Begeman69575232005-10-20 02:15:44 +000010745}
10746
10747/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
10748/// return a DAG expression to select that will generate the same value by
10749/// multiplying by a magic number. See:
10750/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +000010751SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +000010752 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +000010753 SDValue S = TLI.BuildUDIV(N, DAG, LegalOperations, &Built);
Nate Begeman69575232005-10-20 02:15:44 +000010754
Andrew Lenharth232c9102006-06-12 16:07:18 +000010755 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010756 ii != ee; ++ii)
10757 AddToWorkList(*ii);
10758 return S;
Nate Begeman69575232005-10-20 02:15:44 +000010759}
10760
Nate Begemancc66cdd2009-09-25 06:05:26 +000010761/// FindBaseOffset - Return true if base is a frame index, which is known not
Eric Christopher503a64d2010-12-09 04:48:06 +000010762// to alias with anything but itself. Provides base object and offset as
10763// results.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010764static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset,
Roman Divacky2943e372012-09-05 22:15:49 +000010765 const GlobalValue *&GV, const void *&CV) {
Jim Laskey71382342006-10-07 23:37:56 +000010766 // Assume it is a primitive operation.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010767 Base = Ptr; Offset = 0; GV = 0; CV = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000010768
Jim Laskey71382342006-10-07 23:37:56 +000010769 // If it's an adding a simple constant then integrate the offset.
10770 if (Base.getOpcode() == ISD::ADD) {
10771 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
10772 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010773 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +000010774 }
10775 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010776
Nate Begemancc66cdd2009-09-25 06:05:26 +000010777 // Return the underlying GlobalValue, and update the Offset. Return false
10778 // for GlobalAddressSDNode since the same GlobalAddress may be represented
10779 // by multiple nodes with different offsets.
10780 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Base)) {
10781 GV = G->getGlobal();
10782 Offset += G->getOffset();
10783 return false;
10784 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010785
Nate Begemancc66cdd2009-09-25 06:05:26 +000010786 // Return the underlying Constant value, and update the Offset. Return false
10787 // for ConstantSDNodes since the same constant pool entry may be represented
10788 // by multiple nodes with different offsets.
10789 if (ConstantPoolSDNode *C = dyn_cast<ConstantPoolSDNode>(Base)) {
Roman Divacky2943e372012-09-05 22:15:49 +000010790 CV = C->isMachineConstantPoolEntry() ? (const void *)C->getMachineCPVal()
10791 : (const void *)C->getConstVal();
Nate Begemancc66cdd2009-09-25 06:05:26 +000010792 Offset += C->getOffset();
10793 return false;
10794 }
Jim Laskey71382342006-10-07 23:37:56 +000010795 // If it's any of the following then it can't alias with anything but itself.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010796 return isa<FrameIndexSDNode>(Base);
Jim Laskey71382342006-10-07 23:37:56 +000010797}
10798
10799/// isAlias - Return true if there is any possibility that the two addresses
10800/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +000010801bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +000010802 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010803 unsigned SrcValueAlign1,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010804 const MDNode *TBAAInfo1,
Dan Gohman475871a2008-07-27 21:46:04 +000010805 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010806 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010807 unsigned SrcValueAlign2,
10808 const MDNode *TBAAInfo2) const {
Jim Laskey71382342006-10-07 23:37:56 +000010809 // If they are the same then they must be aliases.
10810 if (Ptr1 == Ptr2) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +000010811
Jim Laskey71382342006-10-07 23:37:56 +000010812 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +000010813 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +000010814 int64_t Offset1, Offset2;
Dan Gohman46510a72010-04-15 01:51:59 +000010815 const GlobalValue *GV1, *GV2;
Roman Divacky2943e372012-09-05 22:15:49 +000010816 const void *CV1, *CV2;
Nate Begemancc66cdd2009-09-25 06:05:26 +000010817 bool isFrameIndex1 = FindBaseOffset(Ptr1, Base1, Offset1, GV1, CV1);
10818 bool isFrameIndex2 = FindBaseOffset(Ptr2, Base2, Offset2, GV2, CV2);
Scott Michelfdc40a02009-02-17 22:15:04 +000010819
Nate Begemancc66cdd2009-09-25 06:05:26 +000010820 // If they have a same base address then check to see if they overlap.
10821 if (Base1 == Base2 || (GV1 && (GV1 == GV2)) || (CV1 && (CV1 == CV2)))
Bill Wendling836ca7d2009-01-30 23:59:18 +000010822 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010823
Owen Anderson4a9f1502010-09-20 20:39:59 +000010824 // It is possible for different frame indices to alias each other, mostly
10825 // when tail call optimization reuses return address slots for arguments.
10826 // To catch this case, look up the actual index of frame indices to compute
10827 // the real alias relationship.
10828 if (isFrameIndex1 && isFrameIndex2) {
10829 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10830 Offset1 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base1)->getIndex());
10831 Offset2 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base2)->getIndex());
10832 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
10833 }
10834
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010835 // Otherwise, if we know what the bases are, and they aren't identical, then
Owen Anderson4a9f1502010-09-20 20:39:59 +000010836 // we know they cannot alias.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010837 if ((isFrameIndex1 || CV1 || GV1) && (isFrameIndex2 || CV2 || GV2))
10838 return false;
Jim Laskey096c22e2006-10-18 12:29:57 +000010839
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010840 // If we know required SrcValue1 and SrcValue2 have relatively large alignment
10841 // compared to the size and offset of the access, we may be able to prove they
10842 // do not alias. This check is conservative for now to catch cases created by
10843 // splitting vector types.
10844 if ((SrcValueAlign1 == SrcValueAlign2) &&
10845 (SrcValueOffset1 != SrcValueOffset2) &&
10846 (Size1 == Size2) && (SrcValueAlign1 > Size1)) {
10847 int64_t OffAlign1 = SrcValueOffset1 % SrcValueAlign1;
10848 int64_t OffAlign2 = SrcValueOffset2 % SrcValueAlign1;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010849
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010850 // There is no overlap between these relatively aligned accesses of similar
10851 // size, return no alias.
10852 if ((OffAlign1 + Size1) <= OffAlign2 || (OffAlign2 + Size2) <= OffAlign1)
10853 return false;
10854 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010855
Hal Finkel253acef2013-08-29 03:29:55 +000010856 bool UseAA = CombinerGlobalAA.getNumOccurrences() > 0 ? CombinerGlobalAA :
10857 TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA();
Hal Finkel77364b72013-09-15 02:19:49 +000010858 if (UseAA && SrcValue1 && SrcValue2) {
Jim Laskey07a27092006-10-18 19:08:31 +000010859 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +000010860 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
10861 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
10862 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Scott Michelfdc40a02009-02-17 22:15:04 +000010863 AliasAnalysis::AliasResult AAResult =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010864 AA.alias(AliasAnalysis::Location(SrcValue1, Overlap1, TBAAInfo1),
10865 AliasAnalysis::Location(SrcValue2, Overlap2, TBAAInfo2));
Jim Laskey07a27092006-10-18 19:08:31 +000010866 if (AAResult == AliasAnalysis::NoAlias)
10867 return false;
10868 }
Jim Laskey096c22e2006-10-18 12:29:57 +000010869
10870 // Otherwise we have to assume they alias.
10871 return true;
Jim Laskey71382342006-10-07 23:37:56 +000010872}
10873
Nadav Rotem90e11dc2012-11-29 00:00:08 +000010874bool DAGCombiner::isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1) {
10875 SDValue Ptr0, Ptr1;
10876 int64_t Size0, Size1;
10877 const Value *SrcValue0, *SrcValue1;
10878 int SrcValueOffset0, SrcValueOffset1;
10879 unsigned SrcValueAlign0, SrcValueAlign1;
10880 const MDNode *SrcTBAAInfo0, *SrcTBAAInfo1;
10881 FindAliasInfo(Op0, Ptr0, Size0, SrcValue0, SrcValueOffset0,
10882 SrcValueAlign0, SrcTBAAInfo0);
10883 FindAliasInfo(Op1, Ptr1, Size1, SrcValue1, SrcValueOffset1,
10884 SrcValueAlign1, SrcTBAAInfo1);
10885 return isAlias(Ptr0, Size0, SrcValue0, SrcValueOffset0,
Nadav Rotemdde785c2012-12-06 17:34:13 +000010886 SrcValueAlign0, SrcTBAAInfo0,
10887 Ptr1, Size1, SrcValue1, SrcValueOffset1,
10888 SrcValueAlign1, SrcTBAAInfo1);
Nadav Rotem90e11dc2012-11-29 00:00:08 +000010889}
10890
Jim Laskey71382342006-10-07 23:37:56 +000010891/// FindAliasInfo - Extracts the relevant alias information from the memory
10892/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +000010893bool DAGCombiner::FindAliasInfo(SDNode *N,
Benjamin Kramerae4746b2012-01-15 11:50:43 +000010894 SDValue &Ptr, int64_t &Size,
10895 const Value *&SrcValue,
10896 int &SrcValueOffset,
10897 unsigned &SrcValueAlign,
10898 const MDNode *&TBAAInfo) const {
10899 LSBaseSDNode *LS = cast<LSBaseSDNode>(N);
10900
10901 Ptr = LS->getBasePtr();
10902 Size = LS->getMemoryVT().getSizeInBits() >> 3;
10903 SrcValue = LS->getSrcValue();
10904 SrcValueOffset = LS->getSrcValueOffset();
10905 SrcValueAlign = LS->getOriginalAlignment();
10906 TBAAInfo = LS->getTBAAInfo();
10907 return isa<LoadSDNode>(LS);
Jim Laskey71382342006-10-07 23:37:56 +000010908}
10909
Jim Laskey6ff23e52006-10-04 16:53:27 +000010910/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
10911/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +000010912void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
Craig Toppera0ec3f92013-07-14 04:42:23 +000010913 SmallVectorImpl<SDValue> &Aliases) {
Dan Gohman475871a2008-07-27 21:46:04 +000010914 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010915 SmallPtrSet<SDNode *, 16> Visited; // Visited node set.
Scott Michelfdc40a02009-02-17 22:15:04 +000010916
Jim Laskey279f0532006-09-25 16:29:54 +000010917 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +000010918 SDValue Ptr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010919 int64_t Size;
10920 const Value *SrcValue;
10921 int SrcValueOffset;
10922 unsigned SrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010923 const MDNode *SrcTBAAInfo;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010924 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010925 SrcValueAlign, SrcTBAAInfo);
Jim Laskey279f0532006-09-25 16:29:54 +000010926
Jim Laskey6ff23e52006-10-04 16:53:27 +000010927 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +000010928 Chains.push_back(OriginalChain);
Nate Begeman677c89d2009-10-12 05:53:58 +000010929 unsigned Depth = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010930
Jim Laskeybc588b82006-10-05 15:07:25 +000010931 // Look at each chain and determine if it is an alias. If so, add it to the
10932 // aliases list. If not, then continue up the chain looking for the next
Scott Michelfdc40a02009-02-17 22:15:04 +000010933 // candidate.
Jim Laskeybc588b82006-10-05 15:07:25 +000010934 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010935 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +000010936 Chains.pop_back();
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010937
10938 // For TokenFactor nodes, look at each operand and only continue up the
10939 // chain until we find two aliases. If we've seen two aliases, assume we'll
Nate Begeman677c89d2009-10-12 05:53:58 +000010940 // find more and revert to original chain since the xform is unlikely to be
10941 // profitable.
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010942 //
10943 // FIXME: The depth check could be made to return the last non-aliasing
Nate Begeman677c89d2009-10-12 05:53:58 +000010944 // chain we found before we hit a tokenfactor rather than the original
10945 // chain.
10946 if (Depth > 6 || Aliases.size() == 2) {
10947 Aliases.clear();
10948 Aliases.push_back(OriginalChain);
10949 break;
10950 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010951
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010952 // Don't bother if we've been before.
10953 if (!Visited.insert(Chain.getNode()))
10954 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +000010955
Jim Laskeybc588b82006-10-05 15:07:25 +000010956 switch (Chain.getOpcode()) {
10957 case ISD::EntryToken:
10958 // Entry token is ideal chain operand, but handled in FindBetterChain.
10959 break;
Scott Michelfdc40a02009-02-17 22:15:04 +000010960
Jim Laskeybc588b82006-10-05 15:07:25 +000010961 case ISD::LOAD:
10962 case ISD::STORE: {
10963 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +000010964 SDValue OpPtr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010965 int64_t OpSize;
10966 const Value *OpSrcValue;
10967 int OpSrcValueOffset;
10968 unsigned OpSrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010969 const MDNode *OpSrcTBAAInfo;
Gabor Greifba36cb52008-08-28 21:40:38 +000010970 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010971 OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010972 OpSrcValueAlign,
10973 OpSrcTBAAInfo);
Scott Michelfdc40a02009-02-17 22:15:04 +000010974
Jim Laskeybc588b82006-10-05 15:07:25 +000010975 // If chain is alias then stop here.
10976 if (!(IsLoad && IsOpLoad) &&
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010977 isAlias(Ptr, Size, SrcValue, SrcValueOffset, SrcValueAlign,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010978 SrcTBAAInfo,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010979 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010980 OpSrcValueAlign, OpSrcTBAAInfo)) {
Jim Laskeybc588b82006-10-05 15:07:25 +000010981 Aliases.push_back(Chain);
10982 } else {
10983 // Look further up the chain.
Scott Michelfdc40a02009-02-17 22:15:04 +000010984 Chains.push_back(Chain.getOperand(0));
Nate Begeman677c89d2009-10-12 05:53:58 +000010985 ++Depth;
Jim Laskey279f0532006-09-25 16:29:54 +000010986 }
Jim Laskeybc588b82006-10-05 15:07:25 +000010987 break;
10988 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010989
Jim Laskeybc588b82006-10-05 15:07:25 +000010990 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010991 // We have to check each of the operands of the token factor for "small"
10992 // token factors, so we queue them up. Adding the operands to the queue
10993 // (stack) in reverse order maintains the original order and increases the
10994 // likelihood that getNode will find a matching token factor (CSE.)
10995 if (Chain.getNumOperands() > 16) {
10996 Aliases.push_back(Chain);
10997 break;
10998 }
Jim Laskeybc588b82006-10-05 15:07:25 +000010999 for (unsigned n = Chain.getNumOperands(); n;)
11000 Chains.push_back(Chain.getOperand(--n));
Nate Begeman677c89d2009-10-12 05:53:58 +000011001 ++Depth;
Jim Laskeybc588b82006-10-05 15:07:25 +000011002 break;
Scott Michelfdc40a02009-02-17 22:15:04 +000011003
Jim Laskeybc588b82006-10-05 15:07:25 +000011004 default:
11005 // For all other instructions we will just have to take what we can get.
11006 Aliases.push_back(Chain);
11007 break;
Jim Laskey279f0532006-09-25 16:29:54 +000011008 }
11009 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000011010}
11011
11012/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
11013/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +000011014SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
11015 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +000011016
Jim Laskey6ff23e52006-10-04 16:53:27 +000011017 // Accumulate all the aliases to this node.
11018 GatherAllAliases(N, OldChain, Aliases);
Scott Michelfdc40a02009-02-17 22:15:04 +000011019
Dan Gohman71dc7c92011-05-17 22:20:36 +000011020 // If no operands then chain to entry token.
11021 if (Aliases.size() == 0)
Jim Laskey6ff23e52006-10-04 16:53:27 +000011022 return DAG.getEntryNode();
Dan Gohman71dc7c92011-05-17 22:20:36 +000011023
11024 // If a single operand then chain to it. We don't need to revisit it.
11025 if (Aliases.size() == 1)
Jim Laskey6ff23e52006-10-04 16:53:27 +000011026 return Aliases[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011027
Jim Laskey6ff23e52006-10-04 16:53:27 +000011028 // Construct a custom tailored token factor.
Andrew Trickac6d9be2013-05-25 02:42:55 +000011029 return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000011030 &Aliases[0], Aliases.size());
Jim Laskey279f0532006-09-25 16:29:54 +000011031}
11032
Nate Begeman1d4d4142005-09-01 00:19:25 +000011033// SelectionDAG::Combine - This is the entry point for the file.
11034//
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000011035void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA,
Bill Wendling98a366d2009-04-29 23:29:43 +000011036 CodeGenOpt::Level OptLevel) {
Nate Begeman1d4d4142005-09-01 00:19:25 +000011037 /// run - This is the main entry point to this class.
11038 ///
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000011039 DAGCombiner(*this, AA, OptLevel).Run(Level);
Nate Begeman1d4d4142005-09-01 00:19:25 +000011040}