blob: a01d5636dfe8d50b2b6063837bd4a5e9f7a3d45d [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 Colombet83f743a2013-10-11 18:29:42 +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 Colombet83f743a2013-10-11 18:29:42 +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 Colombet83f743a2013-10-11 18:29:42 +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 Colombet83f743a2013-10-11 18:29:42 +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 Colombet83f743a2013-10-11 18:29:42 +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 Colombet83f743a2013-10-11 18:29:42 +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;
Jack Carteradbd3ae2013-10-17 01:34:33 +0000352 return LegalTypes ? TLI.getScalarShiftAmountTy(LHSTy)
353 : TLI.getPointerTy();
Chris Lattner2392ae72010-04-15 04:48:01 +0000354 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000355
Chris Lattner2392ae72010-04-15 04:48:01 +0000356 /// isTypeLegal - This method returns true if we are running before type
357 /// legalization or if the specified VT is legal.
358 bool isTypeLegal(const EVT &VT) {
359 if (!LegalTypes) return true;
360 return TLI.isTypeLegal(VT);
361 }
Matt Arsenault225ed702013-05-18 00:21:46 +0000362
363 /// getSetCCResultType - Convenience wrapper around
364 /// TargetLowering::getSetCCResultType
365 EVT getSetCCResultType(EVT VT) const {
366 return TLI.getSetCCResultType(*DAG.getContext(), VT);
367 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000368 };
369}
370
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000371
372namespace {
373/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
374/// nodes from the worklist.
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000375class WorkListRemover : public SelectionDAG::DAGUpdateListener {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000376 DAGCombiner &DC;
377public:
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000378 explicit WorkListRemover(DAGCombiner &dc)
379 : SelectionDAG::DAGUpdateListener(dc.getDAG()), DC(dc) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000380
Duncan Sandsedfcf592008-06-11 11:42:12 +0000381 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000382 DC.removeFromWorkList(N);
383 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000384};
385}
386
Chris Lattner24664722006-03-01 04:53:38 +0000387//===----------------------------------------------------------------------===//
388// TargetLowering::DAGCombinerInfo implementation
389//===----------------------------------------------------------------------===//
390
391void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
392 ((DAGCombiner*)DC)->AddToWorkList(N);
393}
394
Cameron Zwariched3caf92011-04-02 02:40:26 +0000395void TargetLowering::DAGCombinerInfo::RemoveFromWorklist(SDNode *N) {
396 ((DAGCombiner*)DC)->removeFromWorkList(N);
397}
398
Dan Gohman475871a2008-07-27 21:46:04 +0000399SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000400CombineTo(SDNode *N, const std::vector<SDValue> &To, bool AddTo) {
401 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000402}
403
Dan Gohman475871a2008-07-27 21:46:04 +0000404SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000405CombineTo(SDNode *N, SDValue Res, bool AddTo) {
406 return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000407}
408
409
Dan Gohman475871a2008-07-27 21:46:04 +0000410SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000411CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) {
412 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000413}
414
Dan Gohmane5af2d32009-01-29 01:59:02 +0000415void TargetLowering::DAGCombinerInfo::
416CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
417 return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO);
418}
Chris Lattner24664722006-03-01 04:53:38 +0000419
Chris Lattner24664722006-03-01 04:53:38 +0000420//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000421// Helper Functions
422//===----------------------------------------------------------------------===//
423
424/// isNegatibleForFree - Return 1 if we can compute the negated form of the
425/// specified expression for the same cost as the expression itself, or 2 if we
426/// can compute the negated form more cheaply than the expression itself.
Duncan Sands25cf2272008-11-24 14:53:14 +0000427static char isNegatibleForFree(SDValue Op, bool LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000428 const TargetLowering &TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000429 const TargetOptions *Options,
Chris Lattner0254e702008-02-26 07:04:54 +0000430 unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000431 // fneg is removable even if it has multiple uses.
432 if (Op.getOpcode() == ISD::FNEG) return 2;
Scott Michelfdc40a02009-02-17 22:15:04 +0000433
Chris Lattner29446522007-05-14 22:04:50 +0000434 // Don't allow anything with multiple uses.
435 if (!Op.hasOneUse()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000436
Chris Lattner3adf9512007-05-25 02:19:06 +0000437 // Don't recurse exponentially.
438 if (Depth > 6) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000439
Chris Lattner29446522007-05-14 22:04:50 +0000440 switch (Op.getOpcode()) {
441 default: return false;
442 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000443 // Don't invert constant FP values after legalize. The negated constant
444 // isn't necessarily legal.
Duncan Sands25cf2272008-11-24 14:53:14 +0000445 return LegalOperations ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000446 case ISD::FADD:
447 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000448 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000449
Owen Andersonafd3d562012-03-06 00:29:31 +0000450 // After operation legalization, it might not be legal to create new FSUBs.
451 if (LegalOperations &&
452 !TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType()))
453 return 0;
454
Craig Topper956342b2012-09-09 22:58:45 +0000455 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Owen Andersonafd3d562012-03-06 00:29:31 +0000456 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
457 Options, Depth + 1))
Chris Lattner29446522007-05-14 22:04:50 +0000458 return V;
Bill Wendlingd34470c2009-01-30 23:10:18 +0000459 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Owen Andersonafd3d562012-03-06 00:29:31 +0000460 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000461 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000462 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000463 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000464 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000465
Bill Wendlingd34470c2009-01-30 23:10:18 +0000466 // fold (fneg (fsub A, B)) -> (fsub B, A)
Chris Lattner29446522007-05-14 22:04:50 +0000467 return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000468
Chris Lattner29446522007-05-14 22:04:50 +0000469 case ISD::FMUL:
470 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000471 if (Options->HonorSignDependentRoundingFPMath()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000472
Bill Wendlingd34470c2009-01-30 23:10:18 +0000473 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y))
Owen Andersonafd3d562012-03-06 00:29:31 +0000474 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
475 Options, Depth + 1))
Chris Lattner29446522007-05-14 22:04:50 +0000476 return V;
Scott Michelfdc40a02009-02-17 22:15:04 +0000477
Owen Andersonafd3d562012-03-06 00:29:31 +0000478 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000479 Depth + 1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000480
Chris Lattner29446522007-05-14 22:04:50 +0000481 case ISD::FP_EXTEND:
482 case ISD::FP_ROUND:
483 case ISD::FSIN:
Owen Andersonafd3d562012-03-06 00:29:31 +0000484 return isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000485 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000486 }
487}
488
489/// GetNegatedExpression - If isNegatibleForFree returns true, this function
490/// returns the newly negated expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000491static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000492 bool LegalOperations, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000493 // fneg is removable even if it has multiple uses.
494 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000495
Chris Lattner29446522007-05-14 22:04:50 +0000496 // Don't allow anything with multiple uses.
497 assert(Op.hasOneUse() && "Unknown reuse!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000498
Chris Lattner3adf9512007-05-25 02:19:06 +0000499 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000500 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000501 default: llvm_unreachable("Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000502 case ISD::ConstantFP: {
503 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
504 V.changeSign();
505 return DAG.getConstantFP(V, Op.getValueType());
506 }
Chris Lattner29446522007-05-14 22:04:50 +0000507 case ISD::FADD:
508 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000509 assert(DAG.getTarget().Options.UnsafeFPMath);
Scott Michelfdc40a02009-02-17 22:15:04 +0000510
Bill Wendlingd34470c2009-01-30 23:10:18 +0000511 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000512 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000513 DAG.getTargetLoweringInfo(),
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000514 &DAG.getTarget().Options, Depth+1))
Andrew Trickac6d9be2013-05-25 02:42:55 +0000515 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000516 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000517 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000518 Op.getOperand(1));
Bill Wendlingd34470c2009-01-30 23:10:18 +0000519 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Andrew Trickac6d9be2013-05-25 02:42:55 +0000520 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000521 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000522 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000523 Op.getOperand(0));
524 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000525 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000526 assert(DAG.getTarget().Options.UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000527
Bill Wendlingd34470c2009-01-30 23:10:18 +0000528 // fold (fneg (fsub 0, B)) -> B
Dan Gohman23ff1822007-07-02 15:48:56 +0000529 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000530 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000531 return Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000532
Bill Wendlingd34470c2009-01-30 23:10:18 +0000533 // fold (fneg (fsub A, B)) -> (fsub B, A)
Andrew Trickac6d9be2013-05-25 02:42:55 +0000534 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Bill Wendling35247c32009-01-30 00:45:56 +0000535 Op.getOperand(1), Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +0000536
Chris Lattner29446522007-05-14 22:04:50 +0000537 case ISD::FMUL:
538 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000539 assert(!DAG.getTarget().Options.HonorSignDependentRoundingFPMath());
Scott Michelfdc40a02009-02-17 22:15:04 +0000540
Bill Wendlingd34470c2009-01-30 23:10:18 +0000541 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000542 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000543 DAG.getTargetLoweringInfo(),
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000544 &DAG.getTarget().Options, Depth+1))
Andrew Trickac6d9be2013-05-25 02:42:55 +0000545 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000546 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000547 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000548 Op.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000549
Bill Wendlingd34470c2009-01-30 23:10:18 +0000550 // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y))
Andrew Trickac6d9be2013-05-25 02:42:55 +0000551 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Chris Lattner29446522007-05-14 22:04:50 +0000552 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000553 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000554 LegalOperations, Depth+1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000555
Chris Lattner29446522007-05-14 22:04:50 +0000556 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000557 case ISD::FSIN:
Andrew Trickac6d9be2013-05-25 02:42:55 +0000558 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000559 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000560 LegalOperations, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000561 case ISD::FP_ROUND:
Andrew Trickac6d9be2013-05-25 02:42:55 +0000562 return DAG.getNode(ISD::FP_ROUND, SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000563 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000564 LegalOperations, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000565 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000566 }
567}
Chris Lattner24664722006-03-01 04:53:38 +0000568
569
Nate Begeman4ebd8052005-09-01 23:24:04 +0000570// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
571// that selects between the values 1 and 0, making it equivalent to a setcc.
Scott Michelfdc40a02009-02-17 22:15:04 +0000572// Also, set the incoming LHS, RHS, and CC references to the appropriate
Nate Begeman646d7e22005-09-02 21:18:40 +0000573// nodes based on the type of node we are checking. This simplifies life a
574// bit for the callers.
Dan Gohman475871a2008-07-27 21:46:04 +0000575static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
576 SDValue &CC) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000577 if (N.getOpcode() == ISD::SETCC) {
578 LHS = N.getOperand(0);
579 RHS = N.getOperand(1);
580 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000581 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000582 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000583 if (N.getOpcode() == ISD::SELECT_CC &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000584 N.getOperand(2).getOpcode() == ISD::Constant &&
585 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000586 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000587 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
588 LHS = N.getOperand(0);
589 RHS = N.getOperand(1);
590 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000591 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000592 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000593 return false;
594}
595
Nate Begeman99801192005-09-07 23:25:52 +0000596// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
597// one use. If this is true, it allows the users to invert the operation for
598// free when it is profitable to do so.
Dan Gohman475871a2008-07-27 21:46:04 +0000599static bool isOneUseSetCC(SDValue N) {
600 SDValue N0, N1, N2;
Gabor Greifba36cb52008-08-28 21:40:38 +0000601 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000602 return true;
603 return false;
604}
605
Andrew Trickac6d9be2013-05-25 02:42:55 +0000606SDValue DAGCombiner::ReassociateOps(unsigned Opc, SDLoc DL,
Bill Wendling35247c32009-01-30 00:45:56 +0000607 SDValue N0, SDValue N1) {
Owen Andersone50ed302009-08-10 22:56:29 +0000608 EVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000609 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
610 if (isa<ConstantSDNode>(N1)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000611 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000612 SDValue OpNode =
613 DAG.FoldConstantArithmetic(Opc, VT,
614 cast<ConstantSDNode>(N0.getOperand(1)),
615 cast<ConstantSDNode>(N1));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000616 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000617 }
618 if (N0.hasOneUse()) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000619 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
Andrew Trickac6d9be2013-05-25 02:42:55 +0000620 SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000621 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000622 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000623 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000624 }
625 }
Bill Wendling35247c32009-01-30 00:45:56 +0000626
Nate Begemancd4d58c2006-02-03 06:46:56 +0000627 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
628 if (isa<ConstantSDNode>(N0)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000629 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000630 SDValue OpNode =
631 DAG.FoldConstantArithmetic(Opc, VT,
632 cast<ConstantSDNode>(N1.getOperand(1)),
633 cast<ConstantSDNode>(N0));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000634 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000635 }
636 if (N1.hasOneUse()) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000637 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
Andrew Trickac6d9be2013-05-25 02:42:55 +0000638 SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000639 N1.getOperand(0), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000640 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000641 return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000642 }
643 }
Bill Wendling35247c32009-01-30 00:45:56 +0000644
Dan Gohman475871a2008-07-27 21:46:04 +0000645 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000646}
647
Dan Gohman475871a2008-07-27 21:46:04 +0000648SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
649 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000650 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
651 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +0000652 DEBUG(dbgs() << "\nReplacing.1 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000653 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000654 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000655 To[0].getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000656 dbgs() << " and " << NumTo-1 << " other values\n";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000657 for (unsigned i = 0, e = NumTo; i != e; ++i)
Jakob Stoklund Olesen9f0d4e62009-12-03 05:15:35 +0000658 assert((!To[i].getNode() ||
659 N->getValueType(i) == To[i].getValueType()) &&
Dan Gohman764fd0c2009-01-21 15:17:51 +0000660 "Cannot combine value to value of different type!"));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000661 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000662 DAG.ReplaceAllUsesWith(N, To);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000663 if (AddTo) {
664 // Push the new nodes and any users onto the worklist
665 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattnerd1980a52009-03-12 06:52:53 +0000666 if (To[i].getNode()) {
667 AddToWorkList(To[i].getNode());
668 AddUsersToWorkList(To[i].getNode());
669 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000670 }
671 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000672
Dan Gohmandbe664a2009-01-19 21:44:21 +0000673 // Finally, if the node is now dead, remove it from the graph. The node
674 // may not be dead if the replacement process recursively simplified to
675 // something else needing this node.
676 if (N->use_empty()) {
677 // Nodes can be reintroduced into the worklist. Make sure we do not
678 // process a node that has been replaced.
679 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000680
Dan Gohmandbe664a2009-01-19 21:44:21 +0000681 // Finally, since the node is now dead, remove it from the graph.
682 DAG.DeleteNode(N);
683 }
Dan Gohman475871a2008-07-27 21:46:04 +0000684 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000685}
686
Evan Chenge5b51ac2010-04-17 06:13:15 +0000687void DAGCombiner::
688CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000689 // Replace all uses. If any nodes become isomorphic to other nodes and
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000690 // are deleted, make sure to remove them from our worklist.
691 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000692 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000693
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000694 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000695 AddToWorkList(TLO.New.getNode());
696 AddUsersToWorkList(TLO.New.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000697
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000698 // Finally, if the node is now dead, remove it from the graph. The node
699 // may not be dead if the replacement process recursively simplified to
700 // something else needing this node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000701 if (TLO.Old.getNode()->use_empty()) {
702 removeFromWorkList(TLO.Old.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000703
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000704 // If the operands of this node are only used by the node, they will now
705 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greifba36cb52008-08-28 21:40:38 +0000706 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
707 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
708 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000709
Gabor Greifba36cb52008-08-28 21:40:38 +0000710 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000711 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000712}
713
714/// SimplifyDemandedBits - Check the specified integer node value to see if
715/// it can be simplified or if things it uses can be simplified by bit
716/// propagation. If so, return true.
717bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000718 TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000719 APInt KnownZero, KnownOne;
720 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
721 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000722
Dan Gohmane5af2d32009-01-29 01:59:02 +0000723 // Revisit the node.
724 AddToWorkList(Op.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000725
Dan Gohmane5af2d32009-01-29 01:59:02 +0000726 // Replace the old value with the new one.
727 ++NodesCombined;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000728 DEBUG(dbgs() << "\nReplacing.2 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000729 TLO.Old.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000730 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000731 TLO.New.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000732 dbgs() << '\n');
Scott Michelfdc40a02009-02-17 22:15:04 +0000733
Dan Gohmane5af2d32009-01-29 01:59:02 +0000734 CommitTargetLoweringOpt(TLO);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000735 return true;
736}
737
Evan Cheng95c57ea2010-04-24 04:43:44 +0000738void DAGCombiner::ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad) {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000739 SDLoc dl(Load);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000740 EVT VT = Load->getValueType(0);
741 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, VT, SDValue(ExtLoad, 0));
Evan Cheng4c26e932010-04-19 19:29:22 +0000742
Evan Cheng95c57ea2010-04-24 04:43:44 +0000743 DEBUG(dbgs() << "\nReplacing.9 ";
744 Load->dump(&DAG);
745 dbgs() << "\nWith: ";
746 Trunc.getNode()->dump(&DAG);
747 dbgs() << '\n');
748 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000749 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 0), Trunc);
750 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), SDValue(ExtLoad, 1));
Evan Cheng95c57ea2010-04-24 04:43:44 +0000751 removeFromWorkList(Load);
752 DAG.DeleteNode(Load);
Evan Chengac7eae52010-04-27 19:48:13 +0000753 AddToWorkList(Trunc.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000754}
755
756SDValue DAGCombiner::PromoteOperand(SDValue Op, EVT PVT, bool &Replace) {
757 Replace = false;
Andrew Trickac6d9be2013-05-25 02:42:55 +0000758 SDLoc dl(Op);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000759 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
Evan Chengac7eae52010-04-27 19:48:13 +0000760 EVT MemVT = LD->getMemoryVT();
761 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000762 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000763 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000764 : LD->getExtensionType();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000765 Replace = true;
Stuart Hastingsa9011292011-02-16 16:23:55 +0000766 return DAG.getExtLoad(ExtType, dl, PVT,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000767 LD->getChain(), LD->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000768 LD->getPointerInfo(),
Evan Chengac7eae52010-04-27 19:48:13 +0000769 MemVT, LD->isVolatile(),
Evan Chenge5b51ac2010-04-17 06:13:15 +0000770 LD->isNonTemporal(), LD->getAlignment());
771 }
772
Evan Cheng4c26e932010-04-19 19:29:22 +0000773 unsigned Opc = Op.getOpcode();
Evan Chengcaf77402010-04-23 19:10:30 +0000774 switch (Opc) {
775 default: break;
776 case ISD::AssertSext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000777 return DAG.getNode(ISD::AssertSext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000778 SExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000779 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000780 case ISD::AssertZext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000781 return DAG.getNode(ISD::AssertZext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000782 ZExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000783 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000784 case ISD::Constant: {
785 unsigned ExtOpc =
Evan Cheng4c26e932010-04-19 19:29:22 +0000786 Op.getValueType().isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Evan Chengcaf77402010-04-23 19:10:30 +0000787 return DAG.getNode(ExtOpc, dl, PVT, Op);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000788 }
Evan Chengcaf77402010-04-23 19:10:30 +0000789 }
790
791 if (!TLI.isOperationLegal(ISD::ANY_EXTEND, PVT))
Evan Chenge5b51ac2010-04-17 06:13:15 +0000792 return SDValue();
Evan Chengcaf77402010-04-23 19:10:30 +0000793 return DAG.getNode(ISD::ANY_EXTEND, dl, PVT, Op);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000794}
795
Evan Cheng95c57ea2010-04-24 04:43:44 +0000796SDValue DAGCombiner::SExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000797 if (!TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, PVT))
798 return SDValue();
799 EVT OldVT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +0000800 SDLoc dl(Op);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000801 bool Replace = false;
802 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
803 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000804 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000805 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000806
807 if (Replace)
808 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
809 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NewOp.getValueType(), NewOp,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000810 DAG.getValueType(OldVT));
811}
812
Evan Cheng95c57ea2010-04-24 04:43:44 +0000813SDValue DAGCombiner::ZExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000814 EVT OldVT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +0000815 SDLoc dl(Op);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000816 bool Replace = false;
817 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
818 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000819 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000820 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000821
822 if (Replace)
823 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
824 return DAG.getZeroExtendInReg(NewOp, dl, OldVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000825}
826
Evan Cheng64b7bf72010-04-16 06:14:10 +0000827/// PromoteIntBinOp - Promote the specified integer binary operation if the
828/// target indicates it is beneficial. e.g. On x86, it's usually better to
829/// promote i16 operations to i32 since i16 instructions are longer.
830SDValue DAGCombiner::PromoteIntBinOp(SDValue Op) {
831 if (!LegalOperations)
832 return SDValue();
833
834 EVT VT = Op.getValueType();
835 if (VT.isVector() || !VT.isInteger())
836 return SDValue();
837
Evan Chenge5b51ac2010-04-17 06:13:15 +0000838 // If operation type is 'undesirable', e.g. i16 on x86, consider
839 // promoting it.
840 unsigned Opc = Op.getOpcode();
841 if (TLI.isTypeDesirableForOp(Opc, VT))
842 return SDValue();
843
Evan Cheng64b7bf72010-04-16 06:14:10 +0000844 EVT PVT = VT;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000845 // Consult target whether it is a good idea to promote this operation and
846 // what's the right type to promote it to.
847 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
Evan Cheng64b7bf72010-04-16 06:14:10 +0000848 assert(PVT != VT && "Don't know what type to promote to!");
849
Evan Cheng95c57ea2010-04-24 04:43:44 +0000850 bool Replace0 = false;
851 SDValue N0 = Op.getOperand(0);
852 SDValue NN0 = PromoteOperand(N0, PVT, Replace0);
853 if (NN0.getNode() == 0)
Evan Cheng07c4e102010-04-22 20:19:46 +0000854 return SDValue();
855
Evan Cheng95c57ea2010-04-24 04:43:44 +0000856 bool Replace1 = false;
857 SDValue N1 = Op.getOperand(1);
Evan Chengaad753b2010-05-10 19:03:57 +0000858 SDValue NN1;
859 if (N0 == N1)
860 NN1 = NN0;
861 else {
862 NN1 = PromoteOperand(N1, PVT, Replace1);
863 if (NN1.getNode() == 0)
864 return SDValue();
865 }
Evan Cheng07c4e102010-04-22 20:19:46 +0000866
Evan Cheng95c57ea2010-04-24 04:43:44 +0000867 AddToWorkList(NN0.getNode());
Evan Chengaad753b2010-05-10 19:03:57 +0000868 if (NN1.getNode())
869 AddToWorkList(NN1.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000870
871 if (Replace0)
872 ReplaceLoadWithPromotedLoad(N0.getNode(), NN0.getNode());
873 if (Replace1)
874 ReplaceLoadWithPromotedLoad(N1.getNode(), NN1.getNode());
Evan Cheng07c4e102010-04-22 20:19:46 +0000875
Evan Chengac7eae52010-04-27 19:48:13 +0000876 DEBUG(dbgs() << "\nPromoting ";
877 Op.getNode()->dump(&DAG));
Andrew Trickac6d9be2013-05-25 02:42:55 +0000878 SDLoc dl(Op);
Evan Cheng07c4e102010-04-22 20:19:46 +0000879 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000880 DAG.getNode(Opc, dl, PVT, NN0, NN1));
Evan Cheng07c4e102010-04-22 20:19:46 +0000881 }
882 return SDValue();
883}
884
885/// PromoteIntShiftOp - Promote the specified integer shift operation if the
886/// target indicates it is beneficial. e.g. On x86, it's usually better to
887/// promote i16 operations to i32 since i16 instructions are longer.
888SDValue DAGCombiner::PromoteIntShiftOp(SDValue Op) {
889 if (!LegalOperations)
890 return SDValue();
891
892 EVT VT = Op.getValueType();
893 if (VT.isVector() || !VT.isInteger())
894 return SDValue();
895
896 // If operation type is 'undesirable', e.g. i16 on x86, consider
897 // promoting it.
898 unsigned Opc = Op.getOpcode();
899 if (TLI.isTypeDesirableForOp(Opc, VT))
900 return SDValue();
901
902 EVT PVT = VT;
903 // Consult target whether it is a good idea to promote this operation and
904 // what's the right type to promote it to.
905 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
906 assert(PVT != VT && "Don't know what type to promote to!");
907
Evan Cheng95c57ea2010-04-24 04:43:44 +0000908 bool Replace = false;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000909 SDValue N0 = Op.getOperand(0);
910 if (Opc == ISD::SRA)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000911 N0 = SExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000912 else if (Opc == ISD::SRL)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000913 N0 = ZExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000914 else
Evan Cheng95c57ea2010-04-24 04:43:44 +0000915 N0 = PromoteOperand(N0, PVT, Replace);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000916 if (N0.getNode() == 0)
917 return SDValue();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000918
Evan Chenge5b51ac2010-04-17 06:13:15 +0000919 AddToWorkList(N0.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000920 if (Replace)
921 ReplaceLoadWithPromotedLoad(Op.getOperand(0).getNode(), N0.getNode());
Evan Cheng64b7bf72010-04-16 06:14:10 +0000922
Evan Chengac7eae52010-04-27 19:48:13 +0000923 DEBUG(dbgs() << "\nPromoting ";
924 Op.getNode()->dump(&DAG));
Andrew Trickac6d9be2013-05-25 02:42:55 +0000925 SDLoc dl(Op);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000926 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng07c4e102010-04-22 20:19:46 +0000927 DAG.getNode(Opc, dl, PVT, N0, Op.getOperand(1)));
Evan Cheng64b7bf72010-04-16 06:14:10 +0000928 }
929 return SDValue();
930}
931
Evan Cheng4c26e932010-04-19 19:29:22 +0000932SDValue DAGCombiner::PromoteExtend(SDValue Op) {
933 if (!LegalOperations)
934 return SDValue();
935
936 EVT VT = Op.getValueType();
937 if (VT.isVector() || !VT.isInteger())
938 return SDValue();
939
940 // If operation type is 'undesirable', e.g. i16 on x86, consider
941 // promoting it.
942 unsigned Opc = Op.getOpcode();
943 if (TLI.isTypeDesirableForOp(Opc, VT))
944 return SDValue();
945
946 EVT PVT = VT;
947 // Consult target whether it is a good idea to promote this operation and
948 // what's the right type to promote it to.
949 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
950 assert(PVT != VT && "Don't know what type to promote to!");
951 // fold (aext (aext x)) -> (aext x)
952 // fold (aext (zext x)) -> (zext x)
953 // fold (aext (sext x)) -> (sext x)
Evan Chengac7eae52010-04-27 19:48:13 +0000954 DEBUG(dbgs() << "\nPromoting ";
955 Op.getNode()->dump(&DAG));
Andrew Trickac6d9be2013-05-25 02:42:55 +0000956 return DAG.getNode(Op.getOpcode(), SDLoc(Op), VT, Op.getOperand(0));
Evan Cheng4c26e932010-04-19 19:29:22 +0000957 }
958 return SDValue();
959}
960
961bool DAGCombiner::PromoteLoad(SDValue Op) {
962 if (!LegalOperations)
963 return false;
964
965 EVT VT = Op.getValueType();
966 if (VT.isVector() || !VT.isInteger())
967 return false;
968
969 // If operation type is 'undesirable', e.g. i16 on x86, consider
970 // promoting it.
971 unsigned Opc = Op.getOpcode();
972 if (TLI.isTypeDesirableForOp(Opc, VT))
973 return false;
974
975 EVT PVT = VT;
976 // Consult target whether it is a good idea to promote this operation and
977 // what's the right type to promote it to.
978 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
979 assert(PVT != VT && "Don't know what type to promote to!");
980
Andrew Trickac6d9be2013-05-25 02:42:55 +0000981 SDLoc dl(Op);
Evan Cheng4c26e932010-04-19 19:29:22 +0000982 SDNode *N = Op.getNode();
983 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chengac7eae52010-04-27 19:48:13 +0000984 EVT MemVT = LD->getMemoryVT();
985 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000986 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000987 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000988 : LD->getExtensionType();
Stuart Hastingsa9011292011-02-16 16:23:55 +0000989 SDValue NewLD = DAG.getExtLoad(ExtType, dl, PVT,
Evan Cheng4c26e932010-04-19 19:29:22 +0000990 LD->getChain(), LD->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000991 LD->getPointerInfo(),
Evan Chengac7eae52010-04-27 19:48:13 +0000992 MemVT, LD->isVolatile(),
Evan Cheng4c26e932010-04-19 19:29:22 +0000993 LD->isNonTemporal(), LD->getAlignment());
994 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, VT, NewLD);
995
Evan Cheng95c57ea2010-04-24 04:43:44 +0000996 DEBUG(dbgs() << "\nPromoting ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000997 N->dump(&DAG);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000998 dbgs() << "\nTo: ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000999 Result.getNode()->dump(&DAG);
1000 dbgs() << '\n');
1001 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001002 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
1003 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), NewLD.getValue(1));
Evan Cheng4c26e932010-04-19 19:29:22 +00001004 removeFromWorkList(N);
1005 DAG.DeleteNode(N);
Evan Chengac7eae52010-04-27 19:48:13 +00001006 AddToWorkList(Result.getNode());
Evan Cheng4c26e932010-04-19 19:29:22 +00001007 return true;
1008 }
1009 return false;
1010}
1011
Evan Chenge5b51ac2010-04-17 06:13:15 +00001012
Chris Lattner29446522007-05-14 22:04:50 +00001013//===----------------------------------------------------------------------===//
1014// Main DAG Combiner implementation
1015//===----------------------------------------------------------------------===//
1016
Duncan Sands25cf2272008-11-24 14:53:14 +00001017void DAGCombiner::Run(CombineLevel AtLevel) {
1018 // set the instance variables, so that the various visit routines may use it.
1019 Level = AtLevel;
Eli Friedman50185242011-11-12 00:35:34 +00001020 LegalOperations = Level >= AfterLegalizeVectorOps;
1021 LegalTypes = Level >= AfterLegalizeTypes;
Nate Begeman4ebd8052005-09-01 23:24:04 +00001022
Evan Cheng17a568b2008-08-29 22:21:44 +00001023 // Add all the dag nodes to the worklist.
Evan Cheng17a568b2008-08-29 22:21:44 +00001024 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
1025 E = DAG.allnodes_end(); I != E; ++I)
James Molloy6660c052012-02-16 09:17:04 +00001026 AddToWorkList(I);
Duncan Sands25cf2272008-11-24 14:53:14 +00001027
Evan Cheng17a568b2008-08-29 22:21:44 +00001028 // Create a dummy node (which is not added to allnodes), that adds a reference
1029 // to the root node, preventing it from being deleted, and tracking any
1030 // changes of the root.
1031 HandleSDNode Dummy(DAG.getRoot());
Scott Michelfdc40a02009-02-17 22:15:04 +00001032
Jim Laskey26f7fa72006-10-17 19:33:52 +00001033 // The root of the dag may dangle to deleted nodes until the dag combiner is
1034 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +00001035 DAG.setRoot(SDValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00001036
James Molloy6660c052012-02-16 09:17:04 +00001037 // while the worklist isn't empty, find a node and
Evan Cheng17a568b2008-08-29 22:21:44 +00001038 // try and combine it.
James Molloy6660c052012-02-16 09:17:04 +00001039 while (!WorkListContents.empty()) {
1040 SDNode *N;
Jack Carteradbd3ae2013-10-17 01:34:33 +00001041 // The WorkListOrder holds the SDNodes in order, but it may contain
1042 // duplicates.
James Molloy6660c052012-02-16 09:17:04 +00001043 // In order to avoid a linear scan, we use a set (O(log N)) to hold what the
1044 // worklist *should* contain, and check the node we want to visit is should
1045 // actually be visited.
1046 do {
Benjamin Kramerd5f76902012-03-10 00:23:58 +00001047 N = WorkListOrder.pop_back_val();
James Molloy6660c052012-02-16 09:17:04 +00001048 } while (!WorkListContents.erase(N));
Scott Michelfdc40a02009-02-17 22:15:04 +00001049
Evan Cheng17a568b2008-08-29 22:21:44 +00001050 // If N has no uses, it is dead. Make sure to revisit all N's operands once
1051 // N is deleted from the DAG, since they too may now be dead or may have a
1052 // reduced number of uses, allowing other xforms.
1053 if (N->use_empty() && N != &Dummy) {
1054 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1055 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001056
Evan Cheng17a568b2008-08-29 22:21:44 +00001057 DAG.DeleteNode(N);
1058 continue;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001059 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001060
Evan Cheng17a568b2008-08-29 22:21:44 +00001061 SDValue RV = combine(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001062
Evan Cheng17a568b2008-08-29 22:21:44 +00001063 if (RV.getNode() == 0)
1064 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001065
Evan Cheng17a568b2008-08-29 22:21:44 +00001066 ++NodesCombined;
Scott Michelfdc40a02009-02-17 22:15:04 +00001067
Evan Cheng17a568b2008-08-29 22:21:44 +00001068 // If we get back the same node we passed in, rather than a new node or
1069 // zero, we know that the node must have defined multiple values and
Scott Michelfdc40a02009-02-17 22:15:04 +00001070 // CombineTo was used. Since CombineTo takes care of the worklist
Evan Cheng17a568b2008-08-29 22:21:44 +00001071 // mechanics for us, we have no work to do in this case.
1072 if (RV.getNode() == N)
1073 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001074
Evan Cheng17a568b2008-08-29 22:21:44 +00001075 assert(N->getOpcode() != ISD::DELETED_NODE &&
1076 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
1077 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +00001078
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001079 DEBUG(dbgs() << "\nReplacing.3 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001080 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001081 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001082 RV.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001083 dbgs() << '\n');
Eric Christopher7332e6e2011-07-14 01:12:15 +00001084
Devang Patel9728ea22011-05-23 22:04:42 +00001085 // Transfer debug value.
1086 DAG.TransferDbgValues(SDValue(N, 0), RV);
Evan Cheng17a568b2008-08-29 22:21:44 +00001087 WorkListRemover DeadNodes(*this);
1088 if (N->getNumValues() == RV.getNode()->getNumValues())
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001089 DAG.ReplaceAllUsesWith(N, RV.getNode());
Evan Cheng17a568b2008-08-29 22:21:44 +00001090 else {
1091 assert(N->getValueType(0) == RV.getValueType() &&
1092 N->getNumValues() == 1 && "Type mismatch");
1093 SDValue OpV = RV;
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001094 DAG.ReplaceAllUsesWith(N, &OpV);
Evan Cheng17a568b2008-08-29 22:21:44 +00001095 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001096
Evan Cheng17a568b2008-08-29 22:21:44 +00001097 // Push the new node and any users onto the worklist
1098 AddToWorkList(RV.getNode());
1099 AddUsersToWorkList(RV.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001100
Evan Cheng17a568b2008-08-29 22:21:44 +00001101 // Add any uses of the old node to the worklist in case this node is the
1102 // last one that uses them. They may become dead after this node is
1103 // deleted.
1104 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1105 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001106
Dan Gohmandbe664a2009-01-19 21:44:21 +00001107 // Finally, if the node is now dead, remove it from the graph. The node
1108 // may not be dead if the replacement process recursively simplified to
1109 // something else needing this node.
1110 if (N->use_empty()) {
1111 // Nodes can be reintroduced into the worklist. Make sure we do not
1112 // process a node that has been replaced.
1113 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001114
Dan Gohmandbe664a2009-01-19 21:44:21 +00001115 // Finally, since the node is now dead, remove it from the graph.
1116 DAG.DeleteNode(N);
1117 }
Evan Cheng17a568b2008-08-29 22:21:44 +00001118 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001119
Chris Lattner95038592005-10-05 06:35:28 +00001120 // If the root changed (e.g. it was a dead load, update the root).
1121 DAG.setRoot(Dummy.getValue());
Hal Finkel31490ba2012-04-16 03:33:22 +00001122 DAG.RemoveDeadNodes();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001123}
1124
Dan Gohman475871a2008-07-27 21:46:04 +00001125SDValue DAGCombiner::visit(SDNode *N) {
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001126 switch (N->getOpcode()) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001127 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +00001128 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001129 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001130 case ISD::ADD: return visitADD(N);
1131 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +00001132 case ISD::ADDC: return visitADDC(N);
Craig Toppercc274522012-01-07 09:06:39 +00001133 case ISD::SUBC: return visitSUBC(N);
Chris Lattner91153682007-03-04 20:03:15 +00001134 case ISD::ADDE: return visitADDE(N);
Craig Toppercc274522012-01-07 09:06:39 +00001135 case ISD::SUBE: return visitSUBE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001136 case ISD::MUL: return visitMUL(N);
1137 case ISD::SDIV: return visitSDIV(N);
1138 case ISD::UDIV: return visitUDIV(N);
1139 case ISD::SREM: return visitSREM(N);
1140 case ISD::UREM: return visitUREM(N);
1141 case ISD::MULHU: return visitMULHU(N);
1142 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001143 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
1144 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00001145 case ISD::SMULO: return visitSMULO(N);
1146 case ISD::UMULO: return visitUMULO(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001147 case ISD::SDIVREM: return visitSDIVREM(N);
1148 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001149 case ISD::AND: return visitAND(N);
1150 case ISD::OR: return visitOR(N);
1151 case ISD::XOR: return visitXOR(N);
1152 case ISD::SHL: return visitSHL(N);
1153 case ISD::SRA: return visitSRA(N);
1154 case ISD::SRL: return visitSRL(N);
1155 case ISD::CTLZ: return visitCTLZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001156 case ISD::CTLZ_ZERO_UNDEF: return visitCTLZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001157 case ISD::CTTZ: return visitCTTZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001158 case ISD::CTTZ_ZERO_UNDEF: return visitCTTZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001159 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +00001160 case ISD::SELECT: return visitSELECT(N);
Benjamin Kramer6242fda2013-04-26 09:19:19 +00001161 case ISD::VSELECT: return visitVSELECT(N);
Nate Begeman452d7be2005-09-16 00:54:12 +00001162 case ISD::SELECT_CC: return visitSELECT_CC(N);
1163 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001164 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
1165 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001166 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001167 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
1168 case ISD::TRUNCATE: return visitTRUNCATE(N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001169 case ISD::BITCAST: return visitBITCAST(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00001170 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001171 case ISD::FADD: return visitFADD(N);
1172 case ISD::FSUB: return visitFSUB(N);
1173 case ISD::FMUL: return visitFMUL(N);
Owen Anderson062c0a52012-05-02 22:17:40 +00001174 case ISD::FMA: return visitFMA(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001175 case ISD::FDIV: return visitFDIV(N);
1176 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +00001177 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001178 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
1179 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
1180 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
1181 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
1182 case ISD::FP_ROUND: return visitFP_ROUND(N);
1183 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
1184 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
1185 case ISD::FNEG: return visitFNEG(N);
1186 case ISD::FABS: return visitFABS(N);
Owen Anderson7c626d32012-08-13 23:32:49 +00001187 case ISD::FFLOOR: return visitFFLOOR(N);
1188 case ISD::FCEIL: return visitFCEIL(N);
1189 case ISD::FTRUNC: return visitFTRUNC(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001190 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001191 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +00001192 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00001193 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +00001194 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +00001195 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +00001196 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
1197 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00001198 case ISD::EXTRACT_SUBVECTOR: return visitEXTRACT_SUBVECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +00001199 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001200 }
Dan Gohman475871a2008-07-27 21:46:04 +00001201 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001202}
1203
Dan Gohman475871a2008-07-27 21:46:04 +00001204SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00001205 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001206
1207 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +00001208 if (RV.getNode() == 0) {
Dan Gohman389079b2007-10-08 17:57:15 +00001209 assert(N->getOpcode() != ISD::DELETED_NODE &&
1210 "Node was deleted but visit returned NULL!");
1211
1212 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
1213 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
1214
1215 // Expose the DAG combiner to the target combiner impls.
Scott Michelfdc40a02009-02-17 22:15:04 +00001216 TargetLowering::DAGCombinerInfo
Nadav Rotem444b4bf2012-12-27 06:47:41 +00001217 DagCombineInfo(DAG, Level, false, this);
Dan Gohman389079b2007-10-08 17:57:15 +00001218
1219 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
1220 }
1221 }
1222
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001223 // If nothing happened still, try promoting the operation.
1224 if (RV.getNode() == 0) {
1225 switch (N->getOpcode()) {
1226 default: break;
1227 case ISD::ADD:
1228 case ISD::SUB:
1229 case ISD::MUL:
1230 case ISD::AND:
1231 case ISD::OR:
1232 case ISD::XOR:
1233 RV = PromoteIntBinOp(SDValue(N, 0));
1234 break;
1235 case ISD::SHL:
1236 case ISD::SRA:
1237 case ISD::SRL:
1238 RV = PromoteIntShiftOp(SDValue(N, 0));
1239 break;
1240 case ISD::SIGN_EXTEND:
1241 case ISD::ZERO_EXTEND:
1242 case ISD::ANY_EXTEND:
1243 RV = PromoteExtend(SDValue(N, 0));
1244 break;
1245 case ISD::LOAD:
1246 if (PromoteLoad(SDValue(N, 0)))
1247 RV = SDValue(N, 0);
1248 break;
1249 }
1250 }
1251
Scott Michelfdc40a02009-02-17 22:15:04 +00001252 // If N is a commutative binary node, try commuting it to enable more
Evan Cheng08b11732008-03-22 01:55:50 +00001253 // sdisel CSE.
Scott Michelfdc40a02009-02-17 22:15:04 +00001254 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +00001255 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
1256 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00001257 SDValue N0 = N->getOperand(0);
1258 SDValue N1 = N->getOperand(1);
Bill Wendling5c71acf2009-01-30 01:13:16 +00001259
Evan Cheng08b11732008-03-22 01:55:50 +00001260 // Constant operands are canonicalized to RHS.
1261 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001262 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +00001263 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
1264 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +00001265 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +00001266 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +00001267 }
1268 }
1269
Dan Gohman389079b2007-10-08 17:57:15 +00001270 return RV;
Scott Michelfdc40a02009-02-17 22:15:04 +00001271}
Dan Gohman389079b2007-10-08 17:57:15 +00001272
Chris Lattner6270f682006-10-08 22:57:01 +00001273/// getInputChainForNode - Given a node, return its input chain if it has one,
1274/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +00001275static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001276 if (unsigned NumOps = N->getNumOperands()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001277 if (N->getOperand(0).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001278 return N->getOperand(0);
Stephen Linb4940152013-07-09 00:44:49 +00001279 if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001280 return N->getOperand(NumOps-1);
1281 for (unsigned i = 1; i < NumOps-1; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +00001282 if (N->getOperand(i).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001283 return N->getOperand(i);
1284 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001285 return SDValue();
Chris Lattner6270f682006-10-08 22:57:01 +00001286}
1287
Dan Gohman475871a2008-07-27 21:46:04 +00001288SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001289 // If N has two operands, where one has an input chain equal to the other,
1290 // the 'other' chain is redundant.
1291 if (N->getNumOperands() == 2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001292 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +00001293 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001294 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +00001295 return N->getOperand(1);
1296 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001297
Chris Lattnerc76d4412007-05-16 06:37:59 +00001298 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +00001299 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001300 SmallPtrSet<SDNode*, 16> SeenOps;
Chris Lattnerc76d4412007-05-16 06:37:59 +00001301 bool Changed = false; // If we should replace this token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001302
Jim Laskey6ff23e52006-10-04 16:53:27 +00001303 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00001304 TFs.push_back(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001305
Jim Laskey71382342006-10-07 23:37:56 +00001306 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +00001307 // encountered.
1308 for (unsigned i = 0; i < TFs.size(); ++i) {
1309 SDNode *TF = TFs[i];
Scott Michelfdc40a02009-02-17 22:15:04 +00001310
Jim Laskey6ff23e52006-10-04 16:53:27 +00001311 // Check each of the operands.
1312 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001313 SDValue Op = TF->getOperand(i);
Scott Michelfdc40a02009-02-17 22:15:04 +00001314
Jim Laskey6ff23e52006-10-04 16:53:27 +00001315 switch (Op.getOpcode()) {
1316 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +00001317 // Entry tokens don't need to be added to the list. They are
1318 // rededundant.
1319 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001320 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001321
Jim Laskey6ff23e52006-10-04 16:53:27 +00001322 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +00001323 if (Op.hasOneUse() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001324 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001325 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +00001326 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001327 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +00001328 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001329 Changed = true;
1330 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001331 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001332 // Fall thru
Scott Michelfdc40a02009-02-17 22:15:04 +00001333
Jim Laskey6ff23e52006-10-04 16:53:27 +00001334 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +00001335 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +00001336 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +00001337 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +00001338 else
1339 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001340 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001341 }
1342 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001343 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001344
Dan Gohman475871a2008-07-27 21:46:04 +00001345 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001346
1347 // If we've change things around then replace token factor.
1348 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +00001349 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001350 // The entry token is the only possible outcome.
1351 Result = DAG.getEntryNode();
1352 } else {
1353 // New and improved token factor.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001354 Result = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson825b72b2009-08-11 20:47:22 +00001355 MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +00001356 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001357
Jim Laskey274062c2006-10-13 23:32:28 +00001358 // Don't add users to work list.
1359 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +00001360 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001361
Jim Laskey6ff23e52006-10-04 16:53:27 +00001362 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001363}
1364
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001365/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +00001366SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001367 WorkListRemover DeadNodes(*this);
Dan Gohman00edf392009-08-10 23:43:19 +00001368 // Replacing results may cause a different MERGE_VALUES to suddenly
1369 // be CSE'd with N, and carry its uses with it. Iterate until no
1370 // uses remain, to ensure that the node can be safely deleted.
Pete Cooper3affd9e2012-06-20 19:35:43 +00001371 // First add the users of this node to the work list so that they
1372 // can be tried again once they have new operands.
1373 AddUsersToWorkList(N);
Dan Gohman00edf392009-08-10 23:43:19 +00001374 do {
1375 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001376 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i));
Dan Gohman00edf392009-08-10 23:43:19 +00001377 } while (!N->use_empty());
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001378 removeFromWorkList(N);
1379 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001380 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001381}
1382
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001383static
Andrew Trickac6d9be2013-05-25 02:42:55 +00001384SDValue combineShlAddConstant(SDLoc DL, SDValue N0, SDValue N1,
Bill Wendlingd69c3142009-01-30 02:23:43 +00001385 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001386 EVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001387 SDValue N00 = N0.getOperand(0);
1388 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001389 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Bill Wendlingd69c3142009-01-30 02:23:43 +00001390
Gabor Greifba36cb52008-08-28 21:40:38 +00001391 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001392 isa<ConstantSDNode>(N00.getOperand(1))) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001393 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Andrew Trickac6d9be2013-05-25 02:42:55 +00001394 N0 = DAG.getNode(ISD::ADD, SDLoc(N0), VT,
1395 DAG.getNode(ISD::SHL, SDLoc(N00), VT,
Bill Wendlingd69c3142009-01-30 02:23:43 +00001396 N00.getOperand(0), N01),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001397 DAG.getNode(ISD::SHL, SDLoc(N01), VT,
Bill Wendlingd69c3142009-01-30 02:23:43 +00001398 N00.getOperand(1), N01));
1399 return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001400 }
Bill Wendlingd69c3142009-01-30 02:23:43 +00001401
Dan Gohman475871a2008-07-27 21:46:04 +00001402 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001403}
1404
Dan Gohman475871a2008-07-27 21:46:04 +00001405SDValue DAGCombiner::visitADD(SDNode *N) {
1406 SDValue N0 = N->getOperand(0);
1407 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001408 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1409 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001410 EVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00001411
1412 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001413 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001414 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001415 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper48b509c2012-12-10 08:12:29 +00001416
1417 // fold (add x, 0) -> x, vector edition
1418 if (ISD::isBuildVectorAllZeros(N1.getNode()))
1419 return N0;
1420 if (ISD::isBuildVectorAllZeros(N0.getNode()))
1421 return N1;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001422 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001423
Dan Gohman613e0d82007-07-03 14:03:57 +00001424 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001425 if (N0.getOpcode() == ISD::UNDEF)
1426 return N0;
1427 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001428 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001429 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001430 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001431 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001432 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001433 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001434 return DAG.getNode(ISD::ADD, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001435 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001436 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001437 return N0;
Dan Gohman6520e202008-10-18 02:06:02 +00001438 // fold (add Sym, c) -> Sym+c
1439 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001440 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman6520e202008-10-18 02:06:02 +00001441 GA->getOpcode() == ISD::GlobalAddress)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001442 return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001443 GA->getOffset() +
1444 (uint64_t)N1C->getSExtValue());
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001445 // fold ((c1-A)+c2) -> (c1+c2)-A
1446 if (N1C && N0.getOpcode() == ISD::SUB)
1447 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001448 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Dan Gohman002e5d02008-03-13 22:13:53 +00001449 DAG.getConstant(N1C->getAPIntValue()+
1450 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001451 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +00001452 // reassociate add
Andrew Trickac6d9be2013-05-25 02:42:55 +00001453 SDValue RADD = ReassociateOps(ISD::ADD, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001454 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001455 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001456 // fold ((0-A) + B) -> B-A
1457 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1458 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001459 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001460 // fold (A + (0-B)) -> A-B
1461 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1462 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001463 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001464 // fold (A+(B-A)) -> B
1465 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001466 return N1.getOperand(0);
Dale Johannesen56eca912008-11-27 00:43:21 +00001467 // fold ((B-A)+A) -> B
1468 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1469 return N0.getOperand(0);
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001470 // fold (A+(B-(A+C))) to (B-C)
1471 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001472 N0 == N1.getOperand(1).getOperand(0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001473 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001474 N1.getOperand(1).getOperand(1));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001475 // fold (A+(B-(C+A))) to (B-C)
1476 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001477 N0 == N1.getOperand(1).getOperand(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001478 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001479 N1.getOperand(1).getOperand(0));
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001480 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesen34d79852008-12-02 18:40:40 +00001481 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1482 N1.getOperand(0).getOpcode() == ISD::SUB &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001483 N0 == N1.getOperand(0).getOperand(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001484 return DAG.getNode(N1.getOpcode(), SDLoc(N), VT,
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001485 N1.getOperand(0).getOperand(0), N1.getOperand(1));
Dale Johannesen34d79852008-12-02 18:40:40 +00001486
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001487 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1488 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1489 SDValue N00 = N0.getOperand(0);
1490 SDValue N01 = N0.getOperand(1);
1491 SDValue N10 = N1.getOperand(0);
1492 SDValue N11 = N1.getOperand(1);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001493
1494 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001495 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
1496 DAG.getNode(ISD::ADD, SDLoc(N0), VT, N00, N10),
1497 DAG.getNode(ISD::ADD, SDLoc(N1), VT, N01, N11));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001498 }
Chris Lattner947c2892006-03-13 06:51:27 +00001499
Dan Gohman475871a2008-07-27 21:46:04 +00001500 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1501 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001502
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001503 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001504 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001505 APInt LHSZero, LHSOne;
1506 APInt RHSZero, RHSOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001507 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001508
Dan Gohman948d8ea2008-02-20 16:33:30 +00001509 if (LHSZero.getBoolValue()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001510 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001511
Chris Lattner947c2892006-03-13 06:51:27 +00001512 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1513 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001514 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001515 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N1);
Chris Lattner947c2892006-03-13 06:51:27 +00001516 }
1517 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001518
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001519 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001520 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001521 SDValue Result = combineShlAddConstant(SDLoc(N), N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001522 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001523 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001524 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001525 SDValue Result = combineShlAddConstant(SDLoc(N), N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001526 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001527 }
1528
Dan Gohmancd9e1552010-01-19 23:30:49 +00001529 // fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n))
1530 if (N1.getOpcode() == ISD::SHL &&
1531 N1.getOperand(0).getOpcode() == ISD::SUB)
1532 if (ConstantSDNode *C =
1533 dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(0)))
1534 if (C->getAPIntValue() == 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001535 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N0,
1536 DAG.getNode(ISD::SHL, SDLoc(N), VT,
Dan Gohmancd9e1552010-01-19 23:30:49 +00001537 N1.getOperand(0).getOperand(1),
1538 N1.getOperand(1)));
1539 if (N0.getOpcode() == ISD::SHL &&
1540 N0.getOperand(0).getOpcode() == ISD::SUB)
1541 if (ConstantSDNode *C =
1542 dyn_cast<ConstantSDNode>(N0.getOperand(0).getOperand(0)))
1543 if (C->getAPIntValue() == 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001544 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1,
1545 DAG.getNode(ISD::SHL, SDLoc(N), VT,
Dan Gohmancd9e1552010-01-19 23:30:49 +00001546 N0.getOperand(0).getOperand(1),
1547 N0.getOperand(1)));
1548
Owen Andersonbc146b02010-09-21 20:42:50 +00001549 if (N1.getOpcode() == ISD::AND) {
1550 SDValue AndOp0 = N1.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001551 ConstantSDNode *AndOp1 = dyn_cast<ConstantSDNode>(N1->getOperand(1));
Owen Andersonbc146b02010-09-21 20:42:50 +00001552 unsigned NumSignBits = DAG.ComputeNumSignBits(AndOp0);
1553 unsigned DestBits = VT.getScalarType().getSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001554
Owen Andersonbc146b02010-09-21 20:42:50 +00001555 // (add z, (and (sbbl x, x), 1)) -> (sub z, (sbbl x, x))
1556 // and similar xforms where the inner op is either ~0 or 0.
1557 if (NumSignBits == DestBits && AndOp1 && AndOp1->isOne()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001558 SDLoc DL(N);
Owen Andersonbc146b02010-09-21 20:42:50 +00001559 return DAG.getNode(ISD::SUB, DL, VT, N->getOperand(0), AndOp0);
1560 }
1561 }
1562
Benjamin Kramerf50125e2010-12-22 23:17:45 +00001563 // add (sext i1), X -> sub X, (zext i1)
1564 if (N0.getOpcode() == ISD::SIGN_EXTEND &&
1565 N0.getOperand(0).getValueType() == MVT::i1 &&
1566 !TLI.isOperationLegal(ISD::SIGN_EXTEND, MVT::i1)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001567 SDLoc DL(N);
Benjamin Kramerf50125e2010-12-22 23:17:45 +00001568 SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0));
1569 return DAG.getNode(ISD::SUB, DL, VT, N1, ZExt);
1570 }
1571
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001572 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001573}
1574
Dan Gohman475871a2008-07-27 21:46:04 +00001575SDValue DAGCombiner::visitADDC(SDNode *N) {
1576 SDValue N0 = N->getOperand(0);
1577 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001578 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1579 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001580 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001581
Chris Lattner91153682007-03-04 20:03:15 +00001582 // If the flag result is dead, turn this into an ADD.
Craig Topper704e1a02012-01-07 18:31:09 +00001583 if (!N->hasAnyUseOfValue(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001584 return CombineTo(N, DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001585 DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001586 SDLoc(N), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001587
Chris Lattner91153682007-03-04 20:03:15 +00001588 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001589 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001590 return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N1, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001591
Chris Lattnerb6541762007-03-04 20:40:38 +00001592 // fold (addc x, 0) -> x + no carry out
1593 if (N1C && N1C->isNullValue())
Dale Johannesen874ae252009-06-02 03:12:52 +00001594 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001595 SDLoc(N), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001596
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001597 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001598 APInt LHSZero, LHSOne;
1599 APInt RHSZero, RHSOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001600 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendling14036c02009-01-30 02:38:00 +00001601
Dan Gohman948d8ea2008-02-20 16:33:30 +00001602 if (LHSZero.getBoolValue()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001603 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001604
Chris Lattnerb6541762007-03-04 20:40:38 +00001605 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1606 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001607 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001608 return CombineTo(N, DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001609 DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001610 SDLoc(N), MVT::Glue));
Chris Lattnerb6541762007-03-04 20:40:38 +00001611 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001612
Dan Gohman475871a2008-07-27 21:46:04 +00001613 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001614}
1615
Dan Gohman475871a2008-07-27 21:46:04 +00001616SDValue DAGCombiner::visitADDE(SDNode *N) {
1617 SDValue N0 = N->getOperand(0);
1618 SDValue N1 = N->getOperand(1);
1619 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001620 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1621 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001622
Chris Lattner91153682007-03-04 20:03:15 +00001623 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001624 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001625 return DAG.getNode(ISD::ADDE, SDLoc(N), N->getVTList(),
Bill Wendling14036c02009-01-30 02:38:00 +00001626 N1, N0, CarryIn);
Scott Michelfdc40a02009-02-17 22:15:04 +00001627
Chris Lattnerb6541762007-03-04 20:40:38 +00001628 // fold (adde x, y, false) -> (addc x, y)
Dale Johannesen874ae252009-06-02 03:12:52 +00001629 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001630 return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001631
Dan Gohman475871a2008-07-27 21:46:04 +00001632 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001633}
1634
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001635// Since it may not be valid to emit a fold to zero for vector initializers
1636// check if we can before folding.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001637static SDValue tryFoldToZero(SDLoc DL, const TargetLowering &TLI, EVT VT,
Hal Finkelbd6f1f62013-07-09 17:02:45 +00001638 SelectionDAG &DAG,
1639 bool LegalOperations, bool LegalTypes) {
Stephen Linb4940152013-07-09 00:44:49 +00001640 if (!VT.isVector())
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001641 return DAG.getConstant(0, VT);
Dan Gohman71dc7c92011-05-17 22:20:36 +00001642 if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001643 // Produce a vector of zeros.
Hal Finkelbd6f1f62013-07-09 17:02:45 +00001644 EVT ElemTy = VT.getVectorElementType();
1645 if (LegalTypes && TLI.getTypeAction(*DAG.getContext(), ElemTy) ==
1646 TargetLowering::TypePromoteInteger)
1647 ElemTy = TLI.getTypeToTransformTo(*DAG.getContext(), ElemTy);
1648 assert((!LegalTypes || TLI.isTypeLegal(ElemTy)) &&
1649 "Type for zero vector elements is not legal");
1650 SDValue El = DAG.getConstant(0, ElemTy);
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001651 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
1652 return DAG.getNode(ISD::BUILD_VECTOR, DL, VT,
1653 &Ops[0], Ops.size());
1654 }
1655 return SDValue();
1656}
1657
Dan Gohman475871a2008-07-27 21:46:04 +00001658SDValue DAGCombiner::visitSUB(SDNode *N) {
1659 SDValue N0 = N->getOperand(0);
1660 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001661 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1662 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Eric Christopher7332e6e2011-07-14 01:12:15 +00001663 ConstantSDNode *N1C1 = N1.getOpcode() != ISD::ADD ? 0 :
1664 dyn_cast<ConstantSDNode>(N1.getOperand(1).getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001665 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001666
Dan Gohman7f321562007-06-25 16:23:39 +00001667 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001668 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001669 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001670 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper48b509c2012-12-10 08:12:29 +00001671
1672 // fold (sub x, 0) -> x, vector edition
1673 if (ISD::isBuildVectorAllZeros(N1.getNode()))
1674 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001675 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001676
Chris Lattner854077d2005-10-17 01:07:11 +00001677 // fold (sub x, x) -> 0
Eric Christopher169e1552011-02-16 01:10:03 +00001678 // FIXME: Refactor this and xor and other similar operations together.
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001679 if (N0 == N1)
Hal Finkelbd6f1f62013-07-09 17:02:45 +00001680 return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations, LegalTypes);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001681 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001682 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001683 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattner05b57432005-10-11 06:07:15 +00001684 // fold (sub x, c) -> (add x, -c)
1685 if (N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001686 return DAG.getNode(ISD::ADD, SDLoc(N), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001687 DAG.getConstant(-N1C->getAPIntValue(), VT));
Evan Cheng1ad0e8b2010-01-18 21:38:44 +00001688 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1)
1689 if (N0C && N0C->isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001690 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0);
Benjamin Kramer2c94b422011-01-29 12:34:05 +00001691 // fold A-(A-B) -> B
1692 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(0))
1693 return N1.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001694 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001695 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001696 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001697 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001698 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Scott Michelfdc40a02009-02-17 22:15:04 +00001699 return N0.getOperand(0);
Eric Christopher7332e6e2011-07-14 01:12:15 +00001700 // fold C2-(A+C1) -> (C2-C1)-A
1701 if (N1.getOpcode() == ISD::ADD && N0C && N1C1) {
Nadav Rotem6dfabb62012-09-20 08:53:31 +00001702 SDValue NewC = DAG.getConstant(N0C->getAPIntValue() - N1C1->getAPIntValue(),
1703 VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001704 return DAG.getNode(ISD::SUB, SDLoc(N), VT, NewC,
Bill Wendling96cb1122012-07-19 00:04:14 +00001705 N1.getOperand(0));
Eric Christopher7332e6e2011-07-14 01:12:15 +00001706 }
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001707 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001708 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001709 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1710 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001711 N0.getOperand(1).getOperand(0) == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001712 return DAG.getNode(N0.getOperand(1).getOpcode(), SDLoc(N), VT,
Bill Wendlingb0702e02009-01-30 02:42:10 +00001713 N0.getOperand(0), N0.getOperand(1).getOperand(1));
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001714 // fold ((A+(C+B))-B) -> A+C
1715 if (N0.getOpcode() == ISD::ADD &&
1716 N0.getOperand(1).getOpcode() == ISD::ADD &&
1717 N0.getOperand(1).getOperand(1) == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001718 return DAG.getNode(ISD::ADD, SDLoc(N), VT,
Bill Wendlingb0702e02009-01-30 02:42:10 +00001719 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dale Johannesen58e39b02008-12-23 01:59:54 +00001720 // fold ((A-(B-C))-C) -> A-B
1721 if (N0.getOpcode() == ISD::SUB &&
1722 N0.getOperand(1).getOpcode() == ISD::SUB &&
1723 N0.getOperand(1).getOperand(1) == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001724 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendlingb0702e02009-01-30 02:42:10 +00001725 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Bill Wendlingb0702e02009-01-30 02:42:10 +00001726
Dan Gohman613e0d82007-07-03 14:03:57 +00001727 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001728 if (N0.getOpcode() == ISD::UNDEF)
1729 return N0;
1730 if (N1.getOpcode() == ISD::UNDEF)
1731 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001732
Dan Gohman6520e202008-10-18 02:06:02 +00001733 // If the relocation model supports it, consider symbol offsets.
1734 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001735 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman6520e202008-10-18 02:06:02 +00001736 // fold (sub Sym, c) -> Sym-c
1737 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001738 return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001739 GA->getOffset() -
1740 (uint64_t)N1C->getSExtValue());
1741 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1742 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1743 if (GA->getGlobal() == GB->getGlobal())
1744 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1745 VT);
1746 }
1747
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001748 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001749}
1750
Craig Toppercc274522012-01-07 09:06:39 +00001751SDValue DAGCombiner::visitSUBC(SDNode *N) {
1752 SDValue N0 = N->getOperand(0);
1753 SDValue N1 = N->getOperand(1);
1754 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1755 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1756 EVT VT = N0.getValueType();
1757
1758 // If the flag result is dead, turn this into an SUB.
Craig Topper704e1a02012-01-07 18:31:09 +00001759 if (!N->hasAnyUseOfValue(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001760 return CombineTo(N, DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, N1),
1761 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001762 MVT::Glue));
1763
1764 // fold (subc x, x) -> 0 + no borrow
1765 if (N0 == N1)
1766 return CombineTo(N, DAG.getConstant(0, VT),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001767 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001768 MVT::Glue));
1769
1770 // fold (subc x, 0) -> x + no borrow
1771 if (N1C && N1C->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001772 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001773 MVT::Glue));
1774
1775 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) + no borrow
1776 if (N0C && N0C->isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001777 return CombineTo(N, DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0),
1778 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001779 MVT::Glue));
1780
1781 return SDValue();
1782}
1783
1784SDValue DAGCombiner::visitSUBE(SDNode *N) {
1785 SDValue N0 = N->getOperand(0);
1786 SDValue N1 = N->getOperand(1);
1787 SDValue CarryIn = N->getOperand(2);
1788
1789 // fold (sube x, y, false) -> (subc x, y)
1790 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001791 return DAG.getNode(ISD::SUBC, SDLoc(N), N->getVTList(), N0, N1);
Craig Toppercc274522012-01-07 09:06:39 +00001792
1793 return SDValue();
1794}
1795
Jack Carteradbd3ae2013-10-17 01:34:33 +00001796/// isConstantSplatVector - Returns true if N is a BUILD_VECTOR node whose
1797/// elements are all the same constant or undefined.
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001798static bool isConstantSplatVector(SDNode *N, APInt& SplatValue) {
1799 BuildVectorSDNode *C = dyn_cast<BuildVectorSDNode>(N);
1800 if (!C)
1801 return false;
1802
1803 APInt SplatUndef;
1804 unsigned SplatBitSize;
1805 bool HasAnyUndefs;
1806 EVT EltVT = N->getValueType(0).getVectorElementType();
1807 return (C->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
1808 HasAnyUndefs) &&
1809 EltVT.getSizeInBits() >= SplatBitSize);
1810}
1811
Dan Gohman475871a2008-07-27 21:46:04 +00001812SDValue DAGCombiner::visitMUL(SDNode *N) {
1813 SDValue N0 = N->getOperand(0);
1814 SDValue N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00001815 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001816
Dan Gohman613e0d82007-07-03 14:03:57 +00001817 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001818 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001819 return DAG.getConstant(0, VT);
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001820
1821 bool N0IsConst = false;
1822 bool N1IsConst = false;
1823 APInt ConstValue0, ConstValue1;
1824 // fold vector ops
1825 if (VT.isVector()) {
1826 SDValue FoldedVOp = SimplifyVBinOp(N);
1827 if (FoldedVOp.getNode()) return FoldedVOp;
1828
1829 N0IsConst = isConstantSplatVector(N0.getNode(), ConstValue0);
1830 N1IsConst = isConstantSplatVector(N1.getNode(), ConstValue1);
1831 } else {
1832 N0IsConst = dyn_cast<ConstantSDNode>(N0) != 0;
Jack Carteradbd3ae2013-10-17 01:34:33 +00001833 ConstValue0 = N0IsConst ? (dyn_cast<ConstantSDNode>(N0))->getAPIntValue()
1834 : APInt();
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001835 N1IsConst = dyn_cast<ConstantSDNode>(N1) != 0;
Jack Carteradbd3ae2013-10-17 01:34:33 +00001836 ConstValue1 = N1IsConst ? (dyn_cast<ConstantSDNode>(N1))->getAPIntValue()
1837 : APInt();
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001838 }
1839
Nate Begeman1d4d4142005-09-01 00:19:25 +00001840 // fold (mul c1, c2) -> c1*c2
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001841 if (N0IsConst && N1IsConst)
1842 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0.getNode(), N1.getNode());
1843
Nate Begeman99801192005-09-07 23:25:52 +00001844 // canonicalize constant to RHS
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001845 if (N0IsConst && !N1IsConst)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001846 return DAG.getNode(ISD::MUL, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001847 // fold (mul x, 0) -> 0
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001848 if (N1IsConst && ConstValue1 == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001849 return N1;
Benjamin Kramer530d09a2013-09-19 13:28:20 +00001850 // We require a splat of the entire scalar bit width for non-contiguous
1851 // bit patterns.
1852 bool IsFullSplat =
1853 ConstValue1.getBitWidth() == VT.getScalarType().getSizeInBits();
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001854 // fold (mul x, 1) -> x
Benjamin Kramer530d09a2013-09-19 13:28:20 +00001855 if (N1IsConst && ConstValue1 == 1 && IsFullSplat)
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001856 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001857 // fold (mul x, -1) -> 0-x
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001858 if (N1IsConst && ConstValue1.isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001859 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001860 DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001861 // fold (mul x, (1 << c)) -> x << c
Benjamin Kramer530d09a2013-09-19 13:28:20 +00001862 if (N1IsConst && ConstValue1.isPowerOf2() && IsFullSplat)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001863 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001864 DAG.getConstant(ConstValue1.logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00001865 getShiftAmountTy(N0.getValueType())));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001866 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Benjamin Kramer530d09a2013-09-19 13:28:20 +00001867 if (N1IsConst && (-ConstValue1).isPowerOf2() && IsFullSplat) {
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001868 unsigned Log2Val = (-ConstValue1).logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00001869 // FIXME: If the input is something that is easily negated (e.g. a
Chris Lattner3e6099b2005-10-30 06:41:49 +00001870 // single-use add), we should put the negate there.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001871 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001872 DAG.getConstant(0, VT),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001873 DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
Owen Anderson95771af2011-02-25 21:41:48 +00001874 DAG.getConstant(Log2Val,
1875 getShiftAmountTy(N0.getValueType()))));
Chris Lattner66b8bc32009-03-09 20:22:18 +00001876 }
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001877
1878 APInt Val;
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001879 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
Stephen Lin155615d2013-07-08 00:37:03 +00001880 if (N1IsConst && N0.getOpcode() == ISD::SHL &&
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001881 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1882 isa<ConstantSDNode>(N0.getOperand(1)))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001883 SDValue C3 = DAG.getNode(ISD::SHL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001884 N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001885 AddToWorkList(C3.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00001886 return DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001887 N0.getOperand(0), C3);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001888 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001889
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001890 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1891 // use.
1892 {
Dan Gohman475871a2008-07-27 21:46:04 +00001893 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001894 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
Stephen Lin155615d2013-07-08 00:37:03 +00001895 if (N0.getOpcode() == ISD::SHL &&
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001896 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1897 isa<ConstantSDNode>(N0.getOperand(1))) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001898 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001899 Sh = N0; Y = N1;
Scott Michelfdc40a02009-02-17 22:15:04 +00001900 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001901 isa<ConstantSDNode>(N1.getOperand(1)) &&
1902 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001903 Sh = N1; Y = N0;
1904 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001905
Gabor Greifba36cb52008-08-28 21:40:38 +00001906 if (Sh.getNode()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001907 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001908 Sh.getOperand(0), Y);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001909 return DAG.getNode(ISD::SHL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001910 Mul, Sh.getOperand(1));
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001911 }
1912 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001913
Chris Lattnera1deca32006-03-04 23:33:26 +00001914 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001915 if (N1IsConst && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
1916 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1917 isa<ConstantSDNode>(N0.getOperand(1))))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001918 return DAG.getNode(ISD::ADD, SDLoc(N), VT,
1919 DAG.getNode(ISD::MUL, SDLoc(N0), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001920 N0.getOperand(0), N1),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001921 DAG.getNode(ISD::MUL, SDLoc(N1), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001922 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001923
Nate Begemancd4d58c2006-02-03 06:46:56 +00001924 // reassociate mul
Andrew Trickac6d9be2013-05-25 02:42:55 +00001925 SDValue RMUL = ReassociateOps(ISD::MUL, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001926 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001927 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001928
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001929 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001930}
1931
Dan Gohman475871a2008-07-27 21:46:04 +00001932SDValue DAGCombiner::visitSDIV(SDNode *N) {
1933 SDValue N0 = N->getOperand(0);
1934 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001935 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1936 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001937 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001938
Dan Gohman7f321562007-06-25 16:23:39 +00001939 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001940 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001941 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001942 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001943 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001944
Nate Begeman1d4d4142005-09-01 00:19:25 +00001945 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001946 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001947 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001948 // fold (sdiv X, 1) -> X
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001949 if (N1C && N1C->getAPIntValue() == 1LL)
Nate Begeman405e3ec2005-10-21 00:02:42 +00001950 return N0;
1951 // fold (sdiv X, -1) -> 0-X
1952 if (N1C && N1C->isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001953 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling944d34b2009-01-30 02:52:17 +00001954 DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001955 // If we know the sign bits of both operands are zero, strength reduce to a
1956 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001957 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001958 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001959 return DAG.getNode(ISD::UDIV, SDLoc(N), N1.getValueType(),
Bill Wendling944d34b2009-01-30 02:52:17 +00001960 N0, N1);
Chris Lattnerf32aac32008-01-27 23:32:17 +00001961 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001962 // fold (sdiv X, pow2) -> simple ops after legalize
Eli Friedman1c663fe2011-12-07 03:55:52 +00001963 if (N1C && !N1C->isNullValue() &&
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001964 (N1C->getAPIntValue().isPowerOf2() ||
1965 (-N1C->getAPIntValue()).isPowerOf2())) {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001966 // If dividing by powers of two is cheap, then don't perform the following
1967 // fold.
1968 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001969 return SDValue();
Bill Wendling944d34b2009-01-30 02:52:17 +00001970
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001971 unsigned lg2 = N1C->getAPIntValue().countTrailingZeros();
Bill Wendling944d34b2009-01-30 02:52:17 +00001972
Chris Lattner8f4880b2006-02-16 08:02:36 +00001973 // Splat the sign bit into the register
Andrew Trickac6d9be2013-05-25 02:42:55 +00001974 SDValue SGN = DAG.getNode(ISD::SRA, SDLoc(N), VT, N0,
Bill Wendling944d34b2009-01-30 02:52:17 +00001975 DAG.getConstant(VT.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00001976 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00001977 AddToWorkList(SGN.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001978
Chris Lattner8f4880b2006-02-16 08:02:36 +00001979 // Add (N0 < 0) ? abs2 - 1 : 0;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001980 SDValue SRL = DAG.getNode(ISD::SRL, SDLoc(N), VT, SGN,
Bill Wendling944d34b2009-01-30 02:52:17 +00001981 DAG.getConstant(VT.getSizeInBits() - lg2,
Owen Anderson95771af2011-02-25 21:41:48 +00001982 getShiftAmountTy(SGN.getValueType())));
Andrew Trickac6d9be2013-05-25 02:42:55 +00001983 SDValue ADD = DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001984 AddToWorkList(SRL.getNode());
1985 AddToWorkList(ADD.getNode()); // Divide by pow2
Andrew Trickac6d9be2013-05-25 02:42:55 +00001986 SDValue SRA = DAG.getNode(ISD::SRA, SDLoc(N), VT, ADD,
Owen Anderson95771af2011-02-25 21:41:48 +00001987 DAG.getConstant(lg2, getShiftAmountTy(ADD.getValueType())));
Bill Wendling944d34b2009-01-30 02:52:17 +00001988
Nate Begeman405e3ec2005-10-21 00:02:42 +00001989 // If we're dividing by a positive value, we're done. Otherwise, we must
1990 // negate the result.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001991 if (N1C->getAPIntValue().isNonNegative())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001992 return SRA;
Bill Wendling944d34b2009-01-30 02:52:17 +00001993
Gabor Greifba36cb52008-08-28 21:40:38 +00001994 AddToWorkList(SRA.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00001995 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling944d34b2009-01-30 02:52:17 +00001996 DAG.getConstant(0, VT), SRA);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001997 }
Bill Wendling944d34b2009-01-30 02:52:17 +00001998
Nate Begeman69575232005-10-20 02:15:44 +00001999 // if integer divide is expensive and we satisfy the requirements, emit an
2000 // alternate sequence.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00002001 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002002 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002003 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00002004 }
Dan Gohman7f321562007-06-25 16:23:39 +00002005
Dan Gohman613e0d82007-07-03 14:03:57 +00002006 // undef / X -> 0
2007 if (N0.getOpcode() == ISD::UNDEF)
2008 return DAG.getConstant(0, VT);
2009 // X / undef -> undef
2010 if (N1.getOpcode() == ISD::UNDEF)
2011 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002012
Dan Gohman475871a2008-07-27 21:46:04 +00002013 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002014}
2015
Dan Gohman475871a2008-07-27 21:46:04 +00002016SDValue DAGCombiner::visitUDIV(SDNode *N) {
2017 SDValue N0 = N->getOperand(0);
2018 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002019 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
2020 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00002021 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002022
Dan Gohman7f321562007-06-25 16:23:39 +00002023 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002024 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002025 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002026 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002027 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002028
Nate Begeman1d4d4142005-09-01 00:19:25 +00002029 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002030 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002031 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002032 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00002033 if (N1C && N1C->getAPIntValue().isPowerOf2())
Andrew Trickac6d9be2013-05-25 02:42:55 +00002034 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00002035 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00002036 getShiftAmountTy(N0.getValueType())));
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002037 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00002038 if (N1.getOpcode() == ISD::SHL) {
2039 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002040 if (SHC->getAPIntValue().isPowerOf2()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002041 EVT ADDVT = N1.getOperand(1).getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002042 SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N), ADDVT,
Bill Wendling07d85142009-01-30 02:55:25 +00002043 N1.getOperand(1),
2044 DAG.getConstant(SHC->getAPIntValue()
2045 .logBase2(),
2046 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002047 AddToWorkList(Add.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002048 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00002049 }
2050 }
2051 }
Nate Begeman69575232005-10-20 02:15:44 +00002052 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00002053 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002054 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002055 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00002056 }
Dan Gohman7f321562007-06-25 16:23:39 +00002057
Dan Gohman613e0d82007-07-03 14:03:57 +00002058 // undef / X -> 0
2059 if (N0.getOpcode() == ISD::UNDEF)
2060 return DAG.getConstant(0, VT);
2061 // X / undef -> undef
2062 if (N1.getOpcode() == ISD::UNDEF)
2063 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002064
Dan Gohman475871a2008-07-27 21:46:04 +00002065 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002066}
2067
Dan Gohman475871a2008-07-27 21:46:04 +00002068SDValue DAGCombiner::visitSREM(SDNode *N) {
2069 SDValue N0 = N->getOperand(0);
2070 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002071 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2072 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002073 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002074
Nate Begeman1d4d4142005-09-01 00:19:25 +00002075 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002076 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002077 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00002078 // If we know the sign bits of both operands are zero, strength reduce to a
2079 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00002080 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002081 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00002082 return DAG.getNode(ISD::UREM, SDLoc(N), VT, N0, N1);
Chris Lattneree339f42008-01-27 23:21:58 +00002083 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002084
Dan Gohman77003042007-11-26 23:46:11 +00002085 // If X/C can be simplified by the division-by-constant logic, lower
2086 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00002087 if (N1C && !N1C->isNullValue()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002088 SDValue Div = DAG.getNode(ISD::SDIV, SDLoc(N), VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002089 AddToWorkList(Div.getNode());
2090 SDValue OptimizedDiv = combine(Div.getNode());
2091 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002092 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002093 OptimizedDiv, N1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002094 SDValue Sub = DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00002095 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00002096 return Sub;
2097 }
Chris Lattner26d29902006-10-12 20:58:32 +00002098 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002099
Dan Gohman613e0d82007-07-03 14:03:57 +00002100 // undef % X -> 0
2101 if (N0.getOpcode() == ISD::UNDEF)
2102 return DAG.getConstant(0, VT);
2103 // X % undef -> undef
2104 if (N1.getOpcode() == ISD::UNDEF)
2105 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002106
Dan Gohman475871a2008-07-27 21:46:04 +00002107 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002108}
2109
Dan Gohman475871a2008-07-27 21:46:04 +00002110SDValue DAGCombiner::visitUREM(SDNode *N) {
2111 SDValue N0 = N->getOperand(0);
2112 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002113 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2114 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002115 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002116
Nate Begeman1d4d4142005-09-01 00:19:25 +00002117 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002118 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002119 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00002120 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002121 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
Andrew Trickac6d9be2013-05-25 02:42:55 +00002122 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00002123 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00002124 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
2125 if (N1.getOpcode() == ISD::SHL) {
2126 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002127 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002128 SDValue Add =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002129 DAG.getNode(ISD::ADD, SDLoc(N), VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002130 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00002131 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002132 AddToWorkList(Add.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002133 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, Add);
Nate Begemanc031e332006-02-05 07:36:48 +00002134 }
2135 }
2136 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002137
Dan Gohman77003042007-11-26 23:46:11 +00002138 // If X/C can be simplified by the division-by-constant logic, lower
2139 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00002140 if (N1C && !N1C->isNullValue()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002141 SDValue Div = DAG.getNode(ISD::UDIV, SDLoc(N), VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00002142 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00002143 SDValue OptimizedDiv = combine(Div.getNode());
2144 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002145 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002146 OptimizedDiv, N1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002147 SDValue Sub = DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00002148 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00002149 return Sub;
2150 }
Chris Lattner26d29902006-10-12 20:58:32 +00002151 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002152
Dan Gohman613e0d82007-07-03 14:03:57 +00002153 // undef % X -> 0
2154 if (N0.getOpcode() == ISD::UNDEF)
2155 return DAG.getConstant(0, VT);
2156 // X % undef -> undef
2157 if (N1.getOpcode() == ISD::UNDEF)
2158 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002159
Dan Gohman475871a2008-07-27 21:46:04 +00002160 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002161}
2162
Dan Gohman475871a2008-07-27 21:46:04 +00002163SDValue DAGCombiner::visitMULHS(SDNode *N) {
2164 SDValue N0 = N->getOperand(0);
2165 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002166 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002167 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002168 SDLoc DL(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00002169
Nate Begeman1d4d4142005-09-01 00:19:25 +00002170 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002171 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002172 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002173 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002174 if (N1C && N1C->getAPIntValue() == 1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002175 return DAG.getNode(ISD::SRA, SDLoc(N), N0.getValueType(), N0,
Bill Wendling326411d2009-01-30 03:00:18 +00002176 DAG.getConstant(N0.getValueType().getSizeInBits() - 1,
Owen Anderson95771af2011-02-25 21:41:48 +00002177 getShiftAmountTy(N0.getValueType())));
Dan Gohman613e0d82007-07-03 14:03:57 +00002178 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002179 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002180 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002181
Chris Lattnerde1c3602010-12-13 08:39:01 +00002182 // If the type twice as wide is legal, transform the mulhs to a wider multiply
2183 // plus a shift.
2184 if (VT.isSimple() && !VT.isVector()) {
2185 MVT Simple = VT.getSimpleVT();
2186 unsigned SimpleSize = Simple.getSizeInBits();
2187 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2188 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2189 N0 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N0);
2190 N1 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N1);
2191 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
Chris Lattner1a0fbe22010-12-15 05:51:39 +00002192 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002193 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002194 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2195 }
2196 }
Owen Anderson95771af2011-02-25 21:41:48 +00002197
Dan Gohman475871a2008-07-27 21:46:04 +00002198 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002199}
2200
Dan Gohman475871a2008-07-27 21:46:04 +00002201SDValue DAGCombiner::visitMULHU(SDNode *N) {
2202 SDValue N0 = N->getOperand(0);
2203 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002204 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002205 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002206 SDLoc DL(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00002207
Nate Begeman1d4d4142005-09-01 00:19:25 +00002208 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002209 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002210 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002211 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00002212 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002213 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00002214 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002215 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002216 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002217
Chris Lattnerde1c3602010-12-13 08:39:01 +00002218 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2219 // plus a shift.
2220 if (VT.isSimple() && !VT.isVector()) {
2221 MVT Simple = VT.getSimpleVT();
2222 unsigned SimpleSize = Simple.getSizeInBits();
2223 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2224 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2225 N0 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N0);
2226 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N1);
2227 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
2228 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002229 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002230 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2231 }
2232 }
Owen Anderson95771af2011-02-25 21:41:48 +00002233
Dan Gohman475871a2008-07-27 21:46:04 +00002234 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002235}
2236
Dan Gohman389079b2007-10-08 17:57:15 +00002237/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
2238/// compute two values. LoOp and HiOp give the opcodes for the two computations
2239/// that are being performed. Return true if a simplification was made.
2240///
Scott Michelfdc40a02009-02-17 22:15:04 +00002241SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Dan Gohman475871a2008-07-27 21:46:04 +00002242 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00002243 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00002244 bool HiExists = N->hasAnyUseOfValue(1);
2245 if (!HiExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002246 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002247 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002248 SDValue Res = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0),
Bill Wendling826d1142009-01-30 03:08:40 +00002249 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002250 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002251 }
2252
2253 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00002254 bool LoExists = N->hasAnyUseOfValue(0);
2255 if (!LoExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002256 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002257 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002258 SDValue Res = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1),
Bill Wendling826d1142009-01-30 03:08:40 +00002259 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002260 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002261 }
2262
Evan Cheng44711942007-11-08 09:25:29 +00002263 // If both halves are used, return as it is.
2264 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00002265 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00002266
2267 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00002268 if (LoExists) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002269 SDValue Lo = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0),
Bill Wendling826d1142009-01-30 03:08:40 +00002270 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002271 AddToWorkList(Lo.getNode());
2272 SDValue LoOpt = combine(Lo.getNode());
2273 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002274 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002275 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002276 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00002277 }
2278
Evan Cheng44711942007-11-08 09:25:29 +00002279 if (HiExists) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002280 SDValue Hi = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1),
Duncan Sands25cf2272008-11-24 14:53:14 +00002281 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002282 AddToWorkList(Hi.getNode());
2283 SDValue HiOpt = combine(Hi.getNode());
2284 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002285 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002286 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002287 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00002288 }
Bill Wendling826d1142009-01-30 03:08:40 +00002289
Dan Gohman475871a2008-07-27 21:46:04 +00002290 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002291}
2292
Dan Gohman475871a2008-07-27 21:46:04 +00002293SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
2294 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00002295 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002296
Chris Lattner33e77d32010-12-15 06:04:19 +00002297 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002298 SDLoc DL(N);
Chris Lattner33e77d32010-12-15 06:04:19 +00002299
2300 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2301 // plus a shift.
2302 if (VT.isSimple() && !VT.isVector()) {
2303 MVT Simple = VT.getSimpleVT();
2304 unsigned SimpleSize = Simple.getSizeInBits();
2305 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2306 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2307 SDValue Lo = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(0));
2308 SDValue Hi = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(1));
2309 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2310 // Compute the high part as N1.
2311 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002312 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002313 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2314 // Compute the low part as N0.
2315 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2316 return CombineTo(N, Lo, Hi);
2317 }
2318 }
Owen Anderson95771af2011-02-25 21:41:48 +00002319
Dan Gohman475871a2008-07-27 21:46:04 +00002320 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002321}
2322
Dan Gohman475871a2008-07-27 21:46:04 +00002323SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
2324 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00002325 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002326
Chris Lattner33e77d32010-12-15 06:04:19 +00002327 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002328 SDLoc DL(N);
Owen Anderson95771af2011-02-25 21:41:48 +00002329
Chris Lattner33e77d32010-12-15 06:04:19 +00002330 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2331 // plus a shift.
2332 if (VT.isSimple() && !VT.isVector()) {
2333 MVT Simple = VT.getSimpleVT();
2334 unsigned SimpleSize = Simple.getSizeInBits();
2335 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2336 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2337 SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(0));
2338 SDValue Hi = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(1));
2339 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2340 // Compute the high part as N1.
2341 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002342 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002343 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2344 // Compute the low part as N0.
2345 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2346 return CombineTo(N, Lo, Hi);
2347 }
2348 }
Owen Anderson95771af2011-02-25 21:41:48 +00002349
Dan Gohman475871a2008-07-27 21:46:04 +00002350 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002351}
2352
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002353SDValue DAGCombiner::visitSMULO(SDNode *N) {
2354 // (smulo x, 2) -> (saddo x, x)
2355 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2356 if (C2->getAPIntValue() == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002357 return DAG.getNode(ISD::SADDO, SDLoc(N), N->getVTList(),
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002358 N->getOperand(0), N->getOperand(0));
2359
2360 return SDValue();
2361}
2362
2363SDValue DAGCombiner::visitUMULO(SDNode *N) {
2364 // (umulo x, 2) -> (uaddo x, x)
2365 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2366 if (C2->getAPIntValue() == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002367 return DAG.getNode(ISD::UADDO, SDLoc(N), N->getVTList(),
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002368 N->getOperand(0), N->getOperand(0));
2369
2370 return SDValue();
2371}
2372
Dan Gohman475871a2008-07-27 21:46:04 +00002373SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
2374 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002375 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002376
Dan Gohman475871a2008-07-27 21:46:04 +00002377 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002378}
2379
Dan Gohman475871a2008-07-27 21:46:04 +00002380SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
2381 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002382 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002383
Dan Gohman475871a2008-07-27 21:46:04 +00002384 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002385}
2386
Chris Lattner35e5c142006-05-05 05:51:50 +00002387/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
2388/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00002389SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
2390 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00002391 EVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00002392 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
Scott Michelfdc40a02009-02-17 22:15:04 +00002393
Dan Gohmanff00a552010-01-14 03:08:49 +00002394 // Bail early if none of these transforms apply.
2395 if (N0.getNode()->getNumOperands() == 0) return SDValue();
2396
Chris Lattner540121f2006-05-05 06:31:05 +00002397 // For each of OP in AND/OR/XOR:
2398 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
2399 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
2400 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002401 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y)) (if trunc isn't free)
Nate Begeman93e0ed32009-12-03 07:11:29 +00002402 //
2403 // do not sink logical op inside of a vector extend, since it may combine
2404 // into a vsetcc.
Evan Chengd40d03e2010-01-06 19:38:29 +00002405 EVT Op0VT = N0.getOperand(0).getValueType();
2406 if ((N0.getOpcode() == ISD::ZERO_EXTEND ||
Dan Gohman97121ba2009-04-08 00:15:30 +00002407 N0.getOpcode() == ISD::SIGN_EXTEND ||
Evan Chenge5b51ac2010-04-17 06:13:15 +00002408 // Avoid infinite looping with PromoteIntBinOp.
2409 (N0.getOpcode() == ISD::ANY_EXTEND &&
2410 (!LegalTypes || TLI.isTypeDesirableForOp(N->getOpcode(), Op0VT))) ||
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002411 (N0.getOpcode() == ISD::TRUNCATE &&
2412 (!TLI.isZExtFree(VT, Op0VT) ||
2413 !TLI.isTruncateFree(Op0VT, VT)) &&
2414 TLI.isTypeLegal(Op0VT))) &&
Nate Begeman93e0ed32009-12-03 07:11:29 +00002415 !VT.isVector() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00002416 Op0VT == N1.getOperand(0).getValueType() &&
2417 (!LegalOperations || TLI.isOperationLegal(N->getOpcode(), Op0VT))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002418 SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0),
Bill Wendlingb74c8672009-01-30 19:25:47 +00002419 N0.getOperand(0).getValueType(),
2420 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002421 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002422 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00002423 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002424
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002425 // For each of OP in SHL/SRL/SRA/AND...
2426 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
2427 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
2428 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00002429 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002430 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00002431 N0.getOperand(1) == N1.getOperand(1)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002432 SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0),
Bill Wendlingb74c8672009-01-30 19:25:47 +00002433 N0.getOperand(0).getValueType(),
2434 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002435 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002436 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Bill Wendlingb74c8672009-01-30 19:25:47 +00002437 ORNode, N0.getOperand(1));
Chris Lattner35e5c142006-05-05 05:51:50 +00002438 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002439
Nadav Rotem4ac90812012-04-01 19:31:22 +00002440 // Simplify xor/and/or (bitcast(A), bitcast(B)) -> bitcast(op (A,B))
2441 // Only perform this optimization after type legalization and before
2442 // LegalizeVectorOprs. LegalizeVectorOprs promotes vector operations by
2443 // adding bitcasts. For example (xor v4i32) is promoted to (v2i64), and
2444 // we don't want to undo this promotion.
2445 // We also handle SCALAR_TO_VECTOR because xor/or/and operations are cheaper
2446 // on scalars.
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002447 if ((N0.getOpcode() == ISD::BITCAST ||
2448 N0.getOpcode() == ISD::SCALAR_TO_VECTOR) &&
2449 Level == AfterLegalizeTypes) {
Nadav Rotem4ac90812012-04-01 19:31:22 +00002450 SDValue In0 = N0.getOperand(0);
2451 SDValue In1 = N1.getOperand(0);
2452 EVT In0Ty = In0.getValueType();
2453 EVT In1Ty = In1.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002454 SDLoc DL(N);
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002455 // If both incoming values are integers, and the original types are the
2456 // same.
Nadav Rotem4ac90812012-04-01 19:31:22 +00002457 if (In0Ty.isInteger() && In1Ty.isInteger() && In0Ty == In1Ty) {
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002458 SDValue Op = DAG.getNode(N->getOpcode(), DL, In0Ty, In0, In1);
2459 SDValue BC = DAG.getNode(N0.getOpcode(), DL, VT, Op);
Nadav Rotem4ac90812012-04-01 19:31:22 +00002460 AddToWorkList(Op.getNode());
2461 return BC;
2462 }
2463 }
2464
2465 // Xor/and/or are indifferent to the swizzle operation (shuffle of one value).
2466 // Simplify xor/and/or (shuff(A), shuff(B)) -> shuff(op (A,B))
2467 // If both shuffles use the same mask, and both shuffle within a single
2468 // vector, then it is worthwhile to move the swizzle after the operation.
2469 // The type-legalizer generates this pattern when loading illegal
2470 // vector types from memory. In many cases this allows additional shuffle
2471 // optimizations.
Craig Topperf9204232012-04-09 07:19:09 +00002472 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
2473 N0.getOperand(1).getOpcode() == ISD::UNDEF &&
2474 N1.getOperand(1).getOpcode() == ISD::UNDEF) {
Nadav Rotem4ac90812012-04-01 19:31:22 +00002475 ShuffleVectorSDNode *SVN0 = cast<ShuffleVectorSDNode>(N0);
2476 ShuffleVectorSDNode *SVN1 = cast<ShuffleVectorSDNode>(N1);
Craig Topperf9204232012-04-09 07:19:09 +00002477
2478 assert(N0.getOperand(0).getValueType() == N1.getOperand(1).getValueType() &&
2479 "Inputs to shuffles are not the same type");
Nadav Rotem4ac90812012-04-01 19:31:22 +00002480
2481 unsigned NumElts = VT.getVectorNumElements();
Nadav Rotem4ac90812012-04-01 19:31:22 +00002482
2483 // Check that both shuffles use the same mask. The masks are known to be of
2484 // the same length because the result vector type is the same.
2485 bool SameMask = true;
2486 for (unsigned i = 0; i != NumElts; ++i) {
2487 int Idx0 = SVN0->getMaskElt(i);
2488 int Idx1 = SVN1->getMaskElt(i);
2489 if (Idx0 != Idx1) {
2490 SameMask = false;
2491 break;
2492 }
2493 }
2494
Craig Topperf9204232012-04-09 07:19:09 +00002495 if (SameMask) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002496 SDValue Op = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Craig Topperf9204232012-04-09 07:19:09 +00002497 N0.getOperand(0), N1.getOperand(0));
Nadav Rotem4ac90812012-04-01 19:31:22 +00002498 AddToWorkList(Op.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002499 return DAG.getVectorShuffle(VT, SDLoc(N), Op,
Craig Topperf9204232012-04-09 07:19:09 +00002500 DAG.getUNDEF(VT), &SVN0->getMask()[0]);
Nadav Rotem4ac90812012-04-01 19:31:22 +00002501 }
2502 }
Craig Topperf9204232012-04-09 07:19:09 +00002503
Dan Gohman475871a2008-07-27 21:46:04 +00002504 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00002505}
2506
Dan Gohman475871a2008-07-27 21:46:04 +00002507SDValue DAGCombiner::visitAND(SDNode *N) {
2508 SDValue N0 = N->getOperand(0);
2509 SDValue N1 = N->getOperand(1);
2510 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00002511 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2512 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002513 EVT VT = N1.getValueType();
Dan Gohman6900a392010-03-04 00:23:16 +00002514 unsigned BitWidth = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002515
Dan Gohman7f321562007-06-25 16:23:39 +00002516 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002517 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002518 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002519 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00002520
2521 // fold (and x, 0) -> 0, vector edition
2522 if (ISD::isBuildVectorAllZeros(N0.getNode()))
2523 return N0;
2524 if (ISD::isBuildVectorAllZeros(N1.getNode()))
2525 return N1;
2526
2527 // fold (and x, -1) -> x, vector edition
2528 if (ISD::isBuildVectorAllOnes(N0.getNode()))
2529 return N1;
2530 if (ISD::isBuildVectorAllOnes(N1.getNode()))
2531 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002532 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002533
Dan Gohman613e0d82007-07-03 14:03:57 +00002534 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002535 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002536 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002537 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002538 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002539 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002540 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002541 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002542 return DAG.getNode(ISD::AND, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002543 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002544 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002545 return N0;
2546 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002547 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002548 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002549 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00002550 // reassociate and
Andrew Trickac6d9be2013-05-25 02:42:55 +00002551 SDValue RAND = ReassociateOps(ISD::AND, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002552 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002553 return RAND;
Bill Wendling7d9f2b92010-03-03 00:35:56 +00002554 // fold (and (or x, C), D) -> D if (C & D) == D
Nate Begeman5dc7e862005-11-02 18:42:59 +00002555 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002556 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002557 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002558 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00002559 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
2560 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00002561 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002562 APInt Mask = ~N1C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00002563 Mask = Mask.trunc(N0Op0.getValueSizeInBits());
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002564 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002565 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N),
Bill Wendling2627a882009-01-30 20:43:18 +00002566 N0.getValueType(), N0Op0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002567
Chris Lattner1ec05d12006-03-01 21:47:21 +00002568 // Replace uses of the AND with uses of the Zero extend node.
2569 CombineTo(N, Zext);
Scott Michelfdc40a02009-02-17 22:15:04 +00002570
Chris Lattner3603cd62006-02-02 07:17:31 +00002571 // We actually want to replace all uses of the any_extend with the
2572 // zero_extend, to avoid duplicating things. This will later cause this
2573 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00002574 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00002575 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00002576 }
2577 }
Stephen Lin155615d2013-07-08 00:37:03 +00002578 // similarly fold (and (X (load ([non_ext|any_ext|zero_ext] V))), c) ->
James Molloy6259dcd2012-02-20 12:02:38 +00002579 // (X (load ([non_ext|zero_ext] V))) if 'and' only clears top bits which must
2580 // already be zero by virtue of the width of the base type of the load.
2581 //
2582 // the 'X' node here can either be nothing or an extract_vector_elt to catch
2583 // more cases.
2584 if ((N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
2585 N0.getOperand(0).getOpcode() == ISD::LOAD) ||
2586 N0.getOpcode() == ISD::LOAD) {
2587 LoadSDNode *Load = cast<LoadSDNode>( (N0.getOpcode() == ISD::LOAD) ?
2588 N0 : N0.getOperand(0) );
2589
2590 // Get the constant (if applicable) the zero'th operand is being ANDed with.
2591 // This can be a pure constant or a vector splat, in which case we treat the
2592 // vector as a scalar and use the splat value.
2593 APInt Constant = APInt::getNullValue(1);
2594 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2595 Constant = C->getAPIntValue();
2596 } else if (BuildVectorSDNode *Vector = dyn_cast<BuildVectorSDNode>(N1)) {
2597 APInt SplatValue, SplatUndef;
2598 unsigned SplatBitSize;
2599 bool HasAnyUndefs;
2600 bool IsSplat = Vector->isConstantSplat(SplatValue, SplatUndef,
2601 SplatBitSize, HasAnyUndefs);
2602 if (IsSplat) {
2603 // Undef bits can contribute to a possible optimisation if set, so
2604 // set them.
2605 SplatValue |= SplatUndef;
2606
2607 // The splat value may be something like "0x00FFFFFF", which means 0 for
2608 // the first vector value and FF for the rest, repeating. We need a mask
2609 // that will apply equally to all members of the vector, so AND all the
2610 // lanes of the constant together.
2611 EVT VT = Vector->getValueType(0);
2612 unsigned BitWidth = VT.getVectorElementType().getSizeInBits();
Silviu Baranga3d5e1612012-09-05 08:57:21 +00002613
2614 // If the splat value has been compressed to a bitlength lower
2615 // than the size of the vector lane, we need to re-expand it to
2616 // the lane size.
2617 if (BitWidth > SplatBitSize)
2618 for (SplatValue = SplatValue.zextOrTrunc(BitWidth);
2619 SplatBitSize < BitWidth;
2620 SplatBitSize = SplatBitSize * 2)
2621 SplatValue |= SplatValue.shl(SplatBitSize);
2622
James Molloy6259dcd2012-02-20 12:02:38 +00002623 Constant = APInt::getAllOnesValue(BitWidth);
Silviu Baranga3d5e1612012-09-05 08:57:21 +00002624 for (unsigned i = 0, n = SplatBitSize/BitWidth; i < n; ++i)
James Molloy6259dcd2012-02-20 12:02:38 +00002625 Constant &= SplatValue.lshr(i*BitWidth).zextOrTrunc(BitWidth);
2626 }
2627 }
2628
2629 // If we want to change an EXTLOAD to a ZEXTLOAD, ensure a ZEXTLOAD is
2630 // actually legal and isn't going to get expanded, else this is a false
2631 // optimisation.
2632 bool CanZextLoadProfitably = TLI.isLoadExtLegal(ISD::ZEXTLOAD,
2633 Load->getMemoryVT());
2634
2635 // Resize the constant to the same size as the original memory access before
2636 // extension. If it is still the AllOnesValue then this AND is completely
2637 // unneeded.
2638 Constant =
2639 Constant.zextOrTrunc(Load->getMemoryVT().getScalarType().getSizeInBits());
2640
2641 bool B;
2642 switch (Load->getExtensionType()) {
2643 default: B = false; break;
2644 case ISD::EXTLOAD: B = CanZextLoadProfitably; break;
2645 case ISD::ZEXTLOAD:
2646 case ISD::NON_EXTLOAD: B = true; break;
2647 }
2648
2649 if (B && Constant.isAllOnesValue()) {
2650 // If the load type was an EXTLOAD, convert to ZEXTLOAD in order to
2651 // preserve semantics once we get rid of the AND.
2652 SDValue NewLoad(Load, 0);
2653 if (Load->getExtensionType() == ISD::EXTLOAD) {
2654 NewLoad = DAG.getLoad(Load->getAddressingMode(), ISD::ZEXTLOAD,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002655 Load->getValueType(0), SDLoc(Load),
James Molloy6259dcd2012-02-20 12:02:38 +00002656 Load->getChain(), Load->getBasePtr(),
2657 Load->getOffset(), Load->getMemoryVT(),
2658 Load->getMemOperand());
2659 // Replace uses of the EXTLOAD with the new ZEXTLOAD.
Hal Finkeld65e4632012-06-20 15:42:48 +00002660 if (Load->getNumValues() == 3) {
2661 // PRE/POST_INC loads have 3 values.
2662 SDValue To[] = { NewLoad.getValue(0), NewLoad.getValue(1),
2663 NewLoad.getValue(2) };
2664 CombineTo(Load, To, 3, true);
2665 } else {
2666 CombineTo(Load, NewLoad.getValue(0), NewLoad.getValue(1));
2667 }
James Molloy6259dcd2012-02-20 12:02:38 +00002668 }
2669
2670 // Fold the AND away, taking care not to fold to the old load node if we
2671 // replaced it.
2672 CombineTo(N, (N0.getNode() == Load) ? NewLoad : N0);
2673
2674 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2675 }
2676 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002677 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
2678 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
2679 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
2680 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00002681
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002682 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002683 LL.getValueType().isInteger()) {
Bill Wendling2627a882009-01-30 20:43:18 +00002684 // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00002685 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002686 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0),
Bill Wendling2627a882009-01-30 20:43:18 +00002687 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002688 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002689 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002690 }
Bill Wendling2627a882009-01-30 20:43:18 +00002691 // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002692 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002693 SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(N0),
Bill Wendling2627a882009-01-30 20:43:18 +00002694 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002695 AddToWorkList(ANDNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002696 return DAG.getSetCC(SDLoc(N), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002697 }
Bill Wendling2627a882009-01-30 20:43:18 +00002698 // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002699 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002700 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0),
Bill Wendling2627a882009-01-30 20:43:18 +00002701 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002702 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002703 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002704 }
2705 }
Jim Grosbach51a02802013-08-13 21:30:58 +00002706 // Simplify (and (setne X, 0), (setne X, -1)) -> (setuge (add X, 1), 2)
2707 if (LL == RL && isa<ConstantSDNode>(LR) && isa<ConstantSDNode>(RR) &&
2708 Op0 == Op1 && LL.getValueType().isInteger() &&
2709 Op0 == ISD::SETNE && ((cast<ConstantSDNode>(LR)->isNullValue() &&
2710 cast<ConstantSDNode>(RR)->isAllOnesValue()) ||
2711 (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
2712 cast<ConstantSDNode>(RR)->isNullValue()))) {
2713 SDValue ADDNode = DAG.getNode(ISD::ADD, SDLoc(N0), LL.getValueType(),
2714 LL, DAG.getConstant(1, LL.getValueType()));
2715 AddToWorkList(ADDNode.getNode());
2716 return DAG.getSetCC(SDLoc(N), VT, ADDNode,
2717 DAG.getConstant(2, LL.getValueType()), ISD::SETUGE);
2718 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002719 // canonicalize equivalent to ll == rl
2720 if (LL == RR && LR == RL) {
2721 Op1 = ISD::getSetCCSwappedOperands(Op1);
2722 std::swap(RL, RR);
2723 }
2724 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002725 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002726 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00002727 if (Result != ISD::SETCC_INVALID &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00002728 (!LegalOperations ||
Owen Anderson39125d92013-02-14 09:07:33 +00002729 (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) &&
2730 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault225ed702013-05-18 00:21:46 +00002731 getSetCCResultType(N0.getSimpleValueType())))))
Andrew Trickac6d9be2013-05-25 02:42:55 +00002732 return DAG.getSetCC(SDLoc(N), N0.getValueType(),
Bill Wendling2627a882009-01-30 20:43:18 +00002733 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002734 }
2735 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002736
Bill Wendling2627a882009-01-30 20:43:18 +00002737 // Simplify: (and (op x...), (op y...)) -> (op (and x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00002738 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002739 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002740 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002741 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002742
Nate Begemande996292006-02-03 22:24:05 +00002743 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
2744 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002745 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002746 SimplifyDemandedBits(SDValue(N, 0)))
2747 return SDValue(N, 0);
Evan Chengd40d03e2010-01-06 19:38:29 +00002748
Nate Begemanded49632005-10-13 03:11:28 +00002749 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002750 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00002751 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002752 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002753 // If we zero all the possible extended bits, then we can turn this into
2754 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002755 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002756 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002757 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002758 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002759 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002760 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT,
Bill Wendling2627a882009-01-30 20:43:18 +00002761 LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002762 LN0->getPointerInfo(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00002763 LN0->isVolatile(), LN0->isNonTemporal(),
2764 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002765 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002766 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002767 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002768 }
2769 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002770 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00002771 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00002772 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002773 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002774 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002775 // If we zero all the possible extended bits, then we can turn this into
2776 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002777 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002778 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002779 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002780 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002781 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002782 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT,
Bill Wendling2627a882009-01-30 20:43:18 +00002783 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002784 LN0->getBasePtr(), LN0->getPointerInfo(),
2785 MemVT,
David Greene1e559442010-02-15 17:00:31 +00002786 LN0->isVolatile(), LN0->isNonTemporal(),
2787 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002788 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002789 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002790 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002791 }
2792 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002793
Chris Lattner35a9f5a2006-02-28 06:49:37 +00002794 // fold (and (load x), 255) -> (zextload x, i8)
2795 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Chengd40d03e2010-01-06 19:38:29 +00002796 // fold (and (any_ext (extload x, i16)), 255) -> (zextload x, i8)
2797 if (N1C && (N0.getOpcode() == ISD::LOAD ||
2798 (N0.getOpcode() == ISD::ANY_EXTEND &&
2799 N0.getOperand(0).getOpcode() == ISD::LOAD))) {
2800 bool HasAnyExt = N0.getOpcode() == ISD::ANY_EXTEND;
2801 LoadSDNode *LN0 = HasAnyExt
2802 ? cast<LoadSDNode>(N0.getOperand(0))
2803 : cast<LoadSDNode>(N0);
Evan Cheng466685d2006-10-09 20:57:25 +00002804 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Tim Northover5bce67a2013-07-02 09:58:53 +00002805 LN0->isUnindexed() && N0.hasOneUse() && SDValue(LN0, 0).hasOneUse()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00002806 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
Evan Chengd40d03e2010-01-06 19:38:29 +00002807 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue())){
2808 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits);
2809 EVT LoadedVT = LN0->getMemoryVT();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002810
Evan Chengd40d03e2010-01-06 19:38:29 +00002811 if (ExtVT == LoadedVT &&
2812 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
Chris Lattneref7634c2010-01-07 21:53:27 +00002813 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002814
2815 SDValue NewLoad =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002816 DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy,
Chris Lattneref7634c2010-01-07 21:53:27 +00002817 LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002818 LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002819 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2820 LN0->getAlignment());
Chris Lattneref7634c2010-01-07 21:53:27 +00002821 AddToWorkList(N);
2822 CombineTo(LN0, NewLoad, NewLoad.getValue(1));
2823 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2824 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002825
Chris Lattneref7634c2010-01-07 21:53:27 +00002826 // Do not change the width of a volatile load.
2827 // Do not generate loads of non-round integer types since these can
2828 // be expensive (and would be wrong if the type is not byte sized).
2829 if (!LN0->isVolatile() && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() &&
2830 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
2831 EVT PtrType = LN0->getOperand(1).getValueType();
Bill Wendling2627a882009-01-30 20:43:18 +00002832
Chris Lattneref7634c2010-01-07 21:53:27 +00002833 unsigned Alignment = LN0->getAlignment();
2834 SDValue NewPtr = LN0->getBasePtr();
2835
2836 // For big endian targets, we need to add an offset to the pointer
2837 // to load the correct bytes. For little endian systems, we merely
2838 // need to read fewer bytes from the same pointer.
2839 if (TLI.isBigEndian()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00002840 unsigned LVTStoreBytes = LoadedVT.getStoreSize();
2841 unsigned EVTStoreBytes = ExtVT.getStoreSize();
2842 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Andrew Trickac6d9be2013-05-25 02:42:55 +00002843 NewPtr = DAG.getNode(ISD::ADD, SDLoc(LN0), PtrType,
Chris Lattneref7634c2010-01-07 21:53:27 +00002844 NewPtr, DAG.getConstant(PtrOff, PtrType));
2845 Alignment = MinAlign(Alignment, PtrOff);
Evan Chengd40d03e2010-01-06 19:38:29 +00002846 }
Chris Lattneref7634c2010-01-07 21:53:27 +00002847
2848 AddToWorkList(NewPtr.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002849
Chris Lattneref7634c2010-01-07 21:53:27 +00002850 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
2851 SDValue Load =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002852 DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy,
Chris Lattneref7634c2010-01-07 21:53:27 +00002853 LN0->getChain(), NewPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002854 LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002855 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2856 Alignment);
Chris Lattneref7634c2010-01-07 21:53:27 +00002857 AddToWorkList(N);
2858 CombineTo(LN0, Load, Load.getValue(1));
2859 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsdc846502007-10-28 12:59:45 +00002860 }
Evan Cheng466685d2006-10-09 20:57:25 +00002861 }
Chris Lattner15045b62006-02-28 06:35:35 +00002862 }
2863 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002864
Evan Chenga9e13ba2012-07-17 18:54:11 +00002865 if (N0.getOpcode() == ISD::ADD && N1.getOpcode() == ISD::SRL &&
2866 VT.getSizeInBits() <= 64) {
2867 if (ConstantSDNode *ADDI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2868 APInt ADDC = ADDI->getAPIntValue();
2869 if (!TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2870 // Look for (and (add x, c1), (lshr y, c2)). If C1 wasn't a legal
2871 // immediate for an add, but it is legal if its top c2 bits are set,
2872 // transform the ADD so the immediate doesn't need to be materialized
2873 // in a register.
2874 if (ConstantSDNode *SRLI = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
2875 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
2876 SRLI->getZExtValue());
2877 if (DAG.MaskedValueIsZero(N0.getOperand(1), Mask)) {
2878 ADDC |= Mask;
2879 if (TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2880 SDValue NewAdd =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002881 DAG.getNode(ISD::ADD, SDLoc(N0), VT,
Evan Chenga9e13ba2012-07-17 18:54:11 +00002882 N0.getOperand(0), DAG.getConstant(ADDC, VT));
2883 CombineTo(N0.getNode(), NewAdd);
2884 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2885 }
2886 }
2887 }
2888 }
2889 }
2890 }
Evan Chenga9e13ba2012-07-17 18:54:11 +00002891
Tim Northover5d8c2e42013-08-27 13:46:45 +00002892 // fold (and (or (srl N, 8), (shl N, 8)), 0xffff) -> (srl (bswap N), const)
2893 if (N1C && N1C->getAPIntValue() == 0xffff && N0.getOpcode() == ISD::OR) {
2894 SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0),
2895 N0.getOperand(1), false);
2896 if (BSwap.getNode())
2897 return BSwap;
2898 }
2899
Evan Chengb3a3d5e2010-04-28 07:10:39 +00002900 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002901}
2902
Evan Cheng9568e5c2011-06-21 06:01:08 +00002903/// MatchBSwapHWord - Match (a >> 8) | (a << 8) as (bswap a) >> 16
2904///
2905SDValue DAGCombiner::MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
2906 bool DemandHighBits) {
2907 if (!LegalOperations)
2908 return SDValue();
2909
2910 EVT VT = N->getValueType(0);
2911 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16)
2912 return SDValue();
2913 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
2914 return SDValue();
2915
2916 // Recognize (and (shl a, 8), 0xff), (and (srl a, 8), 0xff00)
2917 bool LookPassAnd0 = false;
2918 bool LookPassAnd1 = false;
2919 if (N0.getOpcode() == ISD::AND && N0.getOperand(0).getOpcode() == ISD::SRL)
2920 std::swap(N0, N1);
2921 if (N1.getOpcode() == ISD::AND && N1.getOperand(0).getOpcode() == ISD::SHL)
2922 std::swap(N0, N1);
2923 if (N0.getOpcode() == ISD::AND) {
2924 if (!N0.getNode()->hasOneUse())
2925 return SDValue();
2926 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2927 if (!N01C || N01C->getZExtValue() != 0xFF00)
2928 return SDValue();
2929 N0 = N0.getOperand(0);
2930 LookPassAnd0 = true;
2931 }
2932
2933 if (N1.getOpcode() == ISD::AND) {
2934 if (!N1.getNode()->hasOneUse())
2935 return SDValue();
2936 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2937 if (!N11C || N11C->getZExtValue() != 0xFF)
2938 return SDValue();
2939 N1 = N1.getOperand(0);
2940 LookPassAnd1 = true;
2941 }
2942
2943 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
2944 std::swap(N0, N1);
2945 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
2946 return SDValue();
2947 if (!N0.getNode()->hasOneUse() ||
2948 !N1.getNode()->hasOneUse())
2949 return SDValue();
2950
2951 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2952 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2953 if (!N01C || !N11C)
2954 return SDValue();
2955 if (N01C->getZExtValue() != 8 || N11C->getZExtValue() != 8)
2956 return SDValue();
2957
2958 // Look for (shl (and a, 0xff), 8), (srl (and a, 0xff00), 8)
2959 SDValue N00 = N0->getOperand(0);
2960 if (!LookPassAnd0 && N00.getOpcode() == ISD::AND) {
2961 if (!N00.getNode()->hasOneUse())
2962 return SDValue();
2963 ConstantSDNode *N001C = dyn_cast<ConstantSDNode>(N00.getOperand(1));
2964 if (!N001C || N001C->getZExtValue() != 0xFF)
2965 return SDValue();
2966 N00 = N00.getOperand(0);
2967 LookPassAnd0 = true;
2968 }
2969
2970 SDValue N10 = N1->getOperand(0);
2971 if (!LookPassAnd1 && N10.getOpcode() == ISD::AND) {
2972 if (!N10.getNode()->hasOneUse())
2973 return SDValue();
2974 ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N10.getOperand(1));
2975 if (!N101C || N101C->getZExtValue() != 0xFF00)
2976 return SDValue();
2977 N10 = N10.getOperand(0);
2978 LookPassAnd1 = true;
2979 }
2980
2981 if (N00 != N10)
2982 return SDValue();
2983
Tim Northover5d8c2e42013-08-27 13:46:45 +00002984 // Make sure everything beyond the low halfword gets set to zero since the SRL
2985 // 16 will clear the top bits.
Evan Cheng9568e5c2011-06-21 06:01:08 +00002986 unsigned OpSizeInBits = VT.getSizeInBits();
Tim Northover5d8c2e42013-08-27 13:46:45 +00002987 if (DemandHighBits && OpSizeInBits > 16) {
2988 // If the left-shift isn't masked out then the only way this is a bswap is
2989 // if all bits beyond the low 8 are 0. In that case the entire pattern
2990 // reduces to a left shift anyway: leave it for other parts of the combiner.
2991 if (!LookPassAnd0)
2992 return SDValue();
2993
2994 // However, if the right shift isn't masked out then it might be because
2995 // it's not needed. See if we can spot that too.
2996 if (!LookPassAnd1 &&
2997 !DAG.MaskedValueIsZero(
2998 N10, APInt::getHighBitsSet(OpSizeInBits, OpSizeInBits - 16)))
2999 return SDValue();
3000 }
Eric Christopher7332e6e2011-07-14 01:12:15 +00003001
Andrew Trickac6d9be2013-05-25 02:42:55 +00003002 SDValue Res = DAG.getNode(ISD::BSWAP, SDLoc(N), VT, N00);
Evan Cheng9568e5c2011-06-21 06:01:08 +00003003 if (OpSizeInBits > 16)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003004 Res = DAG.getNode(ISD::SRL, SDLoc(N), VT, Res,
Evan Cheng9568e5c2011-06-21 06:01:08 +00003005 DAG.getConstant(OpSizeInBits-16, getShiftAmountTy(VT)));
3006 return Res;
3007}
3008
3009/// isBSwapHWordElement - Return true if the specified node is an element
3010/// that makes up a 32-bit packed halfword byteswap. i.e.
3011/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
Craig Toppera0ec3f92013-07-14 04:42:23 +00003012static bool isBSwapHWordElement(SDValue N, SmallVectorImpl<SDNode *> &Parts) {
Evan Cheng9568e5c2011-06-21 06:01:08 +00003013 if (!N.getNode()->hasOneUse())
3014 return false;
3015
3016 unsigned Opc = N.getOpcode();
3017 if (Opc != ISD::AND && Opc != ISD::SHL && Opc != ISD::SRL)
3018 return false;
3019
3020 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N.getOperand(1));
3021 if (!N1C)
3022 return false;
3023
3024 unsigned Num;
3025 switch (N1C->getZExtValue()) {
3026 default:
3027 return false;
3028 case 0xFF: Num = 0; break;
3029 case 0xFF00: Num = 1; break;
3030 case 0xFF0000: Num = 2; break;
3031 case 0xFF000000: Num = 3; break;
3032 }
3033
3034 // Look for (x & 0xff) << 8 as well as ((x << 8) & 0xff00).
3035 SDValue N0 = N.getOperand(0);
3036 if (Opc == ISD::AND) {
3037 if (Num == 0 || Num == 2) {
3038 // (x >> 8) & 0xff
3039 // (x >> 8) & 0xff0000
3040 if (N0.getOpcode() != ISD::SRL)
3041 return false;
3042 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3043 if (!C || C->getZExtValue() != 8)
3044 return false;
3045 } else {
3046 // (x << 8) & 0xff00
3047 // (x << 8) & 0xff000000
3048 if (N0.getOpcode() != ISD::SHL)
3049 return false;
3050 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3051 if (!C || C->getZExtValue() != 8)
3052 return false;
3053 }
3054 } else if (Opc == ISD::SHL) {
3055 // (x & 0xff) << 8
3056 // (x & 0xff0000) << 8
3057 if (Num != 0 && Num != 2)
3058 return false;
3059 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
3060 if (!C || C->getZExtValue() != 8)
3061 return false;
3062 } else { // Opc == ISD::SRL
3063 // (x & 0xff00) >> 8
3064 // (x & 0xff000000) >> 8
3065 if (Num != 1 && Num != 3)
3066 return false;
3067 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
3068 if (!C || C->getZExtValue() != 8)
3069 return false;
3070 }
3071
3072 if (Parts[Num])
3073 return false;
3074
3075 Parts[Num] = N0.getOperand(0).getNode();
3076 return true;
3077}
3078
3079/// MatchBSwapHWord - Match a 32-bit packed halfword bswap. That is
3080/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
3081/// => (rotl (bswap x), 16)
3082SDValue DAGCombiner::MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1) {
3083 if (!LegalOperations)
3084 return SDValue();
3085
3086 EVT VT = N->getValueType(0);
3087 if (VT != MVT::i32)
3088 return SDValue();
3089 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
3090 return SDValue();
3091
3092 SmallVector<SDNode*,4> Parts(4, (SDNode*)0);
3093 // Look for either
3094 // (or (or (and), (and)), (or (and), (and)))
3095 // (or (or (or (and), (and)), (and)), (and))
3096 if (N0.getOpcode() != ISD::OR)
3097 return SDValue();
3098 SDValue N00 = N0.getOperand(0);
3099 SDValue N01 = N0.getOperand(1);
3100
Evan Cheng9a65a012012-12-13 01:34:32 +00003101 if (N1.getOpcode() == ISD::OR &&
3102 N00.getNumOperands() == 2 && N01.getNumOperands() == 2) {
Evan Cheng9568e5c2011-06-21 06:01:08 +00003103 // (or (or (and), (and)), (or (and), (and)))
3104 SDValue N000 = N00.getOperand(0);
3105 if (!isBSwapHWordElement(N000, Parts))
3106 return SDValue();
3107
3108 SDValue N001 = N00.getOperand(1);
3109 if (!isBSwapHWordElement(N001, Parts))
3110 return SDValue();
3111 SDValue N010 = N01.getOperand(0);
3112 if (!isBSwapHWordElement(N010, Parts))
3113 return SDValue();
3114 SDValue N011 = N01.getOperand(1);
3115 if (!isBSwapHWordElement(N011, Parts))
3116 return SDValue();
3117 } else {
3118 // (or (or (or (and), (and)), (and)), (and))
3119 if (!isBSwapHWordElement(N1, Parts))
3120 return SDValue();
3121 if (!isBSwapHWordElement(N01, Parts))
3122 return SDValue();
3123 if (N00.getOpcode() != ISD::OR)
3124 return SDValue();
3125 SDValue N000 = N00.getOperand(0);
3126 if (!isBSwapHWordElement(N000, Parts))
3127 return SDValue();
3128 SDValue N001 = N00.getOperand(1);
3129 if (!isBSwapHWordElement(N001, Parts))
3130 return SDValue();
3131 }
3132
3133 // Make sure the parts are all coming from the same node.
3134 if (Parts[0] != Parts[1] || Parts[0] != Parts[2] || Parts[0] != Parts[3])
3135 return SDValue();
3136
Andrew Trickac6d9be2013-05-25 02:42:55 +00003137 SDValue BSwap = DAG.getNode(ISD::BSWAP, SDLoc(N), VT,
Evan Cheng9568e5c2011-06-21 06:01:08 +00003138 SDValue(Parts[0],0));
3139
Kay Tiong Khoo670711e2013-09-23 18:43:51 +00003140 // Result of the bswap should be rotated by 16. If it's not legal, then
Evan Cheng9568e5c2011-06-21 06:01:08 +00003141 // do (x << 16) | (x >> 16).
3142 SDValue ShAmt = DAG.getConstant(16, getShiftAmountTy(VT));
3143 if (TLI.isOperationLegalOrCustom(ISD::ROTL, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003144 return DAG.getNode(ISD::ROTL, SDLoc(N), VT, BSwap, ShAmt);
Craig Topper0eb5dad2012-09-29 07:18:53 +00003145 if (TLI.isOperationLegalOrCustom(ISD::ROTR, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003146 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, BSwap, ShAmt);
3147 return DAG.getNode(ISD::OR, SDLoc(N), VT,
3148 DAG.getNode(ISD::SHL, SDLoc(N), VT, BSwap, ShAmt),
3149 DAG.getNode(ISD::SRL, SDLoc(N), VT, BSwap, ShAmt));
Evan Cheng9568e5c2011-06-21 06:01:08 +00003150}
3151
Dan Gohman475871a2008-07-27 21:46:04 +00003152SDValue DAGCombiner::visitOR(SDNode *N) {
3153 SDValue N0 = N->getOperand(0);
3154 SDValue N1 = N->getOperand(1);
3155 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00003156 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3157 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003158 EVT VT = N1.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003159
Dan Gohman7f321562007-06-25 16:23:39 +00003160 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003161 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003162 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003163 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00003164
3165 // fold (or x, 0) -> x, vector edition
3166 if (ISD::isBuildVectorAllZeros(N0.getNode()))
3167 return N1;
3168 if (ISD::isBuildVectorAllZeros(N1.getNode()))
3169 return N0;
3170
3171 // fold (or x, -1) -> -1, vector edition
3172 if (ISD::isBuildVectorAllOnes(N0.getNode()))
3173 return N0;
3174 if (ISD::isBuildVectorAllOnes(N1.getNode()))
3175 return N1;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003176 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003177
Dan Gohman613e0d82007-07-03 14:03:57 +00003178 // fold (or x, undef) -> -1
Bob Wilson86749492010-06-28 23:40:25 +00003179 if (!LegalOperations &&
3180 (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)) {
Nate Begeman93e0ed32009-12-03 07:11:29 +00003181 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
3182 return DAG.getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
3183 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003184 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003185 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003186 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00003187 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003188 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003189 return DAG.getNode(ISD::OR, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003190 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003191 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003192 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003193 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00003194 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003195 return N1;
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003196 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003197 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003198 return N1;
Evan Cheng9568e5c2011-06-21 06:01:08 +00003199
3200 // Recognize halfword bswaps as (bswap + rotl 16) or (bswap + shl 16)
3201 SDValue BSwap = MatchBSwapHWord(N, N0, N1);
3202 if (BSwap.getNode() != 0)
3203 return BSwap;
3204 BSwap = MatchBSwapHWordLow(N, N0, N1);
3205 if (BSwap.getNode() != 0)
3206 return BSwap;
3207
Nate Begemancd4d58c2006-02-03 06:46:56 +00003208 // reassociate or
Andrew Trickac6d9be2013-05-25 02:42:55 +00003209 SDValue ROR = ReassociateOps(ISD::OR, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003210 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00003211 return ROR;
3212 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003213 // iff (c1 & c2) == 0.
Gabor Greifba36cb52008-08-28 21:40:38 +00003214 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00003215 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00003216 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
Bill Wendling32f9eb22010-03-03 01:58:01 +00003217 if ((C1->getAPIntValue() & N1C->getAPIntValue()) != 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003218 return DAG.getNode(ISD::AND, SDLoc(N), VT,
3219 DAG.getNode(ISD::OR, SDLoc(N0), VT,
Bill Wendling7d9f2b92010-03-03 00:35:56 +00003220 N0.getOperand(0), N1),
3221 DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1));
Nate Begeman223df222005-09-08 20:18:10 +00003222 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003223 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
3224 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
3225 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
3226 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00003227
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003228 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003229 LL.getValueType().isInteger()) {
Bill Wendling09025642009-01-30 20:59:34 +00003230 // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0)
3231 // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00003232 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003233 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003234 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(LR),
Bill Wendling09025642009-01-30 20:59:34 +00003235 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003236 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003237 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003238 }
Bill Wendling09025642009-01-30 20:59:34 +00003239 // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1)
3240 // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1)
Scott Michelfdc40a02009-02-17 22:15:04 +00003241 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003242 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003243 SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(LR),
Bill Wendling09025642009-01-30 20:59:34 +00003244 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003245 AddToWorkList(ANDNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003246 return DAG.getSetCC(SDLoc(N), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003247 }
3248 }
3249 // canonicalize equivalent to ll == rl
3250 if (LL == RR && LR == RL) {
3251 Op1 = ISD::getSetCCSwappedOperands(Op1);
3252 std::swap(RL, RR);
3253 }
3254 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003255 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003256 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00003257 if (Result != ISD::SETCC_INVALID &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00003258 (!LegalOperations ||
Owen Anderson39125d92013-02-14 09:07:33 +00003259 (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) &&
3260 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault225ed702013-05-18 00:21:46 +00003261 getSetCCResultType(N0.getValueType())))))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003262 return DAG.getSetCC(SDLoc(N), N0.getValueType(),
Bill Wendling09025642009-01-30 20:59:34 +00003263 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003264 }
3265 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003266
Bill Wendling09025642009-01-30 20:59:34 +00003267 // Simplify: (or (op x...), (op y...)) -> (op (or x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00003268 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003269 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003270 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003271 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003272
Bill Wendling09025642009-01-30 20:59:34 +00003273 // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible.
Chris Lattner1ec72732006-09-14 21:11:37 +00003274 if (N0.getOpcode() == ISD::AND &&
3275 N1.getOpcode() == ISD::AND &&
3276 N0.getOperand(1).getOpcode() == ISD::Constant &&
3277 N1.getOperand(1).getOpcode() == ISD::Constant &&
3278 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00003279 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00003280 // We can only do this xform if we know that bits from X that are set in C2
3281 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003282 const APInt &LHSMask =
3283 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
3284 const APInt &RHSMask =
3285 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003286
Dan Gohmanea859be2007-06-22 14:59:07 +00003287 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
3288 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003289 SDValue X = DAG.getNode(ISD::OR, SDLoc(N0), VT,
Bill Wendling09025642009-01-30 20:59:34 +00003290 N0.getOperand(0), N1.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003291 return DAG.getNode(ISD::AND, SDLoc(N), VT, X,
Bill Wendling09025642009-01-30 20:59:34 +00003292 DAG.getConstant(LHSMask | RHSMask, VT));
Chris Lattner1ec72732006-09-14 21:11:37 +00003293 }
3294 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003295
Chris Lattner516b9622006-09-14 20:50:57 +00003296 // See if this is some rotate idiom.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003297 if (SDNode *Rot = MatchRotate(N0, N1, SDLoc(N)))
Dan Gohman475871a2008-07-27 21:46:04 +00003298 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00003299
Dan Gohman4e39e9d2010-06-24 14:30:44 +00003300 // Simplify the operands using demanded-bits information.
3301 if (!VT.isVector() &&
3302 SimplifyDemandedBits(SDValue(N, 0)))
3303 return SDValue(N, 0);
3304
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003305 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003306}
3307
Chris Lattner516b9622006-09-14 20:50:57 +00003308/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00003309static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00003310 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00003311 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00003312 Mask = Op.getOperand(1);
3313 Op = Op.getOperand(0);
3314 } else {
3315 return false;
3316 }
3317 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003318
Chris Lattner516b9622006-09-14 20:50:57 +00003319 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
3320 Shift = Op;
3321 return true;
3322 }
Bill Wendling09025642009-01-30 20:59:34 +00003323
Scott Michelfdc40a02009-02-17 22:15:04 +00003324 return false;
Chris Lattner516b9622006-09-14 20:50:57 +00003325}
3326
Chris Lattner516b9622006-09-14 20:50:57 +00003327// MatchRotate - Handle an 'or' of two operands. If this is one of the many
3328// idioms for rotate, and if the target supports rotation instructions, generate
3329// a rot[lr].
Andrew Trickac6d9be2013-05-25 02:42:55 +00003330SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, SDLoc DL) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003331 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Owen Andersone50ed302009-08-10 22:56:29 +00003332 EVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00003333 if (!TLI.isTypeLegal(VT)) return 0;
3334
3335 // The target must have at least one rotate flavor.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003336 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
3337 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Chris Lattner516b9622006-09-14 20:50:57 +00003338 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003339
Chris Lattner516b9622006-09-14 20:50:57 +00003340 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00003341 SDValue LHSShift; // The shift.
3342 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00003343 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
3344 return 0; // Not part of a rotate.
3345
Dan Gohman475871a2008-07-27 21:46:04 +00003346 SDValue RHSShift; // The shift.
3347 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00003348 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
3349 return 0; // Not part of a rotate.
Scott Michelfdc40a02009-02-17 22:15:04 +00003350
Chris Lattner516b9622006-09-14 20:50:57 +00003351 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
3352 return 0; // Not shifting the same value.
3353
3354 if (LHSShift.getOpcode() == RHSShift.getOpcode())
3355 return 0; // Shifts must disagree.
Scott Michelfdc40a02009-02-17 22:15:04 +00003356
Chris Lattner516b9622006-09-14 20:50:57 +00003357 // Canonicalize shl to left side in a shl/srl pair.
3358 if (RHSShift.getOpcode() == ISD::SHL) {
3359 std::swap(LHS, RHS);
3360 std::swap(LHSShift, RHSShift);
3361 std::swap(LHSMask , RHSMask );
3362 }
3363
Duncan Sands83ec4b62008-06-06 12:08:01 +00003364 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00003365 SDValue LHSShiftArg = LHSShift.getOperand(0);
3366 SDValue LHSShiftAmt = LHSShift.getOperand(1);
Kai Nackeceb3b462013-09-19 23:00:28 +00003367 SDValue RHSShiftArg = RHSShift.getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00003368 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00003369
3370 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
3371 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00003372 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
3373 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003374 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
3375 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00003376 if ((LShVal + RShVal) != OpSizeInBits)
3377 return 0;
3378
Craig Topper32b73432012-09-29 06:54:22 +00003379 SDValue Rot = DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
3380 LHSShiftArg, HasROTL ? LHSShiftAmt : RHSShiftAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00003381
Chris Lattner516b9622006-09-14 20:50:57 +00003382 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00003383 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003384 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00003385
Gabor Greifba36cb52008-08-28 21:40:38 +00003386 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003387 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
3388 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003389 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003390 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003391 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
3392 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003393 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003394
Bill Wendling317bd702009-01-30 21:14:50 +00003395 Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT));
Chris Lattner516b9622006-09-14 20:50:57 +00003396 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003397
Gabor Greifba36cb52008-08-28 21:40:38 +00003398 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00003399 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003400
Chris Lattner516b9622006-09-14 20:50:57 +00003401 // If there is a mask here, and we have a variable shift, we can't be sure
3402 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00003403 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00003404 return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00003405
Benjamin Kramercd216b22013-09-24 14:21:28 +00003406 // If the shift amount is sign/zext/any-extended just peel it off.
3407 SDValue LExtOp0 = LHSShiftAmt;
3408 SDValue RExtOp0 = RHSShiftAmt;
Craig Topper0eb5dad2012-09-29 07:18:53 +00003409 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND ||
3410 LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND ||
3411 LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND ||
3412 LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
3413 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND ||
3414 RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND ||
3415 RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND ||
3416 RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Benjamin Kramercd216b22013-09-24 14:21:28 +00003417 LExtOp0 = LHSShiftAmt.getOperand(0);
3418 RExtOp0 = RHSShiftAmt.getOperand(0);
3419 }
3420
3421 if (RExtOp0.getOpcode() == ISD::SUB && RExtOp0.getOperand(1) == LExtOp0) {
3422 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
3423 // (rotl x, y)
3424 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
3425 // (rotr x, (sub 32, y))
3426 if (ConstantSDNode *SUBC =
3427 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
3428 if (SUBC->getAPIntValue() == OpSizeInBits) {
3429 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT, LHSShiftArg,
3430 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
3431 } else if (LHSShiftArg.getOpcode() == ISD::ZERO_EXTEND ||
Kai Nackeceb3b462013-09-19 23:00:28 +00003432 LHSShiftArg.getOpcode() == ISD::ANY_EXTEND) {
Benjamin Kramercd216b22013-09-24 14:21:28 +00003433 // fold (or (shl (*ext x), (*ext y)),
3434 // (srl (*ext x), (*ext (sub 32, y)))) ->
3435 // (*ext (rotl x, y))
3436 // fold (or (shl (*ext x), (*ext y)),
3437 // (srl (*ext x), (*ext (sub 32, y)))) ->
3438 // (*ext (rotr x, (sub 32, y)))
3439 SDValue LArgExtOp0 = LHSShiftArg.getOperand(0);
3440 EVT LArgVT = LArgExtOp0.getValueType();
Jin-Gu Kangb70a05a2013-10-03 15:58:48 +00003441 bool HasROTRWithLArg = TLI.isOperationLegalOrCustom(ISD::ROTR, LArgVT);
3442 bool HasROTLWithLArg = TLI.isOperationLegalOrCustom(ISD::ROTL, LArgVT);
3443 if (HasROTRWithLArg || HasROTLWithLArg) {
3444 if (LArgVT.getSizeInBits() == SUBC->getAPIntValue()) {
3445 SDValue V =
3446 DAG.getNode(HasROTLWithLArg ? ISD::ROTL : ISD::ROTR, DL, LArgVT,
3447 LArgExtOp0, HasROTL ? LHSShiftAmt : RHSShiftAmt);
3448 return DAG.getNode(LHSShiftArg.getOpcode(), DL, VT, V).getNode();
Jack Carteradbd3ae2013-10-17 01:34:33 +00003449 }
3450 }
David Blaikiec2286722013-09-20 00:33:11 +00003451 }
Benjamin Kramercd216b22013-09-24 14:21:28 +00003452 }
3453 } else if (LExtOp0.getOpcode() == ISD::SUB &&
3454 RExtOp0 == LExtOp0.getOperand(1)) {
3455 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
3456 // (rotr x, y)
3457 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
3458 // (rotl x, (sub 32, y))
3459 if (ConstantSDNode *SUBC =
3460 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
3461 if (SUBC->getAPIntValue() == OpSizeInBits) {
3462 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT, LHSShiftArg,
3463 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
3464 } else if (RHSShiftArg.getOpcode() == ISD::ZERO_EXTEND ||
Kai Nackeceb3b462013-09-19 23:00:28 +00003465 RHSShiftArg.getOpcode() == ISD::ANY_EXTEND) {
Benjamin Kramercd216b22013-09-24 14:21:28 +00003466 // fold (or (shl (*ext x), (*ext (sub 32, y))),
3467 // (srl (*ext x), (*ext y))) ->
3468 // (*ext (rotl x, y))
3469 // fold (or (shl (*ext x), (*ext (sub 32, y))),
3470 // (srl (*ext x), (*ext y))) ->
3471 // (*ext (rotr x, (sub 32, y)))
3472 SDValue RArgExtOp0 = RHSShiftArg.getOperand(0);
3473 EVT RArgVT = RArgExtOp0.getValueType();
Jin-Gu Kangb70a05a2013-10-03 15:58:48 +00003474 bool HasROTRWithRArg = TLI.isOperationLegalOrCustom(ISD::ROTR, RArgVT);
3475 bool HasROTLWithRArg = TLI.isOperationLegalOrCustom(ISD::ROTL, RArgVT);
3476 if (HasROTRWithRArg || HasROTLWithRArg) {
3477 if (RArgVT.getSizeInBits() == SUBC->getAPIntValue()) {
3478 SDValue V =
3479 DAG.getNode(HasROTRWithRArg ? ISD::ROTR : ISD::ROTL, DL, RArgVT,
3480 RArgExtOp0, HasROTR ? RHSShiftAmt : LHSShiftAmt);
3481 return DAG.getNode(RHSShiftArg.getOpcode(), DL, VT, V).getNode();
3482 }
Kai Nackeceb3b462013-09-19 23:00:28 +00003483 }
David Blaikiec2286722013-09-20 00:33:11 +00003484 }
Chris Lattner516b9622006-09-14 20:50:57 +00003485 }
3486 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003487
Chris Lattner516b9622006-09-14 20:50:57 +00003488 return 0;
3489}
3490
Dan Gohman475871a2008-07-27 21:46:04 +00003491SDValue DAGCombiner::visitXOR(SDNode *N) {
3492 SDValue N0 = N->getOperand(0);
3493 SDValue N1 = N->getOperand(1);
3494 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00003495 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3496 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003497 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003498
Dan Gohman7f321562007-06-25 16:23:39 +00003499 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003500 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003501 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003502 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00003503
3504 // fold (xor x, 0) -> x, vector edition
3505 if (ISD::isBuildVectorAllZeros(N0.getNode()))
3506 return N1;
3507 if (ISD::isBuildVectorAllZeros(N1.getNode()))
3508 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003509 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003510
Evan Cheng26471c42008-03-25 20:08:07 +00003511 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
3512 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
3513 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00003514 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00003515 if (N0.getOpcode() == ISD::UNDEF)
3516 return N0;
3517 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00003518 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003519 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003520 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003521 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00003522 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003523 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003524 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003525 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003526 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003527 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00003528 // reassociate xor
Andrew Trickac6d9be2013-05-25 02:42:55 +00003529 SDValue RXOR = ReassociateOps(ISD::XOR, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003530 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00003531 return RXOR;
Bill Wendlingae89bb12008-11-11 08:25:46 +00003532
Nate Begeman1d4d4142005-09-01 00:19:25 +00003533 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00003534 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003535 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00003536 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
3537 isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003538
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00003539 if (!LegalOperations ||
3540 TLI.isCondCodeLegal(NotCC, LHS.getSimpleValueType())) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00003541 switch (N0.getOpcode()) {
3542 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003543 llvm_unreachable("Unhandled SetCC Equivalent!");
Bill Wendlingae89bb12008-11-11 08:25:46 +00003544 case ISD::SETCC:
Andrew Trickac6d9be2013-05-25 02:42:55 +00003545 return DAG.getSetCC(SDLoc(N), VT, LHS, RHS, NotCC);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003546 case ISD::SELECT_CC:
Andrew Trickac6d9be2013-05-25 02:42:55 +00003547 return DAG.getSelectCC(SDLoc(N), LHS, RHS, N0.getOperand(2),
Bill Wendlingae89bb12008-11-11 08:25:46 +00003548 N0.getOperand(3), NotCC);
3549 }
3550 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003551 }
Bill Wendlingae89bb12008-11-11 08:25:46 +00003552
Chris Lattner61c5ff42007-09-10 21:39:07 +00003553 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00003554 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00003555 N0.getNode()->hasOneUse() &&
3556 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00003557 SDValue V = N0.getOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003558 V = DAG.getNode(ISD::XOR, SDLoc(N0), V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00003559 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003560 AddToWorkList(V.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003561 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, V);
Chris Lattner61c5ff42007-09-10 21:39:07 +00003562 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003563
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003564 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc
Owen Anderson825b72b2009-08-11 20:47:22 +00003565 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00003566 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003567 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003568 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
3569 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Andrew Trickac6d9be2013-05-25 02:42:55 +00003570 LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS
3571 RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003572 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003573 return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003574 }
3575 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003576 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants
Scott Michelfdc40a02009-02-17 22:15:04 +00003577 if (N1C && N1C->isAllOnesValue() &&
Nate Begeman99801192005-09-07 23:25:52 +00003578 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003579 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003580 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
3581 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Andrew Trickac6d9be2013-05-25 02:42:55 +00003582 LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS
3583 RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003584 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003585 return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003586 }
3587 }
David Majnemer363160a2013-05-08 06:44:42 +00003588 // fold (xor (and x, y), y) -> (and (not x), y)
3589 if (N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Benjamin Kramer72a3ee72013-10-16 14:16:19 +00003590 N0->getOperand(1) == N1 && isTypeLegal(VT.getScalarType())) {
David Majnemer363160a2013-05-08 06:44:42 +00003591 SDValue X = N0->getOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003592 SDValue NotX = DAG.getNOT(SDLoc(X), X, VT);
David Majnemer363160a2013-05-08 06:44:42 +00003593 AddToWorkList(NotX.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003594 return DAG.getNode(ISD::AND, SDLoc(N), VT, NotX, N1);
David Majnemer363160a2013-05-08 06:44:42 +00003595 }
Bill Wendling317bd702009-01-30 21:14:50 +00003596 // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2))
Nate Begeman223df222005-09-08 20:18:10 +00003597 if (N1C && N0.getOpcode() == ISD::XOR) {
3598 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
3599 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3600 if (N00C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003601 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N0.getOperand(1),
Bill Wendling317bd702009-01-30 21:14:50 +00003602 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003603 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003604 if (N01C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003605 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N0.getOperand(0),
Bill Wendling317bd702009-01-30 21:14:50 +00003606 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003607 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003608 }
3609 // fold (xor x, x) -> 0
Eric Christopher7bccf6a2011-02-16 04:50:12 +00003610 if (N0 == N1)
Hal Finkelbd6f1f62013-07-09 17:02:45 +00003611 return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations, LegalTypes);
Scott Michelfdc40a02009-02-17 22:15:04 +00003612
Chris Lattner35e5c142006-05-05 05:51:50 +00003613 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
3614 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003615 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003616 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003617 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003618
Chris Lattner3e104b12006-04-08 04:15:24 +00003619 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003620 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00003621 SimplifyDemandedBits(SDValue(N, 0)))
3622 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003623
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003624 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003625}
3626
Chris Lattnere70da202007-12-06 07:33:36 +00003627/// visitShiftByConstant - Handle transforms common to the three shifts, when
3628/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00003629SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003630 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00003631 if (!LHS->hasOneUse()) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003632
Chris Lattnere70da202007-12-06 07:33:36 +00003633 // We want to pull some binops through shifts, so that we have (and (shift))
3634 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
3635 // thing happens with address calculations, so it's important to canonicalize
3636 // it.
3637 bool HighBitSet = false; // Can we transform this if the high bit is set?
Scott Michelfdc40a02009-02-17 22:15:04 +00003638
Chris Lattnere70da202007-12-06 07:33:36 +00003639 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003640 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003641 case ISD::OR:
3642 case ISD::XOR:
3643 HighBitSet = false; // We can only transform sra if the high bit is clear.
3644 break;
3645 case ISD::AND:
3646 HighBitSet = true; // We can only transform sra if the high bit is set.
3647 break;
3648 case ISD::ADD:
Scott Michelfdc40a02009-02-17 22:15:04 +00003649 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00003650 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00003651 HighBitSet = false; // We can only transform sra if the high bit is clear.
3652 break;
3653 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003654
Chris Lattnere70da202007-12-06 07:33:36 +00003655 // We require the RHS of the binop to be a constant as well.
3656 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003657 if (!BinOpCst) return SDValue();
Bill Wendling88103372009-01-30 21:37:17 +00003658
3659 // FIXME: disable this unless the input to the binop is a shift by a constant.
3660 // If it is not a shift, it pessimizes some common cases like:
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003661 //
Bill Wendling88103372009-01-30 21:37:17 +00003662 // void foo(int *X, int i) { X[i & 1235] = 1; }
3663 // int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00003664 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00003665 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003666 BinOpLHSVal->getOpcode() != ISD::SRA &&
3667 BinOpLHSVal->getOpcode() != ISD::SRL) ||
3668 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00003669 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003670
Owen Andersone50ed302009-08-10 22:56:29 +00003671 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003672
Bill Wendling88103372009-01-30 21:37:17 +00003673 // If this is a signed shift right, and the high bit is modified by the
3674 // logical operation, do not perform the transformation. The highBitSet
3675 // boolean indicates the value of the high bit of the constant which would
3676 // cause it to be modified for this operation.
Chris Lattnere70da202007-12-06 07:33:36 +00003677 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00003678 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
3679 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00003680 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003681 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003682
Chris Lattnere70da202007-12-06 07:33:36 +00003683 // Fold the constants, shifting the binop RHS by the shift amount.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003684 SDValue NewRHS = DAG.getNode(N->getOpcode(), SDLoc(LHS->getOperand(1)),
Bill Wendling88103372009-01-30 21:37:17 +00003685 N->getValueType(0),
3686 LHS->getOperand(1), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003687
3688 // Create the new shift.
Eric Christopher503a64d2010-12-09 04:48:06 +00003689 SDValue NewShift = DAG.getNode(N->getOpcode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00003690 SDLoc(LHS->getOperand(0)),
Bill Wendling88103372009-01-30 21:37:17 +00003691 VT, LHS->getOperand(0), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003692
3693 // Create the new binop.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003694 return DAG.getNode(LHS->getOpcode(), SDLoc(N), VT, NewShift, NewRHS);
Chris Lattnere70da202007-12-06 07:33:36 +00003695}
3696
Dan Gohman475871a2008-07-27 21:46:04 +00003697SDValue DAGCombiner::visitSHL(SDNode *N) {
3698 SDValue N0 = N->getOperand(0);
3699 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003700 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3701 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003702 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003703 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003704
Nate Begeman1d4d4142005-09-01 00:19:25 +00003705 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003706 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003707 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003708 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003709 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003710 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003711 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003712 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003713 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003714 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003715 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003716 return N0;
Chad Rosier92bcd962011-06-14 22:29:10 +00003717 // fold (shl undef, x) -> 0
3718 if (N0.getOpcode() == ISD::UNDEF)
3719 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003720 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00003721 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman87862e72009-12-11 21:31:27 +00003722 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003723 return DAG.getConstant(0, VT);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003724 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003725 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003726 N1.getOperand(0).getOpcode() == ISD::AND &&
3727 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003728 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003729 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003730 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003731 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003732 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003733 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003734 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
3735 DAG.getNode(ISD::AND, SDLoc(N), TruncVT,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003736 DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003737 SDLoc(N),
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003738 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003739 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003740 }
3741 }
3742
Dan Gohman475871a2008-07-27 21:46:04 +00003743 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3744 return SDValue(N, 0);
Bill Wendling88103372009-01-30 21:37:17 +00003745
3746 // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00003747 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003748 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003749 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3750 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003751 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003752 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003753 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00003754 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003755 }
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003756
3757 // fold (shl (ext (shl x, c1)), c2) -> (ext (shl x, (add c1, c2)))
3758 // For this to be valid, the second form must not preserve any of the bits
3759 // that are shifted out by the inner shift in the first form. This means
3760 // the outer shift size must be >= the number of bits added by the ext.
3761 // As a corollary, we don't care what kind of ext it is.
3762 if (N1C && (N0.getOpcode() == ISD::ZERO_EXTEND ||
3763 N0.getOpcode() == ISD::ANY_EXTEND ||
3764 N0.getOpcode() == ISD::SIGN_EXTEND) &&
3765 N0.getOperand(0).getOpcode() == ISD::SHL &&
3766 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00003767 uint64_t c1 =
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003768 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3769 uint64_t c2 = N1C->getZExtValue();
3770 EVT InnerShiftVT = N0.getOperand(0).getValueType();
3771 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
3772 if (c2 >= OpSizeInBits - InnerShiftSize) {
3773 if (c1 + c2 >= OpSizeInBits)
3774 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003775 return DAG.getNode(ISD::SHL, SDLoc(N0), VT,
3776 DAG.getNode(N0.getOpcode(), SDLoc(N0), VT,
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003777 N0.getOperand(0)->getOperand(0)),
3778 DAG.getConstant(c1 + c2, N1.getValueType()));
3779 }
3780 }
3781
Andrea Di Biagioa9f113d2013-09-27 11:37:05 +00003782 // fold (shl (zext (srl x, C)), C) -> (zext (shl (srl x, C), C))
3783 // Only fold this if the inner zext has no other uses to avoid increasing
3784 // the total number of instructions.
3785 if (N1C && N0.getOpcode() == ISD::ZERO_EXTEND && N0.hasOneUse() &&
3786 N0.getOperand(0).getOpcode() == ISD::SRL &&
3787 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
3788 uint64_t c1 =
3789 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3790 if (c1 < VT.getSizeInBits()) {
3791 uint64_t c2 = N1C->getZExtValue();
3792 if (c1 == c2) {
3793 SDValue NewOp0 = N0.getOperand(0);
3794 EVT CountVT = NewOp0.getOperand(1).getValueType();
3795 SDValue NewSHL = DAG.getNode(ISD::SHL, SDLoc(N), NewOp0.getValueType(),
3796 NewOp0, DAG.getConstant(c2, CountVT));
3797 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N0), VT, NewSHL);
3798 }
3799 }
3800 }
3801
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003802 // fold (shl (srl x, c1), c2) -> (and (shl x, (sub c2, c1), MASK) or
3803 // (and (srl x, (sub c1, c2), MASK)
Chandler Carruth62dfc512012-01-05 11:05:55 +00003804 // Only fold this if the inner shift has no other uses -- if it does, folding
3805 // this will increase the total number of instructions.
3806 if (N1C && N0.getOpcode() == ISD::SRL && N0.hasOneUse() &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003807 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003808 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
Evan Chengd101a722009-07-21 05:40:15 +00003809 if (c1 < VT.getSizeInBits()) {
3810 uint64_t c2 = N1C->getZExtValue();
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003811 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
3812 VT.getSizeInBits() - c1);
3813 SDValue Shift;
3814 if (c2 > c1) {
3815 Mask = Mask.shl(c2-c1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003816 Shift = DAG.getNode(ISD::SHL, SDLoc(N), VT, N0.getOperand(0),
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003817 DAG.getConstant(c2-c1, N1.getValueType()));
3818 } else {
3819 Mask = Mask.lshr(c1-c2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003820 Shift = DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0),
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003821 DAG.getConstant(c1-c2, N1.getValueType()));
3822 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00003823 return DAG.getNode(ISD::AND, SDLoc(N0), VT, Shift,
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003824 DAG.getConstant(Mask, VT));
Evan Chengd101a722009-07-21 05:40:15 +00003825 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003826 }
Bill Wendling88103372009-01-30 21:37:17 +00003827 // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1))
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003828 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) {
3829 SDValue HiBitsMask =
3830 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
3831 VT.getSizeInBits() -
3832 N1C->getZExtValue()),
3833 VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003834 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0.getOperand(0),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003835 HiBitsMask);
3836 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003837
Evan Chenge5b51ac2010-04-17 06:13:15 +00003838 if (N1C) {
3839 SDValue NewSHL = visitShiftByConstant(N, N1C->getZExtValue());
3840 if (NewSHL.getNode())
3841 return NewSHL;
3842 }
3843
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003844 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003845}
3846
Dan Gohman475871a2008-07-27 21:46:04 +00003847SDValue DAGCombiner::visitSRA(SDNode *N) {
3848 SDValue N0 = N->getOperand(0);
3849 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003850 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3851 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003852 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003853 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003854
Bill Wendling88103372009-01-30 21:37:17 +00003855 // fold (sra c1, c2) -> (sra c1, c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00003856 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003857 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003858 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003859 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003860 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003861 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00003862 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003863 return N0;
Bill Wendling88103372009-01-30 21:37:17 +00003864 // fold (sra x, (setge c, size(x))) -> undef
Dan Gohman87862e72009-12-11 21:31:27 +00003865 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003866 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003867 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003868 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003869 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00003870 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
3871 // sext_inreg.
3872 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohman87862e72009-12-11 21:31:27 +00003873 unsigned LowBits = OpSizeInBits - (unsigned)N1C->getZExtValue();
Dan Gohmand1996362010-01-09 02:13:55 +00003874 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), LowBits);
3875 if (VT.isVector())
3876 ExtVT = EVT::getVectorVT(*DAG.getContext(),
3877 ExtVT, VT.getVectorNumElements());
3878 if ((!LegalOperations ||
3879 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, ExtVT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003880 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Dan Gohmand1996362010-01-09 02:13:55 +00003881 N0.getOperand(0), DAG.getValueType(ExtVT));
Nate Begemanfb7217b2006-02-17 19:54:08 +00003882 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003883
Bill Wendling88103372009-01-30 21:37:17 +00003884 // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2))
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003885 if (N1C && N0.getOpcode() == ISD::SRA) {
3886 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003887 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Dan Gohman87862e72009-12-11 21:31:27 +00003888 if (Sum >= OpSizeInBits) Sum = OpSizeInBits-1;
Andrew Trickac6d9be2013-05-25 02:42:55 +00003889 return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0.getOperand(0),
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003890 DAG.getConstant(Sum, N1C->getValueType(0)));
3891 }
3892 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00003893
Bill Wendling88103372009-01-30 21:37:17 +00003894 // fold (sra (shl X, m), (sub result_size, n))
3895 // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for
Scott Michelfdc40a02009-02-17 22:15:04 +00003896 // result_size - n != m.
3897 // If truncate is free for the target sext(shl) is likely to result in better
Christopher Lambb9b04282008-03-20 04:31:39 +00003898 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00003899 if (N0.getOpcode() == ISD::SHL) {
3900 // Get the two constanst of the shifts, CN0 = m, CN = n.
3901 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3902 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003903 // Determine what the truncate's result bitsize and type would be.
Owen Andersone50ed302009-08-10 22:56:29 +00003904 EVT TruncVT =
Eric Christopher503a64d2010-12-09 04:48:06 +00003905 EVT::getIntegerVT(*DAG.getContext(),
3906 OpSizeInBits - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00003907 // Determine the residual right-shift amount.
Torok Edwin6bb49582009-05-23 17:29:48 +00003908 signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003909
Scott Michelfdc40a02009-02-17 22:15:04 +00003910 // If the shift is not a no-op (in which case this should be just a sign
3911 // extend already), the truncated to type is legal, sign_extend is legal
Dan Gohmanf451cb82010-02-10 16:03:48 +00003912 // on that type, and the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00003913 // perform the transform.
Torok Edwin6bb49582009-05-23 17:29:48 +00003914 if ((ShiftAmt > 0) &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003915 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
3916 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00003917 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003918
Owen Anderson95771af2011-02-25 21:41:48 +00003919 SDValue Amt = DAG.getConstant(ShiftAmt,
3920 getShiftAmountTy(N0.getOperand(0).getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003921 SDValue Shift = DAG.getNode(ISD::SRL, SDLoc(N0), VT,
Bill Wendling88103372009-01-30 21:37:17 +00003922 N0.getOperand(0), Amt);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003923 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), TruncVT,
Bill Wendling88103372009-01-30 21:37:17 +00003924 Shift);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003925 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N),
Bill Wendling88103372009-01-30 21:37:17 +00003926 N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00003927 }
3928 }
3929 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003930
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003931 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003932 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003933 N1.getOperand(0).getOpcode() == ISD::AND &&
3934 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003935 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003936 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003937 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003938 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003939 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003940 TruncC = TruncC.trunc(TruncVT.getScalarType().getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003941 return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0,
3942 DAG.getNode(ISD::AND, SDLoc(N),
Bill Wendling88103372009-01-30 21:37:17 +00003943 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003944 DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003945 SDLoc(N),
Bill Wendling9729c5a2009-01-31 03:12:48 +00003946 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003947 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003948 }
3949 }
3950
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003951 // fold (sra (trunc (sr x, c1)), c2) -> (trunc (sra x, c1+c2))
3952 // if c1 is equal to the number of bits the trunc removes
3953 if (N0.getOpcode() == ISD::TRUNCATE &&
3954 (N0.getOperand(0).getOpcode() == ISD::SRL ||
3955 N0.getOperand(0).getOpcode() == ISD::SRA) &&
3956 N0.getOperand(0).hasOneUse() &&
3957 N0.getOperand(0).getOperand(1).hasOneUse() &&
3958 N1C && isa<ConstantSDNode>(N0.getOperand(0).getOperand(1))) {
3959 EVT LargeVT = N0.getOperand(0).getValueType();
3960 ConstantSDNode *LargeShiftAmt =
3961 cast<ConstantSDNode>(N0.getOperand(0).getOperand(1));
3962
3963 if (LargeVT.getScalarType().getSizeInBits() - OpSizeInBits ==
3964 LargeShiftAmt->getZExtValue()) {
3965 SDValue Amt =
3966 DAG.getConstant(LargeShiftAmt->getZExtValue() + N1C->getZExtValue(),
Owen Anderson95771af2011-02-25 21:41:48 +00003967 getShiftAmountTy(N0.getOperand(0).getOperand(0).getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003968 SDValue SRA = DAG.getNode(ISD::SRA, SDLoc(N), LargeVT,
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003969 N0.getOperand(0).getOperand(0), Amt);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003970 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, SRA);
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003971 }
3972 }
3973
Scott Michelfdc40a02009-02-17 22:15:04 +00003974 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00003975 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3976 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003977
3978
Nate Begeman1d4d4142005-09-01 00:19:25 +00003979 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003980 if (DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003981 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00003982
Evan Chenge5b51ac2010-04-17 06:13:15 +00003983 if (N1C) {
3984 SDValue NewSRA = visitShiftByConstant(N, N1C->getZExtValue());
3985 if (NewSRA.getNode())
3986 return NewSRA;
3987 }
3988
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003989 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003990}
3991
Dan Gohman475871a2008-07-27 21:46:04 +00003992SDValue DAGCombiner::visitSRL(SDNode *N) {
3993 SDValue N0 = N->getOperand(0);
3994 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003995 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3996 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003997 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003998 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003999
Nate Begeman1d4d4142005-09-01 00:19:25 +00004000 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00004001 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00004002 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004003 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00004004 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00004005 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00004006 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004007 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00004008 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004009 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00004010 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00004011 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00004012 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00004013 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004014 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00004015 return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004016
Bill Wendling88103372009-01-30 21:37:17 +00004017 // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00004018 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00004019 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004020 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
4021 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00004022 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00004023 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004024 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00004025 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00004026 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004027
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00004028 // fold (srl (trunc (srl x, c1)), c2) -> 0 or (trunc (srl x, (add c1, c2)))
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00004029 if (N1C && N0.getOpcode() == ISD::TRUNCATE &&
4030 N0.getOperand(0).getOpcode() == ISD::SRL &&
Dale Johannesen025cc6e2010-12-20 20:10:50 +00004031 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00004032 uint64_t c1 =
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00004033 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
4034 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00004035 EVT InnerShiftVT = N0.getOperand(0).getValueType();
4036 EVT ShiftCountVT = N0.getOperand(0)->getOperand(1).getValueType();
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00004037 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
Dale Johannesen025cc6e2010-12-20 20:10:50 +00004038 // This is only valid if the OpSizeInBits + c1 = size of inner shift.
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00004039 if (c1 + OpSizeInBits == InnerShiftSize) {
4040 if (c1 + c2 >= InnerShiftSize)
4041 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004042 return DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT,
4043 DAG.getNode(ISD::SRL, SDLoc(N0), InnerShiftVT,
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00004044 N0.getOperand(0)->getOperand(0),
Dale Johannesenc72b18c2010-12-21 21:55:50 +00004045 DAG.getConstant(c1 + c2, ShiftCountVT)));
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00004046 }
4047 }
4048
Chris Lattnerefcddc32010-04-15 05:28:43 +00004049 // fold (srl (shl x, c), c) -> (and x, cst2)
4050 if (N1C && N0.getOpcode() == ISD::SHL && N0.getOperand(1) == N1 &&
4051 N0.getValueSizeInBits() <= 64) {
4052 uint64_t ShAmt = N1C->getZExtValue()+64-N0.getValueSizeInBits();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004053 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0.getOperand(0),
Chris Lattnerefcddc32010-04-15 05:28:43 +00004054 DAG.getConstant(~0ULL >> ShAmt, VT));
4055 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004056
Michael Liao2da86392013-06-21 18:45:27 +00004057 // fold (srl (anyextend x), c) -> (and (anyextend (srl x, c)), mask)
Chris Lattner06afe072006-05-05 22:53:17 +00004058 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
4059 // Shifting in all undef bits?
Owen Andersone50ed302009-08-10 22:56:29 +00004060 EVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004061 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Dale Johannesene8d72302009-02-06 23:05:02 +00004062 return DAG.getUNDEF(VT);
Chris Lattner06afe072006-05-05 22:53:17 +00004063
Evan Chenge5b51ac2010-04-17 06:13:15 +00004064 if (!LegalTypes || TLI.isTypeDesirableForOp(ISD::SRL, SmallVT)) {
Owen Andersona34d9362011-04-14 17:30:49 +00004065 uint64_t ShiftAmt = N1C->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004066 SDValue SmallShift = DAG.getNode(ISD::SRL, SDLoc(N0), SmallVT,
Owen Andersona34d9362011-04-14 17:30:49 +00004067 N0.getOperand(0),
4068 DAG.getConstant(ShiftAmt, getShiftAmountTy(SmallVT)));
Evan Chenge5b51ac2010-04-17 06:13:15 +00004069 AddToWorkList(SmallShift.getNode());
Michael Liao2da86392013-06-21 18:45:27 +00004070 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits()).lshr(ShiftAmt);
4071 return DAG.getNode(ISD::AND, SDLoc(N), VT,
4072 DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, SmallShift),
4073 DAG.getConstant(Mask, VT));
Evan Chenge5b51ac2010-04-17 06:13:15 +00004074 }
Chris Lattner06afe072006-05-05 22:53:17 +00004075 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004076
Chris Lattner3657ffe2006-10-12 20:23:19 +00004077 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
4078 // bit, which is unmodified by sra.
Bill Wendling88103372009-01-30 21:37:17 +00004079 if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00004080 if (N0.getOpcode() == ISD::SRA)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004081 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0), N1);
Chris Lattner3657ffe2006-10-12 20:23:19 +00004082 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004083
Sylvestre Ledru94c22712012-09-27 10:14:43 +00004084 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
Scott Michelfdc40a02009-02-17 22:15:04 +00004085 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004086 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00004087 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00004088 DAG.ComputeMaskedBits(N0.getOperand(0), KnownZero, KnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00004089
Chris Lattner350bec02006-04-02 06:11:11 +00004090 // If any of the input bits are KnownOne, then the input couldn't be all
4091 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00004092 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004093
Chris Lattner350bec02006-04-02 06:11:11 +00004094 // If all of the bits input the to ctlz node are known to be zero, then
4095 // the result of the ctlz is "32" and the result of the shift is one.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00004096 APInt UnknownBits = ~KnownZero;
Chris Lattner350bec02006-04-02 06:11:11 +00004097 if (UnknownBits == 0) return DAG.getConstant(1, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004098
Chris Lattner350bec02006-04-02 06:11:11 +00004099 // Otherwise, check to see if there is exactly one bit input to the ctlz.
Bill Wendling88103372009-01-30 21:37:17 +00004100 if ((UnknownBits & (UnknownBits - 1)) == 0) {
Chris Lattner350bec02006-04-02 06:11:11 +00004101 // Okay, we know that only that the single bit specified by UnknownBits
Bill Wendling88103372009-01-30 21:37:17 +00004102 // could be set on input to the CTLZ node. If this bit is set, the SRL
4103 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
4104 // to an SRL/XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00004105 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00004106 SDValue Op = N0.getOperand(0);
Bill Wendling88103372009-01-30 21:37:17 +00004107
Chris Lattner350bec02006-04-02 06:11:11 +00004108 if (ShAmt) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004109 Op = DAG.getNode(ISD::SRL, SDLoc(N0), VT, Op,
Owen Anderson95771af2011-02-25 21:41:48 +00004110 DAG.getConstant(ShAmt, getShiftAmountTy(Op.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00004111 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00004112 }
Bill Wendling88103372009-01-30 21:37:17 +00004113
Andrew Trickac6d9be2013-05-25 02:42:55 +00004114 return DAG.getNode(ISD::XOR, SDLoc(N), VT,
Bill Wendling88103372009-01-30 21:37:17 +00004115 Op, DAG.getConstant(1, VT));
Chris Lattner350bec02006-04-02 06:11:11 +00004116 }
4117 }
Evan Chengeb9f8922008-08-30 02:03:58 +00004118
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00004119 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00004120 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00004121 N1.getOperand(0).getOpcode() == ISD::AND &&
4122 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00004123 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00004124 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00004125 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00004126 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00004127 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004128 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004129 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0,
4130 DAG.getNode(ISD::AND, SDLoc(N),
Bill Wendling88103372009-01-30 21:37:17 +00004131 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00004132 DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004133 SDLoc(N),
Bill Wendling9729c5a2009-01-31 03:12:48 +00004134 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00004135 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00004136 }
4137 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004138
Chris Lattner61a4c072007-04-18 03:06:49 +00004139 // fold operands of srl based on knowledge that the low bits are not
4140 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00004141 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
4142 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004143
Evan Cheng9ab2b982009-12-18 21:31:31 +00004144 if (N1C) {
4145 SDValue NewSRL = visitShiftByConstant(N, N1C->getZExtValue());
4146 if (NewSRL.getNode())
4147 return NewSRL;
4148 }
4149
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004150 // Attempt to convert a srl of a load into a narrower zero-extending load.
4151 SDValue NarrowLoad = ReduceLoadWidth(N);
4152 if (NarrowLoad.getNode())
4153 return NarrowLoad;
4154
Evan Cheng9ab2b982009-12-18 21:31:31 +00004155 // Here is a common situation. We want to optimize:
4156 //
4157 // %a = ...
4158 // %b = and i32 %a, 2
4159 // %c = srl i32 %b, 1
4160 // brcond i32 %c ...
4161 //
4162 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004163 //
Evan Cheng9ab2b982009-12-18 21:31:31 +00004164 // %a = ...
4165 // %b = and %a, 2
4166 // %c = setcc eq %b, 0
4167 // brcond %c ...
4168 //
4169 // However when after the source operand of SRL is optimized into AND, the SRL
4170 // itself may not be optimized further. Look for it and add the BRCOND into
4171 // the worklist.
Evan Chengd40d03e2010-01-06 19:38:29 +00004172 if (N->hasOneUse()) {
4173 SDNode *Use = *N->use_begin();
4174 if (Use->getOpcode() == ISD::BRCOND)
4175 AddToWorkList(Use);
4176 else if (Use->getOpcode() == ISD::TRUNCATE && Use->hasOneUse()) {
4177 // Also look pass the truncate.
4178 Use = *Use->use_begin();
4179 if (Use->getOpcode() == ISD::BRCOND)
4180 AddToWorkList(Use);
4181 }
4182 }
Evan Cheng9ab2b982009-12-18 21:31:31 +00004183
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004184 return SDValue();
Evan Cheng4c26e932010-04-19 19:29:22 +00004185}
4186
Dan Gohman475871a2008-07-27 21:46:04 +00004187SDValue DAGCombiner::visitCTLZ(SDNode *N) {
4188 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004189 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004190
4191 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004192 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004193 return DAG.getNode(ISD::CTLZ, SDLoc(N), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004194 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004195}
4196
Chandler Carruth63974b22011-12-13 01:56:10 +00004197SDValue DAGCombiner::visitCTLZ_ZERO_UNDEF(SDNode *N) {
4198 SDValue N0 = N->getOperand(0);
4199 EVT VT = N->getValueType(0);
4200
4201 // fold (ctlz_zero_undef c1) -> c2
4202 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004203 return DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SDLoc(N), VT, N0);
Chandler Carruth63974b22011-12-13 01:56:10 +00004204 return SDValue();
4205}
4206
Dan Gohman475871a2008-07-27 21:46:04 +00004207SDValue DAGCombiner::visitCTTZ(SDNode *N) {
4208 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004209 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004210
Nate Begeman1d4d4142005-09-01 00:19:25 +00004211 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004212 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004213 return DAG.getNode(ISD::CTTZ, SDLoc(N), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004214 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004215}
4216
Chandler Carruth63974b22011-12-13 01:56:10 +00004217SDValue DAGCombiner::visitCTTZ_ZERO_UNDEF(SDNode *N) {
4218 SDValue N0 = N->getOperand(0);
4219 EVT VT = N->getValueType(0);
4220
4221 // fold (cttz_zero_undef c1) -> c2
4222 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004223 return DAG.getNode(ISD::CTTZ_ZERO_UNDEF, SDLoc(N), VT, N0);
Chandler Carruth63974b22011-12-13 01:56:10 +00004224 return SDValue();
4225}
4226
Dan Gohman475871a2008-07-27 21:46:04 +00004227SDValue DAGCombiner::visitCTPOP(SDNode *N) {
4228 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004229 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004230
Nate Begeman1d4d4142005-09-01 00:19:25 +00004231 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004232 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004233 return DAG.getNode(ISD::CTPOP, SDLoc(N), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004234 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004235}
4236
Dan Gohman475871a2008-07-27 21:46:04 +00004237SDValue DAGCombiner::visitSELECT(SDNode *N) {
4238 SDValue N0 = N->getOperand(0);
4239 SDValue N1 = N->getOperand(1);
4240 SDValue N2 = N->getOperand(2);
Nate Begeman452d7be2005-09-16 00:54:12 +00004241 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
4242 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4243 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Owen Andersone50ed302009-08-10 22:56:29 +00004244 EVT VT = N->getValueType(0);
4245 EVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00004246
Bill Wendling34584e62009-01-30 22:02:18 +00004247 // fold (select C, X, X) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00004248 if (N1 == N2)
4249 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00004250 // fold (select true, X, Y) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00004251 if (N0C && !N0C->isNullValue())
4252 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00004253 // fold (select false, X, Y) -> Y
Nate Begeman452d7be2005-09-16 00:54:12 +00004254 if (N0C && N0C->isNullValue())
4255 return N2;
Bill Wendling34584e62009-01-30 22:02:18 +00004256 // fold (select C, 1, X) -> (or C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004257 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004258 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2);
Bill Wendling34584e62009-01-30 22:02:18 +00004259 // fold (select C, 0, 1) -> (xor C, 1)
Bob Wilson67ba2232009-01-22 22:05:48 +00004260 if (VT.isInteger() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004261 (VT0 == MVT::i1 ||
Bob Wilson67ba2232009-01-22 22:05:48 +00004262 (VT0.isInteger() &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00004263 TLI.getBooleanContents(false) ==
4264 TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004265 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00004266 SDValue XORNode;
Evan Cheng571c4782007-08-18 05:57:05 +00004267 if (VT == VT0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004268 return DAG.getNode(ISD::XOR, SDLoc(N), VT0,
Bill Wendling34584e62009-01-30 22:02:18 +00004269 N0, DAG.getConstant(1, VT0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004270 XORNode = DAG.getNode(ISD::XOR, SDLoc(N0), VT0,
Bill Wendling34584e62009-01-30 22:02:18 +00004271 N0, DAG.getConstant(1, VT0));
Gabor Greifba36cb52008-08-28 21:40:38 +00004272 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00004273 if (VT.bitsGT(VT0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004274 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, XORNode);
4275 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, XORNode);
Evan Cheng571c4782007-08-18 05:57:05 +00004276 }
Bill Wendling34584e62009-01-30 22:02:18 +00004277 // fold (select C, 0, X) -> (and (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004278 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004279 SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00004280 AddToWorkList(NOTNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004281 return DAG.getNode(ISD::AND, SDLoc(N), VT, NOTNode, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00004282 }
Bill Wendling34584e62009-01-30 22:02:18 +00004283 // fold (select C, X, 1) -> (or (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004284 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004285 SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00004286 AddToWorkList(NOTNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004287 return DAG.getNode(ISD::OR, SDLoc(N), VT, NOTNode, N1);
Nate Begeman452d7be2005-09-16 00:54:12 +00004288 }
Bill Wendling34584e62009-01-30 22:02:18 +00004289 // fold (select C, X, 0) -> (and C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004290 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00004291 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, N1);
Bill Wendling34584e62009-01-30 22:02:18 +00004292 // fold (select X, X, Y) -> (or X, Y)
4293 // fold (select X, 1, Y) -> (or X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00004294 if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004295 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2);
Bill Wendling34584e62009-01-30 22:02:18 +00004296 // fold (select X, Y, X) -> (and X, Y)
4297 // fold (select X, Y, 0) -> (and X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00004298 if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004299 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004300
Chris Lattner40c62d52005-10-18 06:04:22 +00004301 // If we can fold this based on the true/false value, do so.
4302 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00004303 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004304
Nate Begeman44728a72005-09-19 22:34:01 +00004305 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004306 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004307 // FIXME:
Owen Anderson825b72b2009-08-11 20:47:22 +00004308 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
Nate Begeman750ac1b2006-02-01 07:19:44 +00004309 // having to say they don't support SELECT_CC on every type the DAG knows
4310 // about, since there is no way to mark an opcode illegal at all value types
Owen Anderson825b72b2009-08-11 20:47:22 +00004311 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) &&
Dan Gohman4ea48042009-08-02 16:19:38 +00004312 TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004313 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT,
Bill Wendling34584e62009-01-30 22:02:18 +00004314 N0.getOperand(0), N0.getOperand(1),
Nate Begeman750ac1b2006-02-01 07:19:44 +00004315 N1, N2, N0.getOperand(2));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004316 return SimplifySelect(SDLoc(N), N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004317 }
Bill Wendling34584e62009-01-30 22:02:18 +00004318
Dan Gohman475871a2008-07-27 21:46:04 +00004319 return SDValue();
Nate Begeman452d7be2005-09-16 00:54:12 +00004320}
4321
Benjamin Kramer6242fda2013-04-26 09:19:19 +00004322SDValue DAGCombiner::visitVSELECT(SDNode *N) {
4323 SDValue N0 = N->getOperand(0);
4324 SDValue N1 = N->getOperand(1);
4325 SDValue N2 = N->getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004326 SDLoc DL(N);
Benjamin Kramer6242fda2013-04-26 09:19:19 +00004327
4328 // Canonicalize integer abs.
4329 // vselect (setg[te] X, 0), X, -X ->
4330 // vselect (setgt X, -1), X, -X ->
4331 // vselect (setl[te] X, 0), -X, X ->
4332 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4333 if (N0.getOpcode() == ISD::SETCC) {
4334 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
4335 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
4336 bool isAbs = false;
4337 bool RHSIsAllZeros = ISD::isBuildVectorAllZeros(RHS.getNode());
4338
4339 if (((RHSIsAllZeros && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
4340 (ISD::isBuildVectorAllOnes(RHS.getNode()) && CC == ISD::SETGT)) &&
4341 N1 == LHS && N2.getOpcode() == ISD::SUB && N1 == N2.getOperand(1))
4342 isAbs = ISD::isBuildVectorAllZeros(N2.getOperand(0).getNode());
4343 else if ((RHSIsAllZeros && (CC == ISD::SETLT || CC == ISD::SETLE)) &&
4344 N2 == LHS && N1.getOpcode() == ISD::SUB && N2 == N1.getOperand(1))
4345 isAbs = ISD::isBuildVectorAllZeros(N1.getOperand(0).getNode());
4346
4347 if (isAbs) {
4348 EVT VT = LHS.getValueType();
4349 SDValue Shift = DAG.getNode(
4350 ISD::SRA, DL, VT, LHS,
4351 DAG.getConstant(VT.getScalarType().getSizeInBits() - 1, VT));
4352 SDValue Add = DAG.getNode(ISD::ADD, DL, VT, LHS, Shift);
4353 AddToWorkList(Shift.getNode());
4354 AddToWorkList(Add.getNode());
4355 return DAG.getNode(ISD::XOR, DL, VT, Add, Shift);
4356 }
4357 }
4358
4359 return SDValue();
4360}
4361
Dan Gohman475871a2008-07-27 21:46:04 +00004362SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
4363 SDValue N0 = N->getOperand(0);
4364 SDValue N1 = N->getOperand(1);
4365 SDValue N2 = N->getOperand(2);
4366 SDValue N3 = N->getOperand(3);
4367 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00004368 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00004369
Nate Begeman44728a72005-09-19 22:34:01 +00004370 // fold select_cc lhs, rhs, x, x, cc -> x
4371 if (N2 == N3)
4372 return N2;
Scott Michelfdc40a02009-02-17 22:15:04 +00004373
Chris Lattner5f42a242006-09-20 06:19:26 +00004374 // Determine if the condition we're dealing with is constant
Matt Arsenault225ed702013-05-18 00:21:46 +00004375 SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004376 N0, N1, CC, SDLoc(N), false);
Stephen Lin7e6d6202013-06-15 04:03:33 +00004377 if (SCC.getNode()) {
4378 AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00004379
Stephen Lin7e6d6202013-06-15 04:03:33 +00004380 if (ConstantSDNode *SCCC = dyn_cast<ConstantSDNode>(SCC.getNode())) {
4381 if (!SCCC->isNullValue())
4382 return N2; // cond always true -> true val
4383 else
4384 return N3; // cond always false -> false val
4385 }
4386
4387 // Fold to a simpler select_cc
4388 if (SCC.getOpcode() == ISD::SETCC)
4389 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N2.getValueType(),
4390 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
4391 SCC.getOperand(2));
Chris Lattner5f42a242006-09-20 06:19:26 +00004392 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004393
Chris Lattner40c62d52005-10-18 06:04:22 +00004394 // If we can fold this based on the true/false value, do so.
4395 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00004396 return SDValue(N, 0); // Don't revisit N.
Scott Michelfdc40a02009-02-17 22:15:04 +00004397
Nate Begeman44728a72005-09-19 22:34:01 +00004398 // fold select_cc into other things, such as min/max/abs
Andrew Trickac6d9be2013-05-25 02:42:55 +00004399 return SimplifySelectCC(SDLoc(N), N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00004400}
4401
Dan Gohman475871a2008-07-27 21:46:04 +00004402SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7be2005-09-16 00:54:12 +00004403 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00004404 cast<CondCodeSDNode>(N->getOperand(2))->get(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004405 SDLoc(N));
Nate Begeman452d7be2005-09-16 00:54:12 +00004406}
4407
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004408// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
Dan Gohman57fc82d2009-04-09 03:51:29 +00004409// "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))"
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004410// transformation. Returns true if extension are possible and the above
Scott Michelfdc40a02009-02-17 22:15:04 +00004411// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00004412static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004413 unsigned ExtOpc,
Craig Toppera0ec3f92013-07-14 04:42:23 +00004414 SmallVectorImpl<SDNode *> &ExtendNodes,
Dan Gohman79ce2762009-01-15 19:20:50 +00004415 const TargetLowering &TLI) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004416 bool HasCopyToRegUses = false;
4417 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00004418 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
4419 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004420 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00004421 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004422 if (User == N)
4423 continue;
Dan Gohman57fc82d2009-04-09 03:51:29 +00004424 if (UI.getUse().getResNo() != N0.getResNo())
4425 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004426 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
Dan Gohman57fc82d2009-04-09 03:51:29 +00004427 if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004428 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
4429 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
4430 // Sign bits will be lost after a zext.
4431 return false;
4432 bool Add = false;
4433 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004434 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004435 if (UseOp == N0)
4436 continue;
4437 if (!isa<ConstantSDNode>(UseOp))
4438 return false;
4439 Add = true;
4440 }
4441 if (Add)
4442 ExtendNodes.push_back(User);
Dan Gohman57fc82d2009-04-09 03:51:29 +00004443 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004444 }
Dan Gohman57fc82d2009-04-09 03:51:29 +00004445 // If truncates aren't free and there are users we can't
4446 // extend, it isn't worthwhile.
4447 if (!isTruncFree)
4448 return false;
4449 // Remember if this value is live-out.
4450 if (User->getOpcode() == ISD::CopyToReg)
4451 HasCopyToRegUses = true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004452 }
4453
4454 if (HasCopyToRegUses) {
4455 bool BothLiveOut = false;
4456 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
4457 UI != UE; ++UI) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00004458 SDUse &Use = UI.getUse();
4459 if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) {
4460 BothLiveOut = true;
4461 break;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004462 }
4463 }
4464 if (BothLiveOut)
4465 // Both unextended and extended values are live out. There had better be
Bob Wilsonbebfbc52010-11-28 06:51:19 +00004466 // a good reason for the transformation.
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004467 return ExtendNodes.size();
4468 }
4469 return true;
4470}
4471
Craig Topper6c64fba2013-07-13 07:43:40 +00004472void DAGCombiner::ExtendSetCCUses(const SmallVectorImpl<SDNode *> &SetCCs,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004473 SDValue Trunc, SDValue ExtLoad, SDLoc DL,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004474 ISD::NodeType ExtType) {
4475 // Extend SetCC uses if necessary.
4476 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
4477 SDNode *SetCC = SetCCs[i];
4478 SmallVector<SDValue, 4> Ops;
4479
4480 for (unsigned j = 0; j != 2; ++j) {
4481 SDValue SOp = SetCC->getOperand(j);
4482 if (SOp == Trunc)
4483 Ops.push_back(ExtLoad);
4484 else
4485 Ops.push_back(DAG.getNode(ExtType, DL, ExtLoad->getValueType(0), SOp));
4486 }
4487
4488 Ops.push_back(SetCC->getOperand(2));
4489 CombineTo(SetCC, DAG.getNode(ISD::SETCC, DL, SetCC->getValueType(0),
4490 &Ops[0], Ops.size()));
4491 }
4492}
4493
Dan Gohman475871a2008-07-27 21:46:04 +00004494SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
4495 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004496 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004497
Nate Begeman1d4d4142005-09-01 00:19:25 +00004498 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004499 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004500 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004501
Nadav Rotem0c8607b2013-01-20 08:35:56 +00004502 // fold (sext (sext x)) -> (sext x)
4503 // fold (sext (aext x)) -> (sext x)
4504 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004505 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT,
Nadav Rotem0c8607b2013-01-20 08:35:56 +00004506 N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004507
Chris Lattner22558872007-02-26 03:13:59 +00004508 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004509 // fold (sext (truncate (load x))) -> (sext (smaller load x))
4510 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004511 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4512 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004513 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4514 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004515 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004516 // CombineTo deleted the truncate, if needed, but not what's under it.
4517 AddToWorkList(oye);
4518 }
Dan Gohmanc7b34442009-04-27 02:00:55 +00004519 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004520 }
Evan Chengc88138f2007-03-22 01:54:19 +00004521
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004522 // See if the value being truncated is already sign extended. If so, just
4523 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00004524 SDValue Op = N0.getOperand(0);
Dan Gohmand1996362010-01-09 02:13:55 +00004525 unsigned OpBits = Op.getValueType().getScalarType().getSizeInBits();
4526 unsigned MidBits = N0.getValueType().getScalarType().getSizeInBits();
4527 unsigned DestBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00004528 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004529
Chris Lattner22558872007-02-26 03:13:59 +00004530 if (OpBits == DestBits) {
4531 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
4532 // bits, it is already ready.
4533 if (NumSignBits > DestBits-MidBits)
4534 return Op;
4535 } else if (OpBits < DestBits) {
4536 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
4537 // bits, just sext from i32.
4538 if (NumSignBits > OpBits-MidBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004539 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, Op);
Chris Lattner22558872007-02-26 03:13:59 +00004540 } else {
4541 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
4542 // bits, just truncate to i32.
4543 if (NumSignBits > OpBits-MidBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004544 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00004545 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004546
Chris Lattner22558872007-02-26 03:13:59 +00004547 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sands25cf2272008-11-24 14:53:14 +00004548 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
4549 N0.getValueType())) {
Dan Gohmand1996362010-01-09 02:13:55 +00004550 if (OpBits < DestBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004551 Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N0), VT, Op);
Dan Gohmand1996362010-01-09 02:13:55 +00004552 else if (OpBits > DestBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004553 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT, Op);
4554 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, Op,
Dan Gohmand1996362010-01-09 02:13:55 +00004555 DAG.getValueType(N0.getValueType()));
Chris Lattner22558872007-02-26 03:13:59 +00004556 }
Chris Lattner6007b842006-09-21 06:00:20 +00004557 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004558
Evan Cheng110dec22005-12-14 02:19:23 +00004559 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004560 // None of the supported targets knows how to perform load and sign extend
Nadav Rotemfcd96192011-02-27 07:40:43 +00004561 // on vectors in one instruction. We only perform this transformation on
4562 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004563 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004564 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004565 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004566 bool DoXform = true;
4567 SmallVector<SDNode*, 4> SetCCs;
4568 if (!N0.hasOneUse())
4569 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
4570 if (DoXform) {
4571 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004572 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00004573 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004574 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004575 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004576 LN0->isVolatile(), LN0->isNonTemporal(),
4577 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004578 CombineTo(N, ExtLoad);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004579 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004580 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004581 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004582 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004583 ISD::SIGN_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004584 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004585 }
Nate Begeman3df4d522005-10-12 20:40:40 +00004586 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004587
4588 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
4589 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004590 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4591 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004592 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004593 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004594 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004595 TLI.isLoadExtLegal(ISD::SEXTLOAD, MemVT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004596 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004597 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004598 LN0->getBasePtr(), LN0->getPointerInfo(),
4599 MemVT,
David Greene1e559442010-02-15 17:00:31 +00004600 LN0->isVolatile(), LN0->isNonTemporal(),
4601 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004602 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004603 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004604 DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004605 N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004606 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004607 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004608 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004609 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004610
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004611 // fold (sext (and/or/xor (load x), cst)) ->
4612 // (and/or/xor (sextload x), (sext cst))
4613 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4614 N0.getOpcode() == ISD::XOR) &&
4615 isa<LoadSDNode>(N0.getOperand(0)) &&
4616 N0.getOperand(1).getOpcode() == ISD::Constant &&
4617 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()) &&
4618 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4619 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4620 if (LN0->getExtensionType() != ISD::ZEXTLOAD) {
4621 bool DoXform = true;
4622 SmallVector<SDNode*, 4> SetCCs;
4623 if (!N0.hasOneUse())
4624 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::SIGN_EXTEND,
4625 SetCCs, TLI);
4626 if (DoXform) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004627 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(LN0), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004628 LN0->getChain(), LN0->getBasePtr(),
4629 LN0->getPointerInfo(),
4630 LN0->getMemoryVT(),
4631 LN0->isVolatile(),
4632 LN0->isNonTemporal(),
4633 LN0->getAlignment());
4634 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4635 Mask = Mask.sext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004636 SDValue And = DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004637 ExtLoad, DAG.getConstant(Mask, VT));
4638 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004639 SDLoc(N0.getOperand(0)),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004640 N0.getOperand(0).getValueType(), ExtLoad);
4641 CombineTo(N, And);
4642 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004643 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004644 ISD::SIGN_EXTEND);
4645 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4646 }
4647 }
4648 }
4649
Chris Lattner20a35c32007-04-11 05:32:27 +00004650 if (N0.getOpcode() == ISD::SETCC) {
Chris Lattner2b7a2712009-07-08 00:31:33 +00004651 // sext(setcc) -> sext_in_reg(vsetcc) for vectors.
Dan Gohman3ce89f42010-04-30 17:19:19 +00004652 // Only do this before legalize for now.
Owen Andersoned5707b2013-04-23 18:09:28 +00004653 if (VT.isVector() && !LegalOperations &&
Stephen Lin155615d2013-07-08 00:37:03 +00004654 TLI.getBooleanContents(true) ==
Owen Andersoned5707b2013-04-23 18:09:28 +00004655 TargetLowering::ZeroOrNegativeOneBooleanContent) {
Dan Gohman3ce89f42010-04-30 17:19:19 +00004656 EVT N0VT = N0.getOperand(0).getValueType();
Nadav Rotem2e506192012-04-11 08:26:11 +00004657 // On some architectures (such as SSE/NEON/etc) the SETCC result type is
4658 // of the same size as the compared operands. Only optimize sext(setcc())
4659 // if this is the case.
Matt Arsenault225ed702013-05-18 00:21:46 +00004660 EVT SVT = getSetCCResultType(N0VT);
Nadav Rotem2e506192012-04-11 08:26:11 +00004661
4662 // We know that the # elements of the results is the same as the
4663 // # elements of the compare (and the # elements of the compare result
4664 // for that matter). Check to see that they are the same size. If so,
4665 // we know that the element size of the sext'd result matches the
4666 // element size of the compare operands.
4667 if (VT.getSizeInBits() == SVT.getSizeInBits())
Andrew Trickac6d9be2013-05-25 02:42:55 +00004668 return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00004669 N0.getOperand(1),
4670 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Matt Arsenault9aa8fdf2013-05-17 21:43:43 +00004671
Dan Gohman3ce89f42010-04-30 17:19:19 +00004672 // If the desired elements are smaller or larger than the source
4673 // elements we can use a matching integer vector type and then
4674 // truncate/sign extend
Matt Arsenault9aa8fdf2013-05-17 21:43:43 +00004675 EVT MatchingVectorType = N0VT.changeVectorElementTypeToInteger();
Craig Topper0eb5dad2012-09-29 07:18:53 +00004676 if (SVT == MatchingVectorType) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004677 SDValue VsetCC = DAG.getSetCC(SDLoc(N), MatchingVectorType,
Craig Topper0eb5dad2012-09-29 07:18:53 +00004678 N0.getOperand(0), N0.getOperand(1),
4679 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004680 return DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT);
Dan Gohman3ce89f42010-04-30 17:19:19 +00004681 }
Chris Lattner2b7a2712009-07-08 00:31:33 +00004682 }
Dan Gohman3ce89f42010-04-30 17:19:19 +00004683
Chris Lattner2b7a2712009-07-08 00:31:33 +00004684 // sext(setcc x, y, cc) -> (select_cc x, y, -1, 0, cc)
Dan Gohmana7bcef12010-04-24 01:17:30 +00004685 unsigned ElementWidth = VT.getScalarType().getSizeInBits();
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004686 SDValue NegOne =
Dan Gohmana7bcef12010-04-24 01:17:30 +00004687 DAG.getConstant(APInt::getAllOnesValue(ElementWidth), VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004688 SDValue SCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00004689 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004690 NegOne, DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004691 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004692 if (SCC.getNode()) return SCC;
Matt Arsenaultb05e4772013-06-14 22:04:37 +00004693 if (!VT.isVector() &&
4694 (!LegalOperations ||
4695 TLI.isOperationLegal(ISD::SETCC, getSetCCResultType(VT)))) {
4696 return DAG.getSelect(SDLoc(N), VT,
4697 DAG.getSetCC(SDLoc(N),
Jack Carteradbd3ae2013-10-17 01:34:33 +00004698 getSetCCResultType(VT),
4699 N0.getOperand(0), N0.getOperand(1),
4700 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
Matt Arsenaultb05e4772013-06-14 22:04:37 +00004701 NegOne, DAG.getConstant(0, VT));
4702 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004703 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004704
Dan Gohman8f0ad582008-04-28 16:58:24 +00004705 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sands25cf2272008-11-24 14:53:14 +00004706 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman187db7b2008-04-28 18:47:17 +00004707 DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004708 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004709
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004710 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004711}
4712
Rafael Espindoladecbc432012-04-09 16:06:03 +00004713// isTruncateOf - If N is a truncate of some other value, return true, record
4714// the value being truncated in Op and which of Op's bits are zero in KnownZero.
4715// This function computes KnownZero to avoid a duplicated call to
4716// ComputeMaskedBits in the caller.
4717static bool isTruncateOf(SelectionDAG &DAG, SDValue N, SDValue &Op,
4718 APInt &KnownZero) {
4719 APInt KnownOne;
4720 if (N->getOpcode() == ISD::TRUNCATE) {
4721 Op = N->getOperand(0);
4722 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4723 return true;
4724 }
4725
4726 if (N->getOpcode() != ISD::SETCC || N->getValueType(0) != MVT::i1 ||
4727 cast<CondCodeSDNode>(N->getOperand(2))->get() != ISD::SETNE)
4728 return false;
4729
4730 SDValue Op0 = N->getOperand(0);
4731 SDValue Op1 = N->getOperand(1);
4732 assert(Op0.getValueType() == Op1.getValueType());
4733
4734 ConstantSDNode *COp0 = dyn_cast<ConstantSDNode>(Op0);
4735 ConstantSDNode *COp1 = dyn_cast<ConstantSDNode>(Op1);
Rafael Espindolafdb230a2012-04-10 00:16:22 +00004736 if (COp0 && COp0->isNullValue())
Rafael Espindoladecbc432012-04-09 16:06:03 +00004737 Op = Op1;
Rafael Espindolafdb230a2012-04-10 00:16:22 +00004738 else if (COp1 && COp1->isNullValue())
Rafael Espindoladecbc432012-04-09 16:06:03 +00004739 Op = Op0;
4740 else
4741 return false;
4742
4743 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4744
4745 if (!(KnownZero | APInt(Op.getValueSizeInBits(), 1)).isAllOnesValue())
4746 return false;
4747
4748 return true;
4749}
4750
Dan Gohman475871a2008-07-27 21:46:04 +00004751SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
4752 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004753 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004754
Nate Begeman1d4d4142005-09-01 00:19:25 +00004755 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004756 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004757 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004758 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00004759 // fold (zext (aext x)) -> (zext x)
4760 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004761 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004762 N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00004763
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004764 // fold (zext (truncate x)) -> (zext x) or
4765 // (zext (truncate x)) -> (truncate x)
4766 // This is valid when the truncated bits of x are already zero.
4767 // FIXME: We should extend this to work for vectors too.
Rafael Espindoladecbc432012-04-09 16:06:03 +00004768 SDValue Op;
4769 APInt KnownZero;
4770 if (!VT.isVector() && isTruncateOf(DAG, N0, Op, KnownZero)) {
4771 APInt TruncatedBits =
4772 (Op.getValueSizeInBits() == N0.getValueSizeInBits()) ?
4773 APInt(Op.getValueSizeInBits(), 0) :
4774 APInt::getBitsSet(Op.getValueSizeInBits(),
4775 N0.getValueSizeInBits(),
4776 std::min(Op.getValueSizeInBits(),
4777 VT.getSizeInBits()));
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00004778 if (TruncatedBits == (KnownZero & TruncatedBits)) {
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004779 if (VT.bitsGT(Op.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004780 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, Op);
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004781 if (VT.bitsLT(Op.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004782 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004783
4784 return Op;
4785 }
4786 }
4787
Evan Chengc88138f2007-03-22 01:54:19 +00004788 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4789 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00004790 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004791 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4792 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004793 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4794 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004795 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004796 // CombineTo deleted the truncate, if needed, but not what's under it.
4797 AddToWorkList(oye);
4798 }
Eli Friedmane545d382011-04-16 23:25:34 +00004799 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004800 }
Evan Chengc88138f2007-03-22 01:54:19 +00004801 }
4802
Chris Lattner6007b842006-09-21 06:00:20 +00004803 // fold (zext (truncate x)) -> (and x, mask)
4804 if (N0.getOpcode() == ISD::TRUNCATE &&
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004805 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman394d6292010-11-03 01:47:46 +00004806
4807 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4808 // fold (zext (truncate (srl (load x), c))) -> (zext (smaller load (x+c/n)))
4809 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4810 if (NarrowLoad.getNode()) {
4811 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4812 if (NarrowLoad.getNode() != N0.getNode()) {
4813 CombineTo(N0.getNode(), NarrowLoad);
4814 // CombineTo deleted the truncate, if needed, but not what's under it.
4815 AddToWorkList(oye);
4816 }
4817 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4818 }
4819
Dan Gohman475871a2008-07-27 21:46:04 +00004820 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004821 if (Op.getValueType().bitsLT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004822 Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00004823 AddToWorkList(Op.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00004824 } else if (Op.getValueType().bitsGT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004825 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00004826 AddToWorkList(Op.getNode());
Chris Lattner6007b842006-09-21 06:00:20 +00004827 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00004828 return DAG.getZeroExtendInReg(Op, SDLoc(N),
Dan Gohman87862e72009-12-11 21:31:27 +00004829 N0.getValueType().getScalarType());
Chris Lattner6007b842006-09-21 06:00:20 +00004830 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004831
Dan Gohman97121ba2009-04-08 00:15:30 +00004832 // Fold (zext (and (trunc x), cst)) -> (and x, cst),
4833 // if either of the casts is not free.
Chris Lattner111c2282006-09-21 06:14:31 +00004834 if (N0.getOpcode() == ISD::AND &&
4835 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00004836 N0.getOperand(1).getOpcode() == ISD::Constant &&
4837 (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
4838 N0.getValueType()) ||
4839 !TLI.isZExtFree(N0.getValueType(), VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00004840 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004841 if (X.getValueType().bitsLT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004842 X = DAG.getNode(ISD::ANY_EXTEND, SDLoc(X), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004843 } else if (X.getValueType().bitsGT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004844 X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X);
Chris Lattner111c2282006-09-21 06:14:31 +00004845 }
Dan Gohman220a8232008-03-03 23:51:38 +00004846 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004847 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004848 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004849 X, DAG.getConstant(Mask, VT));
Chris Lattner111c2282006-09-21 06:14:31 +00004850 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004851
Evan Cheng110dec22005-12-14 02:19:23 +00004852 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Nadav Rotemed9b9342011-02-20 12:37:50 +00004853 // None of the supported targets knows how to perform load and vector_zext
Nadav Rotemfcd96192011-02-27 07:40:43 +00004854 // on vectors in one instruction. We only perform this transformation on
4855 // scalars.
Nadav Rotemed9b9342011-02-20 12:37:50 +00004856 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004857 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004858 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004859 bool DoXform = true;
4860 SmallVector<SDNode*, 4> SetCCs;
4861 if (!N0.hasOneUse())
4862 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
4863 if (DoXform) {
4864 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004865 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004866 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004867 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004868 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004869 LN0->isVolatile(), LN0->isNonTemporal(),
4870 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004871 CombineTo(N, ExtLoad);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004872 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004873 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004874 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling6ce610f2009-01-30 22:23:15 +00004875
Andrew Trickac6d9be2013-05-25 02:42:55 +00004876 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004877 ISD::ZERO_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004878 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004879 }
Evan Cheng110dec22005-12-14 02:19:23 +00004880 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004881
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004882 // fold (zext (and/or/xor (load x), cst)) ->
4883 // (and/or/xor (zextload x), (zext cst))
4884 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4885 N0.getOpcode() == ISD::XOR) &&
4886 isa<LoadSDNode>(N0.getOperand(0)) &&
4887 N0.getOperand(1).getOpcode() == ISD::Constant &&
4888 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()) &&
4889 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4890 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4891 if (LN0->getExtensionType() != ISD::SEXTLOAD) {
4892 bool DoXform = true;
4893 SmallVector<SDNode*, 4> SetCCs;
4894 if (!N0.hasOneUse())
4895 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::ZERO_EXTEND,
4896 SetCCs, TLI);
4897 if (DoXform) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004898 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004899 LN0->getChain(), LN0->getBasePtr(),
4900 LN0->getPointerInfo(),
4901 LN0->getMemoryVT(),
4902 LN0->isVolatile(),
4903 LN0->isNonTemporal(),
4904 LN0->getAlignment());
4905 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4906 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004907 SDValue And = DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004908 ExtLoad, DAG.getConstant(Mask, VT));
4909 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004910 SDLoc(N0.getOperand(0)),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004911 N0.getOperand(0).getValueType(), ExtLoad);
4912 CombineTo(N, And);
4913 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004914 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004915 ISD::ZERO_EXTEND);
4916 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4917 }
4918 }
4919 }
4920
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004921 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
4922 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004923 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4924 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004925 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004926 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004927 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004928 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004929 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004930 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004931 LN0->getBasePtr(), LN0->getPointerInfo(),
4932 MemVT,
David Greene1e559442010-02-15 17:00:31 +00004933 LN0->isVolatile(), LN0->isNonTemporal(),
4934 LN0->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004935 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004936 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004937 DAG.getNode(ISD::TRUNCATE, SDLoc(N0), N0.getValueType(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004938 ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004939 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004940 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004941 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004942 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004943
Chris Lattner20a35c32007-04-11 05:32:27 +00004944 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00004945 if (!LegalOperations && VT.isVector()) {
4946 // zext(setcc) -> (and (vsetcc), (1, 1, ...) for vectors.
4947 // Only do this before legalize for now.
4948 EVT N0VT = N0.getOperand(0).getValueType();
4949 EVT EltVT = VT.getVectorElementType();
4950 SmallVector<SDValue,8> OneOps(VT.getVectorNumElements(),
4951 DAG.getConstant(1, EltVT));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004952 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Evan Cheng0a942db2010-05-19 01:08:17 +00004953 // We know that the # elements of the results is the same as the
4954 // # elements of the compare (and the # elements of the compare result
4955 // for that matter). Check to see that they are the same size. If so,
4956 // we know that the element size of the sext'd result matches the
4957 // element size of the compare operands.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004958 return DAG.getNode(ISD::AND, SDLoc(N), VT,
4959 DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Evan Cheng0a942db2010-05-19 01:08:17 +00004960 N0.getOperand(1),
4961 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004962 DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT,
Evan Cheng0a942db2010-05-19 01:08:17 +00004963 &OneOps[0], OneOps.size()));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004964
4965 // If the desired elements are smaller or larger than the source
4966 // elements we can use a matching integer vector type and then
4967 // truncate/sign extend
4968 EVT MatchingElementType =
4969 EVT::getIntegerVT(*DAG.getContext(),
4970 N0VT.getScalarType().getSizeInBits());
4971 EVT MatchingVectorType =
4972 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
4973 N0VT.getVectorNumElements());
4974 SDValue VsetCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00004975 DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0),
Dan Gohman71dc7c92011-05-17 22:20:36 +00004976 N0.getOperand(1),
4977 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004978 return DAG.getNode(ISD::AND, SDLoc(N), VT,
4979 DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT),
4980 DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT,
Dan Gohman71dc7c92011-05-17 22:20:36 +00004981 &OneOps[0], OneOps.size()));
Evan Cheng0a942db2010-05-19 01:08:17 +00004982 }
4983
4984 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00004985 SDValue SCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00004986 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Chris Lattner20a35c32007-04-11 05:32:27 +00004987 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004988 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004989 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00004990 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004991
Evan Cheng9818c042009-12-15 03:00:32 +00004992 // (zext (shl (zext x), cst)) -> (shl (zext x), cst)
Evan Cheng99b653c2009-12-15 00:41:36 +00004993 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL) &&
Evan Cheng9818c042009-12-15 03:00:32 +00004994 isa<ConstantSDNode>(N0.getOperand(1)) &&
Evan Cheng99b653c2009-12-15 00:41:36 +00004995 N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND &&
4996 N0.hasOneUse()) {
Chris Lattnere0751182011-02-13 19:09:16 +00004997 SDValue ShAmt = N0.getOperand(1);
4998 unsigned ShAmtVal = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Evan Cheng9818c042009-12-15 03:00:32 +00004999 if (N0.getOpcode() == ISD::SHL) {
Chris Lattnere0751182011-02-13 19:09:16 +00005000 SDValue InnerZExt = N0.getOperand(0);
Evan Cheng9818c042009-12-15 03:00:32 +00005001 // If the original shl may be shifting out bits, do not perform this
5002 // transformation.
Chris Lattnere0751182011-02-13 19:09:16 +00005003 unsigned KnownZeroBits = InnerZExt.getValueType().getSizeInBits() -
5004 InnerZExt.getOperand(0).getValueType().getSizeInBits();
5005 if (ShAmtVal > KnownZeroBits)
Evan Cheng9818c042009-12-15 03:00:32 +00005006 return SDValue();
5007 }
Chris Lattnere0751182011-02-13 19:09:16 +00005008
Andrew Trickac6d9be2013-05-25 02:42:55 +00005009 SDLoc DL(N);
Owen Anderson95771af2011-02-25 21:41:48 +00005010
5011 // Ensure that the shift amount is wide enough for the shifted value.
Chris Lattnere0751182011-02-13 19:09:16 +00005012 if (VT.getSizeInBits() >= 256)
5013 ShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, ShAmt);
Owen Anderson95771af2011-02-25 21:41:48 +00005014
Chris Lattnere0751182011-02-13 19:09:16 +00005015 return DAG.getNode(N0.getOpcode(), DL, VT,
5016 DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)),
5017 ShAmt);
Evan Cheng99b653c2009-12-15 00:41:36 +00005018 }
5019
Evan Chengb3a3d5e2010-04-28 07:10:39 +00005020 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005021}
5022
Dan Gohman475871a2008-07-27 21:46:04 +00005023SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
5024 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005025 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005026
Chris Lattner5ffc0662006-05-05 05:58:59 +00005027 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00005028 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005029 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, N0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00005030 // fold (aext (aext x)) -> (aext x)
5031 // fold (aext (zext x)) -> (zext x)
5032 // fold (aext (sext x)) -> (sext x)
5033 if (N0.getOpcode() == ISD::ANY_EXTEND ||
5034 N0.getOpcode() == ISD::ZERO_EXTEND ||
5035 N0.getOpcode() == ISD::SIGN_EXTEND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005036 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00005037
Evan Chengc88138f2007-03-22 01:54:19 +00005038 // fold (aext (truncate (load x))) -> (aext (smaller load x))
5039 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
5040 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005041 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
5042 if (NarrowLoad.getNode()) {
Dale Johannesen86234c32010-05-25 18:47:23 +00005043 SDNode* oye = N0.getNode()->getOperand(0).getNode();
5044 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005045 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen86234c32010-05-25 18:47:23 +00005046 // CombineTo deleted the truncate, if needed, but not what's under it.
5047 AddToWorkList(oye);
5048 }
Eli Friedmane545d382011-04-16 23:25:34 +00005049 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00005050 }
Evan Chengc88138f2007-03-22 01:54:19 +00005051 }
5052
Chris Lattner84750582006-09-20 06:29:17 +00005053 // fold (aext (truncate x))
5054 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00005055 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00005056 if (TruncOp.getValueType() == VT)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005057 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005058 if (TruncOp.getValueType().bitsGT(VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005059 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, TruncOp);
5060 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, TruncOp);
Chris Lattner84750582006-09-20 06:29:17 +00005061 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005062
Dan Gohman97121ba2009-04-08 00:15:30 +00005063 // Fold (aext (and (trunc x), cst)) -> (and x, cst)
5064 // if the trunc is not free.
Chris Lattner0e4b9222006-09-21 06:40:43 +00005065 if (N0.getOpcode() == ISD::AND &&
5066 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00005067 N0.getOperand(1).getOpcode() == ISD::Constant &&
5068 !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
5069 N0.getValueType())) {
Dan Gohman475871a2008-07-27 21:46:04 +00005070 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005071 if (X.getValueType().bitsLT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005072 X = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005073 } else if (X.getValueType().bitsGT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005074 X = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, X);
Chris Lattner0e4b9222006-09-21 06:40:43 +00005075 }
Dan Gohman220a8232008-03-03 23:51:38 +00005076 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00005077 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005078 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling683c9572009-01-30 22:27:33 +00005079 X, DAG.getConstant(Mask, VT));
Chris Lattner0e4b9222006-09-21 06:40:43 +00005080 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005081
Chris Lattner5ffc0662006-05-05 05:58:59 +00005082 // fold (aext (load x)) -> (aext (truncate (extload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005083 // None of the supported targets knows how to perform load and any_ext
Nadav Rotemfcd96192011-02-27 07:40:43 +00005084 // on vectors in one instruction. We only perform this transformation on
5085 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005086 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005087 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005088 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00005089 bool DoXform = true;
5090 SmallVector<SDNode*, 4> SetCCs;
5091 if (!N0.hasOneUse())
5092 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI);
5093 if (DoXform) {
5094 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005095 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00005096 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005097 LN0->getBasePtr(), LN0->getPointerInfo(),
Dan Gohman57fc82d2009-04-09 03:51:29 +00005098 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00005099 LN0->isVolatile(), LN0->isNonTemporal(),
5100 LN0->getAlignment());
Dan Gohman57fc82d2009-04-09 03:51:29 +00005101 CombineTo(N, ExtLoad);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005102 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Dan Gohman57fc82d2009-04-09 03:51:29 +00005103 N0.getValueType(), ExtLoad);
5104 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005105 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00005106 ISD::ANY_EXTEND);
Dan Gohman57fc82d2009-04-09 03:51:29 +00005107 return SDValue(N, 0); // Return N so it doesn't get rechecked!
5108 }
Chris Lattner5ffc0662006-05-05 05:58:59 +00005109 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005110
Chris Lattner5ffc0662006-05-05 05:58:59 +00005111 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
5112 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
5113 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00005114 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005115 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00005116 N0.hasOneUse()) {
5117 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005118 EVT MemVT = LN0->getMemoryVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005119 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), SDLoc(N),
Stuart Hastingsa9011292011-02-16 16:23:55 +00005120 VT, LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005121 LN0->getPointerInfo(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00005122 LN0->isVolatile(), LN0->isNonTemporal(),
5123 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00005124 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00005125 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005126 DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling683c9572009-01-30 22:27:33 +00005127 N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00005128 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005129 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00005130 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005131
Chris Lattner20a35c32007-04-11 05:32:27 +00005132 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00005133 // aext(setcc) -> sext_in_reg(vsetcc) for vectors.
5134 // Only do this before legalize for now.
5135 if (VT.isVector() && !LegalOperations) {
5136 EVT N0VT = N0.getOperand(0).getValueType();
5137 // We know that the # elements of the results is the same as the
5138 // # elements of the compare (and the # elements of the compare result
5139 // for that matter). Check to see that they are the same size. If so,
5140 // we know that the element size of the sext'd result matches the
5141 // element size of the compare operands.
5142 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Andrew Trickac6d9be2013-05-25 02:42:55 +00005143 return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00005144 N0.getOperand(1),
5145 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Evan Cheng0a942db2010-05-19 01:08:17 +00005146 // If the desired elements are smaller or larger than the source
5147 // elements we can use a matching integer vector type and then
5148 // truncate/sign extend
5149 else {
Duncan Sands34727662010-07-12 08:16:59 +00005150 EVT MatchingElementType =
5151 EVT::getIntegerVT(*DAG.getContext(),
5152 N0VT.getScalarType().getSizeInBits());
5153 EVT MatchingVectorType =
5154 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
5155 N0VT.getVectorNumElements());
5156 SDValue VsetCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00005157 DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00005158 N0.getOperand(1),
5159 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005160 return DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT);
Evan Cheng0a942db2010-05-19 01:08:17 +00005161 }
5162 }
5163
5164 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00005165 SDValue SCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00005166 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Chris Lattner1eba01e2007-04-11 06:50:51 +00005167 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00005168 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00005169 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005170 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00005171 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005172
Evan Chengb3a3d5e2010-04-28 07:10:39 +00005173 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00005174}
5175
Chris Lattner2b4c2792007-10-13 06:35:54 +00005176/// GetDemandedBits - See if the specified operand can be simplified with the
5177/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00005178/// simpler operand, otherwise return a null SDValue.
5179SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00005180 switch (V.getOpcode()) {
5181 default: break;
Lang Hames5207bf22011-11-08 18:56:23 +00005182 case ISD::Constant: {
5183 const ConstantSDNode *CV = cast<ConstantSDNode>(V.getNode());
5184 assert(CV != 0 && "Const value should be ConstSDNode.");
5185 const APInt &CVal = CV->getAPIntValue();
5186 APInt NewVal = CVal & Mask;
Stephen Linb4940152013-07-09 00:44:49 +00005187 if (NewVal != CVal)
Lang Hames5207bf22011-11-08 18:56:23 +00005188 return DAG.getConstant(NewVal, V.getValueType());
Lang Hames5207bf22011-11-08 18:56:23 +00005189 break;
5190 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00005191 case ISD::OR:
5192 case ISD::XOR:
5193 // If the LHS or RHS don't contribute bits to the or, drop them.
5194 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
5195 return V.getOperand(1);
5196 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
5197 return V.getOperand(0);
5198 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00005199 case ISD::SRL:
5200 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00005201 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00005202 break;
5203 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
5204 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005205 unsigned Amt = RHSC->getZExtValue();
Bill Wendling8509c902009-01-30 22:33:24 +00005206
Dan Gohmancc91d632009-01-03 19:22:06 +00005207 // Watch out for shift count overflow though.
5208 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005209 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00005210 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Bill Wendling8509c902009-01-30 22:33:24 +00005211 if (SimplifyLHS.getNode())
Andrew Trickac6d9be2013-05-25 02:42:55 +00005212 return DAG.getNode(ISD::SRL, SDLoc(V), V.getValueType(),
Chris Lattnere33544c2007-10-13 06:58:48 +00005213 SimplifyLHS, V.getOperand(1));
Chris Lattnere33544c2007-10-13 06:58:48 +00005214 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00005215 }
Dan Gohman475871a2008-07-27 21:46:04 +00005216 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00005217}
5218
Evan Chengc88138f2007-03-22 01:54:19 +00005219/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
5220/// bits and then truncated to a narrower type and where N is a multiple
5221/// of number of bits of the narrower type, transform it to a narrower load
5222/// from address + N / num of bits of new type. If the result is to be
5223/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00005224SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00005225 unsigned Opc = N->getOpcode();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005226
Evan Chengc88138f2007-03-22 01:54:19 +00005227 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00005228 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005229 EVT VT = N->getValueType(0);
5230 EVT ExtVT = VT;
Evan Chengc88138f2007-03-22 01:54:19 +00005231
Dan Gohman7f8613e2008-08-14 20:04:46 +00005232 // This transformation isn't valid for vector loads.
5233 if (VT.isVector())
5234 return SDValue();
5235
Dan Gohmand1996362010-01-09 02:13:55 +00005236 // Special case: SIGN_EXTEND_INREG is basically truncating to ExtVT then
Evan Chenge177e302007-03-23 22:13:36 +00005237 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00005238 if (Opc == ISD::SIGN_EXTEND_INREG) {
5239 ExtType = ISD::SEXTLOAD;
Owen Andersone50ed302009-08-10 22:56:29 +00005240 ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005241 } else if (Opc == ISD::SRL) {
Chris Lattner90b03642010-12-21 18:05:22 +00005242 // Another special-case: SRL is basically zero-extending a narrower value.
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005243 ExtType = ISD::ZEXTLOAD;
5244 N0 = SDValue(N, 0);
5245 ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1));
5246 if (!N01) return SDValue();
5247 ExtVT = EVT::getIntegerVT(*DAG.getContext(),
5248 VT.getSizeInBits() - N01->getZExtValue());
Evan Chengc88138f2007-03-22 01:54:19 +00005249 }
Richard Osborne4e3740e2011-01-31 17:41:44 +00005250 if (LegalOperations && !TLI.isLoadExtLegal(ExtType, ExtVT))
5251 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005252
Owen Andersone50ed302009-08-10 22:56:29 +00005253 unsigned EVTBits = ExtVT.getSizeInBits();
Owen Anderson95771af2011-02-25 21:41:48 +00005254
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005255 // Do not generate loads of non-round integer types since these can
5256 // be expensive (and would be wrong if the type is not byte sized).
5257 if (!ExtVT.isRound())
5258 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005259
Evan Chengc88138f2007-03-22 01:54:19 +00005260 unsigned ShAmt = 0;
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005261 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
Evan Chengc88138f2007-03-22 01:54:19 +00005262 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005263 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005264 // Is the shift amount a multiple of size of VT?
5265 if ((ShAmt & (EVTBits-1)) == 0) {
5266 N0 = N0.getOperand(0);
Eli Friedmand68eea22009-08-19 08:46:10 +00005267 // Is the load width a multiple of size of VT?
5268 if ((N0.getValueType().getSizeInBits() & (EVTBits-1)) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00005269 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005270 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005271
Chris Lattnercbf68df2010-12-22 08:02:57 +00005272 // At this point, we must have a load or else we can't do the transform.
5273 if (!isa<LoadSDNode>(N0)) return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005274
Chandler Carruth1c49fda2012-12-11 00:36:57 +00005275 // Because a SRL must be assumed to *need* to zero-extend the high bits
5276 // (as opposed to anyext the high bits), we can't combine the zextload
5277 // lowering of SRL and an sextload.
5278 if (cast<LoadSDNode>(N0)->getExtensionType() == ISD::SEXTLOAD)
5279 return SDValue();
5280
Chris Lattner2831a192010-10-01 05:36:09 +00005281 // If the shift amount is larger than the input type then we're not
5282 // accessing any of the loaded bytes. If the load was a zextload/extload
5283 // then the result of the shift+trunc is zero/undef (handled elsewhere).
Chris Lattnercbf68df2010-12-22 08:02:57 +00005284 if (ShAmt >= cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits())
Chris Lattner2831a192010-10-01 05:36:09 +00005285 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005286 }
5287 }
5288
Dan Gohman394d6292010-11-03 01:47:46 +00005289 // If the load is shifted left (and the result isn't shifted back right),
5290 // we can fold the truncate through the shift.
5291 unsigned ShLeftAmt = 0;
5292 if (ShAmt == 0 && N0.getOpcode() == ISD::SHL && N0.hasOneUse() &&
Chris Lattner4c32bc22010-12-22 07:36:50 +00005293 ExtVT == VT && TLI.isNarrowingProfitable(N0.getValueType(), VT)) {
Dan Gohman394d6292010-11-03 01:47:46 +00005294 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
5295 ShLeftAmt = N01->getZExtValue();
5296 N0 = N0.getOperand(0);
5297 }
5298 }
Owen Anderson95771af2011-02-25 21:41:48 +00005299
Chris Lattner4c32bc22010-12-22 07:36:50 +00005300 // If we haven't found a load, we can't narrow it. Don't transform one with
5301 // multiple uses, this would require adding a new load.
Bill Schmidt89e88e32013-01-14 22:04:38 +00005302 if (!isa<LoadSDNode>(N0) || !N0.hasOneUse())
5303 return SDValue();
5304
5305 // Don't change the width of a volatile load.
5306 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
5307 if (LN0->isVolatile())
Chris Lattner4c32bc22010-12-22 07:36:50 +00005308 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005309
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005310 // Verify that we are actually reducing a load width here.
Bill Schmidt89e88e32013-01-14 22:04:38 +00005311 if (LN0->getMemoryVT().getSizeInBits() < EVTBits)
Chris Lattner4c32bc22010-12-22 07:36:50 +00005312 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005313
Bill Schmidt89e88e32013-01-14 22:04:38 +00005314 // For the transform to be legal, the load must produce only two values
5315 // (the value loaded and the chain). Don't transform a pre-increment
Stephen Lin155615d2013-07-08 00:37:03 +00005316 // load, for example, which produces an extra value. Otherwise the
Bill Schmidt89e88e32013-01-14 22:04:38 +00005317 // transformation is not equivalent, and the downstream logic to replace
5318 // uses gets things wrong.
5319 if (LN0->getNumValues() > 2)
5320 return SDValue();
5321
Benjamin Kramerf4eeab42013-07-06 14:05:09 +00005322 // If the load that we're shrinking is an extload and we're not just
5323 // discarding the extension we can't simply shrink the load. Bail.
5324 // TODO: It would be possible to merge the extensions in some cases.
5325 if (LN0->getExtensionType() != ISD::NON_EXTLOAD &&
5326 LN0->getMemoryVT().getSizeInBits() < ExtVT.getSizeInBits() + ShAmt)
5327 return SDValue();
5328
Chris Lattner4c32bc22010-12-22 07:36:50 +00005329 EVT PtrType = N0.getOperand(1).getValueType();
Bill Wendling8509c902009-01-30 22:33:24 +00005330
Evan Cheng16436df2012-06-26 01:19:33 +00005331 if (PtrType == MVT::Untyped || PtrType.isExtended())
5332 // It's not possible to generate a constant of extended or untyped type.
5333 return SDValue();
5334
Chris Lattner4c32bc22010-12-22 07:36:50 +00005335 // For big endian targets, we need to adjust the offset to the pointer to
5336 // load the correct bytes.
5337 if (TLI.isBigEndian()) {
5338 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
5339 unsigned EVTStoreBits = ExtVT.getStoreSizeInBits();
5340 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
Evan Chengc88138f2007-03-22 01:54:19 +00005341 }
5342
Chris Lattner4c32bc22010-12-22 07:36:50 +00005343 uint64_t PtrOff = ShAmt / 8;
5344 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005345 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(LN0),
Chris Lattner4c32bc22010-12-22 07:36:50 +00005346 PtrType, LN0->getBasePtr(),
5347 DAG.getConstant(PtrOff, PtrType));
5348 AddToWorkList(NewPtr.getNode());
5349
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005350 SDValue Load;
5351 if (ExtType == ISD::NON_EXTLOAD)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005352 Load = DAG.getLoad(VT, SDLoc(N0), LN0->getChain(), NewPtr,
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005353 LN0->getPointerInfo().getWithOffset(PtrOff),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005354 LN0->isVolatile(), LN0->isNonTemporal(),
5355 LN0->isInvariant(), NewAlign);
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005356 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00005357 Load = DAG.getExtLoad(ExtType, SDLoc(N0), VT, LN0->getChain(),NewPtr,
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005358 LN0->getPointerInfo().getWithOffset(PtrOff),
5359 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
5360 NewAlign);
Chris Lattner4c32bc22010-12-22 07:36:50 +00005361
5362 // Replace the old load's chain with the new load's chain.
5363 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005364 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1));
Chris Lattner4c32bc22010-12-22 07:36:50 +00005365
5366 // Shift the result left, if we've swallowed a left shift.
5367 SDValue Result = Load;
5368 if (ShLeftAmt != 0) {
Owen Anderson95771af2011-02-25 21:41:48 +00005369 EVT ShImmTy = getShiftAmountTy(Result.getValueType());
Chris Lattner4c32bc22010-12-22 07:36:50 +00005370 if (!isUIntN(ShImmTy.getSizeInBits(), ShLeftAmt))
5371 ShImmTy = VT;
Paul Redmond5c974502013-02-12 15:21:21 +00005372 // If the shift amount is as large as the result size (but, presumably,
5373 // no larger than the source) then the useful bits of the result are
5374 // zero; we can't simply return the shortened shift, because the result
5375 // of that operation is undefined.
5376 if (ShLeftAmt >= VT.getSizeInBits())
5377 Result = DAG.getConstant(0, VT);
5378 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00005379 Result = DAG.getNode(ISD::SHL, SDLoc(N0), VT,
Paul Redmond5c974502013-02-12 15:21:21 +00005380 Result, DAG.getConstant(ShLeftAmt, ShImmTy));
Chris Lattner4c32bc22010-12-22 07:36:50 +00005381 }
5382
5383 // Return the new loaded value.
5384 return Result;
Evan Chengc88138f2007-03-22 01:54:19 +00005385}
5386
Dan Gohman475871a2008-07-27 21:46:04 +00005387SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
5388 SDValue N0 = N->getOperand(0);
5389 SDValue N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005390 EVT VT = N->getValueType(0);
5391 EVT EVT = cast<VTSDNode>(N1)->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00005392 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmand1996362010-01-09 02:13:55 +00005393 unsigned EVTBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00005394
Nate Begeman1d4d4142005-09-01 00:19:25 +00005395 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00005396 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005397 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005398
Chris Lattner541a24f2006-05-06 22:43:44 +00005399 // If the input is already sign extended, just drop the extension.
Dan Gohman87862e72009-12-11 21:31:27 +00005400 if (DAG.ComputeNumSignBits(N0) >= VTBits-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00005401 return N0;
Scott Michelfdc40a02009-02-17 22:15:04 +00005402
Nate Begeman646d7e22005-09-02 21:18:40 +00005403 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
5404 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Stephen Linb4940152013-07-09 00:44:49 +00005405 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005406 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005407 N0.getOperand(0), N1);
Chris Lattner4b37e872006-05-08 21:18:59 +00005408
Dan Gohman75dcf082008-07-31 00:50:31 +00005409 // fold (sext_in_reg (sext x)) -> (sext x)
5410 // fold (sext_in_reg (aext x)) -> (sext x)
5411 // if x is small enough.
5412 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
5413 SDValue N00 = N0.getOperand(0);
Evan Cheng003d7c42010-04-16 22:26:19 +00005414 if (N00.getValueType().getScalarType().getSizeInBits() <= EVTBits &&
5415 (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005416 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N00, N1);
Dan Gohman75dcf082008-07-31 00:50:31 +00005417 }
5418
Chris Lattner95a5e052007-04-17 19:03:21 +00005419 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005420 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005421 return DAG.getZeroExtendInReg(N0, SDLoc(N), EVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00005422
Chris Lattner95a5e052007-04-17 19:03:21 +00005423 // fold operands of sext_in_reg based on knowledge that the top bits are not
5424 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00005425 if (SimplifyDemandedBits(SDValue(N, 0)))
5426 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005427
Evan Chengc88138f2007-03-22 01:54:19 +00005428 // fold (sext_in_reg (load x)) -> (smaller sextload x)
5429 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00005430 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005431 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00005432 return NarrowLoad;
5433
Bill Wendling8509c902009-01-30 22:33:24 +00005434 // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005435 // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible.
Chris Lattner4b37e872006-05-08 21:18:59 +00005436 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
5437 if (N0.getOpcode() == ISD::SRL) {
5438 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman87862e72009-12-11 21:31:27 +00005439 if (ShAmt->getZExtValue()+EVTBits <= VTBits) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005440 // We can turn this into an SRA iff the input to the SRL is already sign
Chris Lattner4b37e872006-05-08 21:18:59 +00005441 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00005442 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohman87862e72009-12-11 21:31:27 +00005443 if (VTBits-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005444 return DAG.getNode(ISD::SRA, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005445 N0.getOperand(0), N0.getOperand(1));
Chris Lattner4b37e872006-05-08 21:18:59 +00005446 }
5447 }
Evan Chengc88138f2007-03-22 01:54:19 +00005448
Nate Begemanded49632005-10-13 03:11:28 +00005449 // fold (sext_inreg (extload x)) -> (sextload x)
Scott Michelfdc40a02009-02-17 22:15:04 +00005450 if (ISD::isEXTLoad(N0.getNode()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005451 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005452 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005453 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005454 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005455 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005456 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005457 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005458 LN0->getBasePtr(), LN0->getPointerInfo(),
5459 EVT,
David Greene1e559442010-02-15 17:00:31 +00005460 LN0->isVolatile(), LN0->isNonTemporal(),
5461 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00005462 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00005463 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Elena Demikhovsky4b977312012-12-19 07:50:20 +00005464 AddToWorkList(ExtLoad.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00005465 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00005466 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005467 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00005468 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00005469 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005470 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005471 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005472 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005473 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005474 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005475 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005476 LN0->getBasePtr(), LN0->getPointerInfo(),
5477 EVT,
David Greene1e559442010-02-15 17:00:31 +00005478 LN0->isVolatile(), LN0->isNonTemporal(),
5479 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00005480 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00005481 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005482 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00005483 }
Evan Cheng9568e5c2011-06-21 06:01:08 +00005484
5485 // Form (sext_inreg (bswap >> 16)) or (sext_inreg (rotl (bswap) 16))
5486 if (EVTBits <= 16 && N0.getOpcode() == ISD::OR) {
5487 SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0),
5488 N0.getOperand(1), false);
5489 if (BSwap.getNode() != 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005490 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Evan Cheng9568e5c2011-06-21 06:01:08 +00005491 BSwap, N1);
5492 }
5493
Dan Gohman475871a2008-07-27 21:46:04 +00005494 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005495}
5496
Dan Gohman475871a2008-07-27 21:46:04 +00005497SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
5498 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005499 EVT VT = N->getValueType(0);
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005500 bool isLE = TLI.isLittleEndian();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005501
5502 // noop truncate
5503 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00005504 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00005505 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00005506 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005507 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005508 // fold (truncate (truncate x)) -> (truncate x)
5509 if (N0.getOpcode() == ISD::TRUNCATE)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005510 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00005511 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattner7f893c02010-04-07 18:13:33 +00005512 if (N0.getOpcode() == ISD::ZERO_EXTEND ||
5513 N0.getOpcode() == ISD::SIGN_EXTEND ||
Chris Lattnerb72773b2006-05-05 22:56:26 +00005514 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00005515 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00005516 // if the source is smaller than the dest, we still need an extend
Andrew Trickac6d9be2013-05-25 02:42:55 +00005517 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005518 N0.getOperand(0));
Craig Topper0eb5dad2012-09-29 07:18:53 +00005519 if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00005520 // if the source is larger than the dest, than we just need the truncate
Andrew Trickac6d9be2013-05-25 02:42:55 +00005521 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0));
Craig Topper0eb5dad2012-09-29 07:18:53 +00005522 // if the source and dest are the same type, we can drop both the extend
5523 // and the truncate.
5524 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005525 }
Evan Cheng007b69e2007-03-21 20:14:05 +00005526
Nadav Rotemcc870a82012-02-05 11:39:23 +00005527 // Fold extract-and-trunc into a narrow extract. For example:
5528 // i64 x = EXTRACT_VECTOR_ELT(v2i64 val, i32 1)
5529 // i32 y = TRUNCATE(i64 x)
5530 // -- becomes --
5531 // v16i8 b = BITCAST (v2i64 val)
5532 // i8 x = EXTRACT_VECTOR_ELT(v16i8 b, i32 8)
5533 //
5534 // Note: We only run this optimization after type legalization (which often
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005535 // creates this pattern) and before operation legalization after which
5536 // we need to be more careful about the vector instructions that we generate.
5537 if (N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5538 LegalTypes && !LegalOperations && N0->hasOneUse()) {
5539
5540 EVT VecTy = N0.getOperand(0).getValueType();
5541 EVT ExTy = N0.getValueType();
5542 EVT TrTy = N->getValueType(0);
5543
5544 unsigned NumElem = VecTy.getVectorNumElements();
5545 unsigned SizeRatio = ExTy.getSizeInBits()/TrTy.getSizeInBits();
5546
5547 EVT NVT = EVT::getVectorVT(*DAG.getContext(), TrTy, SizeRatio * NumElem);
5548 assert(NVT.getSizeInBits() == VecTy.getSizeInBits() && "Invalid Size");
5549
5550 SDValue EltNo = N0->getOperand(1);
5551 if (isa<ConstantSDNode>(EltNo) && isTypeLegal(NVT)) {
5552 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Tom Stellard425b76c2013-08-05 22:22:01 +00005553 EVT IndexTy = TLI.getVectorIdxTy();
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005554 int Index = isLE ? (Elt*SizeRatio) : (Elt*SizeRatio + (SizeRatio-1));
5555
Andrew Trickac6d9be2013-05-25 02:42:55 +00005556 SDValue V = DAG.getNode(ISD::BITCAST, SDLoc(N),
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005557 NVT, N0.getOperand(0));
5558
5559 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005560 SDLoc(N), TrTy, V,
Jim Grosbacha249f7d2012-05-08 20:56:07 +00005561 DAG.getConstant(Index, IndexTy));
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005562 }
5563 }
5564
Arnold Schwaighoferc46e2df2013-02-20 21:33:32 +00005565 // Fold a series of buildvector, bitcast, and truncate if possible.
5566 // For example fold
5567 // (2xi32 trunc (bitcast ((4xi32)buildvector x, x, y, y) 2xi64)) to
5568 // (2xi32 (buildvector x, y)).
5569 if (Level == AfterLegalizeVectorOps && VT.isVector() &&
5570 N0.getOpcode() == ISD::BITCAST && N0.hasOneUse() &&
5571 N0.getOperand(0).getOpcode() == ISD::BUILD_VECTOR &&
5572 N0.getOperand(0).hasOneUse()) {
5573
5574 SDValue BuildVect = N0.getOperand(0);
5575 EVT BuildVectEltTy = BuildVect.getValueType().getVectorElementType();
5576 EVT TruncVecEltTy = VT.getVectorElementType();
5577
5578 // Check that the element types match.
5579 if (BuildVectEltTy == TruncVecEltTy) {
5580 // Now we only need to compute the offset of the truncated elements.
5581 unsigned BuildVecNumElts = BuildVect.getNumOperands();
5582 unsigned TruncVecNumElts = VT.getVectorNumElements();
5583 unsigned TruncEltOffset = BuildVecNumElts / TruncVecNumElts;
5584
5585 assert((BuildVecNumElts % TruncVecNumElts) == 0 &&
5586 "Invalid number of elements");
5587
5588 SmallVector<SDValue, 8> Opnds;
5589 for (unsigned i = 0, e = BuildVecNumElts; i != e; i += TruncEltOffset)
5590 Opnds.push_back(BuildVect.getOperand(i));
5591
Andrew Trickac6d9be2013-05-25 02:42:55 +00005592 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT, &Opnds[0],
Arnold Schwaighoferc46e2df2013-02-20 21:33:32 +00005593 Opnds.size());
5594 }
5595 }
5596
Chris Lattner2b4c2792007-10-13 06:35:54 +00005597 // See if we can simplify the input to this truncate through knowledge that
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005598 // only the low bits are being used.
5599 // For example "trunc (or (shl x, 8), y)" // -> trunc y
Nadav Rotemfcd96192011-02-27 07:40:43 +00005600 // Currently we only perform this optimization on scalars because vectors
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005601 // may have different active low bits.
5602 if (!VT.isVector()) {
5603 SDValue Shorter =
5604 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
5605 VT.getSizeInBits()));
5606 if (Shorter.getNode())
Andrew Trickac6d9be2013-05-25 02:42:55 +00005607 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Shorter);
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005608 }
Nate Begeman3df4d522005-10-12 20:40:40 +00005609 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00005610 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005611 if (!LegalTypes || TLI.isTypeDesirableForOp(N0.getOpcode(), VT)) {
5612 SDValue Reduced = ReduceLoadWidth(N);
5613 if (Reduced.getNode())
5614 return Reduced;
5615 }
Michael Liao07edaf32012-10-17 23:45:54 +00005616 // fold (trunc (concat ... x ...)) -> (concat ..., (trunc x), ...)),
5617 // where ... are all 'undef'.
5618 if (N0.getOpcode() == ISD::CONCAT_VECTORS && !LegalTypes) {
5619 SmallVector<EVT, 8> VTs;
5620 SDValue V;
5621 unsigned Idx = 0;
5622 unsigned NumDefs = 0;
5623
5624 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) {
5625 SDValue X = N0.getOperand(i);
5626 if (X.getOpcode() != ISD::UNDEF) {
5627 V = X;
5628 Idx = i;
5629 NumDefs++;
5630 }
5631 // Stop if more than one members are non-undef.
5632 if (NumDefs > 1)
5633 break;
5634 VTs.push_back(EVT::getVectorVT(*DAG.getContext(),
5635 VT.getVectorElementType(),
5636 X.getValueType().getVectorNumElements()));
5637 }
5638
5639 if (NumDefs == 0)
5640 return DAG.getUNDEF(VT);
5641
5642 if (NumDefs == 1) {
5643 assert(V.getNode() && "The single defined operand is empty!");
5644 SmallVector<SDValue, 8> Opnds;
5645 for (unsigned i = 0, e = VTs.size(); i != e; ++i) {
5646 if (i != Idx) {
5647 Opnds.push_back(DAG.getUNDEF(VTs[i]));
5648 continue;
5649 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005650 SDValue NV = DAG.getNode(ISD::TRUNCATE, SDLoc(V), VTs[i], V);
Michael Liao07edaf32012-10-17 23:45:54 +00005651 AddToWorkList(NV.getNode());
5652 Opnds.push_back(NV);
5653 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005654 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Michael Liao07edaf32012-10-17 23:45:54 +00005655 &Opnds[0], Opnds.size());
5656 }
5657 }
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005658
5659 // Simplify the operands using demanded-bits information.
5660 if (!VT.isVector() &&
5661 SimplifyDemandedBits(SDValue(N, 0)))
5662 return SDValue(N, 0);
5663
Evan Chenge5b51ac2010-04-17 06:13:15 +00005664 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005665}
5666
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005667static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005668 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005669 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00005670 return Elt.getNode();
5671 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005672}
5673
5674/// CombineConsecutiveLoads - build_pair (load, load) -> load
Scott Michelfdc40a02009-02-17 22:15:04 +00005675/// if load locations are consecutive.
Owen Andersone50ed302009-08-10 22:56:29 +00005676SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005677 assert(N->getOpcode() == ISD::BUILD_PAIR);
5678
Nate Begemanabc01992009-06-05 21:37:30 +00005679 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0));
5680 LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1));
Chris Lattnerfa459012010-09-21 16:08:50 +00005681 if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse() ||
5682 LD1->getPointerInfo().getAddrSpace() !=
5683 LD2->getPointerInfo().getAddrSpace())
Dan Gohman475871a2008-07-27 21:46:04 +00005684 return SDValue();
Owen Andersone50ed302009-08-10 22:56:29 +00005685 EVT LD1VT = LD1->getValueType(0);
Bill Wendling67a67682009-01-30 22:44:24 +00005686
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005687 if (ISD::isNON_EXTLoad(LD2) &&
5688 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005689 // If both are volatile this would reduce the number of volatile loads.
5690 // If one is volatile it might be ok, but play conservative and bail out.
Nate Begemanabc01992009-06-05 21:37:30 +00005691 !LD1->isVolatile() &&
5692 !LD2->isVolatile() &&
Evan Cheng64fa4a92009-12-09 01:36:00 +00005693 DAG.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1)) {
Nate Begemanabc01992009-06-05 21:37:30 +00005694 unsigned Align = LD1->getAlignment();
Micah Villmow3574eca2012-10-08 16:38:25 +00005695 unsigned NewAlign = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005696 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Bill Wendling67a67682009-01-30 22:44:24 +00005697
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005698 if (NewAlign <= Align &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005699 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005700 return DAG.getLoad(VT, SDLoc(N), LD1->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005701 LD1->getBasePtr(), LD1->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005702 false, false, false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005703 }
Bill Wendling67a67682009-01-30 22:44:24 +00005704
Dan Gohman475871a2008-07-27 21:46:04 +00005705 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005706}
5707
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005708SDValue DAGCombiner::visitBITCAST(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00005709 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005710 EVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00005711
Dan Gohman7f321562007-06-25 16:23:39 +00005712 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
5713 // Only do this before legalize, since afterward the target may be depending
5714 // on the bitconvert.
5715 // First check to see if this is all constant.
Duncan Sands25cf2272008-11-24 14:53:14 +00005716 if (!LegalTypes &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005717 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005718 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00005719 bool isSimple = true;
5720 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
5721 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
5722 N0.getOperand(i).getOpcode() != ISD::Constant &&
5723 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005724 isSimple = false;
Dan Gohman7f321562007-06-25 16:23:39 +00005725 break;
5726 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005727
Owen Andersone50ed302009-08-10 22:56:29 +00005728 EVT DestEltVT = N->getValueType(0).getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005729 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00005730 "Element type of vector ValueType must not be vector!");
Bill Wendling67a67682009-01-30 22:44:24 +00005731 if (isSimple)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005732 return ConstantFoldBITCASTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00005733 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005734
Dan Gohman3dd168d2008-09-05 01:58:21 +00005735 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00005736 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005737 SDValue Res = DAG.getNode(ISD::BITCAST, SDLoc(N), VT, N0);
Dan Gohmana407ca12009-08-10 23:15:10 +00005738 if (Res.getNode() != N) {
5739 if (!LegalOperations ||
5740 TLI.isOperationLegal(Res.getNode()->getOpcode(), VT))
5741 return Res;
5742
5743 // Folding it resulted in an illegal node, and it's too late to
5744 // do that. Clean up the old node and forego the transformation.
5745 // Ideally this won't happen very often, because instcombine
5746 // and the earlier dagcombine runs (where illegal nodes are
5747 // permitted) should have folded most of them already.
5748 DAG.DeleteNode(Res.getNode());
5749 }
Chris Lattner94683772005-12-23 05:30:37 +00005750 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005751
Bill Wendling67a67682009-01-30 22:44:24 +00005752 // (conv (conv x, t1), t2) -> (conv x, t2)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005753 if (N0.getOpcode() == ISD::BITCAST)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005754 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005755 N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00005756
Chris Lattner57104102005-12-23 05:44:41 +00005757 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00005758 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00005759 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005760 // Do not change the width of a volatile load.
5761 !cast<LoadSDNode>(N0)->isVolatile() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005762 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005763 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Micah Villmow3574eca2012-10-08 16:38:25 +00005764 unsigned Align = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005765 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Evan Cheng59d5b682007-05-07 21:27:48 +00005766 unsigned OrigAlign = LN0->getAlignment();
Bill Wendling67a67682009-01-30 22:44:24 +00005767
Evan Cheng59d5b682007-05-07 21:27:48 +00005768 if (Align <= OrigAlign) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005769 SDValue Load = DAG.getLoad(VT, SDLoc(N), LN0->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005770 LN0->getBasePtr(), LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00005771 LN0->isVolatile(), LN0->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005772 LN0->isInvariant(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00005773 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00005774 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005775 DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling67a67682009-01-30 22:44:24 +00005776 N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00005777 Load.getValue(1));
5778 return Load;
5779 }
Chris Lattner57104102005-12-23 05:44:41 +00005780 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005781
Bill Wendling67a67682009-01-30 22:44:24 +00005782 // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
5783 // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
Chris Lattner3bd39d42008-01-27 17:42:27 +00005784 // This often reduces constant pool loads.
Tom Stellard1f67c632013-07-23 23:55:03 +00005785 if (((N0.getOpcode() == ISD::FNEG && !TLI.isFNegFree(N0.getValueType())) ||
5786 (N0.getOpcode() == ISD::FABS && !TLI.isFAbsFree(N0.getValueType()))) &&
Nadav Rotem91a7e012012-09-13 14:54:28 +00005787 N0.getNode()->hasOneUse() && VT.isInteger() &&
5788 !VT.isVector() && !N0.getValueType().isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005789 SDValue NewConv = DAG.getNode(ISD::BITCAST, SDLoc(N0), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005790 N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00005791 AddToWorkList(NewConv.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005792
Duncan Sands83ec4b62008-06-06 12:08:01 +00005793 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005794 if (N0.getOpcode() == ISD::FNEG)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005795 return DAG.getNode(ISD::XOR, SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005796 NewConv, DAG.getConstant(SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005797 assert(N0.getOpcode() == ISD::FABS);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005798 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005799 NewConv, DAG.getConstant(~SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005800 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005801
Bill Wendling67a67682009-01-30 22:44:24 +00005802 // fold (bitconvert (fcopysign cst, x)) ->
5803 // (or (and (bitconvert x), sign), (and cst, (not sign)))
5804 // Note that we don't handle (copysign x, cst) because this can always be
5805 // folded to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00005806 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00005807 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005808 VT.isInteger() && !VT.isVector()) {
5809 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Owen Anderson23b9b192009-08-12 00:36:31 +00005810 EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth);
Chris Lattner2392ae72010-04-15 04:48:01 +00005811 if (isTypeLegal(IntXVT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005812 SDValue X = DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling67a67682009-01-30 22:44:24 +00005813 IntXVT, N0.getOperand(1));
Duncan Sands25cf2272008-11-24 14:53:14 +00005814 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005815
Duncan Sands25cf2272008-11-24 14:53:14 +00005816 // If X has a different width than the result/lhs, sext it or truncate it.
5817 unsigned VTWidth = VT.getSizeInBits();
5818 if (OrigXWidth < VTWidth) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005819 X = DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005820 AddToWorkList(X.getNode());
5821 } else if (OrigXWidth > VTWidth) {
5822 // To get the sign bit in the right place, we have to shift it right
5823 // before truncating.
Andrew Trickac6d9be2013-05-25 02:42:55 +00005824 X = DAG.getNode(ISD::SRL, SDLoc(X),
Bill Wendling67a67682009-01-30 22:44:24 +00005825 X.getValueType(), X,
Duncan Sands25cf2272008-11-24 14:53:14 +00005826 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
5827 AddToWorkList(X.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005828 X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005829 AddToWorkList(X.getNode());
5830 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005831
Duncan Sands25cf2272008-11-24 14:53:14 +00005832 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005833 X = DAG.getNode(ISD::AND, SDLoc(X), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005834 X, DAG.getConstant(SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005835 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005836
Andrew Trickac6d9be2013-05-25 02:42:55 +00005837 SDValue Cst = DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling67a67682009-01-30 22:44:24 +00005838 VT, N0.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005839 Cst = DAG.getNode(ISD::AND, SDLoc(Cst), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005840 Cst, DAG.getConstant(~SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005841 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005842
Andrew Trickac6d9be2013-05-25 02:42:55 +00005843 return DAG.getNode(ISD::OR, SDLoc(N), VT, X, Cst);
Duncan Sands25cf2272008-11-24 14:53:14 +00005844 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00005845 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005846
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005847 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005848 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005849 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
5850 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005851 return CombineLD;
5852 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005853
Dan Gohman475871a2008-07-27 21:46:04 +00005854 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00005855}
5856
Dan Gohman475871a2008-07-27 21:46:04 +00005857SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00005858 EVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005859 return CombineConsecutiveLoads(N, VT);
5860}
5861
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005862/// ConstantFoldBITCASTofBUILD_VECTOR - We know that BV is a build_vector
Scott Michelfdc40a02009-02-17 22:15:04 +00005863/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
Chris Lattner6258fb22006-04-02 02:53:43 +00005864/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00005865SDValue DAGCombiner::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005866ConstantFoldBITCASTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) {
Owen Andersone50ed302009-08-10 22:56:29 +00005867 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005868
Chris Lattner6258fb22006-04-02 02:53:43 +00005869 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00005870 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005871
Duncan Sands83ec4b62008-06-06 12:08:01 +00005872 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
5873 unsigned DstBitSize = DstEltVT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00005874
Chris Lattner6258fb22006-04-02 02:53:43 +00005875 // If this is a conversion of N elements of one type to N elements of another
5876 // type, convert each element. This handles FP<->INT cases.
5877 if (SrcBitSize == DstBitSize) {
Nate Begemane0efc212010-07-27 18:02:18 +00005878 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5879 BV->getValueType(0).getVectorNumElements());
5880
5881 // Due to the FP element handling below calling this routine recursively,
5882 // we can end up with a scalar-to-vector node here.
5883 if (BV->getOpcode() == ISD::SCALAR_TO_VECTOR)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005884 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(BV), VT,
5885 DAG.getNode(ISD::BITCAST, SDLoc(BV),
Nate Begemane0efc212010-07-27 18:02:18 +00005886 DstEltVT, BV->getOperand(0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005887
Dan Gohman475871a2008-07-27 21:46:04 +00005888 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005889 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Bob Wilsonb1303d02009-04-13 22:05:19 +00005890 SDValue Op = BV->getOperand(i);
5891 // If the vector element type is not legal, the BUILD_VECTOR operands
5892 // are promoted and implicitly truncated. Make that explicit here.
Bob Wilsonc8851652009-04-20 17:27:09 +00005893 if (Op.getValueType() != SrcEltVT)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005894 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(BV), SrcEltVT, Op);
5895 Ops.push_back(DAG.getNode(ISD::BITCAST, SDLoc(BV),
Bob Wilsonb1303d02009-04-13 22:05:19 +00005896 DstEltVT, Op));
Gabor Greifba36cb52008-08-28 21:40:38 +00005897 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00005898 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005899 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga87008d2009-02-25 22:49:59 +00005900 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005901 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005902
Chris Lattner6258fb22006-04-02 02:53:43 +00005903 // Otherwise, we're growing or shrinking the elements. To avoid having to
5904 // handle annoying details of growing/shrinking FP values, we convert them to
5905 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005906 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005907 // Convert the input float vector to a int vector where the elements are the
5908 // same sizes.
Owen Anderson825b72b2009-08-11 20:47:22 +00005909 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005910 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005911 BV = ConstantFoldBITCASTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00005912 SrcEltVT = IntVT;
5913 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005914
Chris Lattner6258fb22006-04-02 02:53:43 +00005915 // Now we know the input is an integer vector. If the output is a FP type,
5916 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005917 if (DstEltVT.isFloatingPoint()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005918 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005919 EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005920 SDNode *Tmp = ConstantFoldBITCASTofBUILD_VECTOR(BV, TmpVT).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005921
Chris Lattner6258fb22006-04-02 02:53:43 +00005922 // Next, convert to FP elements of the same size.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005923 return ConstantFoldBITCASTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00005924 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005925
Chris Lattner6258fb22006-04-02 02:53:43 +00005926 // Okay, we know the src/dst types are both integers of differing types.
5927 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005928 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00005929 if (SrcBitSize < DstBitSize) {
5930 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
Scott Michelfdc40a02009-02-17 22:15:04 +00005931
Dan Gohman475871a2008-07-27 21:46:04 +00005932 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005933 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00005934 i += NumInputsPerOutput) {
5935 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00005936 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00005937 bool EltIsUndef = true;
5938 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
5939 // Shift the previously computed bits over.
5940 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00005941 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00005942 if (Op.getOpcode() == ISD::UNDEF) continue;
5943 EltIsUndef = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005944
Jay Foad40f8f622010-12-07 08:25:19 +00005945 NewBits |= cast<ConstantSDNode>(Op)->getAPIntValue().
Dan Gohman58c25872010-04-12 02:24:01 +00005946 zextOrTrunc(SrcBitSize).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005947 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005948
Chris Lattner6258fb22006-04-02 02:53:43 +00005949 if (EltIsUndef)
Dale Johannesene8d72302009-02-06 23:05:02 +00005950 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005951 else
5952 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
5953 }
5954
Owen Anderson23b9b192009-08-12 00:36:31 +00005955 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005956 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga87008d2009-02-25 22:49:59 +00005957 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005958 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005959
Chris Lattner6258fb22006-04-02 02:53:43 +00005960 // Finally, this must be the case where we are shrinking elements: each input
5961 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00005962 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00005963 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Owen Anderson23b9b192009-08-12 00:36:31 +00005964 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5965 NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00005966 SmallVector<SDValue, 8> Ops;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005967
Dan Gohman7f321562007-06-25 16:23:39 +00005968 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005969 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
5970 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
Dale Johannesene8d72302009-02-06 23:05:02 +00005971 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005972 continue;
5973 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005974
Jay Foad40f8f622010-12-07 08:25:19 +00005975 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->
5976 getAPIntValue().zextOrTrunc(SrcBitSize);
Bill Wendlingb0162f52009-01-30 22:53:48 +00005977
Chris Lattner6258fb22006-04-02 02:53:43 +00005978 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Jay Foad40f8f622010-12-07 08:25:19 +00005979 APInt ThisVal = OpVal.trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005980 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Jay Foad40f8f622010-12-07 08:25:19 +00005981 if (isS2V && i == 0 && j == 0 && ThisVal.zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00005982 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
Andrew Trickac6d9be2013-05-25 02:42:55 +00005983 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(BV), VT,
Bill Wendlingb0162f52009-01-30 22:53:48 +00005984 Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00005985 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005986 }
5987
5988 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00005989 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00005990 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
5991 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005992
Andrew Trickac6d9be2013-05-25 02:42:55 +00005993 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga87008d2009-02-25 22:49:59 +00005994 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005995}
5996
Dan Gohman475871a2008-07-27 21:46:04 +00005997SDValue DAGCombiner::visitFADD(SDNode *N) {
5998 SDValue N0 = N->getOperand(0);
5999 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00006000 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6001 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006002 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006003
Dan Gohman7f321562007-06-25 16:23:39 +00006004 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006005 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006006 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006007 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006008 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006009
Lang Hames01806942012-06-14 20:37:15 +00006010 // fold (fadd c1, c2) -> c1 + c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006011 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006012 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00006013 // canonicalize constant to RHS
6014 if (N0CFP && !N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006015 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N0);
Bill Wendlingb0162f52009-01-30 22:53:48 +00006016 // fold (fadd A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006017 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6018 N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00006019 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00006020 // fold (fadd A, (fneg B)) -> (fsub A, B)
Owen Andersonafd3d562012-03-06 00:29:31 +00006021 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00006022 isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006023 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00006024 GetNegatedExpression(N1, DAG, LegalOperations));
Bill Wendlingb0162f52009-01-30 22:53:48 +00006025 // fold (fadd (fneg A), B) -> (fsub B, A)
Owen Andersonafd3d562012-03-06 00:29:31 +00006026 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00006027 isNegatibleForFree(N0, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006028 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N1,
Duncan Sands25cf2272008-11-24 14:53:14 +00006029 GetNegatedExpression(N0, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00006030
Chris Lattnerddae4bd2007-01-08 23:04:05 +00006031 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006032 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6033 N0.getOpcode() == ISD::FADD && N0.getNode()->hasOneUse() &&
6034 isa<ConstantFPSDNode>(N0.getOperand(1)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006035 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0.getOperand(0),
6036 DAG.getNode(ISD::FADD, SDLoc(N), VT,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00006037 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006038
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006039 // No FP constant should be created after legalization as Instruction
6040 // Selection pass has hard time in dealing with FP constant.
6041 //
6042 // We don't need test this condition for transformation like following, as
6043 // the DAG being transformed implies it is legal to take FP constant as
6044 // operand.
Stephen Lin155615d2013-07-08 00:37:03 +00006045 //
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006046 // (fadd (fmul c, x), x) -> (fmul c+1, x)
Stephen Lin155615d2013-07-08 00:37:03 +00006047 //
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006048 bool AllowNewFpConst = (Level < AfterLegalizeDAG);
6049
Owen Anderson607ebde2012-11-01 02:00:53 +00006050 // If allow, fold (fadd (fneg x), x) -> 0.0
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006051 if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath &&
Stephen Linb4940152013-07-09 00:44:49 +00006052 N0.getOpcode() == ISD::FNEG && N0.getOperand(0) == N1)
Owen Anderson607ebde2012-11-01 02:00:53 +00006053 return DAG.getConstantFP(0.0, VT);
Owen Anderson607ebde2012-11-01 02:00:53 +00006054
6055 // If allow, fold (fadd x, (fneg x)) -> 0.0
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006056 if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath &&
Stephen Linb4940152013-07-09 00:44:49 +00006057 N1.getOpcode() == ISD::FNEG && N1.getOperand(0) == N0)
Owen Anderson607ebde2012-11-01 02:00:53 +00006058 return DAG.getConstantFP(0.0, VT);
Owen Anderson607ebde2012-11-01 02:00:53 +00006059
Owen Anderson43da6c72012-08-30 23:35:16 +00006060 // In unsafe math mode, we can fold chains of FADD's of the same value
6061 // into multiplications. This transform is not safe in general because
6062 // we are reducing the number of rounding steps.
6063 if (DAG.getTarget().Options.UnsafeFPMath &&
6064 TLI.isOperationLegalOrCustom(ISD::FMUL, VT) &&
6065 !N0CFP && !N1CFP) {
6066 if (N0.getOpcode() == ISD::FMUL) {
6067 ConstantFPSDNode *CFP00 = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
6068 ConstantFPSDNode *CFP01 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
6069
Stephen Lin38103d12013-06-14 18:17:35 +00006070 // (fadd (fmul c, x), x) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00006071 if (CFP00 && !CFP01 && N0.getOperand(1) == N1) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006072 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006073 SDValue(CFP00, 0),
6074 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006075 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006076 N1, NewCFP);
6077 }
6078
Stephen Lin38103d12013-06-14 18:17:35 +00006079 // (fadd (fmul x, c), x) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00006080 if (CFP01 && !CFP00 && N0.getOperand(0) == N1) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006081 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006082 SDValue(CFP01, 0),
6083 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006084 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006085 N1, NewCFP);
6086 }
6087
Stephen Lin38103d12013-06-14 18:17:35 +00006088 // (fadd (fmul c, x), (fadd x, x)) -> (fmul x, c+2)
Owen Anderson43da6c72012-08-30 23:35:16 +00006089 if (CFP00 && !CFP01 && N1.getOpcode() == ISD::FADD &&
6090 N1.getOperand(0) == N1.getOperand(1) &&
6091 N0.getOperand(1) == N1.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006092 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006093 SDValue(CFP00, 0),
6094 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006095 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006096 N0.getOperand(1), NewCFP);
6097 }
6098
Stephen Lin38103d12013-06-14 18:17:35 +00006099 // (fadd (fmul x, c), (fadd x, x)) -> (fmul x, c+2)
Owen Anderson43da6c72012-08-30 23:35:16 +00006100 if (CFP01 && !CFP00 && N1.getOpcode() == ISD::FADD &&
6101 N1.getOperand(0) == N1.getOperand(1) &&
6102 N0.getOperand(0) == N1.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006103 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006104 SDValue(CFP01, 0),
6105 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006106 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006107 N0.getOperand(0), NewCFP);
6108 }
6109 }
6110
6111 if (N1.getOpcode() == ISD::FMUL) {
6112 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
6113 ConstantFPSDNode *CFP11 = dyn_cast<ConstantFPSDNode>(N1.getOperand(1));
6114
Stephen Lin38103d12013-06-14 18:17:35 +00006115 // (fadd x, (fmul c, x)) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00006116 if (CFP10 && !CFP11 && N1.getOperand(1) == N0) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006117 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006118 SDValue(CFP10, 0),
6119 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006120 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006121 N0, NewCFP);
6122 }
6123
Stephen Lin38103d12013-06-14 18:17:35 +00006124 // (fadd x, (fmul x, c)) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00006125 if (CFP11 && !CFP10 && N1.getOperand(0) == N0) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006126 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006127 SDValue(CFP11, 0),
6128 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006129 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006130 N0, NewCFP);
6131 }
6132
Owen Anderson43da6c72012-08-30 23:35:16 +00006133
Stephen Lin38103d12013-06-14 18:17:35 +00006134 // (fadd (fadd x, x), (fmul c, x)) -> (fmul x, c+2)
6135 if (CFP10 && !CFP11 && N0.getOpcode() == ISD::FADD &&
6136 N0.getOperand(0) == N0.getOperand(1) &&
6137 N1.getOperand(1) == N0.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006138 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006139 SDValue(CFP10, 0),
6140 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006141 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Stephen Lin38103d12013-06-14 18:17:35 +00006142 N1.getOperand(1), NewCFP);
Owen Anderson43da6c72012-08-30 23:35:16 +00006143 }
6144
Stephen Lin38103d12013-06-14 18:17:35 +00006145 // (fadd (fadd x, x), (fmul x, c)) -> (fmul x, c+2)
6146 if (CFP11 && !CFP10 && N0.getOpcode() == ISD::FADD &&
6147 N0.getOperand(0) == N0.getOperand(1) &&
6148 N1.getOperand(0) == N0.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006149 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006150 SDValue(CFP11, 0),
6151 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006152 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Stephen Lin38103d12013-06-14 18:17:35 +00006153 N1.getOperand(0), NewCFP);
Owen Anderson43da6c72012-08-30 23:35:16 +00006154 }
6155 }
6156
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006157 if (N0.getOpcode() == ISD::FADD && AllowNewFpConst) {
Shuxin Yang98b93e52013-02-02 00:22:03 +00006158 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
Stephen Lina553bed2013-06-14 21:33:58 +00006159 // (fadd (fadd x, x), x) -> (fmul x, 3.0)
Shuxin Yang98b93e52013-02-02 00:22:03 +00006160 if (!CFP && N0.getOperand(0) == N0.getOperand(1) &&
Stephen Linb4940152013-07-09 00:44:49 +00006161 (N0.getOperand(0) == N1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006162 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Shuxin Yang98b93e52013-02-02 00:22:03 +00006163 N1, DAG.getConstantFP(3.0, VT));
Shuxin Yang98b93e52013-02-02 00:22:03 +00006164 }
6165
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006166 if (N1.getOpcode() == ISD::FADD && AllowNewFpConst) {
Shuxin Yang98b93e52013-02-02 00:22:03 +00006167 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
Stephen Lina553bed2013-06-14 21:33:58 +00006168 // (fadd x, (fadd x, x)) -> (fmul x, 3.0)
Shuxin Yang98b93e52013-02-02 00:22:03 +00006169 if (!CFP10 && N1.getOperand(0) == N1.getOperand(1) &&
Stephen Linb4940152013-07-09 00:44:49 +00006170 N1.getOperand(0) == N0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006171 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Shuxin Yang98b93e52013-02-02 00:22:03 +00006172 N0, DAG.getConstantFP(3.0, VT));
Shuxin Yang98b93e52013-02-02 00:22:03 +00006173 }
6174
Stephen Lina553bed2013-06-14 21:33:58 +00006175 // (fadd (fadd x, x), (fadd x, x)) -> (fmul x, 4.0)
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006176 if (AllowNewFpConst &&
6177 N0.getOpcode() == ISD::FADD && N1.getOpcode() == ISD::FADD &&
Owen Anderson43da6c72012-08-30 23:35:16 +00006178 N0.getOperand(0) == N0.getOperand(1) &&
6179 N1.getOperand(0) == N1.getOperand(1) &&
Stephen Linb4940152013-07-09 00:44:49 +00006180 N0.getOperand(0) == N1.getOperand(0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006181 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006182 N0.getOperand(0),
6183 DAG.getConstantFP(4.0, VT));
Owen Anderson43da6c72012-08-30 23:35:16 +00006184 }
6185
Lang Hamesd693caf2012-06-19 22:51:23 +00006186 // FADD -> FMA combines:
Lang Hamese0231412012-06-22 01:09:09 +00006187 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hamesd693caf2012-06-19 22:51:23 +00006188 DAG.getTarget().Options.UnsafeFPMath) &&
Stephen Line54885a2013-07-09 18:16:56 +00006189 DAG.getTarget().getTargetLowering()->isFMAFasterThanFMulAndFAdd(VT) &&
6190 (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FMA, VT))) {
Lang Hamesd693caf2012-06-19 22:51:23 +00006191
6192 // fold (fadd (fmul x, y), z) -> (fma x, y, z)
Stephen Linb4940152013-07-09 00:44:49 +00006193 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse())
Andrew Trickac6d9be2013-05-25 02:42:55 +00006194 return DAG.getNode(ISD::FMA, SDLoc(N), VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006195 N0.getOperand(0), N0.getOperand(1), N1);
Owen Anderson43da6c72012-08-30 23:35:16 +00006196
Michael Liaob79bff52012-09-01 04:09:16 +00006197 // fold (fadd x, (fmul y, z)) -> (fma y, z, x)
Lang Hamesd693caf2012-06-19 22:51:23 +00006198 // Note: Commutes FADD operands.
Stephen Linb4940152013-07-09 00:44:49 +00006199 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse())
Andrew Trickac6d9be2013-05-25 02:42:55 +00006200 return DAG.getNode(ISD::FMA, SDLoc(N), VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006201 N1.getOperand(0), N1.getOperand(1), N0);
Lang Hamesd693caf2012-06-19 22:51:23 +00006202 }
6203
Dan Gohman475871a2008-07-27 21:46:04 +00006204 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006205}
6206
Dan Gohman475871a2008-07-27 21:46:04 +00006207SDValue DAGCombiner::visitFSUB(SDNode *N) {
6208 SDValue N0 = N->getOperand(0);
6209 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00006210 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6211 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006212 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006213 SDLoc dl(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00006214
Dan Gohman7f321562007-06-25 16:23:39 +00006215 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006216 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006217 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006218 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006219 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006220
Nate Begemana0e221d2005-10-18 00:28:13 +00006221 // fold (fsub c1, c2) -> c1-c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006222 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006223 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N0, N1);
Bill Wendlingb0162f52009-01-30 22:53:48 +00006224 // fold (fsub A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006225 if (DAG.getTarget().Options.UnsafeFPMath &&
6226 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohmana90c8e62009-01-23 19:10:37 +00006227 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00006228 // fold (fsub 0, B) -> -B
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006229 if (DAG.getTarget().Options.UnsafeFPMath &&
6230 N0CFP && N0CFP->getValueAPF().isZero()) {
Owen Andersonafd3d562012-03-06 00:29:31 +00006231 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00006232 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman760f86f2009-01-22 21:58:43 +00006233 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006234 return DAG.getNode(ISD::FNEG, dl, VT, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00006235 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00006236 // fold (fsub A, (fneg B)) -> (fadd A, B)
Owen Andersonafd3d562012-03-06 00:29:31 +00006237 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006238 return DAG.getNode(ISD::FADD, dl, VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00006239 GetNegatedExpression(N1, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00006240
Bill Wendling5a894342012-03-15 05:12:00 +00006241 // If 'unsafe math' is enabled, fold
Owen Anderson713e9532012-05-07 20:51:25 +00006242 // (fsub x, x) -> 0.0 &
Bill Wendling5a894342012-03-15 05:12:00 +00006243 // (fsub x, (fadd x, y)) -> (fneg y) &
6244 // (fsub x, (fadd y, x)) -> (fneg y)
6245 if (DAG.getTarget().Options.UnsafeFPMath) {
Owen Anderson713e9532012-05-07 20:51:25 +00006246 if (N0 == N1)
6247 return DAG.getConstantFP(0.0f, VT);
6248
Bill Wendling5a894342012-03-15 05:12:00 +00006249 if (N1.getOpcode() == ISD::FADD) {
6250 SDValue N10 = N1->getOperand(0);
6251 SDValue N11 = N1->getOperand(1);
6252
6253 if (N10 == N0 && isNegatibleForFree(N11, LegalOperations, TLI,
6254 &DAG.getTarget().Options))
6255 return GetNegatedExpression(N11, DAG, LegalOperations);
Stephen Lin75d13062013-07-10 20:47:39 +00006256
Stephen Linb4940152013-07-09 00:44:49 +00006257 if (N11 == N0 && isNegatibleForFree(N10, LegalOperations, TLI,
6258 &DAG.getTarget().Options))
Bill Wendling5a894342012-03-15 05:12:00 +00006259 return GetNegatedExpression(N10, DAG, LegalOperations);
6260 }
6261 }
6262
Lang Hamesd693caf2012-06-19 22:51:23 +00006263 // FSUB -> FMA combines:
Lang Hamese0231412012-06-22 01:09:09 +00006264 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hamesd693caf2012-06-19 22:51:23 +00006265 DAG.getTarget().Options.UnsafeFPMath) &&
Stephen Line54885a2013-07-09 18:16:56 +00006266 DAG.getTarget().getTargetLowering()->isFMAFasterThanFMulAndFAdd(VT) &&
6267 (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FMA, VT))) {
Lang Hamesd693caf2012-06-19 22:51:23 +00006268
6269 // fold (fsub (fmul x, y), z) -> (fma x, y, (fneg z))
Stephen Linb4940152013-07-09 00:44:49 +00006270 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse())
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006271 return DAG.getNode(ISD::FMA, dl, VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006272 N0.getOperand(0), N0.getOperand(1),
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006273 DAG.getNode(ISD::FNEG, dl, VT, N1));
Lang Hamesd693caf2012-06-19 22:51:23 +00006274
6275 // fold (fsub x, (fmul y, z)) -> (fma (fneg y), z, x)
6276 // Note: Commutes FSUB operands.
Stephen Lin75d13062013-07-10 20:47:39 +00006277 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse())
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006278 return DAG.getNode(ISD::FMA, dl, VT,
6279 DAG.getNode(ISD::FNEG, dl, VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006280 N1.getOperand(0)),
6281 N1.getOperand(1), N0);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006282
Stephen Linb4940152013-07-09 00:44:49 +00006283 // fold (fsub (fneg (fmul, x, y)), z) -> (fma (fneg x), y, (fneg z))
Stephen Lin155615d2013-07-08 00:37:03 +00006284 if (N0.getOpcode() == ISD::FNEG &&
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006285 N0.getOperand(0).getOpcode() == ISD::FMUL &&
6286 N0->hasOneUse() && N0.getOperand(0).hasOneUse()) {
6287 SDValue N00 = N0.getOperand(0).getOperand(0);
6288 SDValue N01 = N0.getOperand(0).getOperand(1);
6289 return DAG.getNode(ISD::FMA, dl, VT,
6290 DAG.getNode(ISD::FNEG, dl, VT, N00), N01,
6291 DAG.getNode(ISD::FNEG, dl, VT, N1));
6292 }
Lang Hamesd693caf2012-06-19 22:51:23 +00006293 }
6294
Dan Gohman475871a2008-07-27 21:46:04 +00006295 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006296}
6297
Dan Gohman475871a2008-07-27 21:46:04 +00006298SDValue DAGCombiner::visitFMUL(SDNode *N) {
6299 SDValue N0 = N->getOperand(0);
6300 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00006301 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6302 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006303 EVT VT = N->getValueType(0);
Owen Andersonafd3d562012-03-06 00:29:31 +00006304 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner01b3d732005-09-28 22:28:18 +00006305
Dan Gohman7f321562007-06-25 16:23:39 +00006306 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006307 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006308 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006309 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006310 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006311
Nate Begeman11af4ea2005-10-17 20:40:11 +00006312 // fold (fmul c1, c2) -> c1*c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006313 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006314 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00006315 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00006316 if (N0CFP && !N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006317 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N1, N0);
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006318 // fold (fmul A, 0) -> 0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006319 if (DAG.getTarget().Options.UnsafeFPMath &&
6320 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00006321 return N1;
Dan Gohman77b81fe2009-06-04 17:12:12 +00006322 // fold (fmul A, 0) -> 0, vector edition.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006323 if (DAG.getTarget().Options.UnsafeFPMath &&
6324 ISD::isBuildVectorAllZeros(N1.getNode()))
Dan Gohman77b81fe2009-06-04 17:12:12 +00006325 return N1;
Owen Anderson363e4b92012-05-02 21:32:35 +00006326 // fold (fmul A, 1.0) -> A
6327 if (N1CFP && N1CFP->isExactlyValue(1.0))
6328 return N0;
Nate Begeman11af4ea2005-10-17 20:40:11 +00006329 // fold (fmul X, 2.0) -> (fadd X, X)
6330 if (N1CFP && N1CFP->isExactlyValue(+2.0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006331 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N0);
Dan Gohmaneb1fedc2009-08-10 16:50:32 +00006332 // fold (fmul X, -1.0) -> (fneg X)
Chris Lattner29446522007-05-14 22:04:50 +00006333 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman760f86f2009-01-22 21:58:43 +00006334 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006335 return DAG.getNode(ISD::FNEG, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006336
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006337 // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y)
Owen Andersonafd3d562012-03-06 00:29:31 +00006338 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006339 &DAG.getTarget().Options)) {
Stephen Lin155615d2013-07-08 00:37:03 +00006340 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006341 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00006342 // Both can be negated for free, check to see if at least one is cheaper
6343 // negated.
6344 if (LHSNeg == 2 || RHSNeg == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006345 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00006346 GetNegatedExpression(N0, DAG, LegalOperations),
6347 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00006348 }
6349 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006350
Chris Lattnerddae4bd2007-01-08 23:04:05 +00006351 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006352 if (DAG.getTarget().Options.UnsafeFPMath &&
6353 N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006354 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006355 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0.getOperand(0),
6356 DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Dale Johannesende064702009-02-06 21:50:26 +00006357 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006358
Dan Gohman475871a2008-07-27 21:46:04 +00006359 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006360}
6361
Owen Anderson062c0a52012-05-02 22:17:40 +00006362SDValue DAGCombiner::visitFMA(SDNode *N) {
6363 SDValue N0 = N->getOperand(0);
6364 SDValue N1 = N->getOperand(1);
6365 SDValue N2 = N->getOperand(2);
6366 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6367 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
6368 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006369 SDLoc dl(N);
Owen Anderson062c0a52012-05-02 22:17:40 +00006370
Owen Anderson607ebde2012-11-01 02:00:53 +00006371 if (DAG.getTarget().Options.UnsafeFPMath) {
6372 if (N0CFP && N0CFP->isZero())
6373 return N2;
6374 if (N1CFP && N1CFP->isZero())
6375 return N2;
6376 }
Owen Anderson062c0a52012-05-02 22:17:40 +00006377 if (N0CFP && N0CFP->isExactlyValue(1.0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006378 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N2);
Owen Anderson062c0a52012-05-02 22:17:40 +00006379 if (N1CFP && N1CFP->isExactlyValue(1.0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006380 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N2);
Owen Anderson062c0a52012-05-02 22:17:40 +00006381
Owen Anderson85ef6f42012-05-30 18:50:39 +00006382 // Canonicalize (fma c, x, y) -> (fma x, c, y)
Owen Andersonf917d202012-05-30 18:54:50 +00006383 if (N0CFP && !N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006384 return DAG.getNode(ISD::FMA, SDLoc(N), VT, N1, N0, N2);
Owen Anderson85ef6f42012-05-30 18:50:39 +00006385
Owen Anderson58d57292012-09-01 06:04:27 +00006386 // (fma x, c1, (fmul x, c2)) -> (fmul x, c1+c2)
6387 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6388 N2.getOpcode() == ISD::FMUL &&
6389 N0 == N2.getOperand(0) &&
6390 N2.getOperand(1).getOpcode() == ISD::ConstantFP) {
6391 return DAG.getNode(ISD::FMUL, dl, VT, N0,
6392 DAG.getNode(ISD::FADD, dl, VT, N1, N2.getOperand(1)));
6393 }
6394
6395
6396 // (fma (fmul x, c1), c2, y) -> (fma x, c1*c2, y)
6397 if (DAG.getTarget().Options.UnsafeFPMath &&
6398 N0.getOpcode() == ISD::FMUL && N1CFP &&
6399 N0.getOperand(1).getOpcode() == ISD::ConstantFP) {
6400 return DAG.getNode(ISD::FMA, dl, VT,
6401 N0.getOperand(0),
6402 DAG.getNode(ISD::FMUL, dl, VT, N1, N0.getOperand(1)),
6403 N2);
6404 }
6405
6406 // (fma x, 1, y) -> (fadd x, y)
6407 // (fma x, -1, y) -> (fadd (fneg x), y)
6408 if (N1CFP) {
6409 if (N1CFP->isExactlyValue(1.0))
6410 return DAG.getNode(ISD::FADD, dl, VT, N0, N2);
6411
6412 if (N1CFP->isExactlyValue(-1.0) &&
6413 (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))) {
6414 SDValue RHSNeg = DAG.getNode(ISD::FNEG, dl, VT, N0);
6415 AddToWorkList(RHSNeg.getNode());
6416 return DAG.getNode(ISD::FADD, dl, VT, N2, RHSNeg);
6417 }
6418 }
6419
6420 // (fma x, c, x) -> (fmul x, (c+1))
Stephen Linb4940152013-07-09 00:44:49 +00006421 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && N0 == N2)
6422 return DAG.getNode(ISD::FMUL, dl, VT, N0,
Owen Anderson58d57292012-09-01 06:04:27 +00006423 DAG.getNode(ISD::FADD, dl, VT,
6424 N1, DAG.getConstantFP(1.0, VT)));
Owen Anderson58d57292012-09-01 06:04:27 +00006425
6426 // (fma x, c, (fneg x)) -> (fmul x, (c-1))
6427 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
Stephen Linb4940152013-07-09 00:44:49 +00006428 N2.getOpcode() == ISD::FNEG && N2.getOperand(0) == N0)
6429 return DAG.getNode(ISD::FMUL, dl, VT, N0,
Owen Anderson58d57292012-09-01 06:04:27 +00006430 DAG.getNode(ISD::FADD, dl, VT,
6431 N1, DAG.getConstantFP(-1.0, VT)));
Owen Anderson58d57292012-09-01 06:04:27 +00006432
6433
Owen Anderson062c0a52012-05-02 22:17:40 +00006434 return SDValue();
6435}
6436
Dan Gohman475871a2008-07-27 21:46:04 +00006437SDValue DAGCombiner::visitFDIV(SDNode *N) {
6438 SDValue N0 = N->getOperand(0);
6439 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006440 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6441 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006442 EVT VT = N->getValueType(0);
Owen Andersonafd3d562012-03-06 00:29:31 +00006443 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner01b3d732005-09-28 22:28:18 +00006444
Dan Gohman7f321562007-06-25 16:23:39 +00006445 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006446 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006447 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006448 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006449 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006450
Nate Begemana148d982006-01-18 22:35:16 +00006451 // fold (fdiv c1, c2) -> c1/c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006452 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006453 return DAG.getNode(ISD::FDIV, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006454
Duncan Sands3ef3fcf2012-04-08 18:08:12 +00006455 // fold (fdiv X, c2) -> fmul X, 1/c2 if losing precision is acceptable.
Ulrich Weigande669c932012-10-29 18:35:49 +00006456 if (N1CFP && DAG.getTarget().Options.UnsafeFPMath) {
Duncan Sands961d6662012-04-07 20:04:00 +00006457 // Compute the reciprocal 1.0 / c2.
6458 APFloat N1APF = N1CFP->getValueAPF();
6459 APFloat Recip(N1APF.getSemantics(), 1); // 1.0
6460 APFloat::opStatus st = Recip.divide(N1APF, APFloat::rmNearestTiesToEven);
Duncan Sands507bb7a2012-04-10 20:35:27 +00006461 // Only do the transform if the reciprocal is a legal fp immediate that
6462 // isn't too nasty (eg NaN, denormal, ...).
6463 if ((st == APFloat::opOK || st == APFloat::opInexact) && // Not too nasty
Anton Korobeynikov999821c2012-04-10 13:22:49 +00006464 (!LegalOperations ||
6465 // FIXME: custom lowering of ConstantFP might fail (see e.g. ARM
6466 // backend)... we should handle this gracefully after Legalize.
6467 // TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT) ||
6468 TLI.isOperationLegal(llvm::ISD::ConstantFP, VT) ||
6469 TLI.isFPImmLegal(Recip, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006470 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0,
Duncan Sands961d6662012-04-07 20:04:00 +00006471 DAG.getConstantFP(Recip, VT));
6472 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006473
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006474 // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y)
Owen Andersonafd3d562012-03-06 00:29:31 +00006475 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006476 &DAG.getTarget().Options)) {
Owen Andersonafd3d562012-03-06 00:29:31 +00006477 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006478 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00006479 // Both can be negated for free, check to see if at least one is cheaper
6480 // negated.
6481 if (LHSNeg == 2 || RHSNeg == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006482 return DAG.getNode(ISD::FDIV, SDLoc(N), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00006483 GetNegatedExpression(N0, DAG, LegalOperations),
6484 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00006485 }
6486 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006487
Dan Gohman475871a2008-07-27 21:46:04 +00006488 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006489}
6490
Dan Gohman475871a2008-07-27 21:46:04 +00006491SDValue DAGCombiner::visitFREM(SDNode *N) {
6492 SDValue N0 = N->getOperand(0);
6493 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006494 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6495 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006496 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00006497
Nate Begemana148d982006-01-18 22:35:16 +00006498 // fold (frem c1, c2) -> fmod(c1,c2)
Ulrich Weigande669c932012-10-29 18:35:49 +00006499 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006500 return DAG.getNode(ISD::FREM, SDLoc(N), VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00006501
Dan Gohman475871a2008-07-27 21:46:04 +00006502 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006503}
6504
Dan Gohman475871a2008-07-27 21:46:04 +00006505SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
6506 SDValue N0 = N->getOperand(0);
6507 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00006508 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6509 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006510 EVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00006511
Ulrich Weigande669c932012-10-29 18:35:49 +00006512 if (N0CFP && N1CFP) // Constant fold
Andrew Trickac6d9be2013-05-25 02:42:55 +00006513 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006514
Chris Lattner12d83032006-03-05 05:30:57 +00006515 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00006516 const APFloat& V = N1CFP->getValueAPF();
Sylvestre Ledru94c22712012-09-27 10:14:43 +00006517 // copysign(x, c1) -> fabs(x) iff ispos(c1)
6518 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman760f86f2009-01-22 21:58:43 +00006519 if (!V.isNegative()) {
6520 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006521 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Dan Gohman760f86f2009-01-22 21:58:43 +00006522 } else {
6523 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006524 return DAG.getNode(ISD::FNEG, SDLoc(N), VT,
6525 DAG.getNode(ISD::FABS, SDLoc(N0), VT, N0));
Dan Gohman760f86f2009-01-22 21:58:43 +00006526 }
Chris Lattner12d83032006-03-05 05:30:57 +00006527 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006528
Chris Lattner12d83032006-03-05 05:30:57 +00006529 // copysign(fabs(x), y) -> copysign(x, y)
6530 // copysign(fneg(x), y) -> copysign(x, y)
6531 // copysign(copysign(x,z), y) -> copysign(x, y)
6532 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
6533 N0.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006534 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006535 N0.getOperand(0), N1);
Chris Lattner12d83032006-03-05 05:30:57 +00006536
6537 // copysign(x, abs(y)) -> abs(x)
6538 if (N1.getOpcode() == ISD::FABS)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006539 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006540
Chris Lattner12d83032006-03-05 05:30:57 +00006541 // copysign(x, copysign(y,z)) -> copysign(x, z)
6542 if (N1.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006543 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006544 N0, N1.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006545
Chris Lattner12d83032006-03-05 05:30:57 +00006546 // copysign(x, fp_extend(y)) -> copysign(x, y)
6547 // copysign(x, fp_round(y)) -> copysign(x, y)
6548 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006549 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006550 N0, N1.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00006551
Dan Gohman475871a2008-07-27 21:46:04 +00006552 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00006553}
6554
Dan Gohman475871a2008-07-27 21:46:04 +00006555SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
6556 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00006557 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006558 EVT VT = N->getValueType(0);
6559 EVT OpVT = N0.getValueType();
Chris Lattnercda88752008-06-26 00:16:49 +00006560
Nate Begeman1d4d4142005-09-01 00:19:25 +00006561 // fold (sint_to_fp c1) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006562 if (N0C &&
Stuart Hastings7e334182011-03-02 19:36:30 +00006563 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00006564 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00006565 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006566 return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006567
Chris Lattnercda88752008-06-26 00:16:49 +00006568 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
6569 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006570 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
6571 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006572 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00006573 if (DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006574 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00006575 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00006576
Nadav Rotemed1a3352012-07-23 07:59:50 +00006577 // The next optimizations are desireable only if SELECT_CC can be lowered.
6578 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6579 // having to say they don't support SELECT_CC on every type the DAG knows
6580 // about, since there is no way to mark an opcode illegal at all value types
6581 // (See also visitSELECT)
6582 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6583 // fold (sint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
6584 if (N0.getOpcode() == ISD::SETCC && N0.getValueType() == MVT::i1 &&
6585 !VT.isVector() &&
6586 (!LegalOperations ||
6587 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6588 SDValue Ops[] =
6589 { N0.getOperand(0), N0.getOperand(1),
6590 DAG.getConstantFP(-1.0, VT) , DAG.getConstantFP(0.0, VT),
6591 N0.getOperand(2) };
Andrew Trickac6d9be2013-05-25 02:42:55 +00006592 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotemed1a3352012-07-23 07:59:50 +00006593 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006594
Nadav Rotemed1a3352012-07-23 07:59:50 +00006595 // fold (sint_to_fp (zext (setcc x, y, cc))) ->
6596 // (select_cc x, y, 1.0, 0.0,, cc)
6597 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
6598 N0.getOperand(0).getOpcode() == ISD::SETCC &&!VT.isVector() &&
6599 (!LegalOperations ||
6600 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6601 SDValue Ops[] =
6602 { N0.getOperand(0).getOperand(0), N0.getOperand(0).getOperand(1),
6603 DAG.getConstantFP(1.0, VT) , DAG.getConstantFP(0.0, VT),
6604 N0.getOperand(0).getOperand(2) };
Andrew Trickac6d9be2013-05-25 02:42:55 +00006605 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotemed1a3352012-07-23 07:59:50 +00006606 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006607 }
6608
Dan Gohman475871a2008-07-27 21:46:04 +00006609 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006610}
6611
Dan Gohman475871a2008-07-27 21:46:04 +00006612SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
6613 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00006614 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006615 EVT VT = N->getValueType(0);
6616 EVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00006617
Nate Begeman1d4d4142005-09-01 00:19:25 +00006618 // fold (uint_to_fp c1) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006619 if (N0C &&
Stuart Hastings7e334182011-03-02 19:36:30 +00006620 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00006621 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00006622 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006623 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006624
Chris Lattnercda88752008-06-26 00:16:49 +00006625 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
6626 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006627 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
6628 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006629 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00006630 if (DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006631 return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00006632 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006633
Nadav Rotemed1a3352012-07-23 07:59:50 +00006634 // The next optimizations are desireable only if SELECT_CC can be lowered.
6635 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6636 // having to say they don't support SELECT_CC on every type the DAG knows
6637 // about, since there is no way to mark an opcode illegal at all value types
6638 // (See also visitSELECT)
6639 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6640 // fold (uint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
Owen Andersond9bf71f2012-07-09 20:31:12 +00006641
Nadav Rotemed1a3352012-07-23 07:59:50 +00006642 if (N0.getOpcode() == ISD::SETCC && !VT.isVector() &&
6643 (!LegalOperations ||
6644 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6645 SDValue Ops[] =
6646 { N0.getOperand(0), N0.getOperand(1),
6647 DAG.getConstantFP(1.0, VT), DAG.getConstantFP(0.0, VT),
6648 N0.getOperand(2) };
Andrew Trickac6d9be2013-05-25 02:42:55 +00006649 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotemed1a3352012-07-23 07:59:50 +00006650 }
6651 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006652
Dan Gohman475871a2008-07-27 21:46:04 +00006653 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006654}
6655
Dan Gohman475871a2008-07-27 21:46:04 +00006656SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
6657 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006658 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006659 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006660
Nate Begeman1d4d4142005-09-01 00:19:25 +00006661 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00006662 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006663 return DAG.getNode(ISD::FP_TO_SINT, SDLoc(N), VT, N0);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006664
Dan Gohman475871a2008-07-27 21:46:04 +00006665 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006666}
6667
Dan Gohman475871a2008-07-27 21:46:04 +00006668SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
6669 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006670 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006671 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006672
Nate Begeman1d4d4142005-09-01 00:19:25 +00006673 // fold (fp_to_uint c1fp) -> c1
Ulrich Weigande669c932012-10-29 18:35:49 +00006674 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006675 return DAG.getNode(ISD::FP_TO_UINT, SDLoc(N), VT, N0);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006676
Dan Gohman475871a2008-07-27 21:46:04 +00006677 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006678}
6679
Dan Gohman475871a2008-07-27 21:46:04 +00006680SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
6681 SDValue N0 = N->getOperand(0);
6682 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006683 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006684 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006685
Nate Begeman1d4d4142005-09-01 00:19:25 +00006686 // fold (fp_round c1fp) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006687 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006688 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006689
Chris Lattner79dbea52006-03-13 06:26:26 +00006690 // fold (fp_round (fp_extend x)) -> x
6691 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
6692 return N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006693
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00006694 // fold (fp_round (fp_round x)) -> (fp_round x)
6695 if (N0.getOpcode() == ISD::FP_ROUND) {
6696 // This is a value preserving truncation if both round's are.
6697 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006698 N0.getNode()->getConstantOperandVal(1) == 1;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006699 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0.getOperand(0),
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00006700 DAG.getIntPtrConstant(IsTrunc));
6701 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006702
Chris Lattner79dbea52006-03-13 06:26:26 +00006703 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00006704 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006705 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, SDLoc(N0), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006706 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00006707 AddToWorkList(Tmp.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006708 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006709 Tmp, N0.getOperand(1));
Chris Lattner79dbea52006-03-13 06:26:26 +00006710 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006711
Dan Gohman475871a2008-07-27 21:46:04 +00006712 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006713}
6714
Dan Gohman475871a2008-07-27 21:46:04 +00006715SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
6716 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006717 EVT VT = N->getValueType(0);
6718 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00006719 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006720
Nate Begeman1d4d4142005-09-01 00:19:25 +00006721 // fold (fp_round_inreg c1fp) -> c1fp
Chris Lattner2392ae72010-04-15 04:48:01 +00006722 if (N0CFP && isTypeLegal(EVT)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00006723 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006724 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006725 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00006726
Dan Gohman475871a2008-07-27 21:46:04 +00006727 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006728}
6729
Dan Gohman475871a2008-07-27 21:46:04 +00006730SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
6731 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006732 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006733 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006734
Chris Lattner5938bef2007-12-29 06:55:23 +00006735 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Scott Michelfdc40a02009-02-17 22:15:04 +00006736 if (N->hasOneUse() &&
Dan Gohmane7852d02009-01-26 04:35:06 +00006737 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman475871a2008-07-27 21:46:04 +00006738 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00006739
Nate Begeman1d4d4142005-09-01 00:19:25 +00006740 // fold (fp_extend c1fp) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006741 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006742 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00006743
6744 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
6745 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00006746 if (N0.getOpcode() == ISD::FP_ROUND
6747 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00006748 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00006749 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00006750 if (VT.bitsLT(In.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006751 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006752 In, N0.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006753 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, In);
Chris Lattner0bd48932008-01-17 07:00:52 +00006754 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006755
Chris Lattner0bd48932008-01-17 07:00:52 +00006756 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Hal Finkel03c8f8f2013-10-04 22:18:12 +00006757 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00006758 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00006759 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00006760 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006761 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006762 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00006763 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00006764 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00006765 LN0->isVolatile(), LN0->isNonTemporal(),
6766 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00006767 CombineTo(N, ExtLoad);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006768 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00006769 DAG.getNode(ISD::FP_ROUND, SDLoc(N0),
Bill Wendling0225a1d2009-01-30 23:15:49 +00006770 N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00006771 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00006772 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00006773 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00006774
Dan Gohman475871a2008-07-27 21:46:04 +00006775 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006776}
6777
Dan Gohman475871a2008-07-27 21:46:04 +00006778SDValue DAGCombiner::visitFNEG(SDNode *N) {
6779 SDValue N0 = N->getOperand(0);
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006780 EVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00006781
Craig Topperdd201ff2012-09-11 01:45:21 +00006782 if (VT.isVector()) {
6783 SDValue FoldedVOp = SimplifyVUnaryOp(N);
6784 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper956342b2012-09-09 22:58:45 +00006785 }
6786
Owen Andersonafd3d562012-03-06 00:29:31 +00006787 if (isNegatibleForFree(N0, LegalOperations, DAG.getTargetLoweringInfo(),
6788 &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00006789 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohman23ff1822007-07-02 15:48:56 +00006790
Chris Lattner3bd39d42008-01-27 17:42:27 +00006791 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
6792 // constant pool values.
Owen Anderson29f60f32012-04-02 22:10:29 +00006793 if (!TLI.isFNegFree(VT) && N0.getOpcode() == ISD::BITCAST &&
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006794 !VT.isVector() &&
6795 N0.getNode()->hasOneUse() &&
6796 N0.getOperand(0).getValueType().isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006797 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006798 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006799 if (IntVT.isInteger() && !IntVT.isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006800 Int = DAG.getNode(ISD::XOR, SDLoc(N0), IntVT, Int,
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006801 DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00006802 AddToWorkList(Int.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006803 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006804 VT, Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00006805 }
6806 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006807
Owen Anderson58d57292012-09-01 06:04:27 +00006808 // (fneg (fmul c, x)) -> (fmul -c, x)
6809 if (N0.getOpcode() == ISD::FMUL) {
6810 ConstantFPSDNode *CFP1 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
Stephen Linb4940152013-07-09 00:44:49 +00006811 if (CFP1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006812 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson58d57292012-09-01 06:04:27 +00006813 N0.getOperand(0),
Andrew Trickac6d9be2013-05-25 02:42:55 +00006814 DAG.getNode(ISD::FNEG, SDLoc(N), VT,
Owen Anderson58d57292012-09-01 06:04:27 +00006815 N0.getOperand(1)));
Owen Anderson58d57292012-09-01 06:04:27 +00006816 }
6817
Dan Gohman475871a2008-07-27 21:46:04 +00006818 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006819}
6820
Owen Anderson7c626d32012-08-13 23:32:49 +00006821SDValue DAGCombiner::visitFCEIL(SDNode *N) {
6822 SDValue N0 = N->getOperand(0);
6823 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6824 EVT VT = N->getValueType(0);
6825
6826 // fold (fceil c1) -> fceil(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006827 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006828 return DAG.getNode(ISD::FCEIL, SDLoc(N), VT, N0);
Owen Anderson7c626d32012-08-13 23:32:49 +00006829
6830 return SDValue();
6831}
6832
6833SDValue DAGCombiner::visitFTRUNC(SDNode *N) {
6834 SDValue N0 = N->getOperand(0);
6835 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6836 EVT VT = N->getValueType(0);
6837
6838 // fold (ftrunc c1) -> ftrunc(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006839 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006840 return DAG.getNode(ISD::FTRUNC, SDLoc(N), VT, N0);
Owen Anderson7c626d32012-08-13 23:32:49 +00006841
6842 return SDValue();
6843}
6844
6845SDValue DAGCombiner::visitFFLOOR(SDNode *N) {
6846 SDValue N0 = N->getOperand(0);
6847 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6848 EVT VT = N->getValueType(0);
6849
6850 // fold (ffloor c1) -> ffloor(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006851 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006852 return DAG.getNode(ISD::FFLOOR, SDLoc(N), VT, N0);
Owen Anderson7c626d32012-08-13 23:32:49 +00006853
6854 return SDValue();
6855}
6856
Dan Gohman475871a2008-07-27 21:46:04 +00006857SDValue DAGCombiner::visitFABS(SDNode *N) {
6858 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006859 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006860 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006861
Craig Topperdd201ff2012-09-11 01:45:21 +00006862 if (VT.isVector()) {
6863 SDValue FoldedVOp = SimplifyVUnaryOp(N);
6864 if (FoldedVOp.getNode()) return FoldedVOp;
6865 }
6866
Nate Begeman1d4d4142005-09-01 00:19:25 +00006867 // fold (fabs c1) -> fabs(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006868 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006869 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006870 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00006871 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00006872 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006873 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00006874 // fold (fabs (fcopysign x, y)) -> (fabs x)
6875 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006876 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00006877
Chris Lattner3bd39d42008-01-27 17:42:27 +00006878 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
6879 // constant pool values.
Stephen Lin155615d2013-07-08 00:37:03 +00006880 if (!TLI.isFAbsFree(VT) &&
Owen Anderson29f60f32012-04-02 22:10:29 +00006881 N0.getOpcode() == ISD::BITCAST && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00006882 N0.getOperand(0).getValueType().isInteger() &&
6883 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006884 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006885 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006886 if (IntVT.isInteger() && !IntVT.isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006887 Int = DAG.getNode(ISD::AND, SDLoc(N0), IntVT, Int,
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006888 DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00006889 AddToWorkList(Int.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006890 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00006891 N->getValueType(0), Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00006892 }
6893 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006894
Dan Gohman475871a2008-07-27 21:46:04 +00006895 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006896}
6897
Dan Gohman475871a2008-07-27 21:46:04 +00006898SDValue DAGCombiner::visitBRCOND(SDNode *N) {
6899 SDValue Chain = N->getOperand(0);
6900 SDValue N1 = N->getOperand(1);
6901 SDValue N2 = N->getOperand(2);
Scott Michelfdc40a02009-02-17 22:15:04 +00006902
Dan Gohmane0f06c72009-11-17 00:47:23 +00006903 // If N is a constant we could fold this into a fallthrough or unconditional
6904 // branch. However that doesn't happen very often in normal code, because
6905 // Instcombine/SimplifyCFG should have handled the available opportunities.
6906 // If we did this folding here, it would be necessary to update the
6907 // MachineBasicBlock CFG, which is awkward.
6908
Nate Begeman750ac1b2006-02-01 07:19:44 +00006909 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
6910 // on the target.
Scott Michelfdc40a02009-02-17 22:15:04 +00006911 if (N1.getOpcode() == ISD::SETCC &&
Tom Stellard3ef53832013-03-08 15:36:57 +00006912 TLI.isOperationLegalOrCustom(ISD::BR_CC,
6913 N1.getOperand(0).getValueType())) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006914 return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00006915 Chain, N1.getOperand(2),
Nate Begeman750ac1b2006-02-01 07:19:44 +00006916 N1.getOperand(0), N1.getOperand(1), N2);
6917 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00006918
Evan Cheng2a135ae2010-10-04 22:41:01 +00006919 if ((N1.hasOneUse() && N1.getOpcode() == ISD::SRL) ||
6920 ((N1.getOpcode() == ISD::TRUNCATE && N1.hasOneUse()) &&
6921 (N1.getOperand(0).hasOneUse() &&
6922 N1.getOperand(0).getOpcode() == ISD::SRL))) {
6923 SDNode *Trunc = 0;
6924 if (N1.getOpcode() == ISD::TRUNCATE) {
6925 // Look pass the truncate.
6926 Trunc = N1.getNode();
6927 N1 = N1.getOperand(0);
6928 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006929
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006930 // Match this pattern so that we can generate simpler code:
6931 //
6932 // %a = ...
6933 // %b = and i32 %a, 2
6934 // %c = srl i32 %b, 1
6935 // brcond i32 %c ...
6936 //
6937 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006938 //
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006939 // %a = ...
Evan Chengd40d03e2010-01-06 19:38:29 +00006940 // %b = and i32 %a, 2
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006941 // %c = setcc eq %b, 0
6942 // brcond %c ...
6943 //
6944 // This applies only when the AND constant value has one bit set and the
6945 // SRL constant is equal to the log2 of the AND constant. The back-end is
6946 // smart enough to convert the result into a TEST/JMP sequence.
6947 SDValue Op0 = N1.getOperand(0);
6948 SDValue Op1 = N1.getOperand(1);
6949
6950 if (Op0.getOpcode() == ISD::AND &&
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006951 Op1.getOpcode() == ISD::Constant) {
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006952 SDValue AndOp1 = Op0.getOperand(1);
6953
6954 if (AndOp1.getOpcode() == ISD::Constant) {
6955 const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue();
6956
6957 if (AndConst.isPowerOf2() &&
6958 cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) {
6959 SDValue SetCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00006960 DAG.getSetCC(SDLoc(N),
Matt Arsenault225ed702013-05-18 00:21:46 +00006961 getSetCCResultType(Op0.getValueType()),
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006962 Op0, DAG.getConstant(0, Op0.getValueType()),
6963 ISD::SETNE);
6964
Andrew Trickac6d9be2013-05-25 02:42:55 +00006965 SDValue NewBRCond = DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Chengd40d03e2010-01-06 19:38:29 +00006966 MVT::Other, Chain, SetCC, N2);
6967 // Don't add the new BRCond into the worklist or else SimplifySelectCC
6968 // will convert it back to (X & C1) >> C2.
6969 CombineTo(N, NewBRCond, false);
6970 // Truncate is dead.
6971 if (Trunc) {
6972 removeFromWorkList(Trunc);
6973 DAG.DeleteNode(Trunc);
6974 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006975 // Replace the uses of SRL with SETCC
Evan Cheng2c755ba2010-02-27 07:36:59 +00006976 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006977 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006978 removeFromWorkList(N1.getNode());
6979 DAG.DeleteNode(N1.getNode());
Evan Chengd40d03e2010-01-06 19:38:29 +00006980 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006981 }
6982 }
6983 }
Evan Cheng2a135ae2010-10-04 22:41:01 +00006984
6985 if (Trunc)
6986 // Restore N1 if the above transformation doesn't match.
6987 N1 = N->getOperand(1);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006988 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006989
Evan Cheng2c755ba2010-02-27 07:36:59 +00006990 // Transform br(xor(x, y)) -> br(x != y)
6991 // Transform br(xor(xor(x,y), 1)) -> br (x == y)
6992 if (N1.hasOneUse() && N1.getOpcode() == ISD::XOR) {
6993 SDNode *TheXor = N1.getNode();
6994 SDValue Op0 = TheXor->getOperand(0);
6995 SDValue Op1 = TheXor->getOperand(1);
6996 if (Op0.getOpcode() == Op1.getOpcode()) {
6997 // Avoid missing important xor optimizations.
6998 SDValue Tmp = visitXOR(TheXor);
Evan Cheng78ec0252013-01-09 20:56:40 +00006999 if (Tmp.getNode()) {
7000 if (Tmp.getNode() != TheXor) {
7001 DEBUG(dbgs() << "\nReplacing.8 ";
7002 TheXor->dump(&DAG);
7003 dbgs() << "\nWith: ";
7004 Tmp.getNode()->dump(&DAG);
7005 dbgs() << '\n');
7006 WorkListRemover DeadNodes(*this);
7007 DAG.ReplaceAllUsesOfValueWith(N1, Tmp);
7008 removeFromWorkList(TheXor);
7009 DAG.DeleteNode(TheXor);
Andrew Trickac6d9be2013-05-25 02:42:55 +00007010 return DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Cheng78ec0252013-01-09 20:56:40 +00007011 MVT::Other, Chain, Tmp, N2);
7012 }
7013
Benjamin Kramer0b68b752013-03-30 21:28:18 +00007014 // visitXOR has changed XOR's operands or replaced the XOR completely,
7015 // bail out.
7016 return SDValue(N, 0);
Evan Cheng2c755ba2010-02-27 07:36:59 +00007017 }
7018 }
7019
7020 if (Op0.getOpcode() != ISD::SETCC && Op1.getOpcode() != ISD::SETCC) {
7021 bool Equal = false;
7022 if (ConstantSDNode *RHSCI = dyn_cast<ConstantSDNode>(Op0))
7023 if (RHSCI->getAPIntValue() == 1 && Op0.hasOneUse() &&
7024 Op0.getOpcode() == ISD::XOR) {
7025 TheXor = Op0.getNode();
7026 Equal = true;
7027 }
7028
Evan Cheng2a135ae2010-10-04 22:41:01 +00007029 EVT SetCCVT = N1.getValueType();
Evan Cheng2c755ba2010-02-27 07:36:59 +00007030 if (LegalTypes)
Matt Arsenault225ed702013-05-18 00:21:46 +00007031 SetCCVT = getSetCCResultType(SetCCVT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00007032 SDValue SetCC = DAG.getSetCC(SDLoc(TheXor),
Evan Cheng2c755ba2010-02-27 07:36:59 +00007033 SetCCVT,
7034 Op0, Op1,
7035 Equal ? ISD::SETEQ : ISD::SETNE);
7036 // Replace the uses of XOR with SETCC
7037 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007038 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Evan Cheng2a135ae2010-10-04 22:41:01 +00007039 removeFromWorkList(N1.getNode());
7040 DAG.DeleteNode(N1.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00007041 return DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Cheng2c755ba2010-02-27 07:36:59 +00007042 MVT::Other, Chain, SetCC, N2);
7043 }
7044 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00007045
Dan Gohman475871a2008-07-27 21:46:04 +00007046 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00007047}
7048
Chris Lattner3ea0b472005-10-05 06:47:48 +00007049// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
7050//
Dan Gohman475871a2008-07-27 21:46:04 +00007051SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00007052 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00007053 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Scott Michelfdc40a02009-02-17 22:15:04 +00007054
Dan Gohmane0f06c72009-11-17 00:47:23 +00007055 // If N is a constant we could fold this into a fallthrough or unconditional
7056 // branch. However that doesn't happen very often in normal code, because
7057 // Instcombine/SimplifyCFG should have handled the available opportunities.
7058 // If we did this folding here, it would be necessary to update the
7059 // MachineBasicBlock CFG, which is awkward.
7060
Duncan Sands8eab8a22008-06-09 11:32:28 +00007061 // Use SimplifySetCC to simplify SETCC's.
Matt Arsenault225ed702013-05-18 00:21:46 +00007062 SDValue Simp = SimplifySetCC(getSetCCResultType(CondLHS.getValueType()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00007063 CondLHS, CondRHS, CC->get(), SDLoc(N),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00007064 false);
Gabor Greifba36cb52008-08-28 21:40:38 +00007065 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00007066
Nate Begemane17daeb2005-10-05 21:43:42 +00007067 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00007068 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Andrew Trickac6d9be2013-05-25 02:42:55 +00007069 return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00007070 N->getOperand(0), Simp.getOperand(2),
7071 Simp.getOperand(0), Simp.getOperand(1),
7072 N->getOperand(4));
7073
Dan Gohman475871a2008-07-27 21:46:04 +00007074 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00007075}
7076
Evan Chengc4b527a2012-01-13 01:37:24 +00007077/// canFoldInAddressingMode - Return true if 'Use' is a load or a store that
7078/// uses N as its base pointer and that N may be folded in the load / store
Evan Cheng03be3622012-03-06 23:33:32 +00007079/// addressing mode.
Evan Chengc4b527a2012-01-13 01:37:24 +00007080static bool canFoldInAddressingMode(SDNode *N, SDNode *Use,
7081 SelectionDAG &DAG,
7082 const TargetLowering &TLI) {
7083 EVT VT;
7084 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Use)) {
7085 if (LD->isIndexed() || LD->getBasePtr().getNode() != N)
7086 return false;
7087 VT = Use->getValueType(0);
7088 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(Use)) {
7089 if (ST->isIndexed() || ST->getBasePtr().getNode() != N)
7090 return false;
7091 VT = ST->getValue().getValueType();
7092 } else
7093 return false;
7094
Chandler Carruth56d433d2013-01-07 15:14:13 +00007095 TargetLowering::AddrMode AM;
Evan Chengc4b527a2012-01-13 01:37:24 +00007096 if (N->getOpcode() == ISD::ADD) {
7097 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
7098 if (Offset)
Evan Cheng03be3622012-03-06 23:33:32 +00007099 // [reg +/- imm]
Evan Chengc4b527a2012-01-13 01:37:24 +00007100 AM.BaseOffs = Offset->getSExtValue();
7101 else
Evan Cheng03be3622012-03-06 23:33:32 +00007102 // [reg +/- reg]
7103 AM.Scale = 1;
Evan Chengc4b527a2012-01-13 01:37:24 +00007104 } else if (N->getOpcode() == ISD::SUB) {
7105 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
7106 if (Offset)
Evan Cheng03be3622012-03-06 23:33:32 +00007107 // [reg +/- imm]
Evan Chengc4b527a2012-01-13 01:37:24 +00007108 AM.BaseOffs = -Offset->getSExtValue();
7109 else
Evan Cheng03be3622012-03-06 23:33:32 +00007110 // [reg +/- reg]
7111 AM.Scale = 1;
Evan Chengc4b527a2012-01-13 01:37:24 +00007112 } else
7113 return false;
7114
7115 return TLI.isLegalAddressingMode(AM, VT.getTypeForEVT(*DAG.getContext()));
7116}
7117
Duncan Sandsec87aa82008-06-15 20:12:31 +00007118/// CombineToPreIndexedLoadStore - Try turning a load / store into a
7119/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00007120/// and it has other uses besides the load / store. After the
7121/// transformation, the new indexed load / store has effectively folded
7122/// the add / subtract in and all of its other uses are redirected to the
7123/// new load / store.
7124bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00007125 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00007126 return false;
7127
7128 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007129 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00007130 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00007131 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007132 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007133 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007134 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00007135 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00007136 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
7137 return false;
7138 Ptr = LD->getBasePtr();
7139 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007140 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007141 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007142 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007143 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
7144 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
7145 return false;
7146 Ptr = ST->getBasePtr();
7147 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007148 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00007149 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007150 }
Chris Lattner448f2192006-11-11 00:39:41 +00007151
Chris Lattner9f1794e2006-11-11 00:56:29 +00007152 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
7153 // out. There is no reason to make this a preinc/predec.
7154 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00007155 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00007156 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00007157
Chris Lattner9f1794e2006-11-11 00:56:29 +00007158 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00007159 SDValue BasePtr;
7160 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007161 ISD::MemIndexedMode AM = ISD::UNINDEXED;
7162 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
7163 return false;
Hal Finkel089a5f82013-02-08 21:35:47 +00007164
7165 // Backends without true r+i pre-indexed forms may need to pass a
7166 // constant base with a variable offset so that constant coercion
7167 // will work with the patterns in canonical form.
7168 bool Swapped = false;
7169 if (isa<ConstantSDNode>(BasePtr)) {
7170 std::swap(BasePtr, Offset);
7171 Swapped = true;
7172 }
7173
Evan Chenga7d4a042007-05-03 23:52:19 +00007174 // Don't create a indexed load / store with zero offset.
7175 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00007176 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00007177 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007178
Chris Lattner41e53fd2006-11-11 01:00:15 +00007179 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00007180 // 1) The new base ptr is a frame index.
7181 // 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 +00007182 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00007183 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00007184 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00007185 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00007186
Chris Lattner41e53fd2006-11-11 01:00:15 +00007187 // Check #1. Preinc'ing a frame index would require copying the stack pointer
7188 // (plus the implicit offset) to a register to preinc anyway.
Evan Chengcaab1292009-05-06 18:25:01 +00007189 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
Chris Lattner41e53fd2006-11-11 01:00:15 +00007190 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007191
Chris Lattner41e53fd2006-11-11 01:00:15 +00007192 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00007193 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00007194 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007195 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007196 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00007197 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007198
Hal Finkel089a5f82013-02-08 21:35:47 +00007199 // If the offset is a constant, there may be other adds of constants that
7200 // can be folded with this one. We should do this to avoid having to keep
7201 // a copy of the original base pointer.
7202 SmallVector<SDNode *, 16> OtherUses;
7203 if (isa<ConstantSDNode>(Offset))
7204 for (SDNode::use_iterator I = BasePtr.getNode()->use_begin(),
7205 E = BasePtr.getNode()->use_end(); I != E; ++I) {
7206 SDNode *Use = *I;
7207 if (Use == Ptr.getNode())
7208 continue;
7209
7210 if (Use->isPredecessorOf(N))
7211 continue;
7212
7213 if (Use->getOpcode() != ISD::ADD && Use->getOpcode() != ISD::SUB) {
7214 OtherUses.clear();
7215 break;
7216 }
7217
7218 SDValue Op0 = Use->getOperand(0), Op1 = Use->getOperand(1);
7219 if (Op1.getNode() == BasePtr.getNode())
7220 std::swap(Op0, Op1);
7221 assert(Op0.getNode() == BasePtr.getNode() &&
7222 "Use of ADD/SUB but not an operand");
7223
7224 if (!isa<ConstantSDNode>(Op1)) {
7225 OtherUses.clear();
7226 break;
7227 }
7228
7229 // FIXME: In some cases, we can be smarter about this.
7230 if (Op1.getValueType() != Offset.getValueType()) {
7231 OtherUses.clear();
7232 break;
7233 }
7234
7235 OtherUses.push_back(Use);
7236 }
7237
7238 if (Swapped)
7239 std::swap(BasePtr, Offset);
7240
Evan Chengc843abe2007-05-24 02:35:39 +00007241 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00007242 bool RealUse = false;
Lang Hames944520f2011-07-07 04:31:51 +00007243
7244 // Caches for hasPredecessorHelper
7245 SmallPtrSet<const SDNode *, 32> Visited;
7246 SmallVector<const SDNode *, 16> Worklist;
7247
Gabor Greifba36cb52008-08-28 21:40:38 +00007248 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
7249 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00007250 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007251 if (Use == N)
7252 continue;
Lang Hames944520f2011-07-07 04:31:51 +00007253 if (N->hasPredecessorHelper(Use, Visited, Worklist))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007254 return false;
7255
Evan Chengc4b527a2012-01-13 01:37:24 +00007256 // If Ptr may be folded in addressing mode of other use, then it's
7257 // not profitable to do this transformation.
7258 if (!canFoldInAddressingMode(Ptr.getNode(), Use, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007259 RealUse = true;
7260 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007261
Chris Lattner9f1794e2006-11-11 00:56:29 +00007262 if (!RealUse)
7263 return false;
7264
Dan Gohman475871a2008-07-27 21:46:04 +00007265 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007266 if (isLoad)
Andrew Trickac6d9be2013-05-25 02:42:55 +00007267 Result = DAG.getIndexedLoad(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007268 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007269 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00007270 Result = DAG.getIndexedStore(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007271 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007272 ++PreIndexedNodes;
7273 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00007274 DEBUG(dbgs() << "\nReplacing.4 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007275 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007276 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007277 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007278 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007279 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007280 if (isLoad) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007281 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
7282 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007283 } else {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007284 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007285 }
7286
Chris Lattner9f1794e2006-11-11 00:56:29 +00007287 // Finally, since the node is now dead, remove it from the graph.
7288 DAG.DeleteNode(N);
7289
Hal Finkel089a5f82013-02-08 21:35:47 +00007290 if (Swapped)
7291 std::swap(BasePtr, Offset);
7292
7293 // Replace other uses of BasePtr that can be updated to use Ptr
7294 for (unsigned i = 0, e = OtherUses.size(); i != e; ++i) {
7295 unsigned OffsetIdx = 1;
7296 if (OtherUses[i]->getOperand(OffsetIdx).getNode() == BasePtr.getNode())
7297 OffsetIdx = 0;
7298 assert(OtherUses[i]->getOperand(!OffsetIdx).getNode() ==
7299 BasePtr.getNode() && "Expected BasePtr operand");
7300
Silviu Baranga730a5702013-04-26 15:52:24 +00007301 // We need to replace ptr0 in the following expression:
7302 // x0 * offset0 + y0 * ptr0 = t0
7303 // knowing that
7304 // x1 * offset1 + y1 * ptr0 = t1 (the indexed load/store)
Stephen Lin155615d2013-07-08 00:37:03 +00007305 //
Silviu Baranga730a5702013-04-26 15:52:24 +00007306 // where x0, x1, y0 and y1 in {-1, 1} are given by the types of the
7307 // indexed load/store and the expresion that needs to be re-written.
7308 //
7309 // Therefore, we have:
7310 // t0 = (x0 * offset0 - x1 * y0 * y1 *offset1) + (y0 * y1) * t1
Hal Finkel089a5f82013-02-08 21:35:47 +00007311
7312 ConstantSDNode *CN =
7313 cast<ConstantSDNode>(OtherUses[i]->getOperand(OffsetIdx));
Silviu Baranga730a5702013-04-26 15:52:24 +00007314 int X0, X1, Y0, Y1;
7315 APInt Offset0 = CN->getAPIntValue();
7316 APInt Offset1 = cast<ConstantSDNode>(Offset)->getAPIntValue();
Hal Finkel089a5f82013-02-08 21:35:47 +00007317
Silviu Baranga730a5702013-04-26 15:52:24 +00007318 X0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 1) ? -1 : 1;
7319 Y0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 0) ? -1 : 1;
7320 X1 = (AM == ISD::PRE_DEC && !Swapped) ? -1 : 1;
7321 Y1 = (AM == ISD::PRE_DEC && Swapped) ? -1 : 1;
Hal Finkel089a5f82013-02-08 21:35:47 +00007322
Silviu Baranga730a5702013-04-26 15:52:24 +00007323 unsigned Opcode = (Y0 * Y1 < 0) ? ISD::SUB : ISD::ADD;
7324
7325 APInt CNV = Offset0;
7326 if (X0 < 0) CNV = -CNV;
7327 if (X1 * Y0 * Y1 < 0) CNV = CNV + Offset1;
7328 else CNV = CNV - Offset1;
7329
7330 // We can now generate the new expression.
7331 SDValue NewOp1 = DAG.getConstant(CNV, CN->getValueType(0));
7332 SDValue NewOp2 = Result.getValue(isLoad ? 1 : 0);
7333
7334 SDValue NewUse = DAG.getNode(Opcode,
Andrew Trickac6d9be2013-05-25 02:42:55 +00007335 SDLoc(OtherUses[i]),
Hal Finkel089a5f82013-02-08 21:35:47 +00007336 OtherUses[i]->getValueType(0), NewOp1, NewOp2);
7337 DAG.ReplaceAllUsesOfValueWith(SDValue(OtherUses[i], 0), NewUse);
7338 removeFromWorkList(OtherUses[i]);
7339 DAG.DeleteNode(OtherUses[i]);
7340 }
7341
Chris Lattner9f1794e2006-11-11 00:56:29 +00007342 // Replace the uses of Ptr with uses of the updated base value.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007343 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0));
Gabor Greifba36cb52008-08-28 21:40:38 +00007344 removeFromWorkList(Ptr.getNode());
7345 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00007346
7347 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00007348}
7349
Duncan Sandsec87aa82008-06-15 20:12:31 +00007350/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00007351/// add / sub of the base pointer node into a post-indexed load / store.
7352/// The transformation folded the add / subtract into the new indexed
7353/// load / store effectively and all of its uses are redirected to the
7354/// new load / store.
7355bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00007356 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00007357 return false;
7358
7359 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007360 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00007361 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00007362 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007363 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007364 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007365 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007366 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
7367 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
7368 return false;
7369 Ptr = LD->getBasePtr();
7370 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007371 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007372 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007373 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007374 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
7375 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
7376 return false;
7377 Ptr = ST->getBasePtr();
7378 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007379 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00007380 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007381 }
Chris Lattner448f2192006-11-11 00:39:41 +00007382
Gabor Greifba36cb52008-08-28 21:40:38 +00007383 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00007384 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007385
Gabor Greifba36cb52008-08-28 21:40:38 +00007386 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
7387 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00007388 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007389 if (Op == N ||
7390 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
7391 continue;
7392
Dan Gohman475871a2008-07-27 21:46:04 +00007393 SDValue BasePtr;
7394 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007395 ISD::MemIndexedMode AM = ISD::UNINDEXED;
7396 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
Evan Chenga7d4a042007-05-03 23:52:19 +00007397 // Don't create a indexed load / store with zero offset.
7398 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00007399 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00007400 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00007401
Chris Lattner9f1794e2006-11-11 00:56:29 +00007402 // Try turning it into a post-indexed load / store except when
Evan Chengc4b527a2012-01-13 01:37:24 +00007403 // 1) All uses are load / store ops that use it as base ptr (and
7404 // it may be folded as addressing mmode).
Chris Lattner9f1794e2006-11-11 00:56:29 +00007405 // 2) Op must be independent of N, i.e. Op is neither a predecessor
7406 // nor a successor of N. Otherwise, if Op is folded that would
7407 // create a cycle.
7408
Evan Chengcaab1292009-05-06 18:25:01 +00007409 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
7410 continue;
7411
Chris Lattner9f1794e2006-11-11 00:56:29 +00007412 // Check for #1.
7413 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00007414 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
7415 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00007416 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00007417 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00007418 continue;
7419
Chris Lattner9f1794e2006-11-11 00:56:29 +00007420 // If all the uses are load / store addresses, then don't do the
7421 // transformation.
7422 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
7423 bool RealUse = false;
7424 for (SDNode::use_iterator III = Use->use_begin(),
7425 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00007426 SDNode *UseUse = *III;
Stephen Lin155615d2013-07-08 00:37:03 +00007427 if (!canFoldInAddressingMode(Use, UseUse, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007428 RealUse = true;
7429 }
Chris Lattner448f2192006-11-11 00:39:41 +00007430
Chris Lattner9f1794e2006-11-11 00:56:29 +00007431 if (!RealUse) {
7432 TryNext = true;
7433 break;
Chris Lattner448f2192006-11-11 00:39:41 +00007434 }
7435 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007436 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007437
Chris Lattner9f1794e2006-11-11 00:56:29 +00007438 if (TryNext)
7439 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00007440
Chris Lattner9f1794e2006-11-11 00:56:29 +00007441 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00007442 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00007443 SDValue Result = isLoad
Andrew Trickac6d9be2013-05-25 02:42:55 +00007444 ? DAG.getIndexedLoad(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007445 BasePtr, Offset, AM)
Andrew Trickac6d9be2013-05-25 02:42:55 +00007446 : DAG.getIndexedStore(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007447 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007448 ++PostIndexedNodes;
7449 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00007450 DEBUG(dbgs() << "\nReplacing.5 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007451 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007452 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007453 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007454 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007455 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007456 if (isLoad) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007457 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
7458 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007459 } else {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007460 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattner448f2192006-11-11 00:39:41 +00007461 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007462
Chris Lattner9f1794e2006-11-11 00:56:29 +00007463 // Finally, since the node is now dead, remove it from the graph.
7464 DAG.DeleteNode(N);
7465
7466 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00007467 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007468 Result.getValue(isLoad ? 1 : 0));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007469 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007470 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007471 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00007472 }
7473 }
7474 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007475
Chris Lattner448f2192006-11-11 00:39:41 +00007476 return false;
7477}
7478
Dan Gohman475871a2008-07-27 21:46:04 +00007479SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00007480 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007481 SDValue Chain = LD->getChain();
7482 SDValue Ptr = LD->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00007483
Evan Cheng45a7ca92007-05-01 00:38:21 +00007484 // If load is not volatile and there are no uses of the loaded value (and
7485 // the updated indexed value in case of indexed loads), change uses of the
7486 // chain value into uses of the chain input (i.e. delete the dead load).
7487 if (!LD->isVolatile()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007488 if (N->getValueType(1) == MVT::Other) {
Evan Cheng498f5592007-05-01 08:53:39 +00007489 // Unindexed loads.
Craig Topper704e1a02012-01-07 18:31:09 +00007490 if (!N->hasAnyUseOfValue(0)) {
Evan Cheng02c42852008-01-16 23:11:54 +00007491 // It's not safe to use the two value CombineTo variant here. e.g.
7492 // v1, chain2 = load chain1, loc
7493 // v2, chain3 = load chain2, loc
7494 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00007495 // Now we replace use of chain2 with chain1. This makes the second load
7496 // isomorphic to the one we are deleting, and thus makes this load live.
David Greenef1090292010-01-05 01:25:00 +00007497 DEBUG(dbgs() << "\nReplacing.6 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007498 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007499 dbgs() << "\nWith chain: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007500 Chain.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007501 dbgs() << "\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007502 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007503 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain);
Bill Wendlingc0debad2009-01-30 23:27:35 +00007504
Chris Lattner125991a2008-01-24 07:57:06 +00007505 if (N->use_empty()) {
7506 removeFromWorkList(N);
7507 DAG.DeleteNode(N);
7508 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007509
Dan Gohman475871a2008-07-27 21:46:04 +00007510 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00007511 }
Evan Cheng498f5592007-05-01 08:53:39 +00007512 } else {
7513 // Indexed loads.
Owen Anderson825b72b2009-08-11 20:47:22 +00007514 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
Craig Topper704e1a02012-01-07 18:31:09 +00007515 if (!N->hasAnyUseOfValue(0) && !N->hasAnyUseOfValue(1)) {
Dale Johannesene8d72302009-02-06 23:05:02 +00007516 SDValue Undef = DAG.getUNDEF(N->getValueType(0));
Evan Cheng2c755ba2010-02-27 07:36:59 +00007517 DEBUG(dbgs() << "\nReplacing.7 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007518 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007519 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007520 Undef.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007521 dbgs() << " and 2 other values\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007522 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007523 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef);
Dan Gohman475871a2008-07-27 21:46:04 +00007524 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007525 DAG.getUNDEF(N->getValueType(1)));
7526 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain);
Evan Cheng02c42852008-01-16 23:11:54 +00007527 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00007528 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007529 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00007530 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00007531 }
7532 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007533
Chris Lattner01a22022005-10-10 22:04:48 +00007534 // If this load is directly stored, replace the load value with the stored
7535 // value.
7536 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007537 // TODO: Handle TRUNCSTORE/LOADEXT
Evan Cheng9ef82ce2011-03-11 00:48:56 +00007538 if (ISD::isNormalLoad(N) && !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007539 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00007540 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
7541 if (PrevST->getBasePtr() == Ptr &&
7542 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007543 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00007544 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007545 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007546
Evan Cheng255f20f2010-04-01 06:04:33 +00007547 // Try to infer better alignment information than the load already has.
7548 if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00007549 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
Owen Andersonb48783b2013-02-05 19:24:39 +00007550 if (Align > LD->getMemOperand()->getBaseAlignment()) {
7551 SDValue NewLoad =
Andrew Trickac6d9be2013-05-25 02:42:55 +00007552 DAG.getExtLoad(LD->getExtensionType(), SDLoc(N),
Evan Chenged1c0c72011-11-28 22:37:34 +00007553 LD->getValueType(0),
7554 Chain, Ptr, LD->getPointerInfo(),
7555 LD->getMemoryVT(),
7556 LD->isVolatile(), LD->isNonTemporal(), Align);
Owen Andersonb48783b2013-02-05 19:24:39 +00007557 return CombineTo(N, NewLoad, SDValue(NewLoad.getNode(), 1), true);
7558 }
Evan Cheng255f20f2010-04-01 06:04:33 +00007559 }
7560 }
7561
Hal Finkel253acef2013-08-29 03:29:55 +00007562 bool UseAA = CombinerAA.getNumOccurrences() > 0 ? CombinerAA :
7563 TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA();
7564 if (UseAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00007565 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007566 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00007567
Jim Laskey6ff23e52006-10-04 16:53:27 +00007568 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00007569 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00007570 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007571
Jim Laskey279f0532006-09-25 16:29:54 +00007572 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007573 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007574 ReplLoad = DAG.getLoad(N->getValueType(0), SDLoc(LD),
Chris Lattnerfa459012010-09-21 16:08:50 +00007575 BetterChain, Ptr, LD->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00007576 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007577 LD->isInvariant(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007578 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007579 ReplLoad = DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD),
Stuart Hastingsa9011292011-02-16 16:23:55 +00007580 LD->getValueType(0),
Chris Lattnerfa459012010-09-21 16:08:50 +00007581 BetterChain, Ptr, LD->getPointerInfo(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007582 LD->getMemoryVT(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007583 LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00007584 LD->isNonTemporal(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00007585 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007586 }
Jim Laskey279f0532006-09-25 16:29:54 +00007587
Jim Laskey6ff23e52006-10-04 16:53:27 +00007588 // Create token factor to keep old chain connected.
Andrew Trickac6d9be2013-05-25 02:42:55 +00007589 SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson825b72b2009-08-11 20:47:22 +00007590 MVT::Other, Chain, ReplLoad.getValue(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007591
Nate Begemanb6aef5c2009-09-15 00:18:30 +00007592 // Make sure the new and old chains are cleaned up.
7593 AddToWorkList(Token.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007594
Jim Laskey274062c2006-10-13 23:32:28 +00007595 // Replace uses with load result and token factor. Don't add users
7596 // to work list.
7597 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00007598 }
7599 }
7600
Evan Cheng7fc033a2006-11-03 03:06:21 +00007601 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00007602 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00007603 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00007604
Quentin Colombet83f743a2013-10-11 18:29:42 +00007605 // Try to slice up N to more direct loads if the slices are mapped to
7606 // different register banks or pairing can take place.
7607 if (SliceUpLoad(N))
7608 return SDValue(N, 0);
7609
Dan Gohman475871a2008-07-27 21:46:04 +00007610 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00007611}
7612
Quentin Colombet83f743a2013-10-11 18:29:42 +00007613namespace {
7614/// \brief Helper structure used to slice a load in smaller loads.
7615/// Basically a slice is obtained from the following sequence:
7616/// Origin = load Ty1, Base
7617/// Shift = srl Ty1 Origin, CstTy Amount
7618/// Inst = trunc Shift to Ty2
7619///
7620/// Then, it will be rewriten into:
7621/// Slice = load SliceTy, Base + SliceOffset
7622/// [Inst = zext Slice to Ty2], only if SliceTy <> Ty2
7623///
7624/// SliceTy is deduced from the number of bits that are actually used to
7625/// build Inst.
7626struct LoadedSlice {
7627 /// \brief Helper structure used to compute the cost of a slice.
7628 struct Cost {
7629 /// Are we optimizing for code size.
7630 bool ForCodeSize;
7631 /// Various cost.
7632 unsigned Loads;
7633 unsigned Truncates;
7634 unsigned CrossRegisterBanksCopies;
7635 unsigned ZExts;
7636 unsigned Shift;
7637
7638 Cost(bool ForCodeSize = false)
7639 : ForCodeSize(ForCodeSize), Loads(0), Truncates(0),
7640 CrossRegisterBanksCopies(0), ZExts(0), Shift(0) {}
7641
7642 /// \brief Get the cost of one isolated slice.
7643 Cost(const LoadedSlice &LS, bool ForCodeSize = false)
7644 : ForCodeSize(ForCodeSize), Loads(1), Truncates(0),
7645 CrossRegisterBanksCopies(0), ZExts(0), Shift(0) {
7646 EVT TruncType = LS.Inst->getValueType(0);
7647 EVT LoadedType = LS.getLoadedType();
7648 if (TruncType != LoadedType &&
7649 !LS.DAG->getTargetLoweringInfo().isZExtFree(LoadedType, TruncType))
7650 ZExts = 1;
7651 }
7652
7653 /// \brief Account for slicing gain in the current cost.
7654 /// Slicing provide a few gains like removing a shift or a
7655 /// truncate. This method allows to grow the cost of the original
7656 /// load with the gain from this slice.
7657 void addSliceGain(const LoadedSlice &LS) {
7658 // Each slice saves a truncate.
7659 const TargetLowering &TLI = LS.DAG->getTargetLoweringInfo();
7660 if (!TLI.isTruncateFree(LS.Inst->getValueType(0),
7661 LS.Inst->getOperand(0).getValueType()))
7662 ++Truncates;
7663 // If there is a shift amount, this slice gets rid of it.
7664 if (LS.Shift)
7665 ++Shift;
7666 // If this slice can merge a cross register bank copy, account for it.
7667 if (LS.canMergeExpensiveCrossRegisterBankCopy())
7668 ++CrossRegisterBanksCopies;
7669 }
7670
7671 Cost &operator+=(const Cost &RHS) {
7672 Loads += RHS.Loads;
7673 Truncates += RHS.Truncates;
7674 CrossRegisterBanksCopies += RHS.CrossRegisterBanksCopies;
7675 ZExts += RHS.ZExts;
7676 Shift += RHS.Shift;
7677 return *this;
7678 }
7679
7680 bool operator==(const Cost &RHS) const {
7681 return Loads == RHS.Loads && Truncates == RHS.Truncates &&
7682 CrossRegisterBanksCopies == RHS.CrossRegisterBanksCopies &&
7683 ZExts == RHS.ZExts && Shift == RHS.Shift;
7684 }
7685
7686 bool operator!=(const Cost &RHS) const { return !(*this == RHS); }
7687
7688 bool operator<(const Cost &RHS) const {
7689 // Assume cross register banks copies are as expensive as loads.
7690 // FIXME: Do we want some more target hooks?
7691 unsigned ExpensiveOpsLHS = Loads + CrossRegisterBanksCopies;
7692 unsigned ExpensiveOpsRHS = RHS.Loads + RHS.CrossRegisterBanksCopies;
7693 // Unless we are optimizing for code size, consider the
7694 // expensive operation first.
7695 if (!ForCodeSize && ExpensiveOpsLHS != ExpensiveOpsRHS)
7696 return ExpensiveOpsLHS < ExpensiveOpsRHS;
7697 return (Truncates + ZExts + Shift + ExpensiveOpsLHS) <
7698 (RHS.Truncates + RHS.ZExts + RHS.Shift + ExpensiveOpsRHS);
7699 }
7700
7701 bool operator>(const Cost &RHS) const { return RHS < *this; }
7702
7703 bool operator<=(const Cost &RHS) const { return !(RHS < *this); }
7704
7705 bool operator>=(const Cost &RHS) const { return !(*this < RHS); }
7706 };
7707 // The last instruction that represent the slice. This should be a
7708 // truncate instruction.
7709 SDNode *Inst;
7710 // The original load instruction.
7711 LoadSDNode *Origin;
7712 // The right shift amount in bits from the original load.
7713 unsigned Shift;
7714 // The DAG from which Origin came from.
7715 // This is used to get some contextual information about legal types, etc.
7716 SelectionDAG *DAG;
7717
7718 LoadedSlice(SDNode *Inst = NULL, LoadSDNode *Origin = NULL,
7719 unsigned Shift = 0, SelectionDAG *DAG = NULL)
7720 : Inst(Inst), Origin(Origin), Shift(Shift), DAG(DAG) {}
7721
7722 LoadedSlice(const LoadedSlice &LS)
7723 : Inst(LS.Inst), Origin(LS.Origin), Shift(LS.Shift), DAG(LS.DAG) {}
7724
7725 /// \brief Get the bits used in a chunk of bits \p BitWidth large.
7726 /// \return Result is \p BitWidth and has used bits set to 1 and
7727 /// not used bits set to 0.
7728 APInt getUsedBits() const {
7729 // Reproduce the trunc(lshr) sequence:
7730 // - Start from the truncated value.
7731 // - Zero extend to the desired bit width.
7732 // - Shift left.
7733 assert(Origin && "No original load to compare against.");
7734 unsigned BitWidth = Origin->getValueSizeInBits(0);
7735 assert(Inst && "This slice is not bound to an instruction");
7736 assert(Inst->getValueSizeInBits(0) <= BitWidth &&
7737 "Extracted slice is bigger than the whole type!");
7738 APInt UsedBits(Inst->getValueSizeInBits(0), 0);
7739 UsedBits.setAllBits();
7740 UsedBits = UsedBits.zext(BitWidth);
7741 UsedBits <<= Shift;
7742 return UsedBits;
7743 }
7744
7745 /// \brief Get the size of the slice to be loaded in bytes.
7746 unsigned getLoadedSize() const {
7747 unsigned SliceSize = getUsedBits().countPopulation();
7748 assert(!(SliceSize & 0x7) && "Size is not a multiple of a byte.");
7749 return SliceSize / 8;
7750 }
7751
7752 /// \brief Get the type that will be loaded for this slice.
7753 /// Note: This may not be the final type for the slice.
7754 EVT getLoadedType() const {
7755 assert(DAG && "Missing context");
7756 LLVMContext &Ctxt = *DAG->getContext();
7757 return EVT::getIntegerVT(Ctxt, getLoadedSize() * 8);
7758 }
7759
7760 /// \brief Get the alignment of the load used for this slice.
7761 unsigned getAlignment() const {
7762 unsigned Alignment = Origin->getAlignment();
7763 unsigned Offset = getOffsetFromBase();
7764 if (Offset != 0)
7765 Alignment = MinAlign(Alignment, Alignment + Offset);
7766 return Alignment;
7767 }
7768
7769 /// \brief Check if this slice can be rewritten with legal operations.
7770 bool isLegal() const {
7771 // An invalid slice is not legal.
7772 if (!Origin || !Inst || !DAG)
7773 return false;
7774
7775 // Offsets are for indexed load only, we do not handle that.
7776 if (Origin->getOffset().getOpcode() != ISD::UNDEF)
7777 return false;
7778
7779 const TargetLowering &TLI = DAG->getTargetLoweringInfo();
7780
7781 // Check that the type is legal.
7782 EVT SliceType = getLoadedType();
7783 if (!TLI.isTypeLegal(SliceType))
7784 return false;
7785
7786 // Check that the load is legal for this type.
7787 if (!TLI.isOperationLegal(ISD::LOAD, SliceType))
7788 return false;
7789
7790 // Check that the offset can be computed.
7791 // 1. Check its type.
7792 EVT PtrType = Origin->getBasePtr().getValueType();
7793 if (PtrType == MVT::Untyped || PtrType.isExtended())
7794 return false;
7795
7796 // 2. Check that it fits in the immediate.
7797 if (!TLI.isLegalAddImmediate(getOffsetFromBase()))
7798 return false;
7799
7800 // 3. Check that the computation is legal.
7801 if (!TLI.isOperationLegal(ISD::ADD, PtrType))
7802 return false;
7803
7804 // Check that the zext is legal if it needs one.
7805 EVT TruncateType = Inst->getValueType(0);
7806 if (TruncateType != SliceType &&
7807 !TLI.isOperationLegal(ISD::ZERO_EXTEND, TruncateType))
7808 return false;
7809
7810 return true;
7811 }
7812
7813 /// \brief Get the offset in bytes of this slice in the original chunk of
7814 /// bits.
7815 /// \pre DAG != NULL.
7816 uint64_t getOffsetFromBase() const {
7817 assert(DAG && "Missing context.");
7818 bool IsBigEndian =
7819 DAG->getTargetLoweringInfo().getDataLayout()->isBigEndian();
7820 assert(!(Shift & 0x7) && "Shifts not aligned on Bytes are not supported.");
7821 uint64_t Offset = Shift / 8;
7822 unsigned TySizeInBytes = Origin->getValueSizeInBits(0) / 8;
7823 assert(!(Origin->getValueSizeInBits(0) & 0x7) &&
7824 "The size of the original loaded type is not a multiple of a"
7825 " byte.");
7826 // If Offset is bigger than TySizeInBytes, it means we are loading all
7827 // zeros. This should have been optimized before in the process.
7828 assert(TySizeInBytes > Offset &&
7829 "Invalid shift amount for given loaded size");
7830 if (IsBigEndian)
7831 Offset = TySizeInBytes - Offset - getLoadedSize();
7832 return Offset;
7833 }
7834
7835 /// \brief Generate the sequence of instructions to load the slice
7836 /// represented by this object and redirect the uses of this slice to
7837 /// this new sequence of instructions.
7838 /// \pre this->Inst && this->Origin are valid Instructions and this
7839 /// object passed the legal check: LoadedSlice::isLegal returned true.
7840 /// \return The last instruction of the sequence used to load the slice.
7841 SDValue loadSlice() const {
7842 assert(Inst && Origin && "Unable to replace a non-existing slice.");
7843 const SDValue &OldBaseAddr = Origin->getBasePtr();
7844 SDValue BaseAddr = OldBaseAddr;
7845 // Get the offset in that chunk of bytes w.r.t. the endianess.
7846 int64_t Offset = static_cast<int64_t>(getOffsetFromBase());
7847 assert(Offset >= 0 && "Offset too big to fit in int64_t!");
7848 if (Offset) {
7849 // BaseAddr = BaseAddr + Offset.
7850 EVT ArithType = BaseAddr.getValueType();
7851 BaseAddr = DAG->getNode(ISD::ADD, SDLoc(Origin), ArithType, BaseAddr,
7852 DAG->getConstant(Offset, ArithType));
7853 }
7854
7855 // Create the type of the loaded slice according to its size.
7856 EVT SliceType = getLoadedType();
7857
7858 // Create the load for the slice.
7859 SDValue LastInst = DAG->getLoad(
7860 SliceType, SDLoc(Origin), Origin->getChain(), BaseAddr,
7861 Origin->getPointerInfo().getWithOffset(Offset), Origin->isVolatile(),
7862 Origin->isNonTemporal(), Origin->isInvariant(), getAlignment());
7863 // If the final type is not the same as the loaded type, this means that
7864 // we have to pad with zero. Create a zero extend for that.
7865 EVT FinalType = Inst->getValueType(0);
7866 if (SliceType != FinalType)
7867 LastInst =
7868 DAG->getNode(ISD::ZERO_EXTEND, SDLoc(LastInst), FinalType, LastInst);
7869 return LastInst;
7870 }
7871
7872 /// \brief Check if this slice can be merged with an expensive cross register
7873 /// bank copy. E.g.,
7874 /// i = load i32
7875 /// f = bitcast i32 i to float
7876 bool canMergeExpensiveCrossRegisterBankCopy() const {
7877 if (!Inst || !Inst->hasOneUse())
7878 return false;
7879 SDNode *Use = *Inst->use_begin();
7880 if (Use->getOpcode() != ISD::BITCAST)
7881 return false;
7882 assert(DAG && "Missing context");
7883 const TargetLowering &TLI = DAG->getTargetLoweringInfo();
7884 EVT ResVT = Use->getValueType(0);
7885 const TargetRegisterClass *ResRC = TLI.getRegClassFor(ResVT.getSimpleVT());
7886 const TargetRegisterClass *ArgRC =
7887 TLI.getRegClassFor(Use->getOperand(0).getValueType().getSimpleVT());
7888 if (ArgRC == ResRC || !TLI.isOperationLegal(ISD::LOAD, ResVT))
7889 return false;
7890
7891 // At this point, we know that we perform a cross-register-bank copy.
7892 // Check if it is expensive.
7893 const TargetRegisterInfo *TRI = TLI.getTargetMachine().getRegisterInfo();
7894 // Assume bitcasts are cheap, unless both register classes do not
7895 // explicitly share a common sub class.
7896 if (!TRI || TRI->getCommonSubClass(ArgRC, ResRC))
7897 return false;
7898
7899 // Check if it will be merged with the load.
7900 // 1. Check the alignment constraint.
7901 unsigned RequiredAlignment = TLI.getDataLayout()->getABITypeAlignment(
7902 ResVT.getTypeForEVT(*DAG->getContext()));
7903
7904 if (RequiredAlignment > getAlignment())
7905 return false;
7906
7907 // 2. Check that the load is a legal operation for that type.
7908 if (!TLI.isOperationLegal(ISD::LOAD, ResVT))
7909 return false;
7910
7911 // 3. Check that we do not have a zext in the way.
7912 if (Inst->getValueType(0) != getLoadedType())
7913 return false;
7914
7915 return true;
7916 }
7917};
7918}
7919
7920/// \brief Sorts LoadedSlice according to their offset.
7921struct LoadedSliceSorter {
7922 bool operator()(const LoadedSlice &LHS, const LoadedSlice &RHS) {
7923 assert(LHS.Origin == RHS.Origin && "Different bases not implemented.");
7924 return LHS.getOffsetFromBase() < RHS.getOffsetFromBase();
7925 }
7926};
7927
7928/// \brief Check that all bits set in \p UsedBits form a dense region, i.e.,
7929/// \p UsedBits looks like 0..0 1..1 0..0.
7930static bool areUsedBitsDense(const APInt &UsedBits) {
7931 // If all the bits are one, this is dense!
7932 if (UsedBits.isAllOnesValue())
7933 return true;
7934
7935 // Get rid of the unused bits on the right.
7936 APInt NarrowedUsedBits = UsedBits.lshr(UsedBits.countTrailingZeros());
7937 // Get rid of the unused bits on the left.
7938 if (NarrowedUsedBits.countLeadingZeros())
7939 NarrowedUsedBits = NarrowedUsedBits.trunc(NarrowedUsedBits.getActiveBits());
7940 // Check that the chunk of bits is completely used.
7941 return NarrowedUsedBits.isAllOnesValue();
7942}
7943
7944/// \brief Check whether or not \p First and \p Second are next to each other
7945/// in memory. This means that there is no hole between the bits loaded
7946/// by \p First and the bits loaded by \p Second.
7947static bool areSlicesNextToEachOther(const LoadedSlice &First,
7948 const LoadedSlice &Second) {
7949 assert(First.Origin == Second.Origin && First.Origin &&
7950 "Unable to match different memory origins.");
7951 APInt UsedBits = First.getUsedBits();
7952 assert((UsedBits & Second.getUsedBits()) == 0 &&
7953 "Slices are not supposed to overlap.");
7954 UsedBits |= Second.getUsedBits();
7955 return areUsedBitsDense(UsedBits);
7956}
7957
7958/// \brief Adjust the \p GlobalLSCost according to the target
7959/// paring capabilities and the layout of the slices.
7960/// \pre \p GlobalLSCost should account for at least as many loads as
7961/// there is in the slices in \p LoadedSlices.
7962static void adjustCostForPairing(SmallVectorImpl<LoadedSlice> &LoadedSlices,
7963 LoadedSlice::Cost &GlobalLSCost) {
7964 unsigned NumberOfSlices = LoadedSlices.size();
7965 // If there is less than 2 elements, no pairing is possible.
7966 if (NumberOfSlices < 2)
7967 return;
7968
7969 // Sort the slices so that elements that are likely to be next to each
7970 // other in memory are next to each other in the list.
7971 std::sort(LoadedSlices.begin(), LoadedSlices.end(), LoadedSliceSorter());
7972 const TargetLowering &TLI = LoadedSlices[0].DAG->getTargetLoweringInfo();
7973 // First (resp. Second) is the first (resp. Second) potentially candidate
7974 // to be placed in a paired load.
7975 const LoadedSlice *First = NULL;
7976 const LoadedSlice *Second = NULL;
7977 for (unsigned CurrSlice = 0; CurrSlice < NumberOfSlices; ++CurrSlice,
7978 // Set the beginning of the pair.
7979 First = Second) {
7980
7981 Second = &LoadedSlices[CurrSlice];
7982
7983 // If First is NULL, it means we start a new pair.
7984 // Get to the next slice.
7985 if (!First)
7986 continue;
7987
7988 EVT LoadedType = First->getLoadedType();
7989
7990 // If the types of the slices are different, we cannot pair them.
7991 if (LoadedType != Second->getLoadedType())
7992 continue;
7993
7994 // Check if the target supplies paired loads for this type.
7995 unsigned RequiredAlignment = 0;
7996 if (!TLI.hasPairedLoad(LoadedType, RequiredAlignment)) {
7997 // move to the next pair, this type is hopeless.
7998 Second = NULL;
7999 continue;
8000 }
8001 // Check if we meet the alignment requirement.
8002 if (RequiredAlignment > First->getAlignment())
8003 continue;
8004
8005 // Check that both loads are next to each other in memory.
8006 if (!areSlicesNextToEachOther(*First, *Second))
8007 continue;
8008
8009 assert(GlobalLSCost.Loads > 0 && "We save more loads than we created!");
8010 --GlobalLSCost.Loads;
8011 // Move to the next pair.
8012 Second = NULL;
8013 }
8014}
8015
8016/// \brief Check the profitability of all involved LoadedSlice.
8017/// Currently, it is considered profitable if there is exactly two
8018/// involved slices (1) which are (2) next to each other in memory, and
8019/// whose cost (\see LoadedSlice::Cost) is smaller than the original load (3).
8020///
8021/// Note: The order of the elements in \p LoadedSlices may be modified, but not
8022/// the elements themselves.
8023///
8024/// FIXME: When the cost model will be mature enough, we can relax
8025/// constraints (1) and (2).
8026static bool isSlicingProfitable(SmallVectorImpl<LoadedSlice> &LoadedSlices,
8027 const APInt &UsedBits, bool ForCodeSize) {
8028 unsigned NumberOfSlices = LoadedSlices.size();
8029 if (StressLoadSlicing)
8030 return NumberOfSlices > 1;
8031
8032 // Check (1).
8033 if (NumberOfSlices != 2)
8034 return false;
8035
8036 // Check (2).
8037 if (!areUsedBitsDense(UsedBits))
8038 return false;
8039
8040 // Check (3).
8041 LoadedSlice::Cost OrigCost(ForCodeSize), GlobalSlicingCost(ForCodeSize);
8042 // The original code has one big load.
8043 OrigCost.Loads = 1;
8044 for (unsigned CurrSlice = 0; CurrSlice < NumberOfSlices; ++CurrSlice) {
8045 const LoadedSlice &LS = LoadedSlices[CurrSlice];
8046 // Accumulate the cost of all the slices.
8047 LoadedSlice::Cost SliceCost(LS, ForCodeSize);
8048 GlobalSlicingCost += SliceCost;
8049
8050 // Account as cost in the original configuration the gain obtained
8051 // with the current slices.
8052 OrigCost.addSliceGain(LS);
8053 }
8054
8055 // If the target supports paired load, adjust the cost accordingly.
8056 adjustCostForPairing(LoadedSlices, GlobalSlicingCost);
8057 return OrigCost > GlobalSlicingCost;
8058}
8059
8060/// \brief If the given load, \p LI, is used only by trunc or trunc(lshr)
8061/// operations, split it in the various pieces being extracted.
8062///
8063/// This sort of thing is introduced by SROA.
8064/// This slicing takes care not to insert overlapping loads.
8065/// \pre LI is a simple load (i.e., not an atomic or volatile load).
8066bool DAGCombiner::SliceUpLoad(SDNode *N) {
8067 if (Level < AfterLegalizeDAG)
8068 return false;
8069
8070 LoadSDNode *LD = cast<LoadSDNode>(N);
8071 if (LD->isVolatile() || !ISD::isNormalLoad(LD) ||
8072 !LD->getValueType(0).isInteger())
8073 return false;
8074
8075 // Keep track of already used bits to detect overlapping values.
8076 // In that case, we will just abort the transformation.
8077 APInt UsedBits(LD->getValueSizeInBits(0), 0);
8078
8079 SmallVector<LoadedSlice, 4> LoadedSlices;
8080
8081 // Check if this load is used as several smaller chunks of bits.
8082 // Basically, look for uses in trunc or trunc(lshr) and record a new chain
8083 // of computation for each trunc.
8084 for (SDNode::use_iterator UI = LD->use_begin(), UIEnd = LD->use_end();
8085 UI != UIEnd; ++UI) {
8086 // Skip the uses of the chain.
8087 if (UI.getUse().getResNo() != 0)
8088 continue;
8089
8090 SDNode *User = *UI;
8091 unsigned Shift = 0;
8092
8093 // Check if this is a trunc(lshr).
8094 if (User->getOpcode() == ISD::SRL && User->hasOneUse() &&
8095 isa<ConstantSDNode>(User->getOperand(1))) {
8096 Shift = cast<ConstantSDNode>(User->getOperand(1))->getZExtValue();
8097 User = *User->use_begin();
8098 }
8099
8100 // At this point, User is a Truncate, iff we encountered, trunc or
8101 // trunc(lshr).
8102 if (User->getOpcode() != ISD::TRUNCATE)
8103 return false;
8104
8105 // The width of the type must be a power of 2 and greater than 8-bits.
8106 // Otherwise the load cannot be represented in LLVM IR.
8107 // Moreover, if we shifted with a non 8-bits multiple, the slice
8108 // will be accross several bytes. We do not support that.
8109 unsigned Width = User->getValueSizeInBits(0);
8110 if (Width < 8 || !isPowerOf2_32(Width) || (Shift & 0x7))
8111 return 0;
8112
8113 // Build the slice for this chain of computations.
8114 LoadedSlice LS(User, LD, Shift, &DAG);
8115 APInt CurrentUsedBits = LS.getUsedBits();
8116
8117 // Check if this slice overlaps with another.
8118 if ((CurrentUsedBits & UsedBits) != 0)
8119 return false;
8120 // Update the bits used globally.
8121 UsedBits |= CurrentUsedBits;
8122
8123 // Check if the new slice would be legal.
8124 if (!LS.isLegal())
8125 return false;
8126
8127 // Record the slice.
8128 LoadedSlices.push_back(LS);
8129 }
8130
8131 // Abort slicing if it does not seem to be profitable.
8132 if (!isSlicingProfitable(LoadedSlices, UsedBits, ForCodeSize))
8133 return false;
8134
8135 ++SlicedLoads;
8136
8137 // Rewrite each chain to use an independent load.
8138 // By construction, each chain can be represented by a unique load.
8139
8140 // Prepare the argument for the new token factor for all the slices.
8141 SmallVector<SDValue, 8> ArgChains;
8142 for (SmallVectorImpl<LoadedSlice>::const_iterator
8143 LSIt = LoadedSlices.begin(),
8144 LSItEnd = LoadedSlices.end();
8145 LSIt != LSItEnd; ++LSIt) {
8146 SDValue SliceInst = LSIt->loadSlice();
8147 CombineTo(LSIt->Inst, SliceInst, true);
8148 if (SliceInst.getNode()->getOpcode() != ISD::LOAD)
8149 SliceInst = SliceInst.getOperand(0);
8150 assert(SliceInst->getOpcode() == ISD::LOAD &&
8151 "It takes more than a zext to get to the loaded slice!!");
8152 ArgChains.push_back(SliceInst.getValue(1));
8153 }
8154
8155 SDValue Chain = DAG.getNode(ISD::TokenFactor, SDLoc(LD), MVT::Other,
8156 &ArgChains[0], ArgChains.size());
8157 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain);
8158 return true;
8159}
8160
Chris Lattner2392ae72010-04-15 04:48:01 +00008161/// CheckForMaskedLoad - Check to see if V is (and load (ptr), imm), where the
8162/// load is having specific bytes cleared out. If so, return the byte size
8163/// being masked out and the shift amount.
8164static std::pair<unsigned, unsigned>
8165CheckForMaskedLoad(SDValue V, SDValue Ptr, SDValue Chain) {
8166 std::pair<unsigned, unsigned> Result(0, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008167
Chris Lattner2392ae72010-04-15 04:48:01 +00008168 // Check for the structure we're looking for.
8169 if (V->getOpcode() != ISD::AND ||
8170 !isa<ConstantSDNode>(V->getOperand(1)) ||
8171 !ISD::isNormalLoad(V->getOperand(0).getNode()))
8172 return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008173
Chris Lattnere6987582010-04-15 06:10:49 +00008174 // Check the chain and pointer.
Chris Lattner2392ae72010-04-15 04:48:01 +00008175 LoadSDNode *LD = cast<LoadSDNode>(V->getOperand(0));
Chris Lattnere6987582010-04-15 06:10:49 +00008176 if (LD->getBasePtr() != Ptr) return Result; // Not from same pointer.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008177
Chris Lattnere6987582010-04-15 06:10:49 +00008178 // The store should be chained directly to the load or be an operand of a
8179 // tokenfactor.
8180 if (LD == Chain.getNode())
8181 ; // ok.
8182 else if (Chain->getOpcode() != ISD::TokenFactor)
8183 return Result; // Fail.
8184 else {
8185 bool isOk = false;
8186 for (unsigned i = 0, e = Chain->getNumOperands(); i != e; ++i)
8187 if (Chain->getOperand(i).getNode() == LD) {
8188 isOk = true;
8189 break;
8190 }
8191 if (!isOk) return Result;
8192 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008193
Chris Lattner2392ae72010-04-15 04:48:01 +00008194 // This only handles simple types.
8195 if (V.getValueType() != MVT::i16 &&
8196 V.getValueType() != MVT::i32 &&
8197 V.getValueType() != MVT::i64)
8198 return Result;
8199
8200 // Check the constant mask. Invert it so that the bits being masked out are
8201 // 0 and the bits being kept are 1. Use getSExtValue so that leading bits
8202 // follow the sign bit for uniformity.
8203 uint64_t NotMask = ~cast<ConstantSDNode>(V->getOperand(1))->getSExtValue();
Michael J. Spencerc6af2432013-05-24 22:23:49 +00008204 unsigned NotMaskLZ = countLeadingZeros(NotMask);
Chris Lattner2392ae72010-04-15 04:48:01 +00008205 if (NotMaskLZ & 7) return Result; // Must be multiple of a byte.
Michael J. Spencerc6af2432013-05-24 22:23:49 +00008206 unsigned NotMaskTZ = countTrailingZeros(NotMask);
Chris Lattner2392ae72010-04-15 04:48:01 +00008207 if (NotMaskTZ & 7) return Result; // Must be multiple of a byte.
8208 if (NotMaskLZ == 64) return Result; // All zero mask.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008209
Chris Lattner2392ae72010-04-15 04:48:01 +00008210 // See if we have a continuous run of bits. If so, we have 0*1+0*
8211 if (CountTrailingOnes_64(NotMask >> NotMaskTZ)+NotMaskTZ+NotMaskLZ != 64)
8212 return Result;
8213
8214 // Adjust NotMaskLZ down to be from the actual size of the int instead of i64.
8215 if (V.getValueType() != MVT::i64 && NotMaskLZ)
8216 NotMaskLZ -= 64-V.getValueSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008217
Chris Lattner2392ae72010-04-15 04:48:01 +00008218 unsigned MaskedBytes = (V.getValueSizeInBits()-NotMaskLZ-NotMaskTZ)/8;
8219 switch (MaskedBytes) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008220 case 1:
8221 case 2:
Chris Lattner2392ae72010-04-15 04:48:01 +00008222 case 4: break;
8223 default: return Result; // All one mask, or 5-byte mask.
8224 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008225
Chris Lattner2392ae72010-04-15 04:48:01 +00008226 // Verify that the first bit starts at a multiple of mask so that the access
8227 // is aligned the same as the access width.
8228 if (NotMaskTZ && NotMaskTZ/8 % MaskedBytes) return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008229
Chris Lattner2392ae72010-04-15 04:48:01 +00008230 Result.first = MaskedBytes;
8231 Result.second = NotMaskTZ/8;
8232 return Result;
8233}
8234
8235
8236/// ShrinkLoadReplaceStoreWithStore - Check to see if IVal is something that
8237/// provides a value as specified by MaskInfo. If so, replace the specified
8238/// store with a narrower store of truncated IVal.
8239static SDNode *
8240ShrinkLoadReplaceStoreWithStore(const std::pair<unsigned, unsigned> &MaskInfo,
8241 SDValue IVal, StoreSDNode *St,
8242 DAGCombiner *DC) {
8243 unsigned NumBytes = MaskInfo.first;
8244 unsigned ByteShift = MaskInfo.second;
8245 SelectionDAG &DAG = DC->getDAG();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008246
Chris Lattner2392ae72010-04-15 04:48:01 +00008247 // Check to see if IVal is all zeros in the part being masked in by the 'or'
8248 // that uses this. If not, this is not a replacement.
8249 APInt Mask = ~APInt::getBitsSet(IVal.getValueSizeInBits(),
8250 ByteShift*8, (ByteShift+NumBytes)*8);
8251 if (!DAG.MaskedValueIsZero(IVal, Mask)) return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008252
Chris Lattner2392ae72010-04-15 04:48:01 +00008253 // Check that it is legal on the target to do this. It is legal if the new
8254 // VT we're shrinking to (i8/i16/i32) is legal or we're still before type
8255 // legalization.
8256 MVT VT = MVT::getIntegerVT(NumBytes*8);
8257 if (!DC->isTypeLegal(VT))
8258 return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008259
Chris Lattner2392ae72010-04-15 04:48:01 +00008260 // Okay, we can do this! Replace the 'St' store with a store of IVal that is
8261 // shifted by ByteShift and truncated down to NumBytes.
8262 if (ByteShift)
Andrew Trickac6d9be2013-05-25 02:42:55 +00008263 IVal = DAG.getNode(ISD::SRL, SDLoc(IVal), IVal.getValueType(), IVal,
Owen Anderson95771af2011-02-25 21:41:48 +00008264 DAG.getConstant(ByteShift*8,
8265 DC->getShiftAmountTy(IVal.getValueType())));
Chris Lattner2392ae72010-04-15 04:48:01 +00008266
8267 // Figure out the offset for the store and the alignment of the access.
8268 unsigned StOffset;
8269 unsigned NewAlign = St->getAlignment();
8270
8271 if (DAG.getTargetLoweringInfo().isLittleEndian())
8272 StOffset = ByteShift;
8273 else
8274 StOffset = IVal.getValueType().getStoreSize() - ByteShift - NumBytes;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008275
Chris Lattner2392ae72010-04-15 04:48:01 +00008276 SDValue Ptr = St->getBasePtr();
8277 if (StOffset) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008278 Ptr = DAG.getNode(ISD::ADD, SDLoc(IVal), Ptr.getValueType(),
Chris Lattner2392ae72010-04-15 04:48:01 +00008279 Ptr, DAG.getConstant(StOffset, Ptr.getValueType()));
8280 NewAlign = MinAlign(NewAlign, StOffset);
8281 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008282
Chris Lattner2392ae72010-04-15 04:48:01 +00008283 // Truncate down to the new size.
Andrew Trickac6d9be2013-05-25 02:42:55 +00008284 IVal = DAG.getNode(ISD::TRUNCATE, SDLoc(IVal), VT, IVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008285
Chris Lattner2392ae72010-04-15 04:48:01 +00008286 ++OpsNarrowed;
Andrew Trickac6d9be2013-05-25 02:42:55 +00008287 return DAG.getStore(St->getChain(), SDLoc(St), IVal, Ptr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008288 St->getPointerInfo().getWithOffset(StOffset),
Chris Lattner2392ae72010-04-15 04:48:01 +00008289 false, false, NewAlign).getNode();
8290}
8291
Evan Cheng8b944d32009-05-28 00:35:15 +00008292
8293/// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is
8294/// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some
8295/// of the loaded bits, try narrowing the load and store if it would end up
8296/// being a win for performance or code size.
8297SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) {
8298 StoreSDNode *ST = cast<StoreSDNode>(N);
Evan Chengcdcecc02009-05-28 18:41:02 +00008299 if (ST->isVolatile())
8300 return SDValue();
8301
Evan Cheng8b944d32009-05-28 00:35:15 +00008302 SDValue Chain = ST->getChain();
8303 SDValue Value = ST->getValue();
8304 SDValue Ptr = ST->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +00008305 EVT VT = Value.getValueType();
Evan Cheng8b944d32009-05-28 00:35:15 +00008306
8307 if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse())
Evan Chengcdcecc02009-05-28 18:41:02 +00008308 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00008309
8310 unsigned Opc = Value.getOpcode();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008311
Chris Lattner2392ae72010-04-15 04:48:01 +00008312 // If this is "store (or X, Y), P" and X is "(and (load P), cst)", where cst
8313 // is a byte mask indicating a consecutive number of bytes, check to see if
8314 // Y is known to provide just those bytes. If so, we try to replace the
8315 // load + replace + store sequence with a single (narrower) store, which makes
8316 // the load dead.
8317 if (Opc == ISD::OR) {
8318 std::pair<unsigned, unsigned> MaskedLoad;
8319 MaskedLoad = CheckForMaskedLoad(Value.getOperand(0), Ptr, Chain);
8320 if (MaskedLoad.first)
8321 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
8322 Value.getOperand(1), ST,this))
8323 return SDValue(NewST, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008324
Chris Lattner2392ae72010-04-15 04:48:01 +00008325 // Or is commutative, so try swapping X and Y.
8326 MaskedLoad = CheckForMaskedLoad(Value.getOperand(1), Ptr, Chain);
8327 if (MaskedLoad.first)
8328 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
8329 Value.getOperand(0), ST,this))
8330 return SDValue(NewST, 0);
8331 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008332
Evan Cheng8b944d32009-05-28 00:35:15 +00008333 if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) ||
8334 Value.getOperand(1).getOpcode() != ISD::Constant)
Evan Chengcdcecc02009-05-28 18:41:02 +00008335 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00008336
8337 SDValue N0 = Value.getOperand(0);
Dan Gohman24bde5b2010-09-02 21:18:42 +00008338 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
8339 Chain == SDValue(N0.getNode(), 1)) {
Evan Cheng8b944d32009-05-28 00:35:15 +00008340 LoadSDNode *LD = cast<LoadSDNode>(N0);
Chris Lattnerfa459012010-09-21 16:08:50 +00008341 if (LD->getBasePtr() != Ptr ||
8342 LD->getPointerInfo().getAddrSpace() !=
8343 ST->getPointerInfo().getAddrSpace())
Evan Chengcdcecc02009-05-28 18:41:02 +00008344 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00008345
8346 // Find the type to narrow it the load / op / store to.
8347 SDValue N1 = Value.getOperand(1);
8348 unsigned BitWidth = N1.getValueSizeInBits();
8349 APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue();
8350 if (Opc == ISD::AND)
8351 Imm ^= APInt::getAllOnesValue(BitWidth);
Evan Chengd3c76bb2009-05-28 23:52:18 +00008352 if (Imm == 0 || Imm.isAllOnesValue())
8353 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00008354 unsigned ShAmt = Imm.countTrailingZeros();
8355 unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1;
8356 unsigned NewBW = NextPowerOf2(MSB - ShAmt);
Owen Anderson23b9b192009-08-12 00:36:31 +00008357 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00008358 while (NewBW < BitWidth &&
Evan Chengcdcecc02009-05-28 18:41:02 +00008359 !(TLI.isOperationLegalOrCustom(Opc, NewVT) &&
Evan Cheng8b944d32009-05-28 00:35:15 +00008360 TLI.isNarrowingProfitable(VT, NewVT))) {
8361 NewBW = NextPowerOf2(NewBW);
Owen Anderson23b9b192009-08-12 00:36:31 +00008362 NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00008363 }
Evan Chengcdcecc02009-05-28 18:41:02 +00008364 if (NewBW >= BitWidth)
8365 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00008366
8367 // If the lsb changed does not start at the type bitwidth boundary,
8368 // start at the previous one.
8369 if (ShAmt % NewBW)
8370 ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW;
Manman Ren981b9632012-12-12 01:13:50 +00008371 APInt Mask = APInt::getBitsSet(BitWidth, ShAmt,
8372 std::min(BitWidth, ShAmt + NewBW));
Evan Cheng8b944d32009-05-28 00:35:15 +00008373 if ((Imm & Mask) == Imm) {
8374 APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW);
8375 if (Opc == ISD::AND)
8376 NewImm ^= APInt::getAllOnesValue(NewBW);
8377 uint64_t PtrOff = ShAmt / 8;
8378 // For big endian targets, we need to adjust the offset to the pointer to
8379 // load the correct bytes.
8380 if (TLI.isBigEndian())
Evan Chengcdcecc02009-05-28 18:41:02 +00008381 PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff;
Evan Cheng8b944d32009-05-28 00:35:15 +00008382
8383 unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008384 Type *NewVTTy = NewVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00008385 if (NewAlign < TLI.getDataLayout()->getABITypeAlignment(NewVTTy))
Evan Chengcdcecc02009-05-28 18:41:02 +00008386 return SDValue();
8387
Andrew Trickac6d9be2013-05-25 02:42:55 +00008388 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(LD),
Evan Cheng8b944d32009-05-28 00:35:15 +00008389 Ptr.getValueType(), Ptr,
8390 DAG.getConstant(PtrOff, Ptr.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00008391 SDValue NewLD = DAG.getLoad(NewVT, SDLoc(N0),
Evan Cheng8b944d32009-05-28 00:35:15 +00008392 LD->getChain(), NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00008393 LD->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00008394 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008395 LD->isInvariant(), NewAlign);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008396 SDValue NewVal = DAG.getNode(Opc, SDLoc(Value), NewVT, NewLD,
Evan Cheng8b944d32009-05-28 00:35:15 +00008397 DAG.getConstant(NewImm, NewVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00008398 SDValue NewST = DAG.getStore(Chain, SDLoc(N),
Evan Cheng8b944d32009-05-28 00:35:15 +00008399 NewVal, NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00008400 ST->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00008401 false, false, NewAlign);
Evan Cheng8b944d32009-05-28 00:35:15 +00008402
8403 AddToWorkList(NewPtr.getNode());
8404 AddToWorkList(NewLD.getNode());
8405 AddToWorkList(NewVal.getNode());
8406 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00008407 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1));
Evan Cheng8b944d32009-05-28 00:35:15 +00008408 ++OpsNarrowed;
8409 return NewST;
8410 }
8411 }
8412
Evan Chengcdcecc02009-05-28 18:41:02 +00008413 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00008414}
8415
Evan Cheng31959b12011-02-02 01:06:55 +00008416/// TransformFPLoadStorePair - For a given floating point load / store pair,
8417/// if the load value isn't used by any other operations, then consider
8418/// transforming the pair to integer load / store operations if the target
8419/// deems the transformation profitable.
8420SDValue DAGCombiner::TransformFPLoadStorePair(SDNode *N) {
8421 StoreSDNode *ST = cast<StoreSDNode>(N);
8422 SDValue Chain = ST->getChain();
8423 SDValue Value = ST->getValue();
8424 if (ISD::isNormalStore(ST) && ISD::isNormalLoad(Value.getNode()) &&
8425 Value.hasOneUse() &&
8426 Chain == SDValue(Value.getNode(), 1)) {
8427 LoadSDNode *LD = cast<LoadSDNode>(Value);
8428 EVT VT = LD->getMemoryVT();
8429 if (!VT.isFloatingPoint() ||
8430 VT != ST->getMemoryVT() ||
8431 LD->isNonTemporal() ||
8432 ST->isNonTemporal() ||
8433 LD->getPointerInfo().getAddrSpace() != 0 ||
8434 ST->getPointerInfo().getAddrSpace() != 0)
8435 return SDValue();
8436
8437 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
8438 if (!TLI.isOperationLegal(ISD::LOAD, IntVT) ||
8439 !TLI.isOperationLegal(ISD::STORE, IntVT) ||
8440 !TLI.isDesirableToTransformToIntegerOp(ISD::LOAD, VT) ||
8441 !TLI.isDesirableToTransformToIntegerOp(ISD::STORE, VT))
8442 return SDValue();
8443
8444 unsigned LDAlign = LD->getAlignment();
8445 unsigned STAlign = ST->getAlignment();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008446 Type *IntVTTy = IntVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00008447 unsigned ABIAlign = TLI.getDataLayout()->getABITypeAlignment(IntVTTy);
Evan Cheng31959b12011-02-02 01:06:55 +00008448 if (LDAlign < ABIAlign || STAlign < ABIAlign)
8449 return SDValue();
8450
Andrew Trickac6d9be2013-05-25 02:42:55 +00008451 SDValue NewLD = DAG.getLoad(IntVT, SDLoc(Value),
Evan Cheng31959b12011-02-02 01:06:55 +00008452 LD->getChain(), LD->getBasePtr(),
8453 LD->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008454 false, false, false, LDAlign);
Evan Cheng31959b12011-02-02 01:06:55 +00008455
Andrew Trickac6d9be2013-05-25 02:42:55 +00008456 SDValue NewST = DAG.getStore(NewLD.getValue(1), SDLoc(N),
Evan Cheng31959b12011-02-02 01:06:55 +00008457 NewLD, ST->getBasePtr(),
8458 ST->getPointerInfo(),
8459 false, false, STAlign);
8460
8461 AddToWorkList(NewLD.getNode());
8462 AddToWorkList(NewST.getNode());
8463 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00008464 DAG.ReplaceAllUsesOfValueWith(Value.getValue(1), NewLD.getValue(1));
Evan Cheng31959b12011-02-02 01:06:55 +00008465 ++LdStFP2Int;
8466 return NewST;
8467 }
8468
8469 return SDValue();
8470}
8471
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008472/// Helper struct to parse and store a memory address as base + index + offset.
8473/// We ignore sign extensions when it is safe to do so.
8474/// The following two expressions are not equivalent. To differentiate we need
8475/// to store whether there was a sign extension involved in the index
8476/// computation.
8477/// (load (i64 add (i64 copyfromreg %c)
8478/// (i64 signextend (add (i8 load %index)
8479/// (i8 1))))
8480/// vs
8481///
8482/// (load (i64 add (i64 copyfromreg %c)
8483/// (i64 signextend (i32 add (i32 signextend (i8 load %index))
8484/// (i32 1)))))
8485struct BaseIndexOffset {
8486 SDValue Base;
8487 SDValue Index;
8488 int64_t Offset;
8489 bool IsIndexSignExt;
8490
8491 BaseIndexOffset() : Offset(0), IsIndexSignExt(false) {}
8492
8493 BaseIndexOffset(SDValue Base, SDValue Index, int64_t Offset,
8494 bool IsIndexSignExt) :
8495 Base(Base), Index(Index), Offset(Offset), IsIndexSignExt(IsIndexSignExt) {}
8496
8497 bool equalBaseIndex(const BaseIndexOffset &Other) {
8498 return Other.Base == Base && Other.Index == Index &&
8499 Other.IsIndexSignExt == IsIndexSignExt;
Nadav Rotemc653de62012-10-03 16:11:15 +00008500 }
8501
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008502 /// Parses tree in Ptr for base, index, offset addresses.
8503 static BaseIndexOffset match(SDValue Ptr) {
8504 bool IsIndexSignExt = false;
8505
Juergen Ributzka915e9362013-08-21 21:53:38 +00008506 // We only can pattern match BASE + INDEX + OFFSET. If Ptr is not an ADD
8507 // instruction, then it could be just the BASE or everything else we don't
8508 // know how to handle. Just use Ptr as BASE and give up.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008509 if (Ptr->getOpcode() != ISD::ADD)
8510 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
8511
Juergen Ributzka915e9362013-08-21 21:53:38 +00008512 // We know that we have at least an ADD instruction. Try to pattern match
8513 // the simple case of BASE + OFFSET.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008514 if (isa<ConstantSDNode>(Ptr->getOperand(1))) {
8515 int64_t Offset = cast<ConstantSDNode>(Ptr->getOperand(1))->getSExtValue();
8516 return BaseIndexOffset(Ptr->getOperand(0), SDValue(), Offset,
8517 IsIndexSignExt);
8518 }
8519
Juergen Ributzka915e9362013-08-21 21:53:38 +00008520 // Inside a loop the current BASE pointer is calculated using an ADD and a
Juergen Ributzka2b884bc2013-08-28 22:33:58 +00008521 // MUL instruction. In this case Ptr is the actual BASE pointer.
Juergen Ributzka915e9362013-08-21 21:53:38 +00008522 // (i64 add (i64 %array_ptr)
8523 // (i64 mul (i64 %induction_var)
8524 // (i64 %element_size)))
Juergen Ributzka2b884bc2013-08-28 22:33:58 +00008525 if (Ptr->getOperand(1)->getOpcode() == ISD::MUL)
Juergen Ributzka915e9362013-08-21 21:53:38 +00008526 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
Juergen Ributzka915e9362013-08-21 21:53:38 +00008527
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008528 // Look at Base + Index + Offset cases.
8529 SDValue Base = Ptr->getOperand(0);
8530 SDValue IndexOffset = Ptr->getOperand(1);
8531
8532 // Skip signextends.
8533 if (IndexOffset->getOpcode() == ISD::SIGN_EXTEND) {
8534 IndexOffset = IndexOffset->getOperand(0);
8535 IsIndexSignExt = true;
8536 }
8537
8538 // Either the case of Base + Index (no offset) or something else.
8539 if (IndexOffset->getOpcode() != ISD::ADD)
8540 return BaseIndexOffset(Base, IndexOffset, 0, IsIndexSignExt);
8541
8542 // Now we have the case of Base + Index + offset.
8543 SDValue Index = IndexOffset->getOperand(0);
8544 SDValue Offset = IndexOffset->getOperand(1);
8545
8546 if (!isa<ConstantSDNode>(Offset))
8547 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
8548
8549 // Ignore signextends.
8550 if (Index->getOpcode() == ISD::SIGN_EXTEND) {
8551 Index = Index->getOperand(0);
8552 IsIndexSignExt = true;
8553 } else IsIndexSignExt = false;
8554
8555 int64_t Off = cast<ConstantSDNode>(Offset)->getSExtValue();
8556 return BaseIndexOffset(Base, Index, Off, IsIndexSignExt);
8557 }
8558};
Nadav Rotemc653de62012-10-03 16:11:15 +00008559
8560/// Holds a pointer to an LSBaseSDNode as well as information on where it
8561/// is located in a sequence of memory operations connected by a chain.
8562struct MemOpLink {
8563 MemOpLink (LSBaseSDNode *N, int64_t Offset, unsigned Seq):
8564 MemNode(N), OffsetFromBase(Offset), SequenceNum(Seq) { }
8565 // Ptr to the mem node.
8566 LSBaseSDNode *MemNode;
8567 // Offset from the base ptr.
8568 int64_t OffsetFromBase;
8569 // What is the sequence number of this mem node.
8570 // Lowest mem operand in the DAG starts at zero.
8571 unsigned SequenceNum;
8572};
8573
8574/// Sorts store nodes in a link according to their offset from a shared
8575// base ptr.
8576struct ConsecutiveMemoryChainSorter {
8577 bool operator()(MemOpLink LHS, MemOpLink RHS) {
8578 return LHS.OffsetFromBase < RHS.OffsetFromBase;
8579 }
8580};
8581
8582bool DAGCombiner::MergeConsecutiveStores(StoreSDNode* St) {
8583 EVT MemVT = St->getMemoryVT();
8584 int64_t ElementSizeBytes = MemVT.getSizeInBits()/8;
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008585 bool NoVectors = DAG.getMachineFunction().getFunction()->getAttributes().
8586 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Nadav Rotemc653de62012-10-03 16:11:15 +00008587
8588 // Don't merge vectors into wider inputs.
8589 if (MemVT.isVector() || !MemVT.isSimple())
8590 return false;
8591
8592 // Perform an early exit check. Do not bother looking at stored values that
8593 // are not constants or loads.
8594 SDValue StoredVal = St->getValue();
8595 bool IsLoadSrc = isa<LoadSDNode>(StoredVal);
8596 if (!isa<ConstantSDNode>(StoredVal) && !isa<ConstantFPSDNode>(StoredVal) &&
8597 !IsLoadSrc)
8598 return false;
8599
8600 // Only look at ends of store sequences.
8601 SDValue Chain = SDValue(St, 1);
8602 if (Chain->hasOneUse() && Chain->use_begin()->getOpcode() == ISD::STORE)
8603 return false;
8604
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008605 // This holds the base pointer, index, and the offset in bytes from the base
8606 // pointer.
8607 BaseIndexOffset BasePtr = BaseIndexOffset::match(St->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00008608
8609 // We must have a base and an offset.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008610 if (!BasePtr.Base.getNode())
Nadav Rotemc653de62012-10-03 16:11:15 +00008611 return false;
8612
8613 // Do not handle stores to undef base pointers.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008614 if (BasePtr.Base.getOpcode() == ISD::UNDEF)
Nadav Rotemc653de62012-10-03 16:11:15 +00008615 return false;
8616
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008617 // Save the LoadSDNodes that we find in the chain.
8618 // We need to make sure that these nodes do not interfere with
8619 // any of the store nodes.
8620 SmallVector<LSBaseSDNode*, 8> AliasLoadNodes;
8621
8622 // Save the StoreSDNodes that we find in the chain.
Nadav Rotemc653de62012-10-03 16:11:15 +00008623 SmallVector<MemOpLink, 8> StoreNodes;
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008624
Nadav Rotemc653de62012-10-03 16:11:15 +00008625 // Walk up the chain and look for nodes with offsets from the same
8626 // base pointer. Stop when reaching an instruction with a different kind
8627 // or instruction which has a different base pointer.
8628 unsigned Seq = 0;
8629 StoreSDNode *Index = St;
8630 while (Index) {
8631 // If the chain has more than one use, then we can't reorder the mem ops.
8632 if (Index != St && !SDValue(Index, 1)->hasOneUse())
8633 break;
8634
8635 // Find the base pointer and offset for this memory node.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008636 BaseIndexOffset Ptr = BaseIndexOffset::match(Index->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00008637
8638 // Check that the base pointer is the same as the original one.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008639 if (!Ptr.equalBaseIndex(BasePtr))
Nadav Rotemc653de62012-10-03 16:11:15 +00008640 break;
8641
8642 // Check that the alignment is the same.
8643 if (Index->getAlignment() != St->getAlignment())
8644 break;
8645
8646 // The memory operands must not be volatile.
8647 if (Index->isVolatile() || Index->isIndexed())
8648 break;
8649
8650 // No truncation.
8651 if (StoreSDNode *St = dyn_cast<StoreSDNode>(Index))
8652 if (St->isTruncatingStore())
8653 break;
8654
8655 // The stored memory type must be the same.
8656 if (Index->getMemoryVT() != MemVT)
8657 break;
8658
8659 // We do not allow unaligned stores because we want to prevent overriding
8660 // stores.
8661 if (Index->getAlignment()*8 != MemVT.getSizeInBits())
8662 break;
8663
8664 // We found a potential memory operand to merge.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008665 StoreNodes.push_back(MemOpLink(Index, Ptr.Offset, Seq++));
Nadav Rotemc653de62012-10-03 16:11:15 +00008666
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008667 // Find the next memory operand in the chain. If the next operand in the
8668 // chain is a store then move up and continue the scan with the next
8669 // memory operand. If the next operand is a load save it and use alias
8670 // information to check if it interferes with anything.
8671 SDNode *NextInChain = Index->getChain().getNode();
8672 while (1) {
Nadav Rotemdde785c2012-12-06 17:34:13 +00008673 if (StoreSDNode *STn = dyn_cast<StoreSDNode>(NextInChain)) {
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008674 // We found a store node. Use it for the next iteration.
Nadav Rotemdde785c2012-12-06 17:34:13 +00008675 Index = STn;
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008676 break;
8677 } else if (LoadSDNode *Ldn = dyn_cast<LoadSDNode>(NextInChain)) {
8678 // Save the load node for later. Continue the scan.
8679 AliasLoadNodes.push_back(Ldn);
8680 NextInChain = Ldn->getChain().getNode();
8681 continue;
8682 } else {
8683 Index = NULL;
8684 break;
8685 }
8686 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008687 }
8688
8689 // Check if there is anything to merge.
8690 if (StoreNodes.size() < 2)
8691 return false;
8692
8693 // Sort the memory operands according to their distance from the base pointer.
8694 std::sort(StoreNodes.begin(), StoreNodes.end(),
8695 ConsecutiveMemoryChainSorter());
8696
8697 // Scan the memory operations on the chain and find the first non-consecutive
8698 // store memory address.
8699 unsigned LastConsecutiveStore = 0;
8700 int64_t StartAddress = StoreNodes[0].OffsetFromBase;
Nadav Rotemdde785c2012-12-06 17:34:13 +00008701 for (unsigned i = 0, e = StoreNodes.size(); i < e; ++i) {
8702
8703 // Check that the addresses are consecutive starting from the second
8704 // element in the list of stores.
8705 if (i > 0) {
8706 int64_t CurrAddress = StoreNodes[i].OffsetFromBase;
8707 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
8708 break;
8709 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008710
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008711 bool Alias = false;
8712 // Check if this store interferes with any of the loads that we found.
8713 for (unsigned ld = 0, lde = AliasLoadNodes.size(); ld < lde; ++ld)
8714 if (isAlias(AliasLoadNodes[ld], StoreNodes[i].MemNode)) {
8715 Alias = true;
8716 break;
8717 }
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008718 // We found a load that alias with this store. Stop the sequence.
8719 if (Alias)
8720 break;
8721
Nadav Rotemc653de62012-10-03 16:11:15 +00008722 // Mark this node as useful.
8723 LastConsecutiveStore = i;
8724 }
8725
8726 // The node with the lowest store address.
8727 LSBaseSDNode *FirstInChain = StoreNodes[0].MemNode;
8728
8729 // Store the constants into memory as one consecutive store.
8730 if (!IsLoadSrc) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008731 unsigned LastLegalType = 0;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008732 unsigned LastLegalVectorType = 0;
8733 bool NonZero = false;
Nadav Rotemc653de62012-10-03 16:11:15 +00008734 for (unsigned i=0; i<LastConsecutiveStore+1; ++i) {
8735 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8736 SDValue StoredVal = St->getValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008737
8738 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(StoredVal)) {
Benjamin Kramerebd7eab2012-10-05 18:19:44 +00008739 NonZero |= !C->isNullValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008740 } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(StoredVal)) {
Benjamin Kramerebd7eab2012-10-05 18:19:44 +00008741 NonZero |= !C->getConstantFPValue()->isNullValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008742 } else {
8743 // Non constant.
Nadav Rotemc653de62012-10-03 16:11:15 +00008744 break;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008745 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008746
Nadav Rotemc653de62012-10-03 16:11:15 +00008747 // Find a legal type for the constant store.
8748 unsigned StoreBW = (i+1) * ElementSizeBytes * 8;
8749 EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8750 if (TLI.isTypeLegal(StoreTy))
8751 LastLegalType = i+1;
Arnold Schwaighofere7370182013-04-02 15:58:51 +00008752 // Or check whether a truncstore is legal.
8753 else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) ==
8754 TargetLowering::TypePromoteInteger) {
8755 EVT LegalizedStoredValueTy =
8756 TLI.getTypeToTransformTo(*DAG.getContext(), StoredVal.getValueType());
8757 if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy))
8758 LastLegalType = i+1;
8759 }
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008760
8761 // Find a legal type for the vector store.
8762 EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1);
8763 if (TLI.isTypeLegal(Ty))
8764 LastLegalVectorType = i + 1;
Nadav Rotemc653de62012-10-03 16:11:15 +00008765 }
8766
Bob Wilson99d8e762012-12-20 01:36:20 +00008767 // We only use vectors if the constant is known to be zero and the
8768 // function is not marked with the noimplicitfloat attribute.
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008769 if (NonZero || NoVectors)
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008770 LastLegalVectorType = 0;
8771
Nadav Rotemc653de62012-10-03 16:11:15 +00008772 // Check if we found a legal integer type to store.
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008773 if (LastLegalType == 0 && LastLegalVectorType == 0)
Nadav Rotemc653de62012-10-03 16:11:15 +00008774 return false;
8775
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008776 bool UseVector = (LastLegalVectorType > LastLegalType) && !NoVectors;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008777 unsigned NumElem = UseVector ? LastLegalVectorType : LastLegalType;
8778
8779 // Make sure we have something to merge.
8780 if (NumElem < 2)
8781 return false;
Nadav Rotemc653de62012-10-03 16:11:15 +00008782
8783 unsigned EarliestNodeUsed = 0;
8784 for (unsigned i=0; i < NumElem; ++i) {
8785 // Find a chain for the new wide-store operand. Notice that some
8786 // of the store nodes that we found may not be selected for inclusion
8787 // in the wide store. The chain we use needs to be the chain of the
8788 // earliest store node which is *used* and replaced by the wide store.
8789 if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum)
8790 EarliestNodeUsed = i;
8791 }
8792
8793 // The earliest Node in the DAG.
8794 LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode;
Andrew Trickac6d9be2013-05-25 02:42:55 +00008795 SDLoc DL(StoreNodes[0].MemNode);
Nadav Rotemc653de62012-10-03 16:11:15 +00008796
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008797 SDValue StoredVal;
8798 if (UseVector) {
8799 // Find a legal type for the vector store.
8800 EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem);
8801 assert(TLI.isTypeLegal(Ty) && "Illegal vector store");
8802 StoredVal = DAG.getConstant(0, Ty);
8803 } else {
8804 unsigned StoreBW = NumElem * ElementSizeBytes * 8;
8805 APInt StoreInt(StoreBW, 0);
8806
8807 // Construct a single integer constant which is made of the smaller
8808 // constant inputs.
8809 bool IsLE = TLI.isLittleEndian();
8810 for (unsigned i = 0; i < NumElem ; ++i) {
8811 unsigned Idx = IsLE ?(NumElem - 1 - i) : i;
8812 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[Idx].MemNode);
8813 SDValue Val = St->getValue();
8814 StoreInt<<=ElementSizeBytes*8;
8815 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val)) {
8816 StoreInt|=C->getAPIntValue().zext(StoreBW);
8817 } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Val)) {
8818 StoreInt|= C->getValueAPF().bitcastToAPInt().zext(StoreBW);
8819 } else {
8820 assert(false && "Invalid constant element type");
8821 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008822 }
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008823
8824 // Create the new Load and Store operations.
8825 EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8826 StoredVal = DAG.getConstant(StoreInt, StoreTy);
Nadav Rotemc653de62012-10-03 16:11:15 +00008827 }
8828
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008829 SDValue NewStore = DAG.getStore(EarliestOp->getChain(), DL, StoredVal,
Nadav Rotemc653de62012-10-03 16:11:15 +00008830 FirstInChain->getBasePtr(),
8831 FirstInChain->getPointerInfo(),
8832 false, false,
8833 FirstInChain->getAlignment());
8834
8835 // Replace the first store with the new store
8836 CombineTo(EarliestOp, NewStore);
8837 // Erase all other stores.
8838 for (unsigned i = 0; i < NumElem ; ++i) {
8839 if (StoreNodes[i].MemNode == EarliestOp)
8840 continue;
8841 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
Rafael Espindola8e2b8ae2012-11-14 05:08:56 +00008842 // ReplaceAllUsesWith will replace all uses that existed when it was
8843 // called, but graph optimizations may cause new ones to appear. For
8844 // example, the case in pr14333 looks like
8845 //
8846 // St's chain -> St -> another store -> X
8847 //
8848 // And the only difference from St to the other store is the chain.
8849 // When we change it's chain to be St's chain they become identical,
8850 // get CSEed and the net result is that X is now a use of St.
8851 // Since we know that St is redundant, just iterate.
8852 while (!St->use_empty())
8853 DAG.ReplaceAllUsesWith(SDValue(St, 0), St->getChain());
Nadav Rotemc653de62012-10-03 16:11:15 +00008854 removeFromWorkList(St);
8855 DAG.DeleteNode(St);
8856 }
8857
8858 return true;
8859 }
8860
8861 // Below we handle the case of multiple consecutive stores that
8862 // come from multiple consecutive loads. We merge them into a single
8863 // wide load and a single wide store.
8864
8865 // Look for load nodes which are used by the stored values.
8866 SmallVector<MemOpLink, 8> LoadNodes;
8867
8868 // Find acceptable loads. Loads need to have the same chain (token factor),
8869 // must not be zext, volatile, indexed, and they must be consecutive.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008870 BaseIndexOffset LdBasePtr;
Nadav Rotemc653de62012-10-03 16:11:15 +00008871 for (unsigned i=0; i<LastConsecutiveStore+1; ++i) {
8872 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8873 LoadSDNode *Ld = dyn_cast<LoadSDNode>(St->getValue());
8874 if (!Ld) break;
8875
8876 // Loads must only have one use.
8877 if (!Ld->hasNUsesOfValue(1, 0))
8878 break;
8879
8880 // Check that the alignment is the same as the stores.
8881 if (Ld->getAlignment() != St->getAlignment())
8882 break;
8883
8884 // The memory operands must not be volatile.
8885 if (Ld->isVolatile() || Ld->isIndexed())
8886 break;
8887
8888 // We do not accept ext loads.
8889 if (Ld->getExtensionType() != ISD::NON_EXTLOAD)
8890 break;
8891
8892 // The stored memory type must be the same.
8893 if (Ld->getMemoryVT() != MemVT)
8894 break;
8895
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008896 BaseIndexOffset LdPtr = BaseIndexOffset::match(Ld->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00008897 // If this is not the first ptr that we check.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008898 if (LdBasePtr.Base.getNode()) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008899 // The base ptr must be the same.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008900 if (!LdPtr.equalBaseIndex(LdBasePtr))
Nadav Rotemc653de62012-10-03 16:11:15 +00008901 break;
8902 } else {
8903 // Check that all other base pointers are the same as this one.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008904 LdBasePtr = LdPtr;
Nadav Rotemc653de62012-10-03 16:11:15 +00008905 }
8906
8907 // We found a potential memory operand to merge.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008908 LoadNodes.push_back(MemOpLink(Ld, LdPtr.Offset, 0));
Nadav Rotemc653de62012-10-03 16:11:15 +00008909 }
8910
8911 if (LoadNodes.size() < 2)
8912 return false;
8913
8914 // Scan the memory operations on the chain and find the first non-consecutive
8915 // load memory address. These variables hold the index in the store node
8916 // array.
8917 unsigned LastConsecutiveLoad = 0;
8918 // This variable refers to the size and not index in the array.
8919 unsigned LastLegalVectorType = 0;
8920 unsigned LastLegalIntegerType = 0;
8921 StartAddress = LoadNodes[0].OffsetFromBase;
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008922 SDValue FirstChain = LoadNodes[0].MemNode->getChain();
8923 for (unsigned i = 1; i < LoadNodes.size(); ++i) {
8924 // All loads much share the same chain.
8925 if (LoadNodes[i].MemNode->getChain() != FirstChain)
8926 break;
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008927
Nadav Rotemc653de62012-10-03 16:11:15 +00008928 int64_t CurrAddress = LoadNodes[i].OffsetFromBase;
8929 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
8930 break;
8931 LastConsecutiveLoad = i;
8932
8933 // Find a legal type for the vector store.
8934 EVT StoreTy = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1);
8935 if (TLI.isTypeLegal(StoreTy))
8936 LastLegalVectorType = i + 1;
8937
8938 // Find a legal type for the integer store.
8939 unsigned StoreBW = (i+1) * ElementSizeBytes * 8;
8940 StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8941 if (TLI.isTypeLegal(StoreTy))
8942 LastLegalIntegerType = i + 1;
Arnold Schwaighofere7370182013-04-02 15:58:51 +00008943 // Or check whether a truncstore and extload is legal.
8944 else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) ==
8945 TargetLowering::TypePromoteInteger) {
8946 EVT LegalizedStoredValueTy =
8947 TLI.getTypeToTransformTo(*DAG.getContext(), StoreTy);
8948 if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy) &&
8949 TLI.isLoadExtLegal(ISD::ZEXTLOAD, StoreTy) &&
8950 TLI.isLoadExtLegal(ISD::SEXTLOAD, StoreTy) &&
8951 TLI.isLoadExtLegal(ISD::EXTLOAD, StoreTy))
8952 LastLegalIntegerType = i+1;
8953 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008954 }
8955
8956 // Only use vector types if the vector type is larger than the integer type.
8957 // If they are the same, use integers.
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008958 bool UseVectorTy = LastLegalVectorType > LastLegalIntegerType && !NoVectors;
Nadav Rotemc653de62012-10-03 16:11:15 +00008959 unsigned LastLegalType = std::max(LastLegalVectorType, LastLegalIntegerType);
8960
8961 // We add +1 here because the LastXXX variables refer to location while
8962 // the NumElem refers to array/index size.
8963 unsigned NumElem = std::min(LastConsecutiveStore, LastConsecutiveLoad) + 1;
8964 NumElem = std::min(LastLegalType, NumElem);
8965
8966 if (NumElem < 2)
8967 return false;
8968
8969 // The earliest Node in the DAG.
8970 unsigned EarliestNodeUsed = 0;
8971 LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode;
8972 for (unsigned i=1; i<NumElem; ++i) {
8973 // Find a chain for the new wide-store operand. Notice that some
8974 // of the store nodes that we found may not be selected for inclusion
8975 // in the wide store. The chain we use needs to be the chain of the
8976 // earliest store node which is *used* and replaced by the wide store.
8977 if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum)
8978 EarliestNodeUsed = i;
8979 }
8980
8981 // Find if it is better to use vectors or integers to load and store
8982 // to memory.
8983 EVT JointMemOpVT;
8984 if (UseVectorTy) {
8985 JointMemOpVT = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem);
8986 } else {
8987 unsigned StoreBW = NumElem * ElementSizeBytes * 8;
8988 JointMemOpVT = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8989 }
8990
Andrew Trickac6d9be2013-05-25 02:42:55 +00008991 SDLoc LoadDL(LoadNodes[0].MemNode);
8992 SDLoc StoreDL(StoreNodes[0].MemNode);
Nadav Rotemc653de62012-10-03 16:11:15 +00008993
8994 LoadSDNode *FirstLoad = cast<LoadSDNode>(LoadNodes[0].MemNode);
8995 SDValue NewLoad = DAG.getLoad(JointMemOpVT, LoadDL,
8996 FirstLoad->getChain(),
8997 FirstLoad->getBasePtr(),
8998 FirstLoad->getPointerInfo(),
8999 false, false, false,
9000 FirstLoad->getAlignment());
9001
9002 SDValue NewStore = DAG.getStore(EarliestOp->getChain(), StoreDL, NewLoad,
9003 FirstInChain->getBasePtr(),
9004 FirstInChain->getPointerInfo(), false, false,
9005 FirstInChain->getAlignment());
9006
Nadav Rotem2e7d3812012-10-03 19:30:31 +00009007 // Replace one of the loads with the new load.
9008 LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[0].MemNode);
9009 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1),
9010 SDValue(NewLoad.getNode(), 1));
9011
9012 // Remove the rest of the load chains.
9013 for (unsigned i = 1; i < NumElem ; ++i) {
Nadav Rotemc653de62012-10-03 16:11:15 +00009014 // Replace all chain users of the old load nodes with the chain of the new
9015 // load node.
9016 LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[i].MemNode);
Nadav Rotem2e7d3812012-10-03 19:30:31 +00009017 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), Ld->getChain());
9018 }
Nadav Rotemc653de62012-10-03 16:11:15 +00009019
Nadav Rotem2e7d3812012-10-03 19:30:31 +00009020 // Replace the first store with the new store.
9021 CombineTo(EarliestOp, NewStore);
9022 // Erase all other stores.
9023 for (unsigned i = 0; i < NumElem ; ++i) {
Nadav Rotemc653de62012-10-03 16:11:15 +00009024 // Remove all Store nodes.
9025 if (StoreNodes[i].MemNode == EarliestOp)
9026 continue;
9027 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
9028 DAG.ReplaceAllUsesOfValueWith(SDValue(St, 0), St->getChain());
9029 removeFromWorkList(St);
9030 DAG.DeleteNode(St);
9031 }
9032
9033 return true;
9034}
9035
Dan Gohman475871a2008-07-27 21:46:04 +00009036SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00009037 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00009038 SDValue Chain = ST->getChain();
9039 SDValue Value = ST->getValue();
9040 SDValue Ptr = ST->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00009041
Evan Cheng59d5b682007-05-07 21:27:48 +00009042 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00009043 // resultant store does not need a higher alignment than the original.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009044 if (Value.getOpcode() == ISD::BITCAST && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00009045 ST->isUnindexed()) {
Dan Gohman1ba519b2009-02-20 23:29:13 +00009046 unsigned OrigAlign = ST->getAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009047 EVT SVT = Value.getOperand(0).getValueType();
Micah Villmow3574eca2012-10-08 16:38:25 +00009048 unsigned Align = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00009049 getABITypeAlignment(SVT.getTypeForEVT(*DAG.getContext()));
Duncan Sandsd4b9c172008-06-13 19:07:40 +00009050 if (Align <= OrigAlign &&
Duncan Sands25cf2272008-11-24 14:53:14 +00009051 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohmanf560ffa2009-01-28 17:46:25 +00009052 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00009053 return DAG.getStore(Chain, SDLoc(N), Value.getOperand(0),
Chris Lattner6229d0a2010-09-21 18:41:36 +00009054 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00009055 ST->isNonTemporal(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00009056 }
Owen Andersona34d9362011-04-14 17:30:49 +00009057
Chris Lattnerb3452ea2011-04-09 02:32:02 +00009058 // Turn 'store undef, Ptr' -> nothing.
9059 if (Value.getOpcode() == ISD::UNDEF && ST->isUnindexed())
9060 return Chain;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00009061
Nate Begeman2cbba892006-12-11 02:23:46 +00009062 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00009063 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00009064 // NOTE: If the original store is volatile, this transform must not increase
9065 // the number of stores. For example, on x86-32 an f64 can be stored in one
9066 // processor operation but an i64 (which is not legal) requires two. So the
9067 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00009068 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00009069 SDValue Tmp;
Craig Topper0ff11902013-08-15 02:44:19 +00009070 switch (CFP->getSimpleValueType(0).SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009071 default: llvm_unreachable("Unknown FP type");
Pete Cooper438c0402012-06-21 18:00:39 +00009072 case MVT::f16: // We don't do this for these yet.
9073 case MVT::f80:
Owen Anderson825b72b2009-08-11 20:47:22 +00009074 case MVT::f128:
9075 case MVT::ppcf128:
Dale Johannesenc7b21d52007-09-18 18:36:59 +00009076 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00009077 case MVT::f32:
Chris Lattner2392ae72010-04-15 04:48:01 +00009078 if ((isTypeLegal(MVT::i32) && !LegalOperations && !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009079 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00009080 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Owen Anderson825b72b2009-08-11 20:47:22 +00009081 bitcastToAPInt().getZExtValue(), MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00009082 return DAG.getStore(Chain, SDLoc(N), Tmp,
Chris Lattner6229d0a2010-09-21 18:41:36 +00009083 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00009084 ST->isNonTemporal(), ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00009085 }
9086 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00009087 case MVT::f64:
Chris Lattner2392ae72010-04-15 04:48:01 +00009088 if ((TLI.isTypeLegal(MVT::i64) && !LegalOperations &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00009089 !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009090 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen7111b022008-10-09 18:53:47 +00009091 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +00009092 getZExtValue(), MVT::i64);
Andrew Trickac6d9be2013-05-25 02:42:55 +00009093 return DAG.getStore(Chain, SDLoc(N), Tmp,
Chris Lattner6229d0a2010-09-21 18:41:36 +00009094 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00009095 ST->isNonTemporal(), ST->getAlignment());
Chris Lattnerb3452ea2011-04-09 02:32:02 +00009096 }
Owen Andersona34d9362011-04-14 17:30:49 +00009097
Chris Lattnerb3452ea2011-04-09 02:32:02 +00009098 if (!ST->isVolatile() &&
9099 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00009100 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00009101 // argument passing. Since this is so common, custom legalize the
9102 // 64-bit integer store into two 32-bit stores.
Dale Johannesen7111b022008-10-09 18:53:47 +00009103 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009104 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
9105 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00009106 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00009107
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00009108 unsigned Alignment = ST->getAlignment();
9109 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00009110 bool isNonTemporal = ST->isNonTemporal();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00009111
Andrew Trickac6d9be2013-05-25 02:42:55 +00009112 SDValue St0 = DAG.getStore(Chain, SDLoc(ST), Lo,
Chris Lattner6229d0a2010-09-21 18:41:36 +00009113 Ptr, ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00009114 isVolatile, isNonTemporal,
9115 ST->getAlignment());
Andrew Trickac6d9be2013-05-25 02:42:55 +00009116 Ptr = DAG.getNode(ISD::ADD, SDLoc(N), Ptr.getValueType(), Ptr,
Chris Lattner62be1a72006-12-12 04:16:14 +00009117 DAG.getConstant(4, Ptr.getValueType()));
Duncan Sandsdc846502007-10-28 12:59:45 +00009118 Alignment = MinAlign(Alignment, 4U);
Andrew Trickac6d9be2013-05-25 02:42:55 +00009119 SDValue St1 = DAG.getStore(Chain, SDLoc(ST), Hi,
Chris Lattner6229d0a2010-09-21 18:41:36 +00009120 Ptr, ST->getPointerInfo().getWithOffset(4),
9121 isVolatile, isNonTemporal,
David Greene1e559442010-02-15 17:00:31 +00009122 Alignment);
Andrew Trickac6d9be2013-05-25 02:42:55 +00009123 return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other,
Bill Wendlingc144a572009-01-30 23:36:47 +00009124 St0, St1);
Chris Lattner62be1a72006-12-12 04:16:14 +00009125 }
Bill Wendlingc144a572009-01-30 23:36:47 +00009126
Chris Lattner62be1a72006-12-12 04:16:14 +00009127 break;
Evan Cheng25ece662006-12-11 17:25:19 +00009128 }
Nate Begeman2cbba892006-12-11 02:23:46 +00009129 }
Nate Begeman2cbba892006-12-11 02:23:46 +00009130 }
9131
Evan Cheng255f20f2010-04-01 06:04:33 +00009132 // Try to infer better alignment information than the store already has.
9133 if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00009134 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
9135 if (Align > ST->getAlignment())
Andrew Trickac6d9be2013-05-25 02:42:55 +00009136 return DAG.getTruncStore(Chain, SDLoc(N), Value,
Evan Chenged1c0c72011-11-28 22:37:34 +00009137 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
9138 ST->isVolatile(), ST->isNonTemporal(), Align);
Evan Cheng255f20f2010-04-01 06:04:33 +00009139 }
9140 }
9141
Evan Cheng31959b12011-02-02 01:06:55 +00009142 // Try transforming a pair floating point load / store ops to integer
9143 // load / store ops.
9144 SDValue NewST = TransformFPLoadStorePair(N);
9145 if (NewST.getNode())
9146 return NewST;
9147
Hal Finkel253acef2013-08-29 03:29:55 +00009148 bool UseAA = CombinerAA.getNumOccurrences() > 0 ? CombinerAA :
9149 TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA();
9150 if (UseAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00009151 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009152 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00009153
Jim Laskey6ff23e52006-10-04 16:53:27 +00009154 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00009155 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00009156 SDValue ReplStore;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009157
9158 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00009159 if (ST->isTruncatingStore()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009160 ReplStore = DAG.getTruncStore(BetterChain, SDLoc(N), Value, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00009161 ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00009162 ST->getMemoryVT(), ST->isVolatile(),
9163 ST->isNonTemporal(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00009164 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009165 ReplStore = DAG.getStore(BetterChain, SDLoc(N), Value, Ptr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00009166 ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00009167 ST->isVolatile(), ST->isNonTemporal(),
9168 ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00009169 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009170
Jim Laskey279f0532006-09-25 16:29:54 +00009171 // Create token to keep both nodes around.
Andrew Trickac6d9be2013-05-25 02:42:55 +00009172 SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson825b72b2009-08-11 20:47:22 +00009173 MVT::Other, Chain, ReplStore);
Bill Wendlingc144a572009-01-30 23:36:47 +00009174
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009175 // Make sure the new and old chains are cleaned up.
9176 AddToWorkList(Token.getNode());
9177
Jim Laskey274062c2006-10-13 23:32:28 +00009178 // Don't add users to work list.
9179 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00009180 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00009181 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009182
Evan Cheng33dbedc2006-11-05 09:31:14 +00009183 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00009184 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00009185 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00009186
Chris Lattner3c872852007-12-29 06:26:16 +00009187 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00009188 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Nadav Rotembaff46f2011-06-15 11:19:12 +00009189 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00009190 // See if we can simplify the input to this truncstore with knowledge that
9191 // only the low bits are being used. For example:
9192 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Scott Michelfdc40a02009-02-17 22:15:04 +00009193 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00009194 GetDemandedBits(Value,
Nadav Rotembaff46f2011-06-15 11:19:12 +00009195 APInt::getLowBitsSet(
9196 Value.getValueType().getScalarType().getSizeInBits(),
9197 ST->getMemoryVT().getScalarType().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00009198 AddToWorkList(Value.getNode());
9199 if (Shorter.getNode())
Andrew Trickac6d9be2013-05-25 02:42:55 +00009200 return DAG.getTruncStore(Chain, SDLoc(N), Shorter,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00009201 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00009202 ST->isVolatile(), ST->isNonTemporal(),
9203 ST->getAlignment());
Scott Michelfdc40a02009-02-17 22:15:04 +00009204
Chris Lattnere33544c2007-10-13 06:58:48 +00009205 // Otherwise, see if we can simplify the operation with
9206 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00009207 if (SimplifyDemandedBits(Value,
Eric Christopher503a64d2010-12-09 04:48:06 +00009208 APInt::getLowBitsSet(
9209 Value.getValueType().getScalarType().getSizeInBits(),
9210 ST->getMemoryVT().getScalarType().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00009211 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00009212 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009213
Chris Lattner3c872852007-12-29 06:26:16 +00009214 // If this is a load followed by a store to the same location, then the store
9215 // is dead/noop.
9216 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009217 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00009218 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00009219 // There can't be any side effects between the load and store, such as
9220 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00009221 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00009222 // The store is dead, remove it.
9223 return Chain;
9224 }
9225 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00009226
Chris Lattnerddf89562008-01-17 19:59:44 +00009227 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
9228 // truncating store. We can do this even if this is already a truncstore.
9229 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00009230 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00009231 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009232 ST->getMemoryVT())) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009233 return DAG.getTruncStore(Chain, SDLoc(N), Value.getOperand(0),
Chris Lattnerda2d8e12010-09-21 17:42:31 +00009234 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00009235 ST->isVolatile(), ST->isNonTemporal(),
9236 ST->getAlignment());
Chris Lattnerddf89562008-01-17 19:59:44 +00009237 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00009238
Nadav Rotemc653de62012-10-03 16:11:15 +00009239 // Only perform this optimization before the types are legal, because we
Nadav Rotemea2c50c2012-10-04 22:35:15 +00009240 // don't want to perform this optimization on every DAGCombine invocation.
Nadav Rotema569a802012-12-02 17:14:09 +00009241 if (!LegalTypes) {
9242 bool EverChanged = false;
9243
9244 do {
9245 // There can be multiple store sequences on the same chain.
9246 // Keep trying to merge store sequences until we are unable to do so
9247 // or until we merge the last store on the chain.
9248 bool Changed = MergeConsecutiveStores(ST);
9249 EverChanged |= Changed;
9250 if (!Changed) break;
9251 } while (ST->getOpcode() != ISD::DELETED_NODE);
9252
9253 if (EverChanged)
9254 return SDValue(N, 0);
9255 }
Nadav Rotemc653de62012-10-03 16:11:15 +00009256
Evan Cheng8b944d32009-05-28 00:35:15 +00009257 return ReduceLoadOpStoreWidth(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00009258}
9259
Dan Gohman475871a2008-07-27 21:46:04 +00009260SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
9261 SDValue InVec = N->getOperand(0);
9262 SDValue InVal = N->getOperand(1);
9263 SDValue EltNo = N->getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00009264 SDLoc dl(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00009265
Bob Wilson492fd452010-05-19 23:42:58 +00009266 // If the inserted element is an UNDEF, just use the input vector.
9267 if (InVal.getOpcode() == ISD::UNDEF)
9268 return InVec;
9269
Nadav Rotem609d54e2011-02-12 14:40:33 +00009270 EVT VT = InVec.getValueType();
9271
Owen Anderson95771af2011-02-25 21:41:48 +00009272 // If we can't generate a legal BUILD_VECTOR, exit
Nadav Rotem609d54e2011-02-12 14:40:33 +00009273 if (LegalOperations && !TLI.isOperationLegal(ISD::BUILD_VECTOR, VT))
9274 return SDValue();
9275
Eli Friedman9db817f2011-09-09 21:04:06 +00009276 // Check that we know which element is being inserted
9277 if (!isa<ConstantSDNode>(EltNo))
9278 return SDValue();
9279 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009280
Eli Friedman9db817f2011-09-09 21:04:06 +00009281 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
9282 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
9283 // vector elements.
9284 SmallVector<SDValue, 8> Ops;
Quentin Colombet75c94332013-07-30 00:24:09 +00009285 // Do not combine these two vectors if the output vector will not replace
9286 // the input vector.
9287 if (InVec.getOpcode() == ISD::BUILD_VECTOR && InVec.hasOneUse()) {
Eli Friedman9db817f2011-09-09 21:04:06 +00009288 Ops.append(InVec.getNode()->op_begin(),
9289 InVec.getNode()->op_end());
9290 } else if (InVec.getOpcode() == ISD::UNDEF) {
9291 unsigned NElts = VT.getVectorNumElements();
9292 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
9293 } else {
9294 return SDValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00009295 }
Eli Friedman9db817f2011-09-09 21:04:06 +00009296
9297 // Insert the element
9298 if (Elt < Ops.size()) {
9299 // All the operands of BUILD_VECTOR must have the same type;
9300 // we enforce that here.
9301 EVT OpVT = Ops[0].getValueType();
9302 if (InVal.getValueType() != OpVT)
9303 InVal = OpVT.bitsGT(InVal.getValueType()) ?
9304 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
9305 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
9306 Ops[Elt] = InVal;
9307 }
9308
9309 // Return the new vector
9310 return DAG.getNode(ISD::BUILD_VECTOR, dl,
9311 VT, &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00009312}
9313
Dan Gohman475871a2008-07-27 21:46:04 +00009314SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00009315 // (vextract (scalar_to_vector val, 0) -> val
9316 SDValue InVec = N->getOperand(0);
Nadav Rotemba05c912012-01-17 21:44:01 +00009317 EVT VT = InVec.getValueType();
9318 EVT NVT = N->getValueType(0);
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00009319
Duncan Sandsc356f332011-05-09 08:03:33 +00009320 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
9321 // Check if the result type doesn't match the inserted element type. A
9322 // SCALAR_TO_VECTOR may truncate the inserted element and the
9323 // EXTRACT_VECTOR_ELT may widen the extracted vector.
9324 SDValue InOp = InVec.getOperand(0);
Duncan Sandsc356f332011-05-09 08:03:33 +00009325 if (InOp.getValueType() != NVT) {
9326 assert(InOp.getValueType().isInteger() && NVT.isInteger());
Andrew Trickac6d9be2013-05-25 02:42:55 +00009327 return DAG.getSExtOrTrunc(InOp, SDLoc(InVec), NVT);
Duncan Sandsc356f332011-05-09 08:03:33 +00009328 }
9329 return InOp;
9330 }
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009331
Nadav Rotemba05c912012-01-17 21:44:01 +00009332 SDValue EltNo = N->getOperand(1);
9333 bool ConstEltNo = isa<ConstantSDNode>(EltNo);
9334
9335 // Transform: (EXTRACT_VECTOR_ELT( VECTOR_SHUFFLE )) -> EXTRACT_VECTOR_ELT.
9336 // We only perform this optimization before the op legalization phase because
Nadav Rotem6dfabb62012-09-20 08:53:31 +00009337 // we may introduce new vector instructions which are not backed by TD
9338 // patterns. For example on AVX, extracting elements from a wide vector
9339 // without using extract_subvector.
Nadav Rotemba05c912012-01-17 21:44:01 +00009340 if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE
9341 && ConstEltNo && !LegalOperations) {
9342 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
9343 int NumElem = VT.getVectorNumElements();
9344 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(InVec);
9345 // Find the new index to extract from.
9346 int OrigElt = SVOp->getMaskElt(Elt);
9347
9348 // Extracting an undef index is undef.
9349 if (OrigElt == -1)
9350 return DAG.getUNDEF(NVT);
9351
9352 // Select the right vector half to extract from.
9353 if (OrigElt < NumElem) {
9354 InVec = InVec->getOperand(0);
9355 } else {
9356 InVec = InVec->getOperand(1);
9357 OrigElt -= NumElem;
9358 }
9359
Tom Stellard425b76c2013-08-05 22:22:01 +00009360 EVT IndexTy = TLI.getVectorIdxTy();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009361 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N), NVT,
Jim Grosbacha249f7d2012-05-08 20:56:07 +00009362 InVec, DAG.getConstant(OrigElt, IndexTy));
Nadav Rotemba05c912012-01-17 21:44:01 +00009363 }
9364
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009365 // Perform only after legalization to ensure build_vector / vector_shuffle
9366 // optimizations have already been done.
Duncan Sands25cf2272008-11-24 14:53:14 +00009367 if (!LegalOperations) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009368
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00009369 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
9370 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
9371 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Evan Cheng513da432007-10-06 08:19:55 +00009372
Nadav Rotemba05c912012-01-17 21:44:01 +00009373 if (ConstEltNo) {
Eric Christophercaebdd42010-11-03 09:36:40 +00009374 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00009375 bool NewLoad = false;
Mon P Wanga60b5232008-12-11 00:26:16 +00009376 bool BCNumEltsChanged = false;
Owen Andersone50ed302009-08-10 22:56:29 +00009377 EVT ExtVT = VT.getVectorElementType();
9378 EVT LVT = ExtVT;
Bill Wendlingc144a572009-01-30 23:36:47 +00009379
Evan Cheng84387ea2012-03-13 22:00:52 +00009380 // If the result of load has to be truncated, then it's not necessarily
9381 // profitable.
Evan Chenga03d3662012-03-13 22:16:11 +00009382 if (NVT.bitsLT(LVT) && !TLI.isTruncateFree(LVT, NVT))
Evan Cheng84387ea2012-03-13 22:00:52 +00009383 return SDValue();
9384
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009385 if (InVec.getOpcode() == ISD::BITCAST) {
Eli Friedmand6e25602011-12-26 22:49:32 +00009386 // Don't duplicate a load with other uses.
9387 if (!InVec.hasOneUse())
9388 return SDValue();
9389
Owen Andersone50ed302009-08-10 22:56:29 +00009390 EVT BCVT = InVec.getOperand(0).getValueType();
9391 if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00009392 return SDValue();
Mon P Wanga60b5232008-12-11 00:26:16 +00009393 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
9394 BCNumEltsChanged = true;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009395 InVec = InVec.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00009396 ExtVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009397 NewLoad = true;
9398 }
Evan Cheng513da432007-10-06 08:19:55 +00009399
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009400 LoadSDNode *LN0 = NULL;
Nate Begeman5a5ca152009-04-29 05:20:52 +00009401 const ShuffleVectorSDNode *SVN = NULL;
Bill Wendlingc144a572009-01-30 23:36:47 +00009402 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009403 LN0 = cast<LoadSDNode>(InVec);
Bill Wendlingc144a572009-01-30 23:36:47 +00009404 } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Owen Andersone50ed302009-08-10 22:56:29 +00009405 InVec.getOperand(0).getValueType() == ExtVT &&
Bill Wendlingc144a572009-01-30 23:36:47 +00009406 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Eli Friedmand6e25602011-12-26 22:49:32 +00009407 // Don't duplicate a load with other uses.
9408 if (!InVec.hasOneUse())
9409 return SDValue();
9410
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009411 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
Nate Begeman5a5ca152009-04-29 05:20:52 +00009412 } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009413 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
9414 // =>
9415 // (load $addr+1*size)
Scott Michelfdc40a02009-02-17 22:15:04 +00009416
Eli Friedmand6e25602011-12-26 22:49:32 +00009417 // Don't duplicate a load with other uses.
9418 if (!InVec.hasOneUse())
9419 return SDValue();
9420
Mon P Wanga60b5232008-12-11 00:26:16 +00009421 // If the bit convert changed the number of elements, it is unsafe
9422 // to examine the mask.
9423 if (BCNumEltsChanged)
9424 return SDValue();
Nate Begeman5a5ca152009-04-29 05:20:52 +00009425
9426 // Select the input vector, guarding against out of range extract vector.
9427 unsigned NumElems = VT.getVectorNumElements();
Eric Christophercaebdd42010-11-03 09:36:40 +00009428 int Idx = (Elt > (int)NumElems) ? -1 : SVN->getMaskElt(Elt);
Nate Begeman5a5ca152009-04-29 05:20:52 +00009429 InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
9430
Eli Friedmand6e25602011-12-26 22:49:32 +00009431 if (InVec.getOpcode() == ISD::BITCAST) {
9432 // Don't duplicate a load with other uses.
9433 if (!InVec.hasOneUse())
9434 return SDValue();
9435
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009436 InVec = InVec.getOperand(0);
Eli Friedmand6e25602011-12-26 22:49:32 +00009437 }
Gabor Greifba36cb52008-08-28 21:40:38 +00009438 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009439 LN0 = cast<LoadSDNode>(InVec);
Ted Kremenekd0e88f32010-04-08 18:49:30 +00009440 Elt = (Idx < (int)NumElems) ? Idx : Idx - (int)NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00009441 }
9442 }
Bill Wendlingc144a572009-01-30 23:36:47 +00009443
Eli Friedmand6e25602011-12-26 22:49:32 +00009444 // Make sure we found a non-volatile load and the extractelement is
9445 // the only use.
Nadav Rotem42febc62011-05-11 14:40:50 +00009446 if (!LN0 || !LN0->hasNUsesOfValue(1,0) || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00009447 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009448
Eric Christopherd81f17a2010-11-03 20:44:42 +00009449 // If Idx was -1 above, Elt is going to be -1, so just return undef.
9450 if (Elt == -1)
Eli Friedmaned4b4272011-07-25 22:25:42 +00009451 return DAG.getUNDEF(LVT);
Eric Christopherd81f17a2010-11-03 20:44:42 +00009452
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009453 unsigned Align = LN0->getAlignment();
9454 if (NewLoad) {
9455 // Check the resultant load doesn't need a higher alignment than the
9456 // original load.
Bill Wendlingc144a572009-01-30 23:36:47 +00009457 unsigned NewAlign =
Micah Villmow3574eca2012-10-08 16:38:25 +00009458 TLI.getDataLayout()
Eric Christopher503a64d2010-12-09 04:48:06 +00009459 ->getABITypeAlignment(LVT.getTypeForEVT(*DAG.getContext()));
Bill Wendlingc144a572009-01-30 23:36:47 +00009460
Dan Gohmanf560ffa2009-01-28 17:46:25 +00009461 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00009462 return SDValue();
Bill Wendlingc144a572009-01-30 23:36:47 +00009463
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009464 Align = NewAlign;
9465 }
9466
Dan Gohman475871a2008-07-27 21:46:04 +00009467 SDValue NewPtr = LN0->getBasePtr();
Chris Lattnerfa459012010-09-21 16:08:50 +00009468 unsigned PtrOff = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009469
Eric Christopherd81f17a2010-11-03 20:44:42 +00009470 if (Elt) {
Chris Lattnerfa459012010-09-21 16:08:50 +00009471 PtrOff = LVT.getSizeInBits() * Elt / 8;
Owen Andersone50ed302009-08-10 22:56:29 +00009472 EVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009473 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00009474 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Andrew Trickac6d9be2013-05-25 02:42:55 +00009475 NewPtr = DAG.getNode(ISD::ADD, SDLoc(N), PtrType, NewPtr,
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009476 DAG.getConstant(PtrOff, PtrType));
9477 }
Bill Wendlingc144a572009-01-30 23:36:47 +00009478
Eli Friedman4db4add2011-11-16 23:50:22 +00009479 // The replacement we need to do here is a little tricky: we need to
9480 // replace an extractelement of a load with a load.
9481 // Use ReplaceAllUsesOfValuesWith to do the replacement.
Eli Friedmand6e25602011-12-26 22:49:32 +00009482 // Note that this replacement assumes that the extractvalue is the only
9483 // use of the load; that's okay because we don't want to perform this
9484 // transformation in other cases anyway.
Evan Cheng84387ea2012-03-13 22:00:52 +00009485 SDValue Load;
Evan Chenga03d3662012-03-13 22:16:11 +00009486 SDValue Chain;
Evan Cheng84387ea2012-03-13 22:00:52 +00009487 if (NVT.bitsGT(LVT)) {
9488 // If the result type of vextract is wider than the load, then issue an
9489 // extending load instead.
9490 ISD::LoadExtType ExtType = TLI.isLoadExtLegal(ISD::ZEXTLOAD, LVT)
9491 ? ISD::ZEXTLOAD : ISD::EXTLOAD;
Andrew Trickac6d9be2013-05-25 02:42:55 +00009492 Load = DAG.getExtLoad(ExtType, SDLoc(N), NVT, LN0->getChain(),
Evan Cheng84387ea2012-03-13 22:00:52 +00009493 NewPtr, LN0->getPointerInfo().getWithOffset(PtrOff),
9494 LVT, LN0->isVolatile(), LN0->isNonTemporal(),Align);
Evan Chenga03d3662012-03-13 22:16:11 +00009495 Chain = Load.getValue(1);
9496 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009497 Load = DAG.getLoad(LVT, SDLoc(N), LN0->getChain(), NewPtr,
Evan Cheng84387ea2012-03-13 22:00:52 +00009498 LN0->getPointerInfo().getWithOffset(PtrOff),
Stephen Lin155615d2013-07-08 00:37:03 +00009499 LN0->isVolatile(), LN0->isNonTemporal(),
Evan Cheng84387ea2012-03-13 22:00:52 +00009500 LN0->isInvariant(), Align);
Evan Chenga03d3662012-03-13 22:16:11 +00009501 Chain = Load.getValue(1);
9502 if (NVT.bitsLT(LVT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00009503 Load = DAG.getNode(ISD::TRUNCATE, SDLoc(N), NVT, Load);
Evan Chenga03d3662012-03-13 22:16:11 +00009504 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00009505 Load = DAG.getNode(ISD::BITCAST, SDLoc(N), NVT, Load);
Evan Chenga03d3662012-03-13 22:16:11 +00009506 }
Eli Friedman4db4add2011-11-16 23:50:22 +00009507 WorkListRemover DeadNodes(*this);
9508 SDValue From[] = { SDValue(N, 0), SDValue(LN0,1) };
Evan Chenga03d3662012-03-13 22:16:11 +00009509 SDValue To[] = { Load, Chain };
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00009510 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
Eli Friedman4db4add2011-11-16 23:50:22 +00009511 // Since we're explcitly calling ReplaceAllUses, add the new node to the
9512 // worklist explicitly as well.
9513 AddToWorkList(Load.getNode());
Craig Topper0c9da212012-03-20 05:28:39 +00009514 AddUsersToWorkList(Load.getNode()); // Add users too
Eli Friedman4db4add2011-11-16 23:50:22 +00009515 // Make sure to revisit this node to clean it up; it will usually be dead.
9516 AddToWorkList(N);
9517 return SDValue(N, 0);
Evan Cheng513da432007-10-06 08:19:55 +00009518 }
Bill Wendlingc144a572009-01-30 23:36:47 +00009519
Dan Gohman475871a2008-07-27 21:46:04 +00009520 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00009521}
Evan Cheng513da432007-10-06 08:19:55 +00009522
Michael Liaofac14ab2012-10-23 23:06:52 +00009523// Simplify (build_vec (ext )) to (bitcast (build_vec ))
9524SDValue DAGCombiner::reduceBuildVecExtToExtBuildVec(SDNode *N) {
9525 // We perform this optimization post type-legalization because
9526 // the type-legalizer often scalarizes integer-promoted vectors.
9527 // Performing this optimization before may create bit-casts which
9528 // will be type-legalized to complex code sequences.
9529 // We perform this optimization only before the operation legalizer because we
9530 // may introduce illegal operations.
9531 if (Level != AfterLegalizeVectorOps && Level != AfterLegalizeTypes)
9532 return SDValue();
9533
Dan Gohman7f321562007-06-25 16:23:39 +00009534 unsigned NumInScalars = N->getNumOperands();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009535 SDLoc dl(N);
Owen Andersone50ed302009-08-10 22:56:29 +00009536 EVT VT = N->getValueType(0);
Nadav Rotemb87bdac2012-07-15 08:38:23 +00009537
Nadav Rotemb00418a2011-10-29 21:23:04 +00009538 // Check to see if this is a BUILD_VECTOR of a bunch of values
9539 // which come from any_extend or zero_extend nodes. If so, we can create
9540 // a new BUILD_VECTOR using bit-casts which may enable other BUILD_VECTOR
Nadav Rotemf47368b2011-10-31 20:08:25 +00009541 // optimizations. We do not handle sign-extend because we can't fill the sign
9542 // using shuffles.
Nadav Rotemb00418a2011-10-29 21:23:04 +00009543 EVT SourceType = MVT::Other;
Craig Topperd3b58892012-01-17 09:09:48 +00009544 bool AllAnyExt = true;
Nadav Rotemb87bdac2012-07-15 08:38:23 +00009545
Craig Topperd3b58892012-01-17 09:09:48 +00009546 for (unsigned i = 0; i != NumInScalars; ++i) {
Nadav Rotemb00418a2011-10-29 21:23:04 +00009547 SDValue In = N->getOperand(i);
9548 // Ignore undef inputs.
9549 if (In.getOpcode() == ISD::UNDEF) continue;
9550
9551 bool AnyExt = In.getOpcode() == ISD::ANY_EXTEND;
9552 bool ZeroExt = In.getOpcode() == ISD::ZERO_EXTEND;
9553
Nadav Rotemf47368b2011-10-31 20:08:25 +00009554 // Abort if the element is not an extension.
Nadav Rotemb00418a2011-10-29 21:23:04 +00009555 if (!ZeroExt && !AnyExt) {
Nadav Rotemf47368b2011-10-31 20:08:25 +00009556 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00009557 break;
9558 }
9559
9560 // The input is a ZeroExt or AnyExt. Check the original type.
9561 EVT InTy = In.getOperand(0).getValueType();
9562
9563 // Check that all of the widened source types are the same.
9564 if (SourceType == MVT::Other)
Nadav Rotemf47368b2011-10-31 20:08:25 +00009565 // First time.
Nadav Rotemb00418a2011-10-29 21:23:04 +00009566 SourceType = InTy;
9567 else if (InTy != SourceType) {
9568 // Multiple income types. Abort.
Nadav Rotemf47368b2011-10-31 20:08:25 +00009569 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00009570 break;
9571 }
9572
9573 // Check if all of the extends are ANY_EXTENDs.
Craig Topperd3b58892012-01-17 09:09:48 +00009574 AllAnyExt &= AnyExt;
Nadav Rotemb00418a2011-10-29 21:23:04 +00009575 }
9576
Nadav Rotemf47368b2011-10-31 20:08:25 +00009577 // In order to have valid types, all of the inputs must be extended from the
9578 // same source type and all of the inputs must be any or zero extend.
9579 // Scalar sizes must be a power of two.
Michael Liaofac14ab2012-10-23 23:06:52 +00009580 EVT OutScalarTy = VT.getScalarType();
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009581 bool ValidTypes = SourceType != MVT::Other &&
Nadav Rotemf47368b2011-10-31 20:08:25 +00009582 isPowerOf2_32(OutScalarTy.getSizeInBits()) &&
9583 isPowerOf2_32(SourceType.getSizeInBits());
9584
Nadav Rotem6431ff92012-03-15 08:49:06 +00009585 // Create a new simpler BUILD_VECTOR sequence which other optimizations can
9586 // turn into a single shuffle instruction.
Michael Liaofac14ab2012-10-23 23:06:52 +00009587 if (!ValidTypes)
9588 return SDValue();
Nadav Rotemb00418a2011-10-29 21:23:04 +00009589
Michael Liaofac14ab2012-10-23 23:06:52 +00009590 bool isLE = TLI.isLittleEndian();
9591 unsigned ElemRatio = OutScalarTy.getSizeInBits()/SourceType.getSizeInBits();
9592 assert(ElemRatio > 1 && "Invalid element size ratio");
9593 SDValue Filler = AllAnyExt ? DAG.getUNDEF(SourceType):
9594 DAG.getConstant(0, SourceType);
Nadav Rotemb00418a2011-10-29 21:23:04 +00009595
Michael Liaofac14ab2012-10-23 23:06:52 +00009596 unsigned NewBVElems = ElemRatio * VT.getVectorNumElements();
9597 SmallVector<SDValue, 8> Ops(NewBVElems, Filler);
Nadav Rotemb00418a2011-10-29 21:23:04 +00009598
Michael Liaofac14ab2012-10-23 23:06:52 +00009599 // Populate the new build_vector
Jakub Staszakadf38912012-10-24 00:38:25 +00009600 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Michael Liaofac14ab2012-10-23 23:06:52 +00009601 SDValue Cast = N->getOperand(i);
9602 assert((Cast.getOpcode() == ISD::ANY_EXTEND ||
9603 Cast.getOpcode() == ISD::ZERO_EXTEND ||
9604 Cast.getOpcode() == ISD::UNDEF) && "Invalid cast opcode");
9605 SDValue In;
9606 if (Cast.getOpcode() == ISD::UNDEF)
9607 In = DAG.getUNDEF(SourceType);
9608 else
9609 In = Cast->getOperand(0);
9610 unsigned Index = isLE ? (i * ElemRatio) :
9611 (i * ElemRatio + (ElemRatio - 1));
Nadav Rotemb00418a2011-10-29 21:23:04 +00009612
Michael Liaofac14ab2012-10-23 23:06:52 +00009613 assert(Index < Ops.size() && "Invalid index");
9614 Ops[Index] = In;
Nadav Rotemb00418a2011-10-29 21:23:04 +00009615 }
Chris Lattnerca242442006-03-19 01:27:56 +00009616
Michael Liaofac14ab2012-10-23 23:06:52 +00009617 // The type of the new BUILD_VECTOR node.
9618 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), SourceType, NewBVElems);
9619 assert(VecVT.getSizeInBits() == VT.getSizeInBits() &&
9620 "Invalid vector size");
9621 // Check if the new vector type is legal.
9622 if (!isTypeLegal(VecVT)) return SDValue();
9623
9624 // Make the new BUILD_VECTOR.
9625 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], Ops.size());
9626
9627 // The new BUILD_VECTOR node has the potential to be further optimized.
9628 AddToWorkList(BV.getNode());
9629 // Bitcast to the desired type.
9630 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
9631}
9632
Michael Liao1a5cc712012-10-24 04:14:18 +00009633SDValue DAGCombiner::reduceBuildVecConvertToConvertBuildVec(SDNode *N) {
9634 EVT VT = N->getValueType(0);
9635
9636 unsigned NumInScalars = N->getNumOperands();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009637 SDLoc dl(N);
Michael Liao1a5cc712012-10-24 04:14:18 +00009638
9639 EVT SrcVT = MVT::Other;
9640 unsigned Opcode = ISD::DELETED_NODE;
9641 unsigned NumDefs = 0;
9642
9643 for (unsigned i = 0; i != NumInScalars; ++i) {
9644 SDValue In = N->getOperand(i);
9645 unsigned Opc = In.getOpcode();
9646
9647 if (Opc == ISD::UNDEF)
9648 continue;
9649
9650 // If all scalar values are floats and converted from integers.
9651 if (Opcode == ISD::DELETED_NODE &&
9652 (Opc == ISD::UINT_TO_FP || Opc == ISD::SINT_TO_FP)) {
9653 Opcode = Opc;
Michael Liao1a5cc712012-10-24 04:14:18 +00009654 }
Tom Stellardd40758b2013-01-02 22:13:01 +00009655
Michael Liao1a5cc712012-10-24 04:14:18 +00009656 if (Opc != Opcode)
9657 return SDValue();
9658
9659 EVT InVT = In.getOperand(0).getValueType();
9660
9661 // If all scalar values are typed differently, bail out. It's chosen to
9662 // simplify BUILD_VECTOR of integer types.
9663 if (SrcVT == MVT::Other)
9664 SrcVT = InVT;
9665 if (SrcVT != InVT)
9666 return SDValue();
9667 NumDefs++;
9668 }
9669
9670 // If the vector has just one element defined, it's not worth to fold it into
9671 // a vectorized one.
9672 if (NumDefs < 2)
9673 return SDValue();
9674
9675 assert((Opcode == ISD::UINT_TO_FP || Opcode == ISD::SINT_TO_FP)
9676 && "Should only handle conversion from integer to float.");
9677 assert(SrcVT != MVT::Other && "Cannot determine source type!");
9678
9679 EVT NVT = EVT::getVectorVT(*DAG.getContext(), SrcVT, NumInScalars);
Tom Stellardd40758b2013-01-02 22:13:01 +00009680
9681 if (!TLI.isOperationLegalOrCustom(Opcode, NVT))
9682 return SDValue();
9683
Michael Liao1a5cc712012-10-24 04:14:18 +00009684 SmallVector<SDValue, 8> Opnds;
9685 for (unsigned i = 0; i != NumInScalars; ++i) {
9686 SDValue In = N->getOperand(i);
9687
9688 if (In.getOpcode() == ISD::UNDEF)
9689 Opnds.push_back(DAG.getUNDEF(SrcVT));
9690 else
9691 Opnds.push_back(In.getOperand(0));
9692 }
9693 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT,
9694 &Opnds[0], Opnds.size());
9695 AddToWorkList(BV.getNode());
9696
9697 return DAG.getNode(Opcode, dl, VT, BV);
9698}
9699
Michael Liaofac14ab2012-10-23 23:06:52 +00009700SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
9701 unsigned NumInScalars = N->getNumOperands();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009702 SDLoc dl(N);
Michael Liaofac14ab2012-10-23 23:06:52 +00009703 EVT VT = N->getValueType(0);
9704
9705 // A vector built entirely of undefs is undef.
9706 if (ISD::allOperandsUndef(N))
9707 return DAG.getUNDEF(VT);
9708
9709 SDValue V = reduceBuildVecExtToExtBuildVec(N);
9710 if (V.getNode())
9711 return V;
9712
Michael Liao1a5cc712012-10-24 04:14:18 +00009713 V = reduceBuildVecConvertToConvertBuildVec(N);
9714 if (V.getNode())
9715 return V;
9716
Dan Gohman7f321562007-06-25 16:23:39 +00009717 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
9718 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
9719 // at most two distinct vectors, turn this into a shuffle node.
Duncan Sands00294ca2012-03-19 15:35:44 +00009720
9721 // May only combine to shuffle after legalize if shuffle is legal.
9722 if (LegalOperations &&
9723 !TLI.isOperationLegalOrCustom(ISD::VECTOR_SHUFFLE, VT))
9724 return SDValue();
9725
Dan Gohman475871a2008-07-27 21:46:04 +00009726 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00009727 for (unsigned i = 0; i != NumInScalars; ++i) {
9728 // Ignore undef inputs.
9729 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00009730
Dan Gohman7f321562007-06-25 16:23:39 +00009731 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00009732 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00009733 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00009734 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00009735 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009736 break;
9737 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009738
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009739 // We allow up to two distinct input vectors.
Dan Gohman475871a2008-07-27 21:46:04 +00009740 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009741 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
9742 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00009743
Gabor Greifba36cb52008-08-28 21:40:38 +00009744 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00009745 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00009746 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00009747 VecIn2 = ExtractedFromVec;
9748 } else {
9749 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009750 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009751 break;
9752 }
9753 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009754
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009755 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00009756 if (VecIn1.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00009757 SmallVector<int, 8> Mask;
Chris Lattnerd7648c82006-03-28 20:28:38 +00009758 for (unsigned i = 0; i != NumInScalars; ++i) {
9759 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Nate Begeman9008ca62009-04-27 18:41:29 +00009760 Mask.push_back(-1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009761 continue;
9762 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009763
Rafael Espindola15684b22009-04-24 12:40:33 +00009764 // If extracting from the first vector, just use the index directly.
Nate Begeman9008ca62009-04-27 18:41:29 +00009765 SDValue Extract = N->getOperand(i);
Mon P Wang93b74152009-03-17 06:33:10 +00009766 SDValue ExtVal = Extract.getOperand(1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009767 if (Extract.getOperand(0) == VecIn1) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00009768 unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue();
9769 if (ExtIndex > VT.getVectorNumElements())
9770 return SDValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009771
Nate Begeman5a5ca152009-04-29 05:20:52 +00009772 Mask.push_back(ExtIndex);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009773 continue;
9774 }
9775
9776 // Otherwise, use InIdx + VecSize
Mon P Wang93b74152009-03-17 06:33:10 +00009777 unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00009778 Mask.push_back(Idx+NumInScalars);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009779 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009780
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009781 // We can't generate a shuffle node with mismatched input and output types.
9782 // Attempt to transform a single input vector to the correct type.
9783 if ((VT != VecIn1.getValueType())) {
9784 // We don't support shuffeling between TWO values of different types.
9785 if (VecIn2.getNode() != 0)
9786 return SDValue();
9787
9788 // We only support widening of vectors which are half the size of the
9789 // output registers. For example XMM->YMM widening on X86 with AVX.
9790 if (VecIn1.getValueType().getSizeInBits()*2 != VT.getSizeInBits())
9791 return SDValue();
9792
James Molloy8cd08bf2012-09-10 14:01:21 +00009793 // If the input vector type has a different base type to the output
9794 // vector type, bail out.
9795 if (VecIn1.getValueType().getVectorElementType() !=
9796 VT.getVectorElementType())
9797 return SDValue();
9798
Stepan Dyatkovskiyfdeb9fe2012-08-22 09:33:55 +00009799 // Widen the input vector by adding undef values.
Michael Liaofac14ab2012-10-23 23:06:52 +00009800 VecIn1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
Stepan Dyatkovskiyfdeb9fe2012-08-22 09:33:55 +00009801 VecIn1, DAG.getUNDEF(VecIn1.getValueType()));
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009802 }
9803
9804 // If VecIn2 is unused then change it to undef.
9805 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
9806
Nadav Rotem6dfabb62012-09-20 08:53:31 +00009807 // Check that we were able to transform all incoming values to the same
9808 // type.
Nadav Rotem0877fdf2012-02-13 12:42:26 +00009809 if (VecIn2.getValueType() != VecIn1.getValueType() ||
9810 VecIn1.getValueType() != VT)
9811 return SDValue();
9812
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009813 // Only type-legal BUILD_VECTOR nodes are converted to shuffle nodes.
Nadav Rotem0877fdf2012-02-13 12:42:26 +00009814 if (!isTypeLegal(VT))
Duncan Sands25cf2272008-11-24 14:53:14 +00009815 return SDValue();
9816
Dan Gohman7f321562007-06-25 16:23:39 +00009817 // Return the new VECTOR_SHUFFLE node.
Nate Begeman9008ca62009-04-27 18:41:29 +00009818 SDValue Ops[2];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00009819 Ops[0] = VecIn1;
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009820 Ops[1] = VecIn2;
Michael Liaofac14ab2012-10-23 23:06:52 +00009821 return DAG.getVectorShuffle(VT, dl, Ops[0], Ops[1], &Mask[0]);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009822 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009823
Dan Gohman475871a2008-07-27 21:46:04 +00009824 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00009825}
9826
Dan Gohman475871a2008-07-27 21:46:04 +00009827SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00009828 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
9829 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
9830 // inputs come from at most two distinct vectors, turn this into a shuffle
9831 // node.
9832
9833 // If we only have one input vector, we don't need to do any concatenation.
Bill Wendlingc144a572009-01-30 23:36:47 +00009834 if (N->getNumOperands() == 1)
Dan Gohman7f321562007-06-25 16:23:39 +00009835 return N->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00009836
Nadav Rotemb7e230d2012-07-14 21:30:27 +00009837 // Check if all of the operands are undefs.
Nadav Rotemb87bdac2012-07-15 08:38:23 +00009838 if (ISD::allOperandsUndef(N))
Nadav Rotemb7e230d2012-07-14 21:30:27 +00009839 return DAG.getUNDEF(N->getValueType(0));
9840
Nadav Rotemb2ed5fa2013-05-01 19:18:51 +00009841 // Type legalization of vectors and DAG canonicalization of SHUFFLE_VECTOR
9842 // nodes often generate nop CONCAT_VECTOR nodes.
9843 // Scan the CONCAT_VECTOR operands and look for a CONCAT operations that
9844 // place the incoming vectors at the exact same location.
9845 SDValue SingleSource = SDValue();
9846 unsigned PartNumElem = N->getOperand(0).getValueType().getVectorNumElements();
9847
9848 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
9849 SDValue Op = N->getOperand(i);
9850
9851 if (Op.getOpcode() == ISD::UNDEF)
9852 continue;
9853
9854 // Check if this is the identity extract:
9855 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR)
9856 return SDValue();
9857
9858 // Find the single incoming vector for the extract_subvector.
9859 if (SingleSource.getNode()) {
9860 if (Op.getOperand(0) != SingleSource)
9861 return SDValue();
9862 } else {
9863 SingleSource = Op.getOperand(0);
Michael Kuperstein27202482013-05-06 08:06:13 +00009864
9865 // Check the source type is the same as the type of the result.
9866 // If not, this concat may extend the vector, so we can not
9867 // optimize it away.
9868 if (SingleSource.getValueType() != N->getValueType(0))
9869 return SDValue();
Nadav Rotemb2ed5fa2013-05-01 19:18:51 +00009870 }
9871
9872 unsigned IdentityIndex = i * PartNumElem;
9873 ConstantSDNode *CS = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9874 // The extract index must be constant.
9875 if (!CS)
9876 return SDValue();
Stephen Lin155615d2013-07-08 00:37:03 +00009877
Nadav Rotemb2ed5fa2013-05-01 19:18:51 +00009878 // Check that we are reading from the identity index.
9879 if (CS->getZExtValue() != IdentityIndex)
9880 return SDValue();
9881 }
9882
9883 if (SingleSource.getNode())
9884 return SingleSource;
Stephen Lin155615d2013-07-08 00:37:03 +00009885
Dan Gohman475871a2008-07-27 21:46:04 +00009886 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00009887}
9888
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00009889SDValue DAGCombiner::visitEXTRACT_SUBVECTOR(SDNode* N) {
9890 EVT NVT = N->getValueType(0);
9891 SDValue V = N->getOperand(0);
9892
Michael Liao13429e22012-10-17 20:48:33 +00009893 if (V->getOpcode() == ISD::CONCAT_VECTORS) {
9894 // Combine:
9895 // (extract_subvec (concat V1, V2, ...), i)
9896 // Into:
9897 // Vi if possible
Jack Carteradbd3ae2013-10-17 01:34:33 +00009898 // Only operand 0 is checked as 'concat' assumes all inputs of the same
9899 // type.
Michael Liao9aecdb52012-10-19 03:17:00 +00009900 if (V->getOperand(0).getValueType() != NVT)
9901 return SDValue();
Michael Liao13429e22012-10-17 20:48:33 +00009902 unsigned Idx = dyn_cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
9903 unsigned NumElems = NVT.getVectorNumElements();
9904 assert((Idx % NumElems) == 0 &&
9905 "IDX in concat is not a multiple of the result vector length.");
9906 return V->getOperand(Idx / NumElems);
9907 }
9908
Michael Liaob4f98ea2013-03-25 23:47:35 +00009909 // Skip bitcasting
9910 if (V->getOpcode() == ISD::BITCAST)
9911 V = V.getOperand(0);
9912
9913 if (V->getOpcode() == ISD::INSERT_SUBVECTOR) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009914 SDLoc dl(N);
Michael Liaob4f98ea2013-03-25 23:47:35 +00009915 // Handle only simple case where vector being inserted and vector
9916 // being extracted are of same type, and are half size of larger vectors.
9917 EVT BigVT = V->getOperand(0).getValueType();
9918 EVT SmallVT = V->getOperand(1).getValueType();
9919 if (!NVT.bitsEq(SmallVT) || NVT.getSizeInBits()*2 != BigVT.getSizeInBits())
9920 return SDValue();
9921
9922 // Only handle cases where both indexes are constants with the same type.
9923 ConstantSDNode *ExtIdx = dyn_cast<ConstantSDNode>(N->getOperand(1));
9924 ConstantSDNode *InsIdx = dyn_cast<ConstantSDNode>(V->getOperand(2));
9925
9926 if (InsIdx && ExtIdx &&
9927 InsIdx->getValueType(0).getSizeInBits() <= 64 &&
9928 ExtIdx->getValueType(0).getSizeInBits() <= 64) {
9929 // Combine:
9930 // (extract_subvec (insert_subvec V1, V2, InsIdx), ExtIdx)
9931 // Into:
9932 // indices are equal or bit offsets are equal => V1
9933 // otherwise => (extract_subvec V1, ExtIdx)
9934 if (InsIdx->getZExtValue() * SmallVT.getScalarType().getSizeInBits() ==
9935 ExtIdx->getZExtValue() * NVT.getScalarType().getSizeInBits())
9936 return DAG.getNode(ISD::BITCAST, dl, NVT, V->getOperand(1));
9937 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NVT,
9938 DAG.getNode(ISD::BITCAST, dl,
9939 N->getOperand(0).getValueType(),
9940 V->getOperand(0)), N->getOperand(1));
9941 }
9942 }
9943
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00009944 return SDValue();
9945}
9946
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009947// Tries to turn a shuffle of two CONCAT_VECTORS into a single concat.
9948static SDValue partitionShuffleOfConcats(SDNode *N, SelectionDAG &DAG) {
9949 EVT VT = N->getValueType(0);
9950 unsigned NumElts = VT.getVectorNumElements();
9951
9952 SDValue N0 = N->getOperand(0);
9953 SDValue N1 = N->getOperand(1);
9954 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9955
9956 SmallVector<SDValue, 4> Ops;
9957 EVT ConcatVT = N0.getOperand(0).getValueType();
9958 unsigned NumElemsPerConcat = ConcatVT.getVectorNumElements();
9959 unsigned NumConcats = NumElts / NumElemsPerConcat;
9960
9961 // Look at every vector that's inserted. We're looking for exact
9962 // subvector-sized copies from a concatenated vector
9963 for (unsigned I = 0; I != NumConcats; ++I) {
9964 // Make sure we're dealing with a copy.
9965 unsigned Begin = I * NumElemsPerConcat;
Hao Liu3778c042013-05-13 02:07:05 +00009966 bool AllUndef = true, NoUndef = true;
9967 for (unsigned J = Begin; J != Begin + NumElemsPerConcat; ++J) {
9968 if (SVN->getMaskElt(J) >= 0)
9969 AllUndef = false;
9970 else
9971 NoUndef = false;
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009972 }
9973
Hao Liu3778c042013-05-13 02:07:05 +00009974 if (NoUndef) {
Hao Liu3778c042013-05-13 02:07:05 +00009975 if (SVN->getMaskElt(Begin) % NumElemsPerConcat != 0)
9976 return SDValue();
9977
9978 for (unsigned J = 1; J != NumElemsPerConcat; ++J)
9979 if (SVN->getMaskElt(Begin + J - 1) + 1 != SVN->getMaskElt(Begin + J))
9980 return SDValue();
9981
9982 unsigned FirstElt = SVN->getMaskElt(Begin) / NumElemsPerConcat;
9983 if (FirstElt < N0.getNumOperands())
9984 Ops.push_back(N0.getOperand(FirstElt));
9985 else
9986 Ops.push_back(N1.getOperand(FirstElt - N0.getNumOperands()));
9987
9988 } else if (AllUndef) {
9989 Ops.push_back(DAG.getUNDEF(N0.getOperand(0).getValueType()));
9990 } else { // Mixed with general masks and undefs, can't do optimization.
9991 return SDValue();
9992 }
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009993 }
9994
Andrew Trickac6d9be2013-05-25 02:42:55 +00009995 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, Ops.data(),
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009996 Ops.size());
9997}
9998
Dan Gohman475871a2008-07-27 21:46:04 +00009999SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +000010000 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +000010001 unsigned NumElts = VT.getVectorNumElements();
Chris Lattnerf1d0c622006-03-31 22:16:43 +000010002
Mon P Wangaeb06d22008-11-10 04:46:22 +000010003 SDValue N0 = N->getOperand(0);
Craig Topper481b79c2012-01-04 08:07:43 +000010004 SDValue N1 = N->getOperand(1);
Mon P Wangaeb06d22008-11-10 04:46:22 +000010005
Craig Topperae1bec52012-04-09 05:16:56 +000010006 assert(N0.getValueType() == VT && "Vector shuffle must be normalized in DAG");
Mon P Wangaeb06d22008-11-10 04:46:22 +000010007
Craig Topper481b79c2012-01-04 08:07:43 +000010008 // Canonicalize shuffle undef, undef -> undef
10009 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
10010 return DAG.getUNDEF(VT);
10011
10012 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
10013
10014 // Canonicalize shuffle v, v -> v, undef
10015 if (N0 == N1) {
10016 SmallVector<int, 8> NewMask;
10017 for (unsigned i = 0; i != NumElts; ++i) {
10018 int Idx = SVN->getMaskElt(i);
10019 if (Idx >= (int)NumElts) Idx -= NumElts;
10020 NewMask.push_back(Idx);
10021 }
Andrew Trickac6d9be2013-05-25 02:42:55 +000010022 return DAG.getVectorShuffle(VT, SDLoc(N), N0, DAG.getUNDEF(VT),
Craig Topper481b79c2012-01-04 08:07:43 +000010023 &NewMask[0]);
10024 }
10025
10026 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
10027 if (N0.getOpcode() == ISD::UNDEF) {
10028 SmallVector<int, 8> NewMask;
10029 for (unsigned i = 0; i != NumElts; ++i) {
10030 int Idx = SVN->getMaskElt(i);
Craig Topper4b206bd2012-04-09 05:55:33 +000010031 if (Idx >= 0) {
Craig Topper01d22aa2013-08-08 07:38:55 +000010032 if (Idx >= (int)NumElts)
Craig Topper4b206bd2012-04-09 05:55:33 +000010033 Idx -= NumElts;
Craig Topper01d22aa2013-08-08 07:38:55 +000010034 else
10035 Idx = -1; // remove reference to lhs
Craig Topper4b206bd2012-04-09 05:55:33 +000010036 }
10037 NewMask.push_back(Idx);
Craig Topper481b79c2012-01-04 08:07:43 +000010038 }
Andrew Trickac6d9be2013-05-25 02:42:55 +000010039 return DAG.getVectorShuffle(VT, SDLoc(N), N1, DAG.getUNDEF(VT),
Craig Topper481b79c2012-01-04 08:07:43 +000010040 &NewMask[0]);
10041 }
10042
10043 // Remove references to rhs if it is undef
10044 if (N1.getOpcode() == ISD::UNDEF) {
10045 bool Changed = false;
10046 SmallVector<int, 8> NewMask;
10047 for (unsigned i = 0; i != NumElts; ++i) {
10048 int Idx = SVN->getMaskElt(i);
10049 if (Idx >= (int)NumElts) {
10050 Idx = -1;
10051 Changed = true;
10052 }
10053 NewMask.push_back(Idx);
10054 }
10055 if (Changed)
Andrew Trickac6d9be2013-05-25 02:42:55 +000010056 return DAG.getVectorShuffle(VT, SDLoc(N), N0, N1, &NewMask[0]);
Craig Topper481b79c2012-01-04 08:07:43 +000010057 }
Evan Chenge7bec0d2006-07-20 22:44:41 +000010058
Bob Wilson0f1db1a2010-10-28 17:06:14 +000010059 // If it is a splat, check if the argument vector is another splat or a
10060 // build_vector with all scalar elements the same.
Bob Wilson0f1db1a2010-10-28 17:06:14 +000010061 if (SVN->isSplat() && SVN->getSplatIndex() < (int)NumElts) {
Gabor Greifba36cb52008-08-28 21:40:38 +000010062 SDNode *V = N0.getNode();
Evan Cheng917ec982006-07-21 08:25:53 +000010063
Dan Gohman7f321562007-06-25 16:23:39 +000010064 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +000010065 // not the number of vector elements, look through it. Be careful not to
10066 // look though conversions that change things like v4f32 to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010067 if (V->getOpcode() == ISD::BITCAST) {
Dan Gohman475871a2008-07-27 21:46:04 +000010068 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +000010069 if (ConvInput.getValueType().isVector() &&
10070 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +000010071 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +000010072 }
10073
Dan Gohman7f321562007-06-25 16:23:39 +000010074 if (V->getOpcode() == ISD::BUILD_VECTOR) {
Bob Wilson0f1db1a2010-10-28 17:06:14 +000010075 assert(V->getNumOperands() == NumElts &&
10076 "BUILD_VECTOR has wrong number of operands");
10077 SDValue Base;
10078 bool AllSame = true;
10079 for (unsigned i = 0; i != NumElts; ++i) {
10080 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
10081 Base = V->getOperand(i);
10082 break;
Evan Cheng917ec982006-07-21 08:25:53 +000010083 }
Evan Cheng917ec982006-07-21 08:25:53 +000010084 }
Bob Wilson0f1db1a2010-10-28 17:06:14 +000010085 // Splat of <u, u, u, u>, return <u, u, u, u>
10086 if (!Base.getNode())
10087 return N0;
10088 for (unsigned i = 0; i != NumElts; ++i) {
10089 if (V->getOperand(i) != Base) {
10090 AllSame = false;
10091 break;
10092 }
10093 }
10094 // Splat of <x, x, x, x>, return <x, x, x, x>
10095 if (AllSame)
10096 return N0;
Evan Cheng917ec982006-07-21 08:25:53 +000010097 }
10098 }
Nadav Rotem4ac90812012-04-01 19:31:22 +000010099
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +000010100 if (N0.getOpcode() == ISD::CONCAT_VECTORS &&
10101 Level < AfterLegalizeVectorOps &&
10102 (N1.getOpcode() == ISD::UNDEF ||
10103 (N1.getOpcode() == ISD::CONCAT_VECTORS &&
10104 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()))) {
10105 SDValue V = partitionShuffleOfConcats(N, DAG);
10106
10107 if (V.getNode())
10108 return V;
10109 }
10110
Nadav Rotem4ac90812012-04-01 19:31:22 +000010111 // If this shuffle node is simply a swizzle of another shuffle node,
Nadav Rotemd16c8d02012-04-07 21:19:08 +000010112 // and it reverses the swizzle of the previous shuffle then we can
10113 // optimize shuffle(shuffle(x, undef), undef) -> x.
Nadav Rotem4ac90812012-04-01 19:31:22 +000010114 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
10115 N1.getOpcode() == ISD::UNDEF) {
10116
Nadav Rotem4ac90812012-04-01 19:31:22 +000010117 ShuffleVectorSDNode *OtherSV = cast<ShuffleVectorSDNode>(N0);
10118
Nadav Rotemd16c8d02012-04-07 21:19:08 +000010119 // Shuffle nodes can only reverse shuffles with a single non-undef value.
10120 if (N0.getOperand(1).getOpcode() != ISD::UNDEF)
10121 return SDValue();
10122
Craig Topperae1bec52012-04-09 05:16:56 +000010123 // The incoming shuffle must be of the same type as the result of the
10124 // current shuffle.
10125 assert(OtherSV->getOperand(0).getValueType() == VT &&
10126 "Shuffle types don't match");
Nadav Rotem4ac90812012-04-01 19:31:22 +000010127
10128 for (unsigned i = 0; i != NumElts; ++i) {
10129 int Idx = SVN->getMaskElt(i);
Craig Topperae1bec52012-04-09 05:16:56 +000010130 assert(Idx < (int)NumElts && "Index references undef operand");
Nadav Rotem4ac90812012-04-01 19:31:22 +000010131 // Next, this index comes from the first value, which is the incoming
10132 // shuffle. Adopt the incoming index.
10133 if (Idx >= 0)
10134 Idx = OtherSV->getMaskElt(Idx);
10135
Nadav Rotemd16c8d02012-04-07 21:19:08 +000010136 // The combined shuffle must map each index to itself.
Craig Topperae1bec52012-04-09 05:16:56 +000010137 if (Idx >= 0 && (unsigned)Idx != i)
Nadav Rotemd16c8d02012-04-07 21:19:08 +000010138 return SDValue();
Nadav Rotem4ac90812012-04-01 19:31:22 +000010139 }
Nadav Rotemd16c8d02012-04-07 21:19:08 +000010140
10141 return OtherSV->getOperand(0);
Nadav Rotem4ac90812012-04-01 19:31:22 +000010142 }
10143
Dan Gohman475871a2008-07-27 21:46:04 +000010144 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +000010145}
10146
Evan Cheng44f1f092006-04-20 08:56:16 +000010147/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +000010148/// an AND to a vector_shuffle with the destination vector and a zero vector.
10149/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +000010150/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +000010151SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +000010152 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +000010153 SDLoc dl(N);
Dan Gohman475871a2008-07-27 21:46:04 +000010154 SDValue LHS = N->getOperand(0);
10155 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +000010156 if (N->getOpcode() == ISD::AND) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010157 if (RHS.getOpcode() == ISD::BITCAST)
Evan Cheng44f1f092006-04-20 08:56:16 +000010158 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +000010159 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Nate Begeman9008ca62009-04-27 18:41:29 +000010160 SmallVector<int, 8> Indices;
10161 unsigned NumElts = RHS.getNumOperands();
Evan Cheng44f1f092006-04-20 08:56:16 +000010162 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +000010163 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +000010164 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +000010165 return SDValue();
Craig Topperb7135e52012-04-09 05:59:53 +000010166
10167 if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Nate Begeman9008ca62009-04-27 18:41:29 +000010168 Indices.push_back(i);
Evan Cheng44f1f092006-04-20 08:56:16 +000010169 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Nate Begeman9008ca62009-04-27 18:41:29 +000010170 Indices.push_back(NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +000010171 else
Dan Gohman475871a2008-07-27 21:46:04 +000010172 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +000010173 }
10174
10175 // Let's see if the target supports this vector_shuffle.
Owen Andersone50ed302009-08-10 22:56:29 +000010176 EVT RVT = RHS.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +000010177 if (!TLI.isVectorClearMaskLegal(Indices, RVT))
Dan Gohman475871a2008-07-27 21:46:04 +000010178 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +000010179
Dan Gohman7f321562007-06-25 16:23:39 +000010180 // Return the new VECTOR_SHUFFLE node.
Dan Gohman8a55ce42009-09-23 21:02:20 +000010181 EVT EltVT = RVT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +000010182 SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(),
Dan Gohman8a55ce42009-09-23 21:02:20 +000010183 DAG.getConstant(0, EltVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +000010184 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Nate Begeman9008ca62009-04-27 18:41:29 +000010185 RVT, &ZeroOps[0], ZeroOps.size());
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010186 LHS = DAG.getNode(ISD::BITCAST, dl, RVT, LHS);
Nate Begeman9008ca62009-04-27 18:41:29 +000010187 SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010188 return DAG.getNode(ISD::BITCAST, dl, VT, Shuf);
Evan Cheng44f1f092006-04-20 08:56:16 +000010189 }
10190 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010191
Dan Gohman475871a2008-07-27 21:46:04 +000010192 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +000010193}
10194
Dan Gohman7f321562007-06-25 16:23:39 +000010195/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +000010196SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Bob Wilsond7273432010-12-17 23:06:49 +000010197 assert(N->getValueType(0).isVector() &&
10198 "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +000010199
Dan Gohman475871a2008-07-27 21:46:04 +000010200 SDValue LHS = N->getOperand(0);
10201 SDValue RHS = N->getOperand(1);
10202 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +000010203 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +000010204
Dan Gohman7f321562007-06-25 16:23:39 +000010205 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +000010206 // this operation.
Scott Michelfdc40a02009-02-17 22:15:04 +000010207 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
Dan Gohman7f321562007-06-25 16:23:39 +000010208 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +000010209 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +000010210 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +000010211 SDValue LHSOp = LHS.getOperand(i);
10212 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +000010213 // If these two elements can't be folded, bail out.
10214 if ((LHSOp.getOpcode() != ISD::UNDEF &&
10215 LHSOp.getOpcode() != ISD::Constant &&
10216 LHSOp.getOpcode() != ISD::ConstantFP) ||
10217 (RHSOp.getOpcode() != ISD::UNDEF &&
10218 RHSOp.getOpcode() != ISD::Constant &&
10219 RHSOp.getOpcode() != ISD::ConstantFP))
10220 break;
Bill Wendling836ca7d2009-01-30 23:59:18 +000010221
Evan Cheng7b336a82006-05-31 06:08:35 +000010222 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +000010223 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
10224 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +000010225 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +000010226 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +000010227 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +000010228 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +000010229 break;
10230 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010231
Bob Wilsond7273432010-12-17 23:06:49 +000010232 EVT VT = LHSOp.getValueType();
Bob Wilsondb2b18f2011-10-18 17:34:47 +000010233 EVT RVT = RHSOp.getValueType();
10234 if (RVT != VT) {
10235 // Integer BUILD_VECTOR operands may have types larger than the element
10236 // size (e.g., when the element type is not legal). Prior to type
10237 // legalization, the types may not match between the two BUILD_VECTORS.
10238 // Truncate one of the operands to make them match.
10239 if (RVT.getSizeInBits() > VT.getSizeInBits()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010240 RHSOp = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, RHSOp);
Bob Wilsondb2b18f2011-10-18 17:34:47 +000010241 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010242 LHSOp = DAG.getNode(ISD::TRUNCATE, SDLoc(N), RVT, LHSOp);
Bob Wilsondb2b18f2011-10-18 17:34:47 +000010243 VT = RVT;
10244 }
10245 }
Andrew Trickac6d9be2013-05-25 02:42:55 +000010246 SDValue FoldOp = DAG.getNode(N->getOpcode(), SDLoc(LHS), VT,
Evan Chenga0839882010-05-18 00:03:40 +000010247 LHSOp, RHSOp);
10248 if (FoldOp.getOpcode() != ISD::UNDEF &&
10249 FoldOp.getOpcode() != ISD::Constant &&
10250 FoldOp.getOpcode() != ISD::ConstantFP)
10251 break;
10252 Ops.push_back(FoldOp);
10253 AddToWorkList(FoldOp.getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +000010254 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010255
Bob Wilsond7273432010-12-17 23:06:49 +000010256 if (Ops.size() == LHS.getNumOperands())
Andrew Trickac6d9be2013-05-25 02:42:55 +000010257 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Bob Wilsond7273432010-12-17 23:06:49 +000010258 LHS.getValueType(), &Ops[0], Ops.size());
Chris Lattneredab1b92006-04-02 03:25:57 +000010259 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010260
Dan Gohman475871a2008-07-27 21:46:04 +000010261 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +000010262}
10263
Craig Topperdd201ff2012-09-11 01:45:21 +000010264/// SimplifyVUnaryOp - Visit a binary vector operation, like FABS/FNEG.
10265SDValue DAGCombiner::SimplifyVUnaryOp(SDNode *N) {
Craig Topperdd201ff2012-09-11 01:45:21 +000010266 assert(N->getValueType(0).isVector() &&
10267 "SimplifyVUnaryOp only works on vectors!");
10268
10269 SDValue N0 = N->getOperand(0);
10270
10271 if (N0.getOpcode() != ISD::BUILD_VECTOR)
10272 return SDValue();
10273
10274 // Operand is a BUILD_VECTOR node, see if we can constant fold it.
10275 SmallVector<SDValue, 8> Ops;
10276 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) {
10277 SDValue Op = N0.getOperand(i);
10278 if (Op.getOpcode() != ISD::UNDEF &&
10279 Op.getOpcode() != ISD::ConstantFP)
10280 break;
10281 EVT EltVT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +000010282 SDValue FoldOp = DAG.getNode(N->getOpcode(), SDLoc(N0), EltVT, Op);
Craig Topperdd201ff2012-09-11 01:45:21 +000010283 if (FoldOp.getOpcode() != ISD::UNDEF &&
10284 FoldOp.getOpcode() != ISD::ConstantFP)
10285 break;
10286 Ops.push_back(FoldOp);
10287 AddToWorkList(FoldOp.getNode());
10288 }
10289
10290 if (Ops.size() != N0.getNumOperands())
10291 return SDValue();
10292
Andrew Trickac6d9be2013-05-25 02:42:55 +000010293 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Craig Topperdd201ff2012-09-11 01:45:21 +000010294 N0.getValueType(), &Ops[0], Ops.size());
10295}
10296
Andrew Trickac6d9be2013-05-25 02:42:55 +000010297SDValue DAGCombiner::SimplifySelect(SDLoc DL, SDValue N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +000010298 SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +000010299 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
Scott Michelfdc40a02009-02-17 22:15:04 +000010300
Bill Wendling836ca7d2009-01-30 23:59:18 +000010301 SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +000010302 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Bill Wendling836ca7d2009-01-30 23:59:18 +000010303
Nate Begemanf845b452005-10-08 00:29:44 +000010304 // If we got a simplified select_cc node back from SimplifySelectCC, then
10305 // break it down into a new SETCC node, and a new SELECT node, and then return
10306 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +000010307 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +000010308 // Check to see if we got a select_cc back (to turn into setcc/select).
10309 // Otherwise, just return whatever node we got back, like fabs.
10310 if (SCC.getOpcode() == ISD::SELECT_CC) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010311 SDValue SETCC = DAG.getNode(ISD::SETCC, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +000010312 N0.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +000010313 SCC.getOperand(0), SCC.getOperand(1),
Bill Wendling836ca7d2009-01-30 23:59:18 +000010314 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +000010315 AddToWorkList(SETCC.getNode());
Matt Arsenaultb05e4772013-06-14 22:04:37 +000010316 return DAG.getSelect(SDLoc(SCC), SCC.getValueType(),
10317 SCC.getOperand(2), SCC.getOperand(3), SETCC);
Nate Begemanf845b452005-10-08 00:29:44 +000010318 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010319
Nate Begemanf845b452005-10-08 00:29:44 +000010320 return SCC;
10321 }
Dan Gohman475871a2008-07-27 21:46:04 +000010322 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +000010323}
10324
Chris Lattner40c62d52005-10-18 06:04:22 +000010325/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
10326/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +000010327/// select. Callers of this should assume that TheSelect is deleted if this
10328/// returns true. As such, they should return the appropriate thing (e.g. the
10329/// node) back to the top-level of the DAG combiner loop to avoid it being
10330/// looked at.
Scott Michelfdc40a02009-02-17 22:15:04 +000010331bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
Dan Gohman475871a2008-07-27 21:46:04 +000010332 SDValue RHS) {
Scott Michelfdc40a02009-02-17 22:15:04 +000010333
Nadav Rotemf94fdb62011-02-11 19:57:47 +000010334 // Cannot simplify select with vector condition
10335 if (TheSelect->getOperand(0).getValueType().isVector()) return false;
10336
Chris Lattner40c62d52005-10-18 06:04:22 +000010337 // If this is a select from two identical things, try to pull the operation
10338 // through the select.
Chris Lattner18061612010-09-21 15:46:59 +000010339 if (LHS.getOpcode() != RHS.getOpcode() ||
10340 !LHS.hasOneUse() || !RHS.hasOneUse())
10341 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010342
Chris Lattner18061612010-09-21 15:46:59 +000010343 // If this is a load and the token chain is identical, replace the select
10344 // of two loads with a load through a select of the address to load from.
10345 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
10346 // constants have been dropped into the constant pool.
10347 if (LHS.getOpcode() == ISD::LOAD) {
10348 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
10349 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010350
Chris Lattner18061612010-09-21 15:46:59 +000010351 // Token chains must be identical.
10352 if (LHS.getOperand(0) != RHS.getOperand(0) ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +000010353 // Do not let this transformation reduce the number of volatile loads.
Chris Lattner18061612010-09-21 15:46:59 +000010354 LLD->isVolatile() || RLD->isVolatile() ||
10355 // If this is an EXTLOAD, the VT's must match.
10356 LLD->getMemoryVT() != RLD->getMemoryVT() ||
Duncan Sandsdcfd3a72010-11-18 20:05:18 +000010357 // If this is an EXTLOAD, the kind of extension must match.
10358 (LLD->getExtensionType() != RLD->getExtensionType() &&
10359 // The only exception is if one of the extensions is anyext.
10360 LLD->getExtensionType() != ISD::EXTLOAD &&
10361 RLD->getExtensionType() != ISD::EXTLOAD) ||
Dan Gohman75832d72009-10-31 14:14:04 +000010362 // FIXME: this discards src value information. This is
10363 // over-conservative. It would be beneficial to be able to remember
Mon P Wangfe240b12010-01-11 20:12:49 +000010364 // both potential memory locations. Since we are discarding
10365 // src value info, don't do the transformation if the memory
10366 // locations are not in the default address space.
Chris Lattner18061612010-09-21 15:46:59 +000010367 LLD->getPointerInfo().getAddrSpace() != 0 ||
Pete Cooperb0fde6d2013-02-12 03:14:50 +000010368 RLD->getPointerInfo().getAddrSpace() != 0 ||
10369 !TLI.isOperationLegalOrCustom(TheSelect->getOpcode(),
10370 LLD->getBasePtr().getValueType()))
Chris Lattner18061612010-09-21 15:46:59 +000010371 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010372
Chris Lattnerf1658062010-09-21 15:58:55 +000010373 // Check that the select condition doesn't reach either load. If so,
10374 // folding this will induce a cycle into the DAG. If not, this is safe to
10375 // xform, so create a select of the addresses.
Chris Lattner18061612010-09-21 15:46:59 +000010376 SDValue Addr;
10377 if (TheSelect->getOpcode() == ISD::SELECT) {
Chris Lattnerf1658062010-09-21 15:58:55 +000010378 SDNode *CondNode = TheSelect->getOperand(0).getNode();
10379 if ((LLD->hasAnyUseOfValue(1) && LLD->isPredecessorOf(CondNode)) ||
10380 (RLD->hasAnyUseOfValue(1) && RLD->isPredecessorOf(CondNode)))
10381 return false;
Nadav Rotem1c5bf3f2012-10-18 18:06:48 +000010382 // The loads must not depend on one another.
10383 if (LLD->isPredecessorOf(RLD) ||
10384 RLD->isPredecessorOf(LLD))
10385 return false;
Matt Arsenaultb05e4772013-06-14 22:04:37 +000010386 Addr = DAG.getSelect(SDLoc(TheSelect),
10387 LLD->getBasePtr().getValueType(),
10388 TheSelect->getOperand(0), LLD->getBasePtr(),
10389 RLD->getBasePtr());
Chris Lattner18061612010-09-21 15:46:59 +000010390 } else { // Otherwise SELECT_CC
Chris Lattnerf1658062010-09-21 15:58:55 +000010391 SDNode *CondLHS = TheSelect->getOperand(0).getNode();
10392 SDNode *CondRHS = TheSelect->getOperand(1).getNode();
10393
10394 if ((LLD->hasAnyUseOfValue(1) &&
10395 (LLD->isPredecessorOf(CondLHS) || LLD->isPredecessorOf(CondRHS))) ||
Chris Lattner77d95212012-03-27 16:27:21 +000010396 (RLD->hasAnyUseOfValue(1) &&
10397 (RLD->isPredecessorOf(CondLHS) || RLD->isPredecessorOf(CondRHS))))
Chris Lattnerf1658062010-09-21 15:58:55 +000010398 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010399
Andrew Trickac6d9be2013-05-25 02:42:55 +000010400 Addr = DAG.getNode(ISD::SELECT_CC, SDLoc(TheSelect),
Chris Lattnerf1658062010-09-21 15:58:55 +000010401 LLD->getBasePtr().getValueType(),
10402 TheSelect->getOperand(0),
10403 TheSelect->getOperand(1),
10404 LLD->getBasePtr(), RLD->getBasePtr(),
10405 TheSelect->getOperand(4));
Chris Lattner18061612010-09-21 15:46:59 +000010406 }
10407
Chris Lattnerf1658062010-09-21 15:58:55 +000010408 SDValue Load;
10409 if (LLD->getExtensionType() == ISD::NON_EXTLOAD) {
10410 Load = DAG.getLoad(TheSelect->getValueType(0),
Andrew Trickac6d9be2013-05-25 02:42:55 +000010411 SDLoc(TheSelect),
Chris Lattnerf1658062010-09-21 15:58:55 +000010412 // FIXME: Discards pointer info.
10413 LLD->getChain(), Addr, MachinePointerInfo(),
10414 LLD->isVolatile(), LLD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010415 LLD->isInvariant(), LLD->getAlignment());
Chris Lattnerf1658062010-09-21 15:58:55 +000010416 } else {
Duncan Sandsb9064bb2010-11-18 21:16:28 +000010417 Load = DAG.getExtLoad(LLD->getExtensionType() == ISD::EXTLOAD ?
10418 RLD->getExtensionType() : LLD->getExtensionType(),
Andrew Trickac6d9be2013-05-25 02:42:55 +000010419 SDLoc(TheSelect),
Stuart Hastingsa9011292011-02-16 16:23:55 +000010420 TheSelect->getValueType(0),
Chris Lattnerf1658062010-09-21 15:58:55 +000010421 // FIXME: Discards pointer info.
10422 LLD->getChain(), Addr, MachinePointerInfo(),
10423 LLD->getMemoryVT(), LLD->isVolatile(),
10424 LLD->isNonTemporal(), LLD->getAlignment());
Chris Lattner40c62d52005-10-18 06:04:22 +000010425 }
Chris Lattnerf1658062010-09-21 15:58:55 +000010426
10427 // Users of the select now use the result of the load.
10428 CombineTo(TheSelect, Load);
10429
10430 // Users of the old loads now use the new load's chain. We know the
10431 // old-load value is dead now.
10432 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
10433 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
10434 return true;
Chris Lattner40c62d52005-10-18 06:04:22 +000010435 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010436
Chris Lattner40c62d52005-10-18 06:04:22 +000010437 return false;
10438}
10439
Chris Lattner600fec32009-03-11 05:08:08 +000010440/// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3
10441/// where 'cond' is the comparison specified by CC.
Andrew Trickac6d9be2013-05-25 02:42:55 +000010442SDValue DAGCombiner::SimplifySelectCC(SDLoc DL, SDValue N0, SDValue N1,
Dan Gohman475871a2008-07-27 21:46:04 +000010443 SDValue N2, SDValue N3,
10444 ISD::CondCode CC, bool NotExtCompare) {
Chris Lattner600fec32009-03-11 05:08:08 +000010445 // (x ? y : y) -> y.
10446 if (N2 == N3) return N2;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010447
Owen Andersone50ed302009-08-10 22:56:29 +000010448 EVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +000010449 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
10450 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
10451 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +000010452
10453 // Determine if the condition we're dealing with is constant
Matt Arsenault225ed702013-05-18 00:21:46 +000010454 SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +000010455 N0, N1, CC, DL, false);
Gabor Greifba36cb52008-08-28 21:40:38 +000010456 if (SCC.getNode()) AddToWorkList(SCC.getNode());
10457 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +000010458
10459 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +000010460 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +000010461 return N2;
10462 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +000010463 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +000010464 return N3;
Scott Michelfdc40a02009-02-17 22:15:04 +000010465
Nate Begemanf845b452005-10-08 00:29:44 +000010466 // Check to see if we can simplify the select into an fabs node
10467 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
10468 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +000010469 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +000010470 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
10471 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
10472 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
10473 N2 == N3.getOperand(0))
Bill Wendling836ca7d2009-01-30 23:59:18 +000010474 return DAG.getNode(ISD::FABS, DL, VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +000010475
Nate Begemanf845b452005-10-08 00:29:44 +000010476 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
10477 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
10478 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
10479 N2.getOperand(0) == N3)
Bill Wendling836ca7d2009-01-30 23:59:18 +000010480 return DAG.getNode(ISD::FABS, DL, VT, N3);
Nate Begemanf845b452005-10-08 00:29:44 +000010481 }
10482 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010483
Chris Lattner600fec32009-03-11 05:08:08 +000010484 // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)"
10485 // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0
10486 // in it. This is a win when the constant is not otherwise available because
10487 // it replaces two constant pool loads with one. We only do this if the FP
10488 // type is known to be legal, because if it isn't, then we are before legalize
10489 // types an we want the other legalization to happen first (e.g. to avoid
Mon P Wang0b7a7862009-03-14 00:25:19 +000010490 // messing with soft float) and if the ConstantFP is not legal, because if
10491 // it is legal, we may not need to store the FP constant in a constant pool.
Chris Lattner600fec32009-03-11 05:08:08 +000010492 if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2))
10493 if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) {
10494 if (TLI.isTypeLegal(N2.getValueType()) &&
Mon P Wang0b7a7862009-03-14 00:25:19 +000010495 (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) !=
10496 TargetLowering::Legal) &&
Chris Lattner600fec32009-03-11 05:08:08 +000010497 // If both constants have multiple uses, then we won't need to do an
10498 // extra load, they are likely around in registers for other users.
10499 (TV->hasOneUse() || FV->hasOneUse())) {
10500 Constant *Elts[] = {
10501 const_cast<ConstantFP*>(FV->getConstantFPValue()),
10502 const_cast<ConstantFP*>(TV->getConstantFPValue())
10503 };
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010504 Type *FPTy = Elts[0]->getType();
Micah Villmow3574eca2012-10-08 16:38:25 +000010505 const DataLayout &TD = *TLI.getDataLayout();
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010506
Chris Lattner600fec32009-03-11 05:08:08 +000010507 // Create a ConstantArray of the two constants.
Jay Foad26701082011-06-22 09:24:39 +000010508 Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts);
Chris Lattner600fec32009-03-11 05:08:08 +000010509 SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(),
10510 TD.getPrefTypeAlignment(FPTy));
Evan Cheng1606e8e2009-03-13 07:51:59 +000010511 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner600fec32009-03-11 05:08:08 +000010512
10513 // Get the offsets to the 0 and 1 element of the array so that we can
10514 // select between them.
10515 SDValue Zero = DAG.getIntPtrConstant(0);
Duncan Sands777d2302009-05-09 07:06:46 +000010516 unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType());
Chris Lattner600fec32009-03-11 05:08:08 +000010517 SDValue One = DAG.getIntPtrConstant(EltSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010518
Chris Lattner600fec32009-03-11 05:08:08 +000010519 SDValue Cond = DAG.getSetCC(DL,
Matt Arsenault225ed702013-05-18 00:21:46 +000010520 getSetCCResultType(N0.getValueType()),
Chris Lattner600fec32009-03-11 05:08:08 +000010521 N0, N1, CC);
Dan Gohman7b316c92011-09-22 23:01:29 +000010522 AddToWorkList(Cond.getNode());
Matt Arsenaultb05e4772013-06-14 22:04:37 +000010523 SDValue CstOffset = DAG.getSelect(DL, Zero.getValueType(),
10524 Cond, One, Zero);
Dan Gohman7b316c92011-09-22 23:01:29 +000010525 AddToWorkList(CstOffset.getNode());
Tom Stellardedd08f72013-08-26 15:06:10 +000010526 CPIdx = DAG.getNode(ISD::ADD, DL, CPIdx.getValueType(), CPIdx,
Chris Lattner600fec32009-03-11 05:08:08 +000010527 CstOffset);
Dan Gohman7b316c92011-09-22 23:01:29 +000010528 AddToWorkList(CPIdx.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +000010529 return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +000010530 MachinePointerInfo::getConstantPool(), false,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010531 false, false, Alignment);
Chris Lattner600fec32009-03-11 05:08:08 +000010532
10533 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010534 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010535
Nate Begemanf845b452005-10-08 00:29:44 +000010536 // Check to see if we can perform the "gzip trick", transforming
Bill Wendling836ca7d2009-01-30 23:59:18 +000010537 // (select_cc setlt X, 0, A, 0) -> (and (sra X, (sub size(X), 1), A)
Chris Lattnere3152e52006-09-20 06:41:35 +000010538 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +000010539 (N1C->isNullValue() || // (a < 0) ? b : 0
10540 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Owen Andersone50ed302009-08-10 22:56:29 +000010541 EVT XType = N0.getValueType();
10542 EVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +000010543 if (XType.bitsGE(AType)) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +000010544 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +000010545 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +000010546 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
10547 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +000010548 ShCtV = XType.getSizeInBits()-ShCtV-1;
Owen Anderson95771af2011-02-25 21:41:48 +000010549 SDValue ShCt = DAG.getConstant(ShCtV,
10550 getShiftAmountTy(N0.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +000010551 SDValue Shift = DAG.getNode(ISD::SRL, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +000010552 XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +000010553 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +000010554
Duncan Sands8e4eb092008-06-08 20:54:56 +000010555 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +000010556 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +000010557 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +000010558 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010559
10560 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +000010561 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010562
Andrew Trickac6d9be2013-05-25 02:42:55 +000010563 SDValue Shift = DAG.getNode(ISD::SRA, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +000010564 XType, N0,
10565 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +000010566 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +000010567 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +000010568
Duncan Sands8e4eb092008-06-08 20:54:56 +000010569 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +000010570 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +000010571 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +000010572 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010573
10574 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +000010575 }
10576 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010577
Owen Andersoned1088a2010-09-22 22:58:22 +000010578 // fold (select_cc seteq (and x, y), 0, 0, A) -> (and (shr (shl x)) A)
10579 // where y is has a single bit set.
10580 // A plaintext description would be, we can turn the SELECT_CC into an AND
10581 // when the condition can be materialized as an all-ones register. Any
10582 // single bit-test can be materialized as an all-ones register with
10583 // shift-left and shift-right-arith.
10584 if (CC == ISD::SETEQ && N0->getOpcode() == ISD::AND &&
10585 N0->getValueType(0) == VT &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010586 N1C && N1C->isNullValue() &&
Owen Andersoned1088a2010-09-22 22:58:22 +000010587 N2C && N2C->isNullValue()) {
10588 SDValue AndLHS = N0->getOperand(0);
10589 ConstantSDNode *ConstAndRHS = dyn_cast<ConstantSDNode>(N0->getOperand(1));
10590 if (ConstAndRHS && ConstAndRHS->getAPIntValue().countPopulation() == 1) {
10591 // Shift the tested bit over the sign bit.
10592 APInt AndMask = ConstAndRHS->getAPIntValue();
10593 SDValue ShlAmt =
Owen Anderson95771af2011-02-25 21:41:48 +000010594 DAG.getConstant(AndMask.countLeadingZeros(),
10595 getShiftAmountTy(AndLHS.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +000010596 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(N0), VT, AndLHS, ShlAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010597
Owen Andersoned1088a2010-09-22 22:58:22 +000010598 // Now arithmetic right shift it all the way over, so the result is either
10599 // all-ones, or zero.
10600 SDValue ShrAmt =
Owen Anderson95771af2011-02-25 21:41:48 +000010601 DAG.getConstant(AndMask.getBitWidth()-1,
10602 getShiftAmountTy(Shl.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +000010603 SDValue Shr = DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl, ShrAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010604
Owen Andersoned1088a2010-09-22 22:58:22 +000010605 return DAG.getNode(ISD::AND, DL, VT, Shr, N3);
10606 }
10607 }
10608
Nate Begeman07ed4172005-10-10 21:26:48 +000010609 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +000010610 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands28b77e92011-09-06 19:07:46 +000010611 TLI.getBooleanContents(N0.getValueType().isVector()) ==
10612 TargetLowering::ZeroOrOneBooleanContent) {
Scott Michelfdc40a02009-02-17 22:15:04 +000010613
Chris Lattner1eba01e2007-04-11 06:50:51 +000010614 // If the caller doesn't want us to simplify this into a zext of a compare,
10615 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +000010616 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +000010617 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000010618
Nate Begeman07ed4172005-10-10 21:26:48 +000010619 // Get a SetCC of the condition
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010620 // NOTE: Don't create a SETCC if it's not legal on this target.
10621 if (!LegalOperations ||
10622 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault225ed702013-05-18 00:21:46 +000010623 LegalTypes ? getSetCCResultType(N0.getValueType()) : MVT::i1)) {
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010624 SDValue Temp, SCC;
10625 // cast from setcc result type to select result type
10626 if (LegalTypes) {
Matt Arsenault225ed702013-05-18 00:21:46 +000010627 SCC = DAG.getSetCC(DL, getSetCCResultType(N0.getValueType()),
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010628 N0, N1, CC);
10629 if (N2.getValueType().bitsLT(SCC.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +000010630 Temp = DAG.getZeroExtendInReg(SCC, SDLoc(N2),
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010631 N2.getValueType());
10632 else
Andrew Trickac6d9be2013-05-25 02:42:55 +000010633 Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2),
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010634 N2.getValueType(), SCC);
10635 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010636 SCC = DAG.getSetCC(SDLoc(N0), MVT::i1, N0, N1, CC);
10637 Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2),
Bill Wendling836ca7d2009-01-30 23:59:18 +000010638 N2.getValueType(), SCC);
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010639 }
10640
10641 AddToWorkList(SCC.getNode());
10642 AddToWorkList(Temp.getNode());
10643
10644 if (N2C->getAPIntValue() == 1)
10645 return Temp;
10646
10647 // shl setcc result by log2 n2c
Jack Carteradbd3ae2013-10-17 01:34:33 +000010648 return DAG.getNode(
10649 ISD::SHL, DL, N2.getValueType(), Temp,
10650 DAG.getConstant(N2C->getAPIntValue().logBase2(),
10651 getShiftAmountTy(Temp.getValueType())));
Nate Begemanb0d04a72006-02-18 02:40:58 +000010652 }
Nate Begeman07ed4172005-10-10 21:26:48 +000010653 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010654
Nate Begemanf845b452005-10-08 00:29:44 +000010655 // Check to see if this is the equivalent of setcc
10656 // FIXME: Turn all of these into setcc if setcc if setcc is legal
10657 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +000010658 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Owen Andersone50ed302009-08-10 22:56:29 +000010659 EVT XType = N0.getValueType();
Duncan Sands25cf2272008-11-24 14:53:14 +000010660 if (!LegalOperations ||
Matt Arsenault225ed702013-05-18 00:21:46 +000010661 TLI.isOperationLegal(ISD::SETCC, getSetCCResultType(XType))) {
10662 SDValue Res = DAG.getSetCC(DL, getSetCCResultType(XType), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +000010663 if (Res.getValueType() != VT)
Bill Wendling836ca7d2009-01-30 23:59:18 +000010664 Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res);
Nate Begemanf845b452005-10-08 00:29:44 +000010665 return Res;
10666 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010667
Bill Wendling836ca7d2009-01-30 23:59:18 +000010668 // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X))))
Scott Michelfdc40a02009-02-17 22:15:04 +000010669 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands25cf2272008-11-24 14:53:14 +000010670 (!LegalOperations ||
Duncan Sands184a8762008-06-14 17:48:34 +000010671 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010672 SDValue Ctlz = DAG.getNode(ISD::CTLZ, SDLoc(N0), XType, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +000010673 return DAG.getNode(ISD::SRL, DL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +000010674 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Owen Anderson95771af2011-02-25 21:41:48 +000010675 getShiftAmountTy(Ctlz.getValueType())));
Nate Begemanf845b452005-10-08 00:29:44 +000010676 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010677 // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1))
Scott Michelfdc40a02009-02-17 22:15:04 +000010678 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010679 SDValue NegN0 = DAG.getNode(ISD::SUB, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +000010680 XType, DAG.getConstant(0, XType), N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +000010681 SDValue NotN0 = DAG.getNOT(SDLoc(N0), N0, XType);
Bill Wendling836ca7d2009-01-30 23:59:18 +000010682 return DAG.getNode(ISD::SRL, DL, XType,
Bill Wendlingfc4b6772009-02-01 11:19:36 +000010683 DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +000010684 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +000010685 getShiftAmountTy(XType)));
Nate Begemanf845b452005-10-08 00:29:44 +000010686 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010687 // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1))
Nate Begemanf845b452005-10-08 00:29:44 +000010688 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010689 SDValue Sign = DAG.getNode(ISD::SRL, SDLoc(N0), XType, N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +000010690 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +000010691 getShiftAmountTy(N0.getValueType())));
Bill Wendling836ca7d2009-01-30 23:59:18 +000010692 return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType));
Nate Begemanf845b452005-10-08 00:29:44 +000010693 }
10694 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010695
Benjamin Kramercde51102010-07-08 12:09:56 +000010696 // Check to see if this is an integer abs.
10697 // select_cc setg[te] X, 0, X, -X ->
10698 // select_cc setgt X, -1, X, -X ->
10699 // select_cc setl[te] X, 0, -X, X ->
10700 // select_cc setlt X, 1, -X, X ->
Nate Begemanf845b452005-10-08 00:29:44 +000010701 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
Benjamin Kramercde51102010-07-08 12:09:56 +000010702 if (N1C) {
10703 ConstantSDNode *SubC = NULL;
10704 if (((N1C->isNullValue() && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
10705 (N1C->isAllOnesValue() && CC == ISD::SETGT)) &&
10706 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1))
10707 SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0));
10708 else if (((N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE)) ||
10709 (N1C->isOne() && CC == ISD::SETLT)) &&
10710 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1))
10711 SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0));
10712
Owen Andersone50ed302009-08-10 22:56:29 +000010713 EVT XType = N0.getValueType();
Benjamin Kramercde51102010-07-08 12:09:56 +000010714 if (SubC && SubC->isNullValue() && XType.isInteger()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010715 SDValue Shift = DAG.getNode(ISD::SRA, SDLoc(N0), XType,
Benjamin Kramercde51102010-07-08 12:09:56 +000010716 N0,
10717 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +000010718 getShiftAmountTy(N0.getValueType())));
Andrew Trickac6d9be2013-05-25 02:42:55 +000010719 SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N0),
Benjamin Kramercde51102010-07-08 12:09:56 +000010720 XType, N0, Shift);
10721 AddToWorkList(Shift.getNode());
10722 AddToWorkList(Add.getNode());
10723 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Nate Begemanf845b452005-10-08 00:29:44 +000010724 }
10725 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010726
Dan Gohman475871a2008-07-27 21:46:04 +000010727 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +000010728}
10729
Evan Chengfa1eb272007-02-08 22:13:59 +000010730/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Owen Andersone50ed302009-08-10 22:56:29 +000010731SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0,
Dan Gohman475871a2008-07-27 21:46:04 +000010732 SDValue N1, ISD::CondCode Cond,
Andrew Trickac6d9be2013-05-25 02:42:55 +000010733 SDLoc DL, bool foldBooleans) {
Scott Michelfdc40a02009-02-17 22:15:04 +000010734 TargetLowering::DAGCombinerInfo
Nadav Rotem444b4bf2012-12-27 06:47:41 +000010735 DagCombineInfo(DAG, Level, false, this);
Dale Johannesenff97d4f2009-02-03 00:47:48 +000010736 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL);
Nate Begeman452d7be2005-09-16 00:54:12 +000010737}
10738
Nate Begeman69575232005-10-20 02:15:44 +000010739/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
10740/// return a DAG expression to select that will generate the same value by
10741/// multiplying by a magic number. See:
10742/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +000010743SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +000010744 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +000010745 SDValue S = TLI.BuildSDIV(N, DAG, LegalOperations, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010746
Andrew Lenharth232c9102006-06-12 16:07:18 +000010747 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010748 ii != ee; ++ii)
10749 AddToWorkList(*ii);
10750 return S;
Nate Begeman69575232005-10-20 02:15:44 +000010751}
10752
10753/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
10754/// return a DAG expression to select that will generate the same value by
10755/// multiplying by a magic number. See:
10756/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +000010757SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +000010758 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +000010759 SDValue S = TLI.BuildUDIV(N, DAG, LegalOperations, &Built);
Nate Begeman69575232005-10-20 02:15:44 +000010760
Andrew Lenharth232c9102006-06-12 16:07:18 +000010761 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010762 ii != ee; ++ii)
10763 AddToWorkList(*ii);
10764 return S;
Nate Begeman69575232005-10-20 02:15:44 +000010765}
10766
Nate Begemancc66cdd2009-09-25 06:05:26 +000010767/// FindBaseOffset - Return true if base is a frame index, which is known not
Eric Christopher503a64d2010-12-09 04:48:06 +000010768// to alias with anything but itself. Provides base object and offset as
10769// results.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010770static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset,
Roman Divacky2943e372012-09-05 22:15:49 +000010771 const GlobalValue *&GV, const void *&CV) {
Jim Laskey71382342006-10-07 23:37:56 +000010772 // Assume it is a primitive operation.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010773 Base = Ptr; Offset = 0; GV = 0; CV = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000010774
Jim Laskey71382342006-10-07 23:37:56 +000010775 // If it's an adding a simple constant then integrate the offset.
10776 if (Base.getOpcode() == ISD::ADD) {
10777 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
10778 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010779 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +000010780 }
10781 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010782
Nate Begemancc66cdd2009-09-25 06:05:26 +000010783 // Return the underlying GlobalValue, and update the Offset. Return false
10784 // for GlobalAddressSDNode since the same GlobalAddress may be represented
10785 // by multiple nodes with different offsets.
10786 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Base)) {
10787 GV = G->getGlobal();
10788 Offset += G->getOffset();
10789 return false;
10790 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010791
Nate Begemancc66cdd2009-09-25 06:05:26 +000010792 // Return the underlying Constant value, and update the Offset. Return false
10793 // for ConstantSDNodes since the same constant pool entry may be represented
10794 // by multiple nodes with different offsets.
10795 if (ConstantPoolSDNode *C = dyn_cast<ConstantPoolSDNode>(Base)) {
Roman Divacky2943e372012-09-05 22:15:49 +000010796 CV = C->isMachineConstantPoolEntry() ? (const void *)C->getMachineCPVal()
10797 : (const void *)C->getConstVal();
Nate Begemancc66cdd2009-09-25 06:05:26 +000010798 Offset += C->getOffset();
10799 return false;
10800 }
Jim Laskey71382342006-10-07 23:37:56 +000010801 // If it's any of the following then it can't alias with anything but itself.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010802 return isa<FrameIndexSDNode>(Base);
Jim Laskey71382342006-10-07 23:37:56 +000010803}
10804
10805/// isAlias - Return true if there is any possibility that the two addresses
10806/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +000010807bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +000010808 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010809 unsigned SrcValueAlign1,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010810 const MDNode *TBAAInfo1,
Dan Gohman475871a2008-07-27 21:46:04 +000010811 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010812 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010813 unsigned SrcValueAlign2,
10814 const MDNode *TBAAInfo2) const {
Jim Laskey71382342006-10-07 23:37:56 +000010815 // If they are the same then they must be aliases.
10816 if (Ptr1 == Ptr2) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +000010817
Jim Laskey71382342006-10-07 23:37:56 +000010818 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +000010819 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +000010820 int64_t Offset1, Offset2;
Dan Gohman46510a72010-04-15 01:51:59 +000010821 const GlobalValue *GV1, *GV2;
Roman Divacky2943e372012-09-05 22:15:49 +000010822 const void *CV1, *CV2;
Nate Begemancc66cdd2009-09-25 06:05:26 +000010823 bool isFrameIndex1 = FindBaseOffset(Ptr1, Base1, Offset1, GV1, CV1);
10824 bool isFrameIndex2 = FindBaseOffset(Ptr2, Base2, Offset2, GV2, CV2);
Scott Michelfdc40a02009-02-17 22:15:04 +000010825
Nate Begemancc66cdd2009-09-25 06:05:26 +000010826 // If they have a same base address then check to see if they overlap.
10827 if (Base1 == Base2 || (GV1 && (GV1 == GV2)) || (CV1 && (CV1 == CV2)))
Bill Wendling836ca7d2009-01-30 23:59:18 +000010828 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010829
Owen Anderson4a9f1502010-09-20 20:39:59 +000010830 // It is possible for different frame indices to alias each other, mostly
10831 // when tail call optimization reuses return address slots for arguments.
10832 // To catch this case, look up the actual index of frame indices to compute
10833 // the real alias relationship.
10834 if (isFrameIndex1 && isFrameIndex2) {
10835 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10836 Offset1 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base1)->getIndex());
10837 Offset2 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base2)->getIndex());
10838 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
10839 }
10840
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010841 // Otherwise, if we know what the bases are, and they aren't identical, then
Owen Anderson4a9f1502010-09-20 20:39:59 +000010842 // we know they cannot alias.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010843 if ((isFrameIndex1 || CV1 || GV1) && (isFrameIndex2 || CV2 || GV2))
10844 return false;
Jim Laskey096c22e2006-10-18 12:29:57 +000010845
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010846 // If we know required SrcValue1 and SrcValue2 have relatively large alignment
10847 // compared to the size and offset of the access, we may be able to prove they
10848 // do not alias. This check is conservative for now to catch cases created by
10849 // splitting vector types.
10850 if ((SrcValueAlign1 == SrcValueAlign2) &&
10851 (SrcValueOffset1 != SrcValueOffset2) &&
10852 (Size1 == Size2) && (SrcValueAlign1 > Size1)) {
10853 int64_t OffAlign1 = SrcValueOffset1 % SrcValueAlign1;
10854 int64_t OffAlign2 = SrcValueOffset2 % SrcValueAlign1;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010855
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010856 // There is no overlap between these relatively aligned accesses of similar
10857 // size, return no alias.
10858 if ((OffAlign1 + Size1) <= OffAlign2 || (OffAlign2 + Size2) <= OffAlign1)
10859 return false;
10860 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010861
Hal Finkel253acef2013-08-29 03:29:55 +000010862 bool UseAA = CombinerGlobalAA.getNumOccurrences() > 0 ? CombinerGlobalAA :
10863 TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA();
Hal Finkel77364b72013-09-15 02:19:49 +000010864 if (UseAA && SrcValue1 && SrcValue2) {
Jim Laskey07a27092006-10-18 19:08:31 +000010865 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +000010866 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
10867 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
10868 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Scott Michelfdc40a02009-02-17 22:15:04 +000010869 AliasAnalysis::AliasResult AAResult =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010870 AA.alias(AliasAnalysis::Location(SrcValue1, Overlap1, TBAAInfo1),
10871 AliasAnalysis::Location(SrcValue2, Overlap2, TBAAInfo2));
Jim Laskey07a27092006-10-18 19:08:31 +000010872 if (AAResult == AliasAnalysis::NoAlias)
10873 return false;
10874 }
Jim Laskey096c22e2006-10-18 12:29:57 +000010875
10876 // Otherwise we have to assume they alias.
10877 return true;
Jim Laskey71382342006-10-07 23:37:56 +000010878}
10879
Nadav Rotem90e11dc2012-11-29 00:00:08 +000010880bool DAGCombiner::isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1) {
10881 SDValue Ptr0, Ptr1;
10882 int64_t Size0, Size1;
10883 const Value *SrcValue0, *SrcValue1;
10884 int SrcValueOffset0, SrcValueOffset1;
10885 unsigned SrcValueAlign0, SrcValueAlign1;
10886 const MDNode *SrcTBAAInfo0, *SrcTBAAInfo1;
10887 FindAliasInfo(Op0, Ptr0, Size0, SrcValue0, SrcValueOffset0,
10888 SrcValueAlign0, SrcTBAAInfo0);
10889 FindAliasInfo(Op1, Ptr1, Size1, SrcValue1, SrcValueOffset1,
10890 SrcValueAlign1, SrcTBAAInfo1);
10891 return isAlias(Ptr0, Size0, SrcValue0, SrcValueOffset0,
Nadav Rotemdde785c2012-12-06 17:34:13 +000010892 SrcValueAlign0, SrcTBAAInfo0,
10893 Ptr1, Size1, SrcValue1, SrcValueOffset1,
10894 SrcValueAlign1, SrcTBAAInfo1);
Nadav Rotem90e11dc2012-11-29 00:00:08 +000010895}
10896
Jim Laskey71382342006-10-07 23:37:56 +000010897/// FindAliasInfo - Extracts the relevant alias information from the memory
10898/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +000010899bool DAGCombiner::FindAliasInfo(SDNode *N,
Benjamin Kramerae4746b2012-01-15 11:50:43 +000010900 SDValue &Ptr, int64_t &Size,
10901 const Value *&SrcValue,
10902 int &SrcValueOffset,
10903 unsigned &SrcValueAlign,
10904 const MDNode *&TBAAInfo) const {
10905 LSBaseSDNode *LS = cast<LSBaseSDNode>(N);
10906
10907 Ptr = LS->getBasePtr();
10908 Size = LS->getMemoryVT().getSizeInBits() >> 3;
10909 SrcValue = LS->getSrcValue();
10910 SrcValueOffset = LS->getSrcValueOffset();
10911 SrcValueAlign = LS->getOriginalAlignment();
10912 TBAAInfo = LS->getTBAAInfo();
10913 return isa<LoadSDNode>(LS);
Jim Laskey71382342006-10-07 23:37:56 +000010914}
10915
Jim Laskey6ff23e52006-10-04 16:53:27 +000010916/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
10917/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +000010918void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
Craig Toppera0ec3f92013-07-14 04:42:23 +000010919 SmallVectorImpl<SDValue> &Aliases) {
Dan Gohman475871a2008-07-27 21:46:04 +000010920 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010921 SmallPtrSet<SDNode *, 16> Visited; // Visited node set.
Scott Michelfdc40a02009-02-17 22:15:04 +000010922
Jim Laskey279f0532006-09-25 16:29:54 +000010923 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +000010924 SDValue Ptr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010925 int64_t Size;
10926 const Value *SrcValue;
10927 int SrcValueOffset;
10928 unsigned SrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010929 const MDNode *SrcTBAAInfo;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010930 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010931 SrcValueAlign, SrcTBAAInfo);
Jim Laskey279f0532006-09-25 16:29:54 +000010932
Jim Laskey6ff23e52006-10-04 16:53:27 +000010933 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +000010934 Chains.push_back(OriginalChain);
Nate Begeman677c89d2009-10-12 05:53:58 +000010935 unsigned Depth = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010936
Jim Laskeybc588b82006-10-05 15:07:25 +000010937 // Look at each chain and determine if it is an alias. If so, add it to the
10938 // aliases list. If not, then continue up the chain looking for the next
Scott Michelfdc40a02009-02-17 22:15:04 +000010939 // candidate.
Jim Laskeybc588b82006-10-05 15:07:25 +000010940 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010941 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +000010942 Chains.pop_back();
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010943
10944 // For TokenFactor nodes, look at each operand and only continue up the
10945 // chain until we find two aliases. If we've seen two aliases, assume we'll
Nate Begeman677c89d2009-10-12 05:53:58 +000010946 // find more and revert to original chain since the xform is unlikely to be
10947 // profitable.
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010948 //
10949 // FIXME: The depth check could be made to return the last non-aliasing
Nate Begeman677c89d2009-10-12 05:53:58 +000010950 // chain we found before we hit a tokenfactor rather than the original
10951 // chain.
10952 if (Depth > 6 || Aliases.size() == 2) {
10953 Aliases.clear();
10954 Aliases.push_back(OriginalChain);
10955 break;
10956 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010957
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010958 // Don't bother if we've been before.
10959 if (!Visited.insert(Chain.getNode()))
10960 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +000010961
Jim Laskeybc588b82006-10-05 15:07:25 +000010962 switch (Chain.getOpcode()) {
10963 case ISD::EntryToken:
10964 // Entry token is ideal chain operand, but handled in FindBetterChain.
10965 break;
Scott Michelfdc40a02009-02-17 22:15:04 +000010966
Jim Laskeybc588b82006-10-05 15:07:25 +000010967 case ISD::LOAD:
10968 case ISD::STORE: {
10969 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +000010970 SDValue OpPtr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010971 int64_t OpSize;
10972 const Value *OpSrcValue;
10973 int OpSrcValueOffset;
10974 unsigned OpSrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010975 const MDNode *OpSrcTBAAInfo;
Gabor Greifba36cb52008-08-28 21:40:38 +000010976 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010977 OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010978 OpSrcValueAlign,
10979 OpSrcTBAAInfo);
Scott Michelfdc40a02009-02-17 22:15:04 +000010980
Jim Laskeybc588b82006-10-05 15:07:25 +000010981 // If chain is alias then stop here.
10982 if (!(IsLoad && IsOpLoad) &&
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010983 isAlias(Ptr, Size, SrcValue, SrcValueOffset, SrcValueAlign,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010984 SrcTBAAInfo,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010985 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010986 OpSrcValueAlign, OpSrcTBAAInfo)) {
Jim Laskeybc588b82006-10-05 15:07:25 +000010987 Aliases.push_back(Chain);
10988 } else {
10989 // Look further up the chain.
Scott Michelfdc40a02009-02-17 22:15:04 +000010990 Chains.push_back(Chain.getOperand(0));
Nate Begeman677c89d2009-10-12 05:53:58 +000010991 ++Depth;
Jim Laskey279f0532006-09-25 16:29:54 +000010992 }
Jim Laskeybc588b82006-10-05 15:07:25 +000010993 break;
10994 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010995
Jim Laskeybc588b82006-10-05 15:07:25 +000010996 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010997 // We have to check each of the operands of the token factor for "small"
10998 // token factors, so we queue them up. Adding the operands to the queue
10999 // (stack) in reverse order maintains the original order and increases the
11000 // likelihood that getNode will find a matching token factor (CSE.)
11001 if (Chain.getNumOperands() > 16) {
11002 Aliases.push_back(Chain);
11003 break;
11004 }
Jim Laskeybc588b82006-10-05 15:07:25 +000011005 for (unsigned n = Chain.getNumOperands(); n;)
11006 Chains.push_back(Chain.getOperand(--n));
Nate Begeman677c89d2009-10-12 05:53:58 +000011007 ++Depth;
Jim Laskeybc588b82006-10-05 15:07:25 +000011008 break;
Scott Michelfdc40a02009-02-17 22:15:04 +000011009
Jim Laskeybc588b82006-10-05 15:07:25 +000011010 default:
11011 // For all other instructions we will just have to take what we can get.
11012 Aliases.push_back(Chain);
11013 break;
Jim Laskey279f0532006-09-25 16:29:54 +000011014 }
11015 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000011016}
11017
11018/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
11019/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +000011020SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
11021 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +000011022
Jim Laskey6ff23e52006-10-04 16:53:27 +000011023 // Accumulate all the aliases to this node.
11024 GatherAllAliases(N, OldChain, Aliases);
Scott Michelfdc40a02009-02-17 22:15:04 +000011025
Dan Gohman71dc7c92011-05-17 22:20:36 +000011026 // If no operands then chain to entry token.
11027 if (Aliases.size() == 0)
Jim Laskey6ff23e52006-10-04 16:53:27 +000011028 return DAG.getEntryNode();
Dan Gohman71dc7c92011-05-17 22:20:36 +000011029
11030 // If a single operand then chain to it. We don't need to revisit it.
11031 if (Aliases.size() == 1)
Jim Laskey6ff23e52006-10-04 16:53:27 +000011032 return Aliases[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011033
Jim Laskey6ff23e52006-10-04 16:53:27 +000011034 // Construct a custom tailored token factor.
Andrew Trickac6d9be2013-05-25 02:42:55 +000011035 return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000011036 &Aliases[0], Aliases.size());
Jim Laskey279f0532006-09-25 16:29:54 +000011037}
11038
Nate Begeman1d4d4142005-09-01 00:19:25 +000011039// SelectionDAG::Combine - This is the entry point for the file.
11040//
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000011041void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA,
Bill Wendling98a366d2009-04-29 23:29:43 +000011042 CodeGenOpt::Level OptLevel) {
Nate Begeman1d4d4142005-09-01 00:19:25 +000011043 /// run - This is the main entry point to this class.
11044 ///
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000011045 DAGCombiner(*this, AA, OptLevel).Run(Level);
Nate Begeman1d4d4142005-09-01 00:19:25 +000011046}